Skip to content

Commit aab7f8c

Browse files
authored
[tests] Fix docs about example tests (#1302)
## Summary We were abusing the "example test" term to really mean "run `devbox run run_test` on any devbox.json". Updated code and docs to reflect reality, since it was confusing while I was debugging a failed test. ## How was it tested? `DEVBOX_RUN_DEVBOX_JSON_TESTS=1 go test testscripts [-run TestExamples/flake_remote_run_test`
1 parent 1c5cc8c commit aab7f8c

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

.github/workflows/cli-tests.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ on:
1919
type: boolean
2020
workflow_dispatch:
2121
inputs:
22-
run-example-tests:
22+
run-devbox-json-tests:
2323
type: boolean
2424
run-mac-tests:
2525
type: boolean
@@ -73,15 +73,15 @@ jobs:
7373
- ${{ github.ref == 'refs/heads/main' }}
7474
os: [ubuntu-latest, macos-latest]
7575
# This is an optimization that runs tests twice, with and without
76-
# the examples. We can require non-example tests to complete before
76+
# the devbox.json tests. We can require the other tests to complete before
7777
# merging, while keeping the others as an additional non-required signal
78-
run-example-tests: [true, false]
78+
run-devbox-json-tests: [true, false]
7979
exclude:
8080
- is-main: false
8181
os: "${{ inputs.run-mac-tests && 'dummy' || 'macos-latest' }}"
8282
- is-main: true
83-
run-example-tests: false
84-
- run-example-tests: true
83+
run-devbox-json-tests: false
84+
- run-devbox-json-tests: true
8585
os: macos-latest
8686
runs-on: ${{ matrix.os }}
8787
timeout-minutes: ${{ (github.ref == 'refs/heads/main' || inputs.run-mac-tests) && 37 || 20 }}
@@ -108,10 +108,10 @@ jobs:
108108
nix-build-user-count: 4
109109
- name: Run tests
110110
env:
111-
# For example tests, we default to non-debug mode since the debug output is less useful than for unit testscripts.
111+
# For devbox.json tests, we default to non-debug mode since the debug output is less useful than for unit testscripts.
112112
# But we allow overriding via inputs.example-debug
113-
DEVBOX_DEBUG: ${{ (!matrix.run-example-tests || inputs.example-debug) && '1' || '0' }}
114-
DEVBOX_EXAMPLE_TESTS: ${{ matrix.run-example-tests }}
113+
DEVBOX_DEBUG: ${{ (!matrix.run-devbox-json-tests || inputs.example-debug) && '1' || '0' }}
114+
DEVBOX_RUN_DEVBOX_JSON_TESTS: ${{ matrix.run-devbox-json-tests }}
115115
# Used in `go test -timeout` flag. Needs a value that time.ParseDuration can parse.
116116
DEVBOX_GOLANG_TEST_TIMEOUT: "${{ (github.ref == 'refs/heads/main' || inputs.run-mac-tests) && '35m' || '20m' }}"
117117
run: |

testscripts/testrunner/examplesrunner.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ import (
2222
// virtenvs of devbox plugins in this directory. We need to use a custom
2323
// path that is intentionally short, since some plugins store unix sockets in
2424
// their virtenv and unix sockets require their paths to be short.
25-
const xdgStateHomeDir = "/tmp/devbox-example-testscripts"
25+
const xdgStateHomeDir = "/tmp/devbox-testscripts"
2626

27-
// RunExamplesTestscripts generates testscripts for each example devbox-project.
28-
func RunExamplesTestscripts(t *testing.T, examplesDir string) {
27+
// RunDevboxTestscripts generates and runs a testscript test for each Devbox project in dir.
28+
// For each project, runs `devbox run run_test` (if script exists) and asserts it succeeds.
29+
func RunDevboxTestscripts(t *testing.T, dir string) {
2930
// ensure the state home dir for devbox exists
3031
err := os.MkdirAll(xdgStateHomeDir, 0700)
3132
if err != nil && !errors.Is(err, fs.ErrNotExist) {
3233
t.Error(err)
3334
}
3435

35-
err = filepath.WalkDir(examplesDir, func(path string, entry os.DirEntry, err error) error {
36+
err = filepath.WalkDir(dir, func(path string, entry os.DirEntry, err error) error {
3637
if err != nil {
3738
return err
3839
}
@@ -75,16 +76,16 @@ func RunExamplesTestscripts(t *testing.T, examplesDir string) {
7576
}
7677

7778
t.Logf("running testscript for example: %s\n", path)
78-
runSingleExampleTestscript(t, examplesDir, path)
79+
runSingleDevboxTestscript(t, dir, path)
7980
return nil
8081
})
8182
if err != nil {
8283
t.Error(err)
8384
}
8485
}
8586

86-
func runSingleExampleTestscript(t *testing.T, examplesDir, projectDir string) {
87-
testscriptDir, err := generateTestscript(t, examplesDir, projectDir)
87+
func runSingleDevboxTestscript(t *testing.T, dir, projectDir string) {
88+
testscriptDir, err := generateTestscript(t, dir, projectDir)
8889
if err != nil {
8990
t.Error(err)
9091
}
@@ -127,10 +128,10 @@ func runSingleExampleTestscript(t *testing.T, examplesDir, projectDir string) {
127128
}
128129

129130
// generateTestscript will create a temp-directory and place the generic
130-
// testscript file (.test.txt) for all examples devbox-projects in it.
131+
// testscript file (.test.txt) for all devbox-projects in the dir.
131132
// It returns the directory containing the testscript file.
132-
func generateTestscript(t *testing.T, examplesDir, projectDir string) (string, error) {
133-
testPath, err := filepath.Rel(examplesDir, projectDir)
133+
func generateTestscript(t *testing.T, dir, projectDir string) (string, error) {
134+
testPath, err := filepath.Rel(dir, projectDir)
134135
if err != nil {
135136
return "", errors.WithStack(err)
136137
}

testscripts/testscripts_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import (
88
"go.jetpack.io/devbox/testscripts/testrunner"
99
)
1010

11-
const exampleTestsEnvName = "DEVBOX_EXAMPLE_TESTS"
11+
// When true, tests that `devbox run run_test` succeeds on every devbox.json
12+
// found in examples/.. and testscripts/..
13+
const runDevboxJSONTests = "DEVBOX_RUN_DEVBOX_JSON_TESTS"
1214

1315
func TestScripts(t *testing.T) {
16+
// To run a specific test, say, testscripts/foo/bar.test.text, then run
17+
// go test ./testscripts -run TestScripts/bar
1418
testrunner.RunTestscripts(t, ".")
1519
}
1620

@@ -20,19 +24,22 @@ func TestMain(m *testing.M) {
2024

2125
// TestExamples runs testscripts on the devbox-projects in the examples folder.
2226
func TestExamples(t *testing.T) {
23-
isOn, err := strconv.ParseBool(os.Getenv(exampleTestsEnvName))
27+
isOn, err := strconv.ParseBool(os.Getenv(runDevboxJSONTests))
2428
if err != nil || !isOn {
25-
t.Skipf("Skipping TestExamples. To enable, set %s=1.", exampleTestsEnvName)
29+
t.Skipf("Skipping TestExamples. To enable, set %s=1.", runDevboxJSONTests)
2630
}
2731

28-
testrunner.RunExamplesTestscripts(t, "../examples")
32+
// To run a specific test, say, examples/foo/bar, then run
33+
// go test ./testscripts -run TestExamples/foo_bar_run_test
34+
testrunner.RunDevboxTestscripts(t, "../examples")
2935
}
3036

37+
// TestScriptsWithDevboxJSON runs testscripts on the devbox-projects in the testscripts folder.
3138
func TestScriptsWithDevboxJSON(t *testing.T) {
32-
isOn, err := strconv.ParseBool(os.Getenv(exampleTestsEnvName))
39+
isOn, err := strconv.ParseBool(os.Getenv(runDevboxJSONTests))
3340
if err != nil || !isOn {
34-
t.Skipf("Skipping TestExamples. To enable, set %s=1.", exampleTestsEnvName)
41+
t.Skipf("Skipping TestExamples. To enable, set %s=1.", runDevboxJSONTests)
3542
}
3643

37-
testrunner.RunExamplesTestscripts(t, ".")
44+
testrunner.RunDevboxTestscripts(t, ".")
3845
}

0 commit comments

Comments
 (0)