|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package cli |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/github/gh-aw/pkg/fileutil" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +type addWizardTuistorySetup struct { |
| 20 | + tempDir string |
| 21 | + binaryPath string |
| 22 | + workflowPath string |
| 23 | +} |
| 24 | + |
| 25 | +func setupAddWizardTuistoryTest(t *testing.T) *addWizardTuistorySetup { |
| 26 | + t.Helper() |
| 27 | + |
| 28 | + tempDir, err := os.MkdirTemp("", "gh-aw-add-wizard-tuistory-*") |
| 29 | + require.NoError(t, err, "Failed to create temp directory") |
| 30 | + |
| 31 | + // Initialize git repository required by add-wizard preconditions. |
| 32 | + gitInit := exec.Command("git", "init") |
| 33 | + gitInit.Dir = tempDir |
| 34 | + output, err := gitInit.CombinedOutput() |
| 35 | + require.NoError(t, err, "Failed to initialize git repository: %s", string(output)) |
| 36 | + |
| 37 | + gitConfigName := exec.Command("git", "config", "user.name", "Test User") |
| 38 | + gitConfigName.Dir = tempDir |
| 39 | + _ = gitConfigName.Run() |
| 40 | + |
| 41 | + gitConfigEmail := exec.Command("git", "config", "user.email", "test@example.com") |
| 42 | + gitConfigEmail.Dir = tempDir |
| 43 | + _ = gitConfigEmail.Run() |
| 44 | + |
| 45 | + binaryPath := filepath.Join(tempDir, "gh-aw") |
| 46 | + err = fileutil.CopyFile(globalBinaryPath, binaryPath) |
| 47 | + require.NoError(t, err, "Failed to copy gh-aw binary") |
| 48 | + |
| 49 | + err = os.Chmod(binaryPath, 0755) |
| 50 | + require.NoError(t, err, "Failed to make gh-aw binary executable") |
| 51 | + |
| 52 | + workflowPath := filepath.Join(tempDir, "local-test-workflow.md") |
| 53 | + workflowContent := `--- |
| 54 | +name: Local Add Wizard Integration |
| 55 | +on: |
| 56 | + workflow_dispatch: |
| 57 | +engine: copilot |
| 58 | +--- |
| 59 | +
|
| 60 | +# Local Add Wizard Integration |
| 61 | +
|
| 62 | +This workflow is used by add-wizard tuistory integration tests. |
| 63 | +` |
| 64 | + err = os.WriteFile(workflowPath, []byte(workflowContent), 0644) |
| 65 | + require.NoError(t, err, "Failed to write local workflow fixture") |
| 66 | + |
| 67 | + return &addWizardTuistorySetup{ |
| 68 | + tempDir: tempDir, |
| 69 | + binaryPath: binaryPath, |
| 70 | + workflowPath: workflowPath, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func runTuistory(t *testing.T, args ...string) (string, error) { |
| 75 | + t.Helper() |
| 76 | + |
| 77 | + cmd := exec.Command("npx", append([]string{"-y", "tuistory"}, args...)...) |
| 78 | + output, err := cmd.CombinedOutput() |
| 79 | + return string(output), err |
| 80 | +} |
| 81 | + |
| 82 | +func waitForTuistoryText(t *testing.T, sessionName string, text string, timeoutMs int) { |
| 83 | + t.Helper() |
| 84 | + output, err := runTuistory(t, "-s", sessionName, "wait", text, "--timeout", fmt.Sprintf("%d", timeoutMs)) |
| 85 | + require.NoError(t, err, "Expected tuistory to find %q. Output: %s", text, output) |
| 86 | +} |
| 87 | + |
| 88 | +func TestTuistoryAddWizardIntegration(t *testing.T) { |
| 89 | + const launchTimeoutMs = 30000 // 30 seconds |
| 90 | + |
| 91 | + if _, err := exec.LookPath("npx"); err != nil { |
| 92 | + t.Skip("npx not available, skipping tuistory add-wizard integration test") |
| 93 | + } |
| 94 | + |
| 95 | + versionOutput, err := runTuistory(t, "--version") |
| 96 | + if err != nil { |
| 97 | + t.Skipf("tuistory is not usable in this environment: %v (%s)", err, versionOutput) |
| 98 | + } |
| 99 | + |
| 100 | + setup := setupAddWizardTuistoryTest(t) |
| 101 | + defer func() { |
| 102 | + _ = os.RemoveAll(setup.tempDir) |
| 103 | + }() |
| 104 | + |
| 105 | + sessionName := fmt.Sprintf("gh-aw-add-wizard-%d", time.Now().UnixNano()) |
| 106 | + command := fmt.Sprintf("%s add-wizard ./%s --engine copilot --skip-secret", setup.binaryPath, filepath.Base(setup.workflowPath)) |
| 107 | + |
| 108 | + launchArgs := []string{ |
| 109 | + "launch", command, |
| 110 | + "-s", sessionName, |
| 111 | + "--cwd", setup.tempDir, |
| 112 | + "--cols", "140", |
| 113 | + "--rows", "40", |
| 114 | + "--env", "CI=", |
| 115 | + "--env", "GO_TEST_MODE=", |
| 116 | + "--timeout", fmt.Sprintf("%d", launchTimeoutMs), |
| 117 | + } |
| 118 | + |
| 119 | + launchOutput, err := runTuistory(t, launchArgs...) |
| 120 | + if err != nil { |
| 121 | + t.Skipf("tuistory launch is not usable in this environment: %v (%s)", err, launchOutput) |
| 122 | + } |
| 123 | + |
| 124 | + defer func() { |
| 125 | + _, _ = runTuistory(t, "-s", sessionName, "close") |
| 126 | + }() |
| 127 | + |
| 128 | + // No git remote in the test repository forces add-wizard to prompt for owner/repo. |
| 129 | + waitForTuistoryText(t, sessionName, "Enter the target repository (owner/repo):", 120000) |
| 130 | + |
| 131 | + typeOutput, err := runTuistory(t, "-s", sessionName, "type", "github/gh-aw") |
| 132 | + require.NoError(t, err, "Failed to type repository slug. Output: %s", typeOutput) |
| 133 | + |
| 134 | + enterOutput, err := runTuistory(t, "-s", sessionName, "press", "enter") |
| 135 | + require.NoError(t, err, "Failed to press enter after repository slug. Output: %s", enterOutput) |
| 136 | + |
| 137 | + waitForTuistoryText(t, sessionName, "Do you want to proceed with these changes?", 120000) |
| 138 | + |
| 139 | + cancelOutput, err := runTuistory(t, "-s", sessionName, "press", "ctrl", "c") |
| 140 | + require.NoError(t, err, "Failed to send Ctrl+C to add-wizard session. Output: %s", cancelOutput) |
| 141 | + |
| 142 | + // Collect complete session output and assert cancellation occurred before changes were applied. |
| 143 | + readOutput, err := runTuistory(t, "-s", sessionName, "read", "--all") |
| 144 | + require.NoError(t, err, "Failed to read tuistory output after cancellation") |
| 145 | + assert.True(t, |
| 146 | + strings.Contains(readOutput, "confirmation failed") || strings.Contains(readOutput, "interrupted"), |
| 147 | + "Expected cancellation-related output, got:\n%s", |
| 148 | + readOutput, |
| 149 | + ) |
| 150 | + |
| 151 | + addedWorkflowPath := filepath.Join(setup.tempDir, ".github", "workflows", filepath.Base(setup.workflowPath)) |
| 152 | + _, statErr := os.Stat(addedWorkflowPath) |
| 153 | + assert.ErrorIs(t, statErr, os.ErrNotExist, "Workflow file should not be created when add-wizard is cancelled") |
| 154 | +} |
0 commit comments