Skip to content

Commit

Permalink
internal/e2e: tweak how we use secret env vars
Browse files Browse the repository at this point in the history
Only require GITHUB_TOKEN when a script calls create-github-token.
This allows other tests which don't require github auth to be run
on their own via `go test -run` without having to set this secret.
The failure is now done via t.Fatal instead of panic,
and the env var is only passed along for `git push` via the builtin.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I54d7822938cea7d1c1c66b9f7849e60b680e5342
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1171910
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mvdan committed Nov 8, 2023
1 parent f9c8d98 commit d3efde7
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions internal/e2e/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ var (
// This is necessary since we will create a new repository per test,
// and there's no way to easily install the app on each repo via the API.
githubOrg = envOr("GITHUB_ORG", "cue-labs-modules-testing")
// githubToken should have read and write access to repository
// administration and contents within githubOrg,
// to be able to create repositories under the org and git push to them.
githubToken = envMust("GITHUB_TOKEN")
// githubKeep leaves the newly created repo around when set to true.
githubKeep = envOr("GITHUB_KEEP", "false")
)
Expand All @@ -92,7 +88,6 @@ func TestScript(t *testing.T) {
env.Setenv("CUE_EXPERIMENT", "modules")
env.Setenv("CUE_REGISTRY", "registry.cue.works")
env.Setenv("CUE_CACHED_GOBIN", os.Getenv("CUE_CACHED_GOBIN"))
env.Setenv("GITHUB_TOKEN", githubToken) // needed for "git push"
return nil
},
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
Expand All @@ -102,6 +97,13 @@ func TestScript(t *testing.T) {
if neg || len(args) > 0 {
ts.Fatalf("usage: create-github-repo")
}

// githubToken should have read and write access to repository
// administration and contents within githubOrg,
// to be able to create repositories under the org and git push to them.
// Not a global, since
githubToken := envMust(t, "GITHUB_TOKEN")

// TODO: name the repo after ts.Name once the API lands
// TODO: add a short random suffix to prevent time collisions
repoName := time.Now().UTC().Format("2006-01-02.15-04-05")
Expand All @@ -127,6 +129,7 @@ func TestScript(t *testing.T) {
})

ts.Setenv("MODULE", fmt.Sprintf("github.com/%s/%s", githubOrg, repoName))
ts.Setenv("GITHUB_TOKEN", githubToken) // needed for "git push"
},
// env-fill rewrites its argument files to replace any environment variable
// references with their values, using the same algorithm as cmpenv.
Expand Down Expand Up @@ -174,11 +177,12 @@ func envOr(name, fallback string) string {
return fallback
}

func envMust(name string) string {
func envMust(t *testing.T, name string) string {
if s := os.Getenv(name); s != "" {
return s
}
panic(fmt.Sprintf("%s must be set", name))
t.Fatalf("%s must be set", name)
return ""
}

func tsExpand(ts *testscript.TestScript, s string) string {
Expand Down

0 comments on commit d3efde7

Please sign in to comment.