1515from gitlab_codeowners_linter .parser import CodeownerEntry
1616from gitlab_codeowners_linter .parser import CodeownerSection
1717from gitlab_codeowners_linter .sorting import sort_paths
18+ from gitlab_codeowners_linter .sorting import sort_section_names
1819
1920
2021class Test_Functions (unittest .TestCase ):
@@ -73,7 +74,70 @@ class TestCase:
7374 case .expected_no_autofix ,
7475 no_autofix ,))
7576
76- def test_sort_function (self ):
77+ def test_sort_sections_function (self ):
78+ @dataclass
79+ class TestCase :
80+ name : str
81+ input : list [str ]
82+ expected : list [str ]
83+
84+ testcases = [
85+ TestCase (
86+ name = 'unsorted' ,
87+ input = [
88+ '[BUILD]' ,
89+ '[SECURITY]' ,
90+ '[SYSTEM]' ,
91+ '^[And_a_last_section]' ,
92+ '^[SYSTEM]' ,
93+ ],
94+ expected = [
95+ '^[And_a_last_section]' ,
96+ '[BUILD]' ,
97+ '[SECURITY]' ,
98+ '^[SYSTEM]' ,
99+ '[SYSTEM]' ,
100+ ],
101+ ),
102+ TestCase (name = 'empty_slice' , input = [], expected = []),
103+ TestCase (
104+ name = 'already_sorted' ,
105+ input = [
106+ '^[And_a_last_section]' ,
107+ '[BUILD]' ,
108+ '[SECURITY]' ,
109+ '^[SYSTEM]' ,
110+ '[SYSTEM]' ,
111+ ],
112+ expected = [
113+ '^[And_a_last_section]' ,
114+ '[BUILD]' ,
115+ '[SECURITY]' ,
116+ '^[SYSTEM]' ,
117+ '[SYSTEM]' ,
118+ ],
119+ ),
120+ ]
121+ sort_section_key = cmp_to_key (sort_section_names )
122+
123+ for case in testcases :
124+ data = []
125+ actual = data
126+ for section in case .input :
127+ data .append (CodeownerSection (section , [], []))
128+ actual = sorted (data , key = sort_section_key )
129+ actual_names = [x .codeowner_section for x in actual ]
130+ self .assertListEqual (
131+ case .expected ,
132+ actual_names ,
133+ 'failed test {} expected {}, actual {}' .format (
134+ case .name ,
135+ case .expected ,
136+ actual_names ,
137+ ),
138+ )
139+
140+ def test_sort_path_function (self ):
77141 @dataclass
78142 class TestCase :
79143 name : str
@@ -189,10 +253,10 @@ class TestCase:
189253 ),
190254 expected_check = [
191255 'Sections are not sorted' ,
192- 'The sections SECURITY, security are duplicates' ,
193- 'The paths in sections __default_codeowner_section__, Security, SYSTEM, SECURITY are not sorted' ,
256+ 'The sections [ SECURITY], [ security] are duplicates' ,
257+ 'The paths in sections __default_codeowner_section__, [ Security], [ SYSTEM], [ SECURITY] are not sorted' ,
194258 'The sections __default_codeowner_section__ have duplicate paths' ,
195- 'The sections __default_codeowner_section__, Security, SYSTEM, SECURITY, security have non-existing paths' ],
259+ 'The sections __default_codeowner_section__, [ Security], [ SYSTEM], [ SECURITY], [ security] have non-existing paths' ],
196260 expected_fix = os .path .join (
197261 os .path .dirname (os .path .abspath (__file__ )),
198262 'resources/existing_paths_autofix.txt' ,
@@ -268,8 +332,8 @@ class TestCase:
268332 ),
269333 expected_check = [
270334 'Sections are not sorted' ,
271- 'There are blank lines in the sections __default_codeowner_section__, BUILD, SECURITY' ,
272- 'The paths in sections __default_codeowner_section__, BUILD, SYSTEM, TEST_SECTION are not sorted' ,
335+ 'There are blank lines in the sections __default_codeowner_section__, [ BUILD], [ SECURITY] ' ,
336+ 'The paths in sections __default_codeowner_section__, [ BUILD], [ SYSTEM], [ TEST_SECTION] are not sorted' ,
273337 'The sections __default_codeowner_section__ have duplicate paths' ,
274338 ],
275339 expected_fix = os .path .join (
@@ -285,16 +349,32 @@ class TestCase:
285349 ),
286350 expected_check = [
287351 'Sections are not sorted' ,
288- 'The sections SECTION_NAME, section_name, Section_Name are duplicates' ,
289- 'There are blank lines in the sections Section_name, BUILD, SECURITY' ,
290- 'The paths in sections Section_name, BUILD, SYSTEM, SECTION_NAME, TEST_SECTION are not sorted' ,
291- 'The sections Section_name, SECTION_NAME have duplicate paths' ,
352+ 'The sections [ SECTION_NAME], [ section_name], [ Section_Name] are duplicates' ,
353+ 'There are blank lines in the sections [ Section_name], [ BUILD], [ SECURITY] ' ,
354+ 'The paths in sections [ Section_name], [ BUILD], [ SYSTEM], [ SECTION_NAME], [ TEST_SECTION] are not sorted' ,
355+ 'The sections [ Section_name], [ SECTION_NAME] have duplicate paths' ,
292356 ],
293357 expected_fix = os .path .join (
294358 os .path .dirname (os .path .abspath (__file__ )),
295359 'resources/no_default_section_autofix.txt' ,
296360 ),
297361 ),
362+ TestCase (
363+ name = 'optional_sections_not_formatted' ,
364+ input = os .path .join (
365+ os .path .dirname (os .path .abspath (__file__ )),
366+ 'resources/optional_sections_input.txt' ,
367+ ),
368+ expected_check = [
369+ 'Sections are not sorted' ,
370+ 'The sections [System], [SYSTEM], [SYSTEM] are duplicates' ,
371+
372+ ],
373+ expected_fix = os .path .join (
374+ os .path .dirname (os .path .abspath (__file__ )),
375+ 'resources/optional_sections_autofix.txt' ,
376+ ),
377+ ),
298378 TestCase (
299379 name = 'empty_file' ,
300380 input = os .path .join (
0 commit comments