Skip to content

Commit ea79a59

Browse files
committed
fix: ensure invalid token test works when PIVNET_TOKEN is set
- Add runTileDiffWithEnv helper to allow environment variable overrides - Update "fails with meaningful error for invalid Pivnet token" test to unset PIVNET_TOKEN - Prevents CI failure when PIVNET_TOKEN secret is configured in GitHub Actions Without this fix, the test would fail in CI because the real PIVNET_TOKEN from secrets would override the invalid token passed via --pivnet-token flag.
1 parent 5d43979 commit ea79a59

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

test/acceptance_helpers_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/exec"
99
"path/filepath"
10+
"strings"
1011

1112
. "github.com/onsi/gomega"
1213
)
@@ -32,11 +33,45 @@ func getEnvOrDefault(key, defaultVal string) string {
3233

3334
// runTileDiff executes the tile-diff binary with given arguments
3435
func runTileDiff(args ...string) (string, error) {
36+
return runTileDiffWithEnv(nil, args...)
37+
}
38+
39+
// runTileDiffWithEnv executes the tile-diff binary with custom environment overrides
40+
func runTileDiffWithEnv(envOverrides map[string]string, args ...string) (string, error) {
3541
cmd := exec.Command(tileDiffBin, args...)
36-
cmd.Env = append(os.Environ(),
42+
43+
// Start with base environment
44+
env := append([]string{},
3745
fmt.Sprintf("PIVNET_CACHE_DIR=%s", testCacheDir),
3846
fmt.Sprintf("PIVNET_EULA_FILE=%s", testEULAFile),
3947
)
48+
49+
// Add environment variables, but skip PIVNET_TOKEN if it will be overridden
50+
skipVars := make(map[string]bool)
51+
for k := range envOverrides {
52+
skipVars[k] = true
53+
}
54+
55+
for _, envVar := range os.Environ() {
56+
// Skip variables that will be overridden
57+
isSkip := false
58+
for skipVar := range skipVars {
59+
if strings.HasPrefix(envVar, skipVar+"=") {
60+
isSkip = true
61+
break
62+
}
63+
}
64+
if !isSkip {
65+
env = append(env, envVar)
66+
}
67+
}
68+
69+
// Apply overrides
70+
for k, v := range envOverrides {
71+
env = append(env, fmt.Sprintf("%s=%s", k, v))
72+
}
73+
74+
cmd.Env = env
4075
output, err := cmd.CombinedOutput()
4176
return string(output), err
4277
}

test/acceptance_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,9 @@ var _ = Describe("Pivnet Integration", func() {
278278
// NOTE: Tests proper error messages for invalid inputs and failure scenarios
279279

280280
It("fails with meaningful error for invalid Pivnet token", func() {
281-
output, err := runTileDiff(
281+
// Unset PIVNET_TOKEN env var to ensure the invalid token from --pivnet-token flag is used
282+
output, err := runTileDiffWithEnv(
283+
map[string]string{"PIVNET_TOKEN": ""},
282284
"--product-slug", "harbor-container-registry",
283285
"--old-version", "2.10.2",
284286
"--new-version", "2.10.3",

0 commit comments

Comments
 (0)