1+ C = {
2+ reset = " \27 [0m" ,
3+ red = " \27 [31m" ,
4+ green = " \27 [32m" ,
5+ yellow = " \27 [33m" ,
6+ }
7+
18local health = {
2- has_error = false ,
9+ has_error = false ,
310
4- start = function (_ , s )
5- print (" START: " .. s )
6- end ,
11+ start = function (_ , s )
12+ print (" START: " .. s )
13+ end ,
714
8- error = function (self , s )
9- print (" ERROR: " .. s )
10- self .has_error = true
11- end ,
15+ error = function (self , s )
16+ print (C . red .. " ERROR: " .. C . reset .. s )
17+ self .has_error = true
18+ end ,
1219
13- ok = function (_ , s )
14- print (" OK: " .. s )
15- end ,
20+ ok = function (_ , s )
21+ print (C . green .. " OK: " .. C . reset .. s )
22+ end ,
1623}
1724
1825local function same_set (a , b )
@@ -41,7 +48,7 @@ local function run(label, fn)
4148 health :start (" Tests " .. label )
4249 local ok , err = pcall (fn )
4350 if not ok then
44- health :error (" Failed" )
51+ health :error (" Failed " .. err )
4552 return
4653 end
4754 health :ok (" Passed" )
5259---- ------------------------------------------------
5360local repo = " /tmp/codeowners_test"
5461os.execute (" rm -rf " .. repo )
55- os.execute (" mkdir -p " .. repo .. " /.gitlab" )
5662
5763local main = require (" lua.gitlab-codeowners.main" )
5864
@@ -80,6 +86,7 @@ local function write(path, content)
8086end
8187
8288local function test_1 ()
89+ os.execute (" mkdir -p " .. repo .. " /.gitlab" )
8390 write (
8491 repo .. " /.gitlab/CODEOWNERS" ,
8592 [[
@@ -93,15 +100,17 @@ local function test_1()
93100
94101 README.md @readme-md
95102
96- /docs / @all-docs
97- /docs /* @root-docs
98- /docs /**/*.md @markdown-docs # Match specific file types in any subdirectory
103+ /documents / @all-docs
104+ /documents /* @root-docs
105+ /documents /**/*.md @markdown-docs # Match specific file types in any subdirectory
99106 ]]
100107 )
101108
102109 run (" .gitlab/CODEOWNERS found" , function ()
103110 local co_file = main .get_codeowners_file (repo .. " /something" )
104- assert (co_file and co_file .co_file == repo .. " /.gitlab/CODEOWNERS" )
111+ assert (co_file )
112+ assert (co_file .co_file == repo .. " /.gitlab/CODEOWNERS" )
113+ assert (co_file .repo == repo )
105114 end )
106115
107116 run (" README.md matches both top-level and nested" , function ()
@@ -118,13 +127,13 @@ local function test_1()
118127 end )
119128
120129 run (" docs directory patterns" , function ()
121- assert_owner (" /docs /a/file.txt" , " @all-docs" )
122- assert_owner (" /docs /b/c/d.md" , " @markdown-docs" )
123- assert_owner (" /docs /file1.txt" , " @root-docs" )
124- assert_owner (" /docs /subdir/file2.txt" , " @all-docs" )
125- assert_owner (" /docs /subdir/file.md" , " @markdown-docs" )
126- assert_owner (" /docs /file.md" , " @markdown-docs" )
127- assert_owners (" /sth/docs /file.md" , { " @md-owner" , " @md-owner2" })
130+ assert_owner (" /documents /a/file.txt" , " @all-docs" )
131+ assert_owner (" /documents /b/c/d.md" , " @markdown-docs" )
132+ assert_owner (" /documents /file1.txt" , " @root-docs" )
133+ assert_owner (" /documents /subdir/file2.txt" , " @all-docs" )
134+ assert_owner (" /documents /subdir/file.md" , " @markdown-docs" )
135+ assert_owner (" /documents /file.md" , " @markdown-docs" )
136+ assert_owners (" /sth/documents /file.md" , { " @md-owner" , " @md-owner2" })
128137 end )
129138
130139 run (" default wildcard *" , function ()
@@ -136,54 +145,103 @@ local function test_1()
136145 run (" priority tests" , function ()
137146 local md_owners = { " @md-owner" , " @md-owner2" }
138147 assert_owner (" /README.md" , " @readme-md" )
139- assert_owner (" /docs /README.md" , " @markdown-docs" )
148+ assert_owner (" /documents /README.md" , " @markdown-docs" )
140149 assert_owners (" /notes.md" , md_owners )
141- assert_owner (" /docs /notes.md" , " @markdown-docs" )
142- assert_owners (" /something/docs /notes.md" , md_owners )
150+ assert_owner (" /documents /notes.md" , " @markdown-docs" )
151+ assert_owners (" /something/documents /notes.md" , md_owners )
143152 end )
144153end
145154
146155local function test_2 ()
156+ os.execute (" mkdir -p " .. repo .. " /docs" )
147157 write (
148- repo .. " /CODEOWNERS" ,
158+ repo .. " /docs/ CODEOWNERS" ,
149159 [[
150160 * @default-owner
151161 *.md @md-owner
152- /docs / @all-docs
162+ /documents / @all-docs
153163
154164 [Docs]
155- /docs / @all-docs
156- /docs /*.md @all-docs
165+ /documents / @all-docs
166+ /documents /*.md @all-docs
157167 README.md @readme
158168
159169 [Misc]
160- /docs /*.misc @misc
170+ /documents /*.misc @misc
161171 ]]
162172 )
163173
164- run (" CODEOWNERS overwrites .gitlab/" , function ()
174+ run (" docs/ CODEOWNERS overwrites .gitlab/" , function ()
165175 local co_file = main .get_codeowners_file (repo .. " /something/a/b/c" )
166- assert (co_file and co_file .co_file == repo .. " /CODEOWNERS" )
176+ assert (co_file )
177+ assert (co_file .co_file == repo .. " /docs/CODEOWNERS" )
178+ assert (co_file .repo == repo )
167179 end )
168180
169181 run (" section" , function ()
170182 assert_owners (" /README.md" , { " @md-owner" , " @readme" })
171- assert_owners (" /docs /README.md" , { " @all-docs" , " @readme" })
183+ assert_owners (" /documents /README.md" , { " @all-docs" , " @readme" })
172184 assert_owners (" /sth/docs/README.md" , { " @md-owner" , " @readme" })
173185 assert_owner (" /something.txt" , " @default-owner" )
174186
175187 -- repeated entry
176- assert_owners (" /docs/something.misc" , { " @all-docs" , " @misc" })
177- assert_owner (" /docs/file.md" , " @all-docs" )
188+ assert_owners (" /documents/something.misc" , { " @all-docs" , " @misc" })
189+ assert_owner (" /documents/file.md" , " @all-docs" )
190+ end )
191+ end
192+
193+ local function test_3 ()
194+ write (
195+ repo .. " /CODEOWNERS" ,
196+ [[
197+ * @default-owner
198+ *.md @md-owner
199+ /documents/ @all-docs
200+
201+ [Docs] @doc-default
202+ /documents/
203+ /documents/*.md @all-docs
204+ README.md
205+
206+ [Algorithm] @cpp-master @algo-team
207+ /cpp/
208+ /algo/*.md @all-docs
209+ /algo/*.txt @algo-team
210+ /cpp/includes/ @cpp-master
211+ ]]
212+ )
213+
214+ run (" /CODEOWNERS overwrites docs/" , function ()
215+ local co_file = main .get_codeowners_file (repo .. " /something/a/b/c" )
216+ assert (co_file and co_file .co_file == repo .. " /CODEOWNERS" )
217+ end )
218+
219+ run (" section default " , function ()
220+ assert_owners (" /README.md" , { " @md-owner" , " @doc-default" })
221+ assert_owners (" /documents/README.md" , { " @all-docs" , " @doc-default" })
222+ assert_owner (" /documents/FILE.md" , " @all-docs" )
223+ assert_owners (" /sth/documents/README.md" , { " @md-owner" , " @doc-default" })
224+ assert_owner (" /something.txt" , " @default-owner" )
178225 end )
226+
227+ run (" section default more than 1 owners" , function ()
228+ assert_owners (" /cpp/main.cpp" , { " @default-owner" , " @cpp-master" , " @algo-team" })
229+ assert_owners (" /algo/foo.md" , { " @md-owner" , " @all-docs" })
230+ assert_owners (" /algo/bar.txt" , { " @default-owner" , " @algo-team" })
231+ assert_owners (" /cpp/includes/header.h" , { " @default-owner" , " @cpp-master" })
232+ assert_owner (" /other/path/file.txt" , " @default-owner" )
233+ end )
234+
235+
179236end
180237
181238local function check ()
182239 test_1 ()
183240 test_2 ()
184- if health .has_error then
185- os.exit (1 )
186- end
241+ test_3 ()
242+ if health .has_error then
243+ os.exit (1 )
244+ end
187245end
188246
189- check ();
247+ check ()
0 commit comments