|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "reflect" |
| 7 | + "sort" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +// Helper to create files and directories for testing |
| 12 | +func createTestFiles(t *testing.T, root string, files map[string]string) { |
| 13 | + for path, content := range files { |
| 14 | + fullPath := filepath.Join(root, path) |
| 15 | + dir := filepath.Dir(fullPath) |
| 16 | + if err := os.MkdirAll(dir, 0o755); err != nil { |
| 17 | + t.Fatalf("failed to create dir %s: %v", dir, err) |
| 18 | + } |
| 19 | + if err := os.WriteFile(fullPath, []byte(content), 0o644); err != nil { |
| 20 | + t.Fatalf("failed to write file %s: %v", fullPath, err) |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestListFiles_NonRecursive(t *testing.T) { |
| 26 | + tmpDir := t.TempDir() |
| 27 | + files := map[string]string{ |
| 28 | + "file1.yaml": "a: b", |
| 29 | + "file2.yml": "c: d", |
| 30 | + "file3.json": "{}", |
| 31 | + "file4.txt": "not yaml", |
| 32 | + "subdir/file5.yaml": "e: f", |
| 33 | + } |
| 34 | + createTestFiles(t, tmpDir, files) |
| 35 | + |
| 36 | + got, err := listFiles(tmpDir, true) |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("unexpected error: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + want := []string{ |
| 42 | + filepath.Join(tmpDir, "file1.yaml"), |
| 43 | + filepath.Join(tmpDir, "file2.yml"), |
| 44 | + filepath.Join(tmpDir, "file3.json"), |
| 45 | + } |
| 46 | + sort.Strings(got) |
| 47 | + sort.Strings(want) |
| 48 | + if !reflect.DeepEqual(got, want) { |
| 49 | + t.Errorf("expected %v, got %v", want, got) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestListFiles_Recursive(t *testing.T) { |
| 54 | + tmpDir := t.TempDir() |
| 55 | + files := map[string]string{ |
| 56 | + "file1.yaml": "a: b", |
| 57 | + "file2.yml": "c: d", |
| 58 | + "file3.json": "{}", |
| 59 | + "file4.txt": "not yaml", |
| 60 | + "subdir/file5.yaml": "e: f", |
| 61 | + "subdir/file6.json": "{}", |
| 62 | + "subdir2/file7.yml": "g: h", |
| 63 | + "subdir2/file8.txt": "not yaml", |
| 64 | + } |
| 65 | + createTestFiles(t, tmpDir, files) |
| 66 | + |
| 67 | + got, err := listFiles(tmpDir, false) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("unexpected error: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + want := []string{ |
| 73 | + filepath.Join(tmpDir, "file1.yaml"), |
| 74 | + filepath.Join(tmpDir, "file2.yml"), |
| 75 | + filepath.Join(tmpDir, "file3.json"), |
| 76 | + filepath.Join(tmpDir, "subdir", "file5.yaml"), |
| 77 | + filepath.Join(tmpDir, "subdir", "file6.json"), |
| 78 | + filepath.Join(tmpDir, "subdir2", "file7.yml"), |
| 79 | + } |
| 80 | + sort.Strings(got) |
| 81 | + sort.Strings(want) |
| 82 | + if !reflect.DeepEqual(got, want) { |
| 83 | + t.Errorf("expected %v, got %v", want, got) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestListFiles_Error(t *testing.T) { |
| 88 | + // Non-existent directory should return an error |
| 89 | + _, err := listFiles("/non/existent/dir", false) |
| 90 | + if err == nil { |
| 91 | + t.Error("expected error for non-existent directory, got nil") |
| 92 | + } |
| 93 | +} |
0 commit comments