|
| 1 | +package scm |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestFilterGitlabGroupByMatchRegex(t *testing.T) { |
| 9 | + testCases := []struct { |
| 10 | + name string |
| 11 | + regex string |
| 12 | + groups []string |
| 13 | + expectedGroups []string |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "matches specific group names", |
| 17 | + regex: "^(subgroup-a|subgroup-b)$", |
| 18 | + groups: []string{"subgroup-a", "subgroup-b", "subgroup-c", "subgroup-d"}, |
| 19 | + expectedGroups: []string{"subgroup-a", "subgroup-b"}, |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "matches groups with partial regex", |
| 23 | + regex: "subgroup-a", |
| 24 | + groups: []string{"subgroup-a", "subgroup-ab", "subgroup-b"}, |
| 25 | + expectedGroups: []string{"subgroup-a", "subgroup-ab"}, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "no matches returns empty", |
| 29 | + regex: "^nonexistent$", |
| 30 | + groups: []string{"subgroup-a", "subgroup-b"}, |
| 31 | + expectedGroups: []string{}, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "matches all groups", |
| 35 | + regex: ".*", |
| 36 | + groups: []string{"subgroup-a", "subgroup-b", "subgroup-c"}, |
| 37 | + expectedGroups: []string{"subgroup-a", "subgroup-b", "subgroup-c"}, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "case insensitive matching", |
| 41 | + regex: "(?i:^SUBGROUP-A$)", |
| 42 | + groups: []string{"subgroup-a", "subgroup-b", "Subgroup-A"}, |
| 43 | + expectedGroups: []string{"subgroup-a", "Subgroup-A"}, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "matches numeric group IDs", |
| 47 | + regex: "^(123|456)$", |
| 48 | + groups: []string{"123", "456", "789", "101"}, |
| 49 | + expectedGroups: []string{"123", "456"}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "empty groups list", |
| 53 | + regex: "anything", |
| 54 | + groups: []string{}, |
| 55 | + expectedGroups: []string{}, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "matches groups with path-like names", |
| 59 | + regex: "my-org/subgroup", |
| 60 | + groups: []string{"my-org/subgroup-a", "my-org/subgroup-b", "other-org/group-c"}, |
| 61 | + expectedGroups: []string{"my-org/subgroup-a", "my-org/subgroup-b"}, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tc := range testCases { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + os.Setenv("GHORG_GITLAB_GROUP_MATCH_REGEX", tc.regex) |
| 68 | + defer os.Unsetenv("GHORG_GITLAB_GROUP_MATCH_REGEX") |
| 69 | + |
| 70 | + result := filterGitlabGroupByMatchRegex(tc.groups) |
| 71 | + if len(result) == 0 && len(tc.expectedGroups) == 0 { |
| 72 | + return // Both empty, test passes |
| 73 | + } |
| 74 | + if len(result) != len(tc.expectedGroups) { |
| 75 | + t.Errorf("Expected %d groups, got %d. Expected: %v, Got: %v", |
| 76 | + len(tc.expectedGroups), len(result), tc.expectedGroups, result) |
| 77 | + return |
| 78 | + } |
| 79 | + for i, group := range result { |
| 80 | + if group != tc.expectedGroups[i] { |
| 81 | + t.Errorf("Expected group at index %d to be %s, got %s", i, tc.expectedGroups[i], group) |
| 82 | + } |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestFilterGitlabGroupByExcludeMatchRegex(t *testing.T) { |
| 89 | + testCases := []struct { |
| 90 | + name string |
| 91 | + regex string |
| 92 | + groups []string |
| 93 | + expectedGroups []string |
| 94 | + }{ |
| 95 | + { |
| 96 | + name: "excludes specific group names", |
| 97 | + regex: "^(subgroup-a|subgroup-b)$", |
| 98 | + groups: []string{"subgroup-a", "subgroup-b", "subgroup-c", "subgroup-d"}, |
| 99 | + expectedGroups: []string{"subgroup-c", "subgroup-d"}, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "no exclusions when nothing matches", |
| 103 | + regex: "^nonexistent$", |
| 104 | + groups: []string{"subgroup-a", "subgroup-b"}, |
| 105 | + expectedGroups: []string{"subgroup-a", "subgroup-b"}, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "excludes all groups", |
| 109 | + regex: "subgroup", |
| 110 | + groups: []string{"subgroup-a", "subgroup-b"}, |
| 111 | + expectedGroups: []string{}, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "empty groups list", |
| 115 | + regex: "anything", |
| 116 | + groups: []string{}, |
| 117 | + expectedGroups: []string{}, |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + for _, tc := range testCases { |
| 122 | + t.Run(tc.name, func(t *testing.T) { |
| 123 | + os.Setenv("GHORG_GITLAB_GROUP_EXCLUDE_MATCH_REGEX", tc.regex) |
| 124 | + defer os.Unsetenv("GHORG_GITLAB_GROUP_EXCLUDE_MATCH_REGEX") |
| 125 | + |
| 126 | + result := filterGitlabGroupByExcludeMatchRegex(tc.groups) |
| 127 | + if len(result) == 0 && len(tc.expectedGroups) == 0 { |
| 128 | + return // Both empty, test passes |
| 129 | + } |
| 130 | + if len(result) != len(tc.expectedGroups) { |
| 131 | + t.Errorf("Expected %d groups, got %d. Expected: %v, Got: %v", |
| 132 | + len(tc.expectedGroups), len(result), tc.expectedGroups, result) |
| 133 | + return |
| 134 | + } |
| 135 | + for i, group := range result { |
| 136 | + if group != tc.expectedGroups[i] { |
| 137 | + t.Errorf("Expected group at index %d to be %s, got %s", i, tc.expectedGroups[i], group) |
| 138 | + } |
| 139 | + } |
| 140 | + }) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func TestFilterGitlabGroupMatchAndExcludeCombined(t *testing.T) { |
| 145 | + // Test that match and exclude work together: |
| 146 | + // First include only matching, then exclude from those |
| 147 | + t.Run("match then exclude narrows results", func(t *testing.T) { |
| 148 | + groups := []string{"subgroup-a", "subgroup-b", "subgroup-c", "other-group"} |
| 149 | + |
| 150 | + // First apply match: include only subgroup-* groups |
| 151 | + os.Setenv("GHORG_GITLAB_GROUP_MATCH_REGEX", "^subgroup-") |
| 152 | + defer os.Unsetenv("GHORG_GITLAB_GROUP_MATCH_REGEX") |
| 153 | + |
| 154 | + matched := filterGitlabGroupByMatchRegex(groups) |
| 155 | + |
| 156 | + // Then apply exclude: remove subgroup-c from the result |
| 157 | + os.Setenv("GHORG_GITLAB_GROUP_EXCLUDE_MATCH_REGEX", "^subgroup-c$") |
| 158 | + defer os.Unsetenv("GHORG_GITLAB_GROUP_EXCLUDE_MATCH_REGEX") |
| 159 | + |
| 160 | + result := filterGitlabGroupByExcludeMatchRegex(matched) |
| 161 | + |
| 162 | + expected := []string{"subgroup-a", "subgroup-b"} |
| 163 | + if len(result) != len(expected) { |
| 164 | + t.Errorf("Expected %d groups, got %d. Expected: %v, Got: %v", |
| 165 | + len(expected), len(result), expected, result) |
| 166 | + return |
| 167 | + } |
| 168 | + for i, group := range result { |
| 169 | + if group != expected[i] { |
| 170 | + t.Errorf("Expected group at index %d to be %s, got %s", i, expected[i], group) |
| 171 | + } |
| 172 | + } |
| 173 | + }) |
| 174 | +} |
| 175 | + |
| 176 | +func TestFilterGitlabGroupMatchRegexWithAlternation(t *testing.T) { |
| 177 | + // This is the primary use case from the feature request: |
| 178 | + // User wants to include only 3 subgroups out of 20 |
| 179 | + t.Run("include only 3 out of many subgroups using alternation", func(t *testing.T) { |
| 180 | + // Simulate 10 subgroups |
| 181 | + groups := []string{ |
| 182 | + "subgroup-1", "subgroup-2", "subgroup-3", "subgroup-4", "subgroup-5", |
| 183 | + "subgroup-6", "subgroup-7", "subgroup-8", "subgroup-9", "subgroup-10", |
| 184 | + } |
| 185 | + |
| 186 | + // Include only 3 specific subgroups |
| 187 | + os.Setenv("GHORG_GITLAB_GROUP_MATCH_REGEX", "^(subgroup-2|subgroup-5|subgroup-8)$") |
| 188 | + defer os.Unsetenv("GHORG_GITLAB_GROUP_MATCH_REGEX") |
| 189 | + |
| 190 | + result := filterGitlabGroupByMatchRegex(groups) |
| 191 | + |
| 192 | + expected := []string{"subgroup-2", "subgroup-5", "subgroup-8"} |
| 193 | + if len(result) != len(expected) { |
| 194 | + t.Errorf("Expected %d groups, got %d. Expected: %v, Got: %v", |
| 195 | + len(expected), len(result), expected, result) |
| 196 | + return |
| 197 | + } |
| 198 | + for i, group := range result { |
| 199 | + if group != expected[i] { |
| 200 | + t.Errorf("Expected group at index %d to be %s, got %s", i, expected[i], group) |
| 201 | + } |
| 202 | + } |
| 203 | + }) |
| 204 | +} |
0 commit comments