|
| 1 | +//go:build !integration |
| 2 | + |
| 3 | +package cli |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestExpandCompileArg_RegularFile(t *testing.T) { |
| 16 | + // A plain workflow name should be returned as-is |
| 17 | + result, err := expandCompileArg("my-workflow", false) |
| 18 | + require.NoError(t, err, "plain workflow name should not error") |
| 19 | + assert.Equal(t, []string{"my-workflow"}, result, "plain workflow name should be returned unchanged") |
| 20 | +} |
| 21 | + |
| 22 | +func TestExpandCompileArg_FilePath(t *testing.T) { |
| 23 | + // A non-existent file path should be returned as-is |
| 24 | + result, err := expandCompileArg(".github/workflows/my-workflow.md", false) |
| 25 | + require.NoError(t, err, "non-existent file path should not error") |
| 26 | + assert.Equal(t, []string{".github/workflows/my-workflow.md"}, result, "file path should be returned unchanged") |
| 27 | +} |
| 28 | + |
| 29 | +func TestExpandCompileArg_LocalDirectory(t *testing.T) { |
| 30 | + // Create a temp directory with workflow files |
| 31 | + tmpDir := t.TempDir() |
| 32 | + writeWorkflowFile(t, tmpDir, "workflow-a.md") |
| 33 | + writeWorkflowFile(t, tmpDir, "workflow-b.md") |
| 34 | + |
| 35 | + result, err := expandCompileArg(tmpDir, false) |
| 36 | + require.NoError(t, err, "local directory should expand without error") |
| 37 | + assert.Len(t, result, 2, "should return all .md files in the directory") |
| 38 | +} |
| 39 | + |
| 40 | +func TestExpandCompileArg_LocalDirectory_Empty(t *testing.T) { |
| 41 | + // Directory with no .md files should error |
| 42 | + tmpDir := t.TempDir() |
| 43 | + _, err := expandCompileArg(tmpDir, false) |
| 44 | + assert.Error(t, err, "empty directory should return an error") |
| 45 | + assert.Contains(t, err.Error(), "no workflow markdown files found", "error should mention no workflow files") |
| 46 | +} |
| 47 | + |
| 48 | +func TestExpandCompileArg_URLPassthrough(t *testing.T) { |
| 49 | + // URLs should be returned as-is (not processed) |
| 50 | + url := "https://github.com/org/repo/tree/main/.github/workflows" |
| 51 | + result, err := expandCompileArg(url, false) |
| 52 | + require.NoError(t, err, "URL should not error") |
| 53 | + assert.Equal(t, []string{url}, result, "URL should be returned unchanged") |
| 54 | +} |
| 55 | + |
| 56 | +func TestResolveCompileArgs_Empty(t *testing.T) { |
| 57 | + result, err := resolveCompileArgs(nil, false) |
| 58 | + require.NoError(t, err) |
| 59 | + assert.Empty(t, result) |
| 60 | +} |
| 61 | + |
| 62 | +func TestResolveCompileArgs_Mixed(t *testing.T) { |
| 63 | + // Create a temp directory with a workflow file |
| 64 | + tmpDir := t.TempDir() |
| 65 | + writeWorkflowFile(t, tmpDir, "workflow-a.md") |
| 66 | + |
| 67 | + // Mix: a plain workflow name + a directory |
| 68 | + result, err := resolveCompileArgs([]string{"plain-workflow", tmpDir}, false) |
| 69 | + require.NoError(t, err, "mixed args should expand without error") |
| 70 | + require.Len(t, result, 2, "should have one plain name + one expanded file") |
| 71 | + |
| 72 | + // The plain workflow name should be unchanged |
| 73 | + assert.Equal(t, "plain-workflow", result[0], "first arg should be unchanged") |
| 74 | + // The directory arg should be the single .md file in tmpDir |
| 75 | + assert.True(t, strings.HasSuffix(result[1], "workflow-a.md"), "second arg should be the expanded .md file") |
| 76 | +} |
| 77 | + |
| 78 | +// writeWorkflowFile creates a minimal workflow .md file in dir with the given name. |
| 79 | +func writeWorkflowFile(t *testing.T, dir, name string) { |
| 80 | + t.Helper() |
| 81 | + content := "---\non: push\n---\n\n# Test Workflow\n" |
| 82 | + require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(content), 0644), |
| 83 | + "write workflow file %s", name) |
| 84 | +} |
0 commit comments