|
1 | 1 | package owners |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
4 | 6 | "testing" |
5 | 7 | ) |
6 | 8 |
|
7 | | -func TestReadCodeownersConfig(t *testing.T) { |
8 | | - conf, err := ReadConfig("../../test_project/") |
9 | | - if err != nil { |
10 | | - t.Errorf("Error reading codeowners config: %v", err) |
11 | | - return |
12 | | - } |
13 | | - if conf == nil { |
14 | | - t.Errorf("Error reading codeowners config") |
15 | | - return |
16 | | - } |
17 | | - // if *conf.MaxReviews != 2 { |
18 | | - // t.Errorf("Expected max reviews 2, got %d", *conf.MaxReviews) |
19 | | - // } |
20 | | - if conf.MaxReviews != nil { |
21 | | - t.Errorf("Expected max reviews to be nil, got %d", *conf.MaxReviews) |
22 | | - } |
23 | | - if len(conf.UnskippableReviewers) != 2 { |
24 | | - t.Errorf("Expected 2 unskippable reviewers, got %d", len(conf.UnskippableReviewers)) |
25 | | - } |
26 | | - if conf.UnskippableReviewers[0] != "@user1" { |
27 | | - t.Errorf("Expected unskippable reviewer @user1, got %s", conf.UnskippableReviewers[0]) |
28 | | - } |
29 | | - if conf.UnskippableReviewers[1] != "@user2" { |
30 | | - t.Errorf("Expected unskippable reviewer @user2, got %s", conf.UnskippableReviewers[0]) |
| 9 | +func TestReadConfig(t *testing.T) { |
| 10 | + tt := []struct { |
| 11 | + name string |
| 12 | + configContent string |
| 13 | + path string |
| 14 | + expected *Config |
| 15 | + expectedErr bool |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "default config when no file exists", |
| 19 | + path: "nonexistent/", |
| 20 | + expected: &Config{ |
| 21 | + MaxReviews: nil, |
| 22 | + MinReviews: nil, |
| 23 | + UnskippableReviewers: []string{}, |
| 24 | + Ignore: []string{}, |
| 25 | + Enforcement: &Enforcement{Approval: false, FailCheck: true}, |
| 26 | + }, |
| 27 | + expectedErr: false, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "valid config with all fields", |
| 31 | + configContent: ` |
| 32 | +max_reviews = 2 |
| 33 | +min_reviews = 1 |
| 34 | +unskippable_reviewers = ["@user1", "@user2"] |
| 35 | +ignore = ["ignored/"] |
| 36 | +[enforcement] |
| 37 | +approval = true |
| 38 | +fail_check = false |
| 39 | +`, |
| 40 | + path: "testdata/", |
| 41 | + expected: &Config{ |
| 42 | + MaxReviews: intPtr(2), |
| 43 | + MinReviews: intPtr(1), |
| 44 | + UnskippableReviewers: []string{"@user1", "@user2"}, |
| 45 | + Ignore: []string{"ignored/"}, |
| 46 | + Enforcement: &Enforcement{Approval: true, FailCheck: false}, |
| 47 | + }, |
| 48 | + expectedErr: false, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "partial config with defaults", |
| 52 | + configContent: ` |
| 53 | +max_reviews = 3 |
| 54 | +unskippable_reviewers = ["@user1"] |
| 55 | +`, |
| 56 | + path: "testdata/", |
| 57 | + expected: &Config{ |
| 58 | + MaxReviews: intPtr(3), |
| 59 | + MinReviews: nil, |
| 60 | + UnskippableReviewers: []string{"@user1"}, |
| 61 | + Ignore: []string{}, |
| 62 | + Enforcement: &Enforcement{Approval: false, FailCheck: true}, |
| 63 | + }, |
| 64 | + expectedErr: false, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "invalid toml", |
| 68 | + configContent: ` |
| 69 | +max_reviews = invalid |
| 70 | +`, |
| 71 | + path: "testdata/", |
| 72 | + expectedErr: true, |
| 73 | + }, |
31 | 74 | } |
32 | | - if len(conf.Ignore) != 1 { |
33 | | - t.Errorf("Expected 1 ignore dir, got %d", len(conf.Ignore)) |
| 75 | + |
| 76 | + for _, tc := range tt { |
| 77 | + t.Run(tc.name, func(t *testing.T) { |
| 78 | + // Create test directory |
| 79 | + testDir := t.TempDir() |
| 80 | + configPath := filepath.Join(testDir, tc.path) |
| 81 | + |
| 82 | + // Create config file if content is provided |
| 83 | + if tc.configContent != "" { |
| 84 | + err := os.MkdirAll(configPath, 0755) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("failed to create test directory: %v", err) |
| 87 | + } |
| 88 | + err = os.WriteFile(filepath.Join(configPath, "codeowners.toml"), []byte(tc.configContent), 0644) |
| 89 | + if err != nil { |
| 90 | + t.Fatalf("failed to write test config: %v", err) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Test with and without trailing slash |
| 95 | + paths := []string{configPath, configPath + "/"} |
| 96 | + for _, path := range paths { |
| 97 | + got, err := ReadConfig(path) |
| 98 | + if tc.expectedErr { |
| 99 | + if err == nil { |
| 100 | + t.Error("expected error but got none") |
| 101 | + } |
| 102 | + continue |
| 103 | + } |
| 104 | + |
| 105 | + if err != nil { |
| 106 | + t.Errorf("unexpected error: %v", err) |
| 107 | + continue |
| 108 | + } |
| 109 | + |
| 110 | + if got == nil { |
| 111 | + t.Error("got nil config") |
| 112 | + continue |
| 113 | + } |
| 114 | + |
| 115 | + // Compare fields |
| 116 | + if tc.expected.MaxReviews != nil { |
| 117 | + if got.MaxReviews == nil { |
| 118 | + t.Error("expected MaxReviews to be set") |
| 119 | + } else if *got.MaxReviews != *tc.expected.MaxReviews { |
| 120 | + t.Errorf("MaxReviews: expected %d, got %d", *tc.expected.MaxReviews, *got.MaxReviews) |
| 121 | + } |
| 122 | + } else if got.MaxReviews != nil { |
| 123 | + t.Errorf("MaxReviews: expected nil, got %d", *got.MaxReviews) |
| 124 | + } |
| 125 | + |
| 126 | + if tc.expected.MinReviews != nil { |
| 127 | + if got.MinReviews == nil { |
| 128 | + t.Error("expected MinReviews to be set") |
| 129 | + } else if *got.MinReviews != *tc.expected.MinReviews { |
| 130 | + t.Errorf("MinReviews: expected %d, got %d", *tc.expected.MinReviews, *got.MinReviews) |
| 131 | + } |
| 132 | + } else if got.MinReviews != nil { |
| 133 | + t.Errorf("MinReviews: expected nil, got %d", *got.MinReviews) |
| 134 | + } |
| 135 | + |
| 136 | + if !sliceEqual(got.UnskippableReviewers, tc.expected.UnskippableReviewers) { |
| 137 | + t.Errorf("UnskippableReviewers: expected %v, got %v", tc.expected.UnskippableReviewers, got.UnskippableReviewers) |
| 138 | + } |
| 139 | + |
| 140 | + if !sliceEqual(got.Ignore, tc.expected.Ignore) { |
| 141 | + t.Errorf("Ignore: expected %v, got %v", tc.expected.Ignore, got.Ignore) |
| 142 | + } |
| 143 | + |
| 144 | + if tc.expected.Enforcement != nil { |
| 145 | + if got.Enforcement == nil { |
| 146 | + t.Error("expected Enforcement to be set") |
| 147 | + } else { |
| 148 | + if got.Enforcement.Approval != tc.expected.Enforcement.Approval { |
| 149 | + t.Errorf("Enforcement.Approval: expected %v, got %v", tc.expected.Enforcement.Approval, got.Enforcement.Approval) |
| 150 | + } |
| 151 | + if got.Enforcement.FailCheck != tc.expected.Enforcement.FailCheck { |
| 152 | + t.Errorf("Enforcement.FailCheck: expected %v, got %v", tc.expected.Enforcement.FailCheck, got.Enforcement.FailCheck) |
| 153 | + } |
| 154 | + } |
| 155 | + } else if got.Enforcement != nil { |
| 156 | + t.Error("Enforcement: expected nil, got non-nil") |
| 157 | + } |
| 158 | + } |
| 159 | + }) |
34 | 160 | } |
35 | | - if conf.Ignore[0] != "ignored" { |
36 | | - t.Errorf("Expected ignore dir ignored, got %s", conf.Ignore[0]) |
| 161 | +} |
| 162 | + |
| 163 | +func TestReadConfigFileError(t *testing.T) { |
| 164 | + // Create a directory with no read permissions |
| 165 | + testDir := t.TempDir() |
| 166 | + configPath := filepath.Join(testDir, "test/") |
| 167 | + err := os.MkdirAll(configPath, 0000) |
| 168 | + if err != nil { |
| 169 | + t.Fatalf("failed to create test directory: %v", err) |
37 | 170 | } |
38 | | - if conf.Enforcement == nil { |
39 | | - t.Errorf("Expected enforcement to be set") |
| 171 | + |
| 172 | + // Try to read config from directory with no permissions |
| 173 | + _, err = ReadConfig(configPath) |
| 174 | + if err == nil { |
| 175 | + t.Error("expected error when reading from directory with no permissions") |
40 | 176 | } |
41 | | - if conf.Enforcement.Approval != false { |
42 | | - t.Errorf("Expected Approval to be false") |
| 177 | +} |
| 178 | + |
| 179 | +// Helper functions |
| 180 | +func intPtr(i int) *int { |
| 181 | + return &i |
| 182 | +} |
| 183 | + |
| 184 | +func sliceEqual(a, b []string) bool { |
| 185 | + if len(a) != len(b) { |
| 186 | + return false |
43 | 187 | } |
44 | | - if conf.Enforcement.FailCheck != true { |
45 | | - t.Errorf("Expected FailCheck to be true") |
| 188 | + for i := range a { |
| 189 | + if a[i] != b[i] { |
| 190 | + return false |
| 191 | + } |
46 | 192 | } |
| 193 | + return true |
47 | 194 | } |
0 commit comments