Skip to content

Commit 9589d3b

Browse files
authored
feat: Add optional VSCode integration (#499)
Document explains how to make it possible to run our envtest-based tests using VSCode.
1 parent 78db141 commit 9589d3b

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,21 @@ mocks: ## Generate mocks for the project
337337

338338
GOTESTPKGS = $(shell go list ./... | grep -v /mocks | grep -v /templates)
339339

340+
KUBEBUILDER_ASSETS=$(shell setup-envtest use --print path $(ENVTEST_K8S_VERSION) --arch=amd64)
341+
342+
.PHONY: print-envtest
343+
print-envtest: ## Set up envtest (download kubebuilder assets)
344+
@echo KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS)
345+
340346
.PHONY: unit-test
341347
unit-test: mocks ## Run unit tests.
342-
KUBEBUILDER_ASSETS="$(shell setup-envtest use $(ENVTEST_K8S_VERSION) --arch=amd64 -p path)" $(GOTEST) $(GOTESTPKGS)
348+
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" \
349+
$(GOTEST) $(GOTESTPKGS)
343350

344351
.PHONY: coverage
345352
coverage: mocks ## Run the tests of the project and export the coverage
346-
KUBEBUILDER_ASSETS="$(shell setup-envtest use $(ENVTEST_K8S_VERSION) --arch=amd64 -p path)" $(GOTEST) -race -coverprofile=coverage.out -covermode=atomic $(GOTESTPKGS)
353+
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" \
354+
$(GOTEST) -race -coverprofile=coverage.out -covermode=atomic $(GOTESTPKGS)
347355

348356
.PHONY: template-test
349357
template-test: docker-build prepare-local-clusterctl ## Run the template tests

docs/vscode-integration/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# VSCode Integration
2+
3+
Some of the unit tests use the [envtest package](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest) from the controller-runtime project to create a temporary Kubernetes API server.
4+
5+
Envtest reads the location of the necessary binaries, i.e. etcd and kube-apiserver, from an environment variable. This environment variable is initialized by our make targets, but VSCode does not run tests using make.
6+
7+
We can use a VSCode task to write the location to a file, and configure vscode-go, which runs the tests, to initialize its environment from this file.
8+
9+
To run envtest-based tests from VSCode, follow these steps:
10+
11+
1. Install setup-envtest (and other build/test dependencies) by running `devbox install`.
12+
2. Copy `settings.json` and `tasks.json` in this directory into the `.vscode` folder at the root of the repository.
13+
3. Restart VSCode.

docs/vscode-integration/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"go.testEnvFile": "${workspaceFolder}/.vscode/test.env"
3+
}

docs/vscode-integration/tasks.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"type": "shell",
8+
"label": "Prepare vscode to run envtest-based tests",
9+
"detail": "Install envtest and configure the vscode-go test environment.",
10+
"group": {
11+
"kind": "test",
12+
"isDefault": true
13+
},
14+
"command": [
15+
"echo $(make print-envtest) > ${workspaceFolder}/.vscode/test.env",
16+
],
17+
"presentation": {
18+
"echo": true,
19+
"reveal": "silent",
20+
"focus": false,
21+
"panel": "shared",
22+
"showReuseMessage": false,
23+
"clear": false
24+
},
25+
"runOptions": {
26+
"runOn": "folderOpen",
27+
"instanceLimit": 1,
28+
},
29+
"promptOnClose": true,
30+
}
31+
]
32+
}

0 commit comments

Comments
 (0)