|
| 1 | +package github_app |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "reflect" |
| 7 | + "sort" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestGetFilesToCommit(t *testing.T) { |
| 12 | + // Create a temporary directory structure for testing |
| 13 | + tmpDir, err := os.MkdirTemp("", "gitops-test") |
| 14 | + if err != nil { |
| 15 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 16 | + } |
| 17 | + defer os.RemoveAll(tmpDir) |
| 18 | + |
| 19 | + // Create test directory structure |
| 20 | + testDirs := []string{ |
| 21 | + "buildkite-agents/ai-services/config", |
| 22 | + "buildkite-agents/airflow-dev/settings", |
| 23 | + "buildkite-agents/single-file-dir", |
| 24 | + } |
| 25 | + |
| 26 | + testFiles := []string{ |
| 27 | + "buildkite-agents/ai-services/config/file1.yaml", |
| 28 | + "buildkite-agents/ai-services/config/file2.yaml", |
| 29 | + "buildkite-agents/airflow-dev/settings/settings.json", |
| 30 | + "buildkite-agents/single-file-dir/single.txt", |
| 31 | + } |
| 32 | + |
| 33 | + // Create directories |
| 34 | + for _, dir := range testDirs { |
| 35 | + err := os.MkdirAll(filepath.Join(tmpDir, dir), 0755) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("Failed to create directory %s: %v", dir, err) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // Create files with some content |
| 42 | + for _, file := range testFiles { |
| 43 | + err := os.WriteFile( |
| 44 | + filepath.Join(tmpDir, file), |
| 45 | + []byte("test content"), |
| 46 | + 0644, |
| 47 | + ) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("Failed to create file %s: %v", file, err) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + tests := []struct { |
| 54 | + name string |
| 55 | + inputPaths []string |
| 56 | + expectedFiles []string |
| 57 | + expectedError bool |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "Single directory with multiple files", |
| 61 | + inputPaths: []string{ |
| 62 | + "buildkite-agents/ai-services", |
| 63 | + }, |
| 64 | + expectedFiles: []string{ |
| 65 | + "buildkite-agents/ai-services/config/file1.yaml", |
| 66 | + "buildkite-agents/ai-services/config/file2.yaml", |
| 67 | + }, |
| 68 | + expectedError: false, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "Multiple directories", |
| 72 | + inputPaths: []string{ |
| 73 | + "buildkite-agents/ai-services", |
| 74 | + "buildkite-agents/airflow-dev", |
| 75 | + }, |
| 76 | + expectedFiles: []string{ |
| 77 | + "buildkite-agents/ai-services/config/file1.yaml", |
| 78 | + "buildkite-agents/ai-services/config/file2.yaml", |
| 79 | + "buildkite-agents/airflow-dev/settings/settings.json", |
| 80 | + }, |
| 81 | + expectedError: false, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "Single file", |
| 85 | + inputPaths: []string{ |
| 86 | + "buildkite-agents/single-file-dir/single.txt", |
| 87 | + }, |
| 88 | + expectedFiles: []string{ |
| 89 | + "buildkite-agents/single-file-dir/single.txt", |
| 90 | + }, |
| 91 | + expectedError: false, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "Mixed files and directories", |
| 95 | + inputPaths: []string{ |
| 96 | + "buildkite-agents/ai-services", |
| 97 | + "buildkite-agents/single-file-dir/single.txt", |
| 98 | + }, |
| 99 | + expectedFiles: []string{ |
| 100 | + "buildkite-agents/ai-services/config/file1.yaml", |
| 101 | + "buildkite-agents/ai-services/config/file2.yaml", |
| 102 | + "buildkite-agents/single-file-dir/single.txt", |
| 103 | + }, |
| 104 | + expectedError: false, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "Non-existent path", |
| 108 | + inputPaths: []string{ |
| 109 | + "buildkite-agents/non-existent", |
| 110 | + }, |
| 111 | + expectedFiles: nil, |
| 112 | + expectedError: true, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + for _, tt := range tests { |
| 117 | + t.Run(tt.name, func(t *testing.T) { |
| 118 | + fileEntries, err := getFilesToCommit(tmpDir, tt.inputPaths) |
| 119 | + |
| 120 | + // Check error condition |
| 121 | + if (err != nil) != tt.expectedError { |
| 122 | + t.Errorf("getFilesToCommit() error = %v, expectedError %v", err, tt.expectedError) |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + if tt.expectedError { |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + // Get actual relative paths |
| 131 | + var actualFiles []string |
| 132 | + for _, entry := range fileEntries { |
| 133 | + actualFiles = append(actualFiles, entry.RelativePath) |
| 134 | + } |
| 135 | + |
| 136 | + // Sort both slices for comparison |
| 137 | + sort.Strings(actualFiles) |
| 138 | + sort.Strings(tt.expectedFiles) |
| 139 | + |
| 140 | + // Compare results |
| 141 | + if !reflect.DeepEqual(actualFiles, tt.expectedFiles) { |
| 142 | + t.Errorf("getFilesToCommit() got = %v, want %v", actualFiles, tt.expectedFiles) |
| 143 | + } |
| 144 | + |
| 145 | + // Verify that FullPath exists for each entry |
| 146 | + for _, entry := range fileEntries { |
| 147 | + if _, err := os.Stat(entry.FullPath); os.IsNotExist(err) { |
| 148 | + t.Errorf("FullPath %s does not exist", entry.FullPath) |
| 149 | + } |
| 150 | + } |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
0 commit comments