From bfa5b6afa5bb1f39990dda9ddbb03e639cdfd8ab Mon Sep 17 00:00:00 2001 From: William Martin Date: Mon, 14 Oct 2024 13:59:55 +0200 Subject: [PATCH] Support skipping Acceptance test cleanup --- acceptance/README.md | 4 +-- acceptance/acceptance_test.go | 49 +++++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/acceptance/README.md b/acceptance/README.md index cc3af0f98..1311704d4 100644 --- a/acceptance/README.md +++ b/acceptance/README.md @@ -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 diff --git a/acceptance/acceptance_test.go b/acceptance/acceptance_test.go index 6a5e32ff7..8d4d4b302 100644 --- a/acceptance/acceptance_test.go +++ b/acceptance/acceptance_test.go @@ -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 }