Skip to content

Commit bffcf1f

Browse files
author
Lachlan Donald
committed
Test failed gh auth token call
1 parent 3653835 commit bffcf1f

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

github/auth/provider.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ func (p *GHCliProvider) GetToken() (string, error) {
7070

7171
// Run gh auth token
7272
cmd := exec.Command("gh", "auth", "token")
73-
output, err := cmd.Output()
73+
output, err := cmd.CombinedOutput()
7474
if err != nil {
75-
return "", errors.Wrap(err, "failed to get token from gh CLI")
75+
p.ui.Warnf("gh auth failed: %s", strings.TrimSpace(string(output)))
76+
return "", errors.Wrap(err, "gh auth failed")
7677
}
7778

7879
p.token = strings.TrimSpace(string(output))

integration/integration_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,51 @@ EOF
517517
expectations: exp{
518518
filesExist("bin/cashapp-private-pkg.sh"),
519519
}},
520+
{name: "GitHubTokenAuthWithGHCliFails",
521+
preparations: prep{
522+
fixture("github_token_auth"),
523+
activateWithMocks("."),
524+
mockGitHub(
525+
github.WithRequiredToken("mock-gh-token"),
526+
github.WithMockRelease(github.MockRelease{
527+
Repo: "cashapp/hermit-packages-private",
528+
TagName: "v1.0.0",
529+
Name: "cashapp-private-pkg-1.0.0.tar.gz",
530+
Files: []string{"testdata/github_token_auth/packages/cashapp-private-pkg.sh"},
531+
}),
532+
),
533+
},
534+
script: `
535+
# Make sure the gh mock is executable
536+
chmod +x mocks/gh
537+
538+
# Configure to use gh CLI auth
539+
printf "gh-cli-auth = true\n" > "$HERMIT_USER_CONFIG"
540+
541+
# Set the environment variable to make gh auth fail
542+
export GH_AUTH_FAIL=1
543+
544+
# Install should fail because gh cli auth fails and no env var is set
545+
hermit install cashapp-private-pkg || true
546+
547+
# Verify gh CLI was called and failed
548+
assert test -f gh-failures
549+
assert grep -q "gh auth token failed" gh-failures
550+
551+
# Now unset the failure flag and set environment token as fallback
552+
unset GH_AUTH_FAIL
553+
export HERMIT_GITHUB_TOKEN="mock-gh-token"
554+
555+
# Install should succeed with env var
556+
hermit install cashapp-private-pkg
557+
558+
# Verify package was installed and works
559+
assert test "$(cashapp-private-pkg.sh)" = "cashapp-private-pkg 1.0.0"
560+
`,
561+
expectations: exp{
562+
filesExist("bin/cashapp-private-pkg.sh"),
563+
outputContains("gh auth failed: no oauth token found for github.com"),
564+
}},
520565
}
521566

522567
checkForShells(t)

integration/testdata/github_token_auth/mocks/gh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
if [ "$1" = "auth" ] && [ "$2" = "token" ]; then
44
echo "gh $*" >> gh-calls
5+
6+
# Check if we should simulate a failure
7+
if [ -n "$GH_AUTH_FAIL" ]; then
8+
echo "no oauth token found for github.com" >&2
9+
echo "gh auth token failed (simulated)" >> gh-failures
10+
exit 1
11+
fi
12+
513
echo "mock-gh-token"
614
exit 0
715
fi

0 commit comments

Comments
 (0)