|
3 | 3 | package cli |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "bytes" |
6 | 7 | "encoding/json" |
| 8 | + "io" |
7 | 9 | "os" |
8 | 10 | "path/filepath" |
9 | 11 | "testing" |
10 | 12 |
|
| 13 | + "github.com/github/gh-aw/pkg/parser" |
11 | 14 | "github.com/stretchr/testify/assert" |
12 | 15 | "github.com/stretchr/testify/require" |
13 | 16 | ) |
@@ -120,3 +123,88 @@ func TestNewListCommand(t *testing.T) { |
120 | 123 | labelFlag := cmd.Flags().Lookup("label") |
121 | 124 | assert.NotNil(t, labelFlag, "Command should have --label flag") |
122 | 125 | } |
| 126 | + |
| 127 | +// captureListOutput calls RunListWorkflows and returns the captured stdout as a string. |
| 128 | +func captureListOutput(t *testing.T, dir, pattern string) string { |
| 129 | + t.Helper() |
| 130 | + oldStdout := os.Stdout |
| 131 | + r, w, err := os.Pipe() |
| 132 | + require.NoError(t, err, "Should create pipe") |
| 133 | + os.Stdout = w |
| 134 | + |
| 135 | + runErr := RunListWorkflows("", dir, pattern, false, true, "") |
| 136 | + |
| 137 | + w.Close() |
| 138 | + os.Stdout = oldStdout |
| 139 | + |
| 140 | + var buf bytes.Buffer |
| 141 | + _, _ = io.Copy(&buf, r) |
| 142 | + require.NoError(t, runErr, "RunListWorkflows should not error") |
| 143 | + return buf.String() |
| 144 | +} |
| 145 | + |
| 146 | +func TestRunListWorkflows_CompiledField(t *testing.T) { |
| 147 | + tmpDir := t.TempDir() |
| 148 | + |
| 149 | + // Minimal valid workflow markdown |
| 150 | + mdContent := `--- |
| 151 | +name: test-workflow |
| 152 | +on: |
| 153 | + workflow_dispatch: |
| 154 | +engine: copilot |
| 155 | +--- |
| 156 | +
|
| 157 | +# Test Workflow |
| 158 | +
|
| 159 | +A simple test workflow. |
| 160 | +` |
| 161 | + mdPath := filepath.Join(tmpDir, "test-workflow.md") |
| 162 | + require.NoError(t, os.WriteFile(mdPath, []byte(mdContent), 0644), "Should write test workflow") |
| 163 | + |
| 164 | + // Compute the real frontmatter hash |
| 165 | + cache := parser.NewImportCache("") |
| 166 | + realHash, err := parser.ComputeFrontmatterHashFromFile(mdPath, cache) |
| 167 | + require.NoError(t, err, "Should compute frontmatter hash") |
| 168 | + |
| 169 | + t.Run("compiled Yes when hash matches", func(t *testing.T) { |
| 170 | + lockPath := filepath.Join(tmpDir, "test-workflow.lock.yml") |
| 171 | + lockContent := `# gh-aw-metadata: {"schema_version":"v1","frontmatter_hash":"` + realHash + `"} |
| 172 | +name: test-workflow |
| 173 | +` |
| 174 | + require.NoError(t, os.WriteFile(lockPath, []byte(lockContent), 0644), "Should write lock file") |
| 175 | + |
| 176 | + output := captureListOutput(t, tmpDir, "") |
| 177 | + |
| 178 | + var items []WorkflowListItem |
| 179 | + require.NoError(t, json.Unmarshal([]byte(output), &items), "Should unmarshal JSON output") |
| 180 | + require.Len(t, items, 1, "Should have one workflow") |
| 181 | + assert.Equal(t, "Yes", items[0].Compiled, "Compiled should be Yes when hash matches") |
| 182 | + }) |
| 183 | + |
| 184 | + t.Run("compiled No when hash mismatches", func(t *testing.T) { |
| 185 | + lockPath := filepath.Join(tmpDir, "test-workflow.lock.yml") |
| 186 | + lockContent := `# gh-aw-metadata: {"schema_version":"v1","frontmatter_hash":"0000000000000000000000000000000000000000000000000000000000000000"} |
| 187 | +name: test-workflow |
| 188 | +` |
| 189 | + require.NoError(t, os.WriteFile(lockPath, []byte(lockContent), 0644), "Should write lock file with wrong hash") |
| 190 | + |
| 191 | + output := captureListOutput(t, tmpDir, "") |
| 192 | + |
| 193 | + var items []WorkflowListItem |
| 194 | + require.NoError(t, json.Unmarshal([]byte(output), &items), "Should unmarshal JSON output") |
| 195 | + require.Len(t, items, 1, "Should have one workflow") |
| 196 | + assert.Equal(t, "No", items[0].Compiled, "Compiled should be No when hash mismatches") |
| 197 | + }) |
| 198 | + |
| 199 | + t.Run("compiled NA when no lock file", func(t *testing.T) { |
| 200 | + // Remove lock file |
| 201 | + _ = os.Remove(filepath.Join(tmpDir, "test-workflow.lock.yml")) |
| 202 | + |
| 203 | + output := captureListOutput(t, tmpDir, "") |
| 204 | + |
| 205 | + var items []WorkflowListItem |
| 206 | + require.NoError(t, json.Unmarshal([]byte(output), &items), "Should unmarshal JSON output") |
| 207 | + require.Len(t, items, 1, "Should have one workflow") |
| 208 | + assert.Equal(t, "N/A", items[0].Compiled, "Compiled should be N/A when no lock file exists") |
| 209 | + }) |
| 210 | +} |
0 commit comments