-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_test.go
More file actions
74 lines (67 loc) · 2.14 KB
/
token_test.go
File metadata and controls
74 lines (67 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"errors"
"strings"
"testing"
)
func TestResolveTokenFrom(t *testing.T) {
const ghToken = "gho_fromcli"
notFound := func(string) (string, error) { return "", errors.New("not found") }
found := func(string) (string, error) { return "/usr/bin/gh", nil }
ghOK := func() (string, error) { return ghToken, nil }
ghErr := func() (string, error) { return "", errors.New("not logged in") }
noEnv := func(string) string { return "" }
t.Run("env wins", func(t *testing.T) {
env := func(k string) string {
if k == "GITHUB_TOKEN" {
return " env_token " // surrounding space is trimmed
}
return ""
}
// gh would error, but the env var means we never consult it.
tok, note := resolveTokenFrom(env, found, ghErr)
if tok != "env_token" {
t.Errorf("token = %q, want env_token", tok)
}
if note != "" {
t.Errorf("note = %q, want empty for env-sourced token", note)
}
})
t.Run("falls back to gh", func(t *testing.T) {
tok, note := resolveTokenFrom(noEnv, found, ghOK)
if tok != ghToken {
t.Errorf("token = %q, want %q", tok, ghToken)
}
if note == "" {
t.Error("expected a note indicating the token came from gh")
}
})
t.Run("gh missing", func(t *testing.T) {
tok, note := resolveTokenFrom(noEnv, notFound, ghOK)
if tok != "" {
t.Errorf("token = %q, want empty when gh is missing", tok)
}
if !strings.Contains(note, "was not found") {
t.Errorf("hint should explain gh is missing, got %q", note)
}
})
t.Run("gh not logged in", func(t *testing.T) {
tok, note := resolveTokenFrom(noEnv, found, ghErr)
if tok != "" {
t.Errorf("token = %q, want empty when gh is not logged in", tok)
}
if !strings.Contains(note, "not logged in") {
t.Errorf("hint should explain gh is not logged in, got %q", note)
}
})
t.Run("gh returns empty token", func(t *testing.T) {
ghEmpty := func() (string, error) { return " ", nil }
tok, note := resolveTokenFrom(noEnv, found, ghEmpty)
if tok != "" {
t.Errorf("token = %q, want empty when gh yields nothing", tok)
}
if !strings.Contains(note, "not logged in") {
t.Errorf("hint should treat empty token as not logged in, got %q", note)
}
})
}