Skip to content

Commit e50337f

Browse files
committed
Fix nil cfg panic when setup flows into auth configure
setup built newCfg locally but never assigned to package-level cfg. When user answered "y" to configure prompt, runAuthConfigure called configureGhHosts which dereferenced nil cfg.
1 parent 0fdd895 commit e50337f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

internal/cmd/cmd_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,40 @@ func TestSetupCmd(t *testing.T) {
200200
assert.Equal(t, int64(456), loaded.InstallationID)
201201
}
202202

203+
func TestSetupCmd_SetsCfgBeforeAuthConfigure(t *testing.T) {
204+
saveRestore(t)
205+
206+
keyPath := testPEMPath(t)
207+
cfgDir := t.TempDir()
208+
cfgFile := filepath.Join(cfgDir, "config.yaml")
209+
cfgPath = cfgFile
210+
211+
mock := &mockTokenGenerator{
212+
result: &auth.TokenResult{
213+
Token: "ghs_setup_token",
214+
ExpiresAt: time.Now().Add(time.Hour),
215+
},
216+
}
217+
tokenGenerator = mock
218+
219+
// Answer "n" to configure prompt — we just verify cfg is set
220+
// after setup completes (the bug was cfg remaining nil)
221+
input := "123\n456\n" + keyPath + "\nn\n"
222+
223+
buf := new(bytes.Buffer)
224+
rootCmd.SetOut(buf)
225+
rootCmd.SetIn(strings.NewReader(input))
226+
rootCmd.SetArgs([]string{"setup"})
227+
require.NoError(t, rootCmd.Execute())
228+
229+
// Regression: cfg must be set from setup's newCfg so that
230+
// runAuthConfigure (if called) doesn't hit nil pointer panic
231+
require.NotNil(t, cfg, "cfg must be set after setup")
232+
assert.Equal(t, int64(123), cfg.AppID)
233+
assert.Equal(t, int64(456), cfg.InstallationID)
234+
assert.Equal(t, keyPath, cfg.PrivateKeyPath)
235+
}
236+
203237
func TestSetupCmd_InvalidPEM(t *testing.T) {
204238
saveRestore(t)
205239

internal/cmd/setup.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ func runSetup(cmd *cobra.Command, args []string) error {
9797

9898
fmt.Fprintf(out, "\nConfig saved to %s\n", savePath)
9999

100+
cfg = newCfg
101+
100102
fmt.Fprintln(out)
101103
if confirm(cmd, "Configure git + gh auth now?") {
102104
fmt.Fprintln(out)

0 commit comments

Comments
 (0)