Support skipping Acceptance test cleanup

This commit is contained in:
William Martin 2024-10-14 13:59:55 +02:00
parent 4d986aaed4
commit bfa5b6afa5
2 changed files with 34 additions and 19 deletions

View file

@ -113,9 +113,7 @@ This is generally enough information to understand why a test has failed. Howeve
> [!WARNING]
> Verbose mode dumps the `testscript` environment variables, including the `GH_TOKEN`, so be careful.
Finally, the `testscript.Params` struct has a `TestWork` field; when `TestWork` is set to true, the `WORK` directory will not be cleaned up, allowing for manual investigation of state in the shell. However, `defer` statements will still be run.
TODO: Probably we want to add a `NO_CLEANUP` flag so `defer` is not run. Maybe this implies we want `defer` to be called `cleanup`? It might not be so clear in its intent though. Maybe `NO_CLEANUP_WORK` and `NO_DEFER`?
By default `testscript` removes the directory in which it was running the script, and if you've been a conscientious engineer, you should be cleaning up resources using the `defer` statement. However, this can be an impediment to debugging. As such you can set `GH_ACCEPTANCE_PRESERVE_WORK_DIR=true` and `GH_ACCEPTANCE_SKIP_DEFER=true` to skip these cleanup steps.
### Effective Test Authoring

View file

@ -40,9 +40,10 @@ func testScriptParamsFor(tsEnv testScriptEnv, dir string) testscript.Params {
Dir: path.Join("testdata", dir),
Files: []string{},
Setup: sharedSetup(tsEnv),
Cmds: sharedCmds,
Cmds: sharedCmds(tsEnv),
RequireExplicitExec: true,
RequireUniqueNames: true,
TestWork: tsEnv.preserveWorkDir,
}
}
@ -65,22 +66,32 @@ func sharedSetup(tsEnv testScriptEnv) func(ts *testscript.Env) error {
}
}
var sharedCmds = map[string]func(ts *testscript.TestScript, neg bool, args []string){
"defer": func(ts *testscript.TestScript, neg bool, args []string) {
ts.Defer(func() {
ts.Check(ts.Exec(args[0], args[1:]...))
})
},
"stdout2env": func(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported: ! stdout2env")
}
if len(args) != 1 {
ts.Fatalf("usage: stdout2env name")
}
func sharedCmds(tsEnv testScriptEnv) map[string]func(ts *testscript.TestScript, neg bool, args []string) {
return map[string]func(ts *testscript.TestScript, neg bool, args []string){
"defer": func(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported: ! defer")
}
ts.Setenv(args[0], strings.TrimRight(ts.ReadFile("stdout"), "\n"))
},
if tsEnv.skipDefer {
return
}
ts.Defer(func() {
ts.Check(ts.Exec(args[0], args[1:]...))
})
},
"stdout2env": func(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported: ! stdout2env")
}
if len(args) != 1 {
ts.Fatalf("usage: stdout2env name")
}
ts.Setenv(args[0], strings.TrimRight(ts.ReadFile("stdout"), "\n"))
},
}
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
@ -115,6 +126,9 @@ type testScriptEnv struct {
host string
org string
token string
skipDefer bool
preserveWorkDir bool
}
func (e *testScriptEnv) fromEnv() error {
@ -149,5 +163,8 @@ func (e *testScriptEnv) fromEnv() error {
e.org = envMap["GH_ACCEPTANCE_ORG"]
e.token = envMap["GH_ACCEPTANCE_TOKEN"]
e.preserveWorkDir = os.Getenv("GH_ACCEPTANCE_PRESERVE_WORK_DIR") == "true"
e.skipDefer = os.Getenv("GH_ACCEPTANCE_SKIP_DEFER") == "true"
return nil
}