Merge pull request #7 from JMR-dev/fix-bug-on-repo-state

fix state path bug
This commit was merged in pull request #7.
This commit is contained in:
Jason Ross
2026-05-26 17:04:04 -05:00
committed by GitHub
2 changed files with 62 additions and 0 deletions
+5
View File
@@ -82,6 +82,11 @@ func Run(ctx context.Context, opts *cli.Options) error {
if err := os.MkdirAll(stateDir, 0o700); err != nil {
return fmt.Errorf("creating state dir: %w", err)
}
absStateDir, err := filepath.Abs(stateDir)
if err != nil {
return fmt.Errorf("resolving state dir: %w", err)
}
stateDir = absStateDir
// --- Auth: prefer caller-supplied GITHUB_TOKEN, else borrow from gh ---
if os.Getenv("GITHUB_TOKEN") == "" {
+57
View File
@@ -117,6 +117,63 @@ func (m *mockStack) Destroy(ctx context.Context, opts ...optdestroy.Option) (aut
return auto.DestroyResult{}, nil
}
// TestRun_RelativeStateDir verifies that a relative state_dir (e.g. "./state"
// from a TOML config) does not cause a "state/state" double-path in the
// file:// backend URL. Before the fix, upsertStack was called with a relative
// file:// URL that Pulumi resolved against its WorkDir, producing the wrong path.
func TestRun_RelativeStateDir(t *testing.T) {
oldExec := execCommand
oldGithubExec := githubapi.ExecCommand
oldUpsert := upsertStack
execCommand = mockExec
githubapi.ExecCommand = mockExec
defer func() {
execCommand = oldExec
githubapi.ExecCommand = oldGithubExec
upsertStack = oldUpsert
}()
// Capture the PULUMI_BACKEND_URL env var passed to upsertStack.
var capturedBackendURL string
mStack := &mockStack{setConfigCalls: make(map[string]string)}
upsertStack = func(ctx context.Context, stackName, projectName string, program pulumi.RunFunc, opts ...auto.LocalWorkspaceOption) (stackInterface, error) {
// Apply options to a scratch workspace to extract env vars.
ws, err := auto.NewLocalWorkspace(ctx, opts...)
if err == nil {
env := ws.GetEnvVars()
capturedBackendURL = env["PULUMI_BACKEND_URL"]
}
return mStack, nil
}
parent := t.TempDir()
oldWd, _ := os.Getwd()
_ = os.Chdir(parent)
defer os.Chdir(oldWd)
opts := &cli.Options{
Owner: "JMR-dev",
Repo: "test-repo",
Branch: "main",
Action: cli.ActionApply,
StateDir: "./state",
Environments: []*cli.EnvSpec{{Name: "production"}},
}
oldToken := os.Getenv("GITHUB_TOKEN")
os.Setenv("GITHUB_TOKEN", "test-token")
defer os.Setenv("GITHUB_TOKEN", oldToken)
if err := Run(context.Background(), opts); err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "file://" + filepath.Join(parent, "state")
if capturedBackendURL != want {
t.Errorf("PULUMI_BACKEND_URL = %q, want %q", capturedBackendURL, want)
}
}
func TestRun_Apply(t *testing.T) {
oldExec := execCommand
oldGithubExec := githubapi.ExecCommand