|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// TestEnvTokenSource_EnvVarWins pins that a configured env var, when present, |
| 10 | +// short-circuits both GITLAB_TOKEN and the glab fallback. The glab fake is |
| 11 | +// installed to prove it is NOT consulted on the happy path. |
| 12 | +func TestEnvTokenSource_EnvVarWins(t *testing.T) { |
| 13 | + t.Setenv("AO_GITLAB_TOKEN", "from-env") |
| 14 | + t.Setenv("GITLAB_TOKEN", "from-default-env") |
| 15 | + src := EnvTokenSource{ |
| 16 | + EnvVars: []string{"AO_GITLAB_TOKEN"}, |
| 17 | + GLAB: func(context.Context) (string, error) { |
| 18 | + t.Fatalf("glab fallback called even though env var was set") |
| 19 | + return "", nil |
| 20 | + }, |
| 21 | + } |
| 22 | + got, err := src.Token(context.Background()) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("Token: %v", err) |
| 25 | + } |
| 26 | + if got != "from-env" { |
| 27 | + t.Fatalf("token = %q, want from-env", got) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// TestEnvTokenSource_DefaultEnvWins pins that GITLAB_TOKEN is used when the |
| 32 | +// configured EnvVars list is empty or yields nothing, before falling through |
| 33 | +// to glab. |
| 34 | +func TestEnvTokenSource_DefaultEnvWins(t *testing.T) { |
| 35 | + t.Setenv("GITLAB_TOKEN", "from-default-env") |
| 36 | + src := EnvTokenSource{ |
| 37 | + GLAB: func(context.Context) (string, error) { |
| 38 | + t.Fatalf("glab fallback called even though GITLAB_TOKEN was set") |
| 39 | + return "", nil |
| 40 | + }, |
| 41 | + } |
| 42 | + got, err := src.Token(context.Background()) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("Token: %v", err) |
| 45 | + } |
| 46 | + if got != "from-default-env" { |
| 47 | + t.Fatalf("token = %q, want from-default-env", got) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// TestEnvTokenSource_GlabFallback pins that when no env var yields a token, |
| 52 | +// the glab shell-out is used. |
| 53 | +func TestEnvTokenSource_GlabFallback(t *testing.T) { |
| 54 | + t.Setenv("GITLAB_TOKEN", "") |
| 55 | + var called bool |
| 56 | + src := EnvTokenSource{ |
| 57 | + GLAB: func(context.Context) (string, error) { |
| 58 | + called = true |
| 59 | + return "glpat-from-glab", nil |
| 60 | + }, |
| 61 | + } |
| 62 | + got, err := src.Token(context.Background()) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("Token: %v", err) |
| 65 | + } |
| 66 | + if !called { |
| 67 | + t.Fatalf("glab fallback was not called") |
| 68 | + } |
| 69 | + if got != "glpat-from-glab" { |
| 70 | + t.Fatalf("token = %q, want glpat-from-glab", got) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// TestEnvTokenSource_GlabFallbackTrimsWhitespace pins that the glab stdout is |
| 75 | +// trimmed (the real CLI ends with a newline). |
| 76 | +func TestEnvTokenSource_GlabFallbackTrimsWhitespace(t *testing.T) { |
| 77 | + t.Setenv("GITLAB_TOKEN", "") |
| 78 | + src := EnvTokenSource{ |
| 79 | + GLAB: func(context.Context) (string, error) { |
| 80 | + return " glpat-padded\n", nil |
| 81 | + }, |
| 82 | + } |
| 83 | + got, err := src.Token(context.Background()) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("Token: %v", err) |
| 86 | + } |
| 87 | + if got != "glpat-padded" { |
| 88 | + t.Fatalf("token = %q, want glpat-padded", got) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// TestEnvTokenSource_GlabFailureFallsThrough pins the silent-fallthrough |
| 93 | +// contract: if glab errors (not installed, not logged in, etc.) the source |
| 94 | +// returns ErrNoToken instead of propagating the exec error. This keeps the |
| 95 | +// caller's failure mode uniform — "no token configured" — regardless of |
| 96 | +// whether the user has glab installed. |
| 97 | +func TestEnvTokenSource_GlabFailureFallsThrough(t *testing.T) { |
| 98 | + t.Setenv("GITLAB_TOKEN", "") |
| 99 | + src := EnvTokenSource{ |
| 100 | + GLAB: func(context.Context) (string, error) { |
| 101 | + return "", errors.New("exec: glab: executable file not found in $PATH") |
| 102 | + }, |
| 103 | + } |
| 104 | + _, err := src.Token(context.Background()) |
| 105 | + if !errors.Is(err, ErrNoToken) { |
| 106 | + t.Fatalf("err = %v, want ErrNoToken (glab error should fall through silently)", err) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// TestEnvTokenSource_GlabEmptyFallsThrough pins that an empty (whitespace-only) |
| 111 | +// glab output is treated as "no token" rather than as a valid token of zero |
| 112 | +// length — otherwise StaticTokenSource-style downstream checks would yield |
| 113 | +// "no token configured" inconsistently. |
| 114 | +func TestEnvTokenSource_GlabEmptyFallsThrough(t *testing.T) { |
| 115 | + t.Setenv("GITLAB_TOKEN", "") |
| 116 | + src := EnvTokenSource{ |
| 117 | + GLAB: func(context.Context) (string, error) { |
| 118 | + return " \n", nil |
| 119 | + }, |
| 120 | + } |
| 121 | + _, err := src.Token(context.Background()) |
| 122 | + if !errors.Is(err, ErrNoToken) { |
| 123 | + t.Fatalf("err = %v, want ErrNoToken (empty glab output should fall through)", err) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// TestEnvTokenSource_NilGlabUsesRealExec is a smoke test: with the GLAB field |
| 128 | +// nil and no env vars, the source attempts a real exec. We don't depend on |
| 129 | +// glab being installed on the test machine — we just assert that the |
| 130 | +// fallthrough produces ErrNoToken (either because glab isn't installed OR |
| 131 | +// because it has no token to give). Either way, the public contract holds: |
| 132 | +// "no token configured" surfaces as ErrNoToken, never as a raw exec error. |
| 133 | +func TestEnvTokenSource_NilGlabUsesRealExec(t *testing.T) { |
| 134 | + t.Setenv("GITLAB_TOKEN", "") |
| 135 | + src := EnvTokenSource{} // GLAB nil → defaults to real exec. |
| 136 | + // Either glab is installed and returns a real token (rare on CI), or it |
| 137 | + // fails / is missing. In both failure modes we must see ErrNoToken; in |
| 138 | + // the rare success case we get a non-empty token. Both are valid. |
| 139 | + tok, err := src.Token(context.Background()) |
| 140 | + if err != nil && !errors.Is(err, ErrNoToken) { |
| 141 | + t.Fatalf("err = %v, want ErrNoToken or a real token", err) |
| 142 | + } |
| 143 | + if err == nil && tok == "" { |
| 144 | + t.Fatalf("got empty token with nil error") |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// TestEnvTokenSource_ContextThreadedToGlab pins that the ctx is passed through |
| 149 | +// to the GLAB hook. The github adapter's parallel work uses the same shape, |
| 150 | +// so any future caller wiring a deadline at startup gets honored. |
| 151 | +func TestEnvTokenSource_ContextThreadedToGlab(t *testing.T) { |
| 152 | + t.Setenv("GITLAB_TOKEN", "") |
| 153 | + type ctxKey struct{} |
| 154 | + parent := context.WithValue(context.Background(), ctxKey{}, "sentinel") |
| 155 | + src := EnvTokenSource{ |
| 156 | + GLAB: func(ctx context.Context) (string, error) { |
| 157 | + if v, _ := ctx.Value(ctxKey{}).(string); v != "sentinel" { |
| 158 | + t.Fatalf("ctx not threaded through to GLAB hook: got %v", v) |
| 159 | + } |
| 160 | + return "tok", nil |
| 161 | + }, |
| 162 | + } |
| 163 | + if _, err := src.Token(parent); err != nil { |
| 164 | + t.Fatalf("Token: %v", err) |
| 165 | + } |
| 166 | +} |
0 commit comments