Address @williammartin PR feedback

This commit is contained in:
Andy Feller 2024-10-21 11:53:05 -04:00
parent f4f161c096
commit acf62dacf2
3 changed files with 11 additions and 5 deletions

View file

@ -57,7 +57,7 @@ The following custom environment variables are made available to the scripts:
* `ORG`: Set to the value of the `GH_ACCEPTANCE_ORG` env var provided to `go test`
* `GH_TOKEN`: Set to the value of the `GH_ACCEPTANCE_TOKEN` env var provided to `go test`
* `RANDOM_STRING`: Set to a length 10 random string of letters to help isolate globally visible resources
* `SCRIPT_NAME`: Set to the name of the `testscript` currently running, without extension e.g. `pr-view`
* `SCRIPT_NAME`: Set to the name of the `testscript` currently running, without extension and replacing hyphens with underscores e.g. `pr_view`
* `HOME`: Set to the initial working directory. Required for `git` operations
* `GH_CONFIG_DIR`: Set to the initial working directory. Required for `gh` operations

View file

@ -111,7 +111,11 @@ func sharedSetup(tsEnv testScriptEnv) func(ts *testscript.Env) error {
if !ok {
ts.T().Fatal("script name not found")
}
ts.Setenv("SCRIPT_NAME", scriptName)
// When using script name to uniquely identify where test data comes from,
// some places like GitHub Actions secret names don't accept hyphens.
// Replace them with underscores until such a time this becomes a problem.
ts.Setenv("SCRIPT_NAME", strings.ReplaceAll(scriptName, "-", "_"))
ts.Setenv("HOME", ts.Cd)
ts.Setenv("GH_CONFIG_DIR", ts.Cd)
@ -182,6 +186,8 @@ func sharedCmds(tsEnv testScriptEnv) map[string]func(ts *testscript.TestScript,
src := ts.MkAbs(args[0])
ts.Logf("replace src: %s", src)
// Preserve the existing file mode while replacing the contents similar to native cp behavior
info, err := os.Stat(src)
ts.Check(err)
mode := info.Mode() & 0o777
@ -198,7 +204,7 @@ func sharedCmds(tsEnv testScriptEnv) map[string]func(ts *testscript.TestScript,
value := arg[i+1:]
ts.Logf("replace %s: %s", name, value)
// `replace` was originally built similar to `cmpenv`, expanding environment variables within a file.
// `replace` was originally built similar to `cp` and `cmpenv`, expanding environment variables within a file.
// However files with content that looks like environments variable such as GitHub Actions workflows
// were being modified unexpectedly. Thus `replace` has been designed to using string replacement
// looking for `$KEY` specifically.

View file

@ -1,6 +1,6 @@
# Setup environment variables used for testscript
env REPO=$SCRIPT_NAME-$RANDOM_STRING
env2upper SECRET_NAME=$SCRIPT_NAME_$RANDOM_STRING
env REPO=${SCRIPT_NAME}-${RANDOM_STRING}
env2upper SECRET_NAME=${SCRIPT_NAME}_${RANDOM_STRING}
# Use gh as a credential helper
exec gh auth setup-git