110
110
111
111
* `@has-dir PATH` checks for the existence of the given directory.
112
112
113
+ * `@files FOLDER_PATH [ENTRIES]`, checks that `FOLDER_PATH` contains exactly
114
+ `[ENTRIES]`.
115
+
113
116
All conditions can be negated with `!`. `@!has foo/type.NoSuch.html`
114
117
checks if the given file does not exist, for example.
115
118
@@ -321,12 +324,15 @@ def resolve_path(self, path):
321
324
else :
322
325
return self .last_path
323
326
327
+ def get_absolute_path (self , path ):
328
+ return os .path .join (self .root , path )
329
+
324
330
def get_file (self , path ):
325
331
path = self .resolve_path (path )
326
332
if path in self .files :
327
333
return self .files [path ]
328
334
329
- abspath = os . path . join ( self .root , path )
335
+ abspath = self .get_absolute_path ( path )
330
336
if not (os .path .exists (abspath ) and os .path .isfile (abspath )):
331
337
raise FailedCheck ('File does not exist {!r}' .format (path ))
332
338
@@ -340,7 +346,7 @@ def get_tree(self, path):
340
346
if path in self .trees :
341
347
return self .trees [path ]
342
348
343
- abspath = os . path . join ( self .root , path )
349
+ abspath = self .get_absolute_path ( path )
344
350
if not (os .path .exists (abspath ) and os .path .isfile (abspath )):
345
351
raise FailedCheck ('File does not exist {!r}' .format (path ))
346
352
@@ -356,7 +362,7 @@ def get_tree(self, path):
356
362
357
363
def get_dir (self , path ):
358
364
path = self .resolve_path (path )
359
- abspath = os . path . join ( self .root , path )
365
+ abspath = self .get_absolute_path ( path )
360
366
if not (os .path .exists (abspath ) and os .path .isdir (abspath )):
361
367
raise FailedCheck ('Directory does not exist {!r}' .format (path ))
362
368
@@ -538,6 +544,41 @@ def get_nb_matching_elements(cache, c, regexp, stop_at_first):
538
544
return check_tree_text (cache .get_tree (c .args [0 ]), pat , c .args [2 ], regexp , stop_at_first )
539
545
540
546
547
+ def check_files_in_folder (c , cache , folder , files ):
548
+ files = files .strip ()
549
+ if not files .startswith ('[' ) or not files .endswith (']' ):
550
+ raise InvalidCheck ("Expected list as second argument of @{} (ie '[]')" .format (c .cmd ))
551
+
552
+ folder = cache .get_absolute_path (folder )
553
+
554
+ # First we create a set of files to check if there are duplicates.
555
+ files = shlex .split (files [1 :- 1 ].replace ("," , "" ))
556
+ files_set = set ()
557
+ for file in files :
558
+ if file in files_set :
559
+ raise InvalidCheck ("Duplicated file `{}` in @{}" .format (file , c .cmd ))
560
+ files_set .add (file )
561
+ folder_set = set ([f for f in os .listdir (folder ) if f != "." and f != ".." ])
562
+
563
+ # Then we remove entries from both sets (we clone `folder_set` so we can iterate it while
564
+ # removing its elements).
565
+ for entry in set (folder_set ):
566
+ if entry in files_set :
567
+ files_set .remove (entry )
568
+ folder_set .remove (entry )
569
+
570
+ error = 0
571
+ if len (files_set ) != 0 :
572
+ print_err (c .lineno , c .context , "Entries not found in folder `{}`: `{}`" .format (
573
+ folder , files_set ))
574
+ error += 1
575
+ if len (folder_set ) != 0 :
576
+ print_err (c .lineno , c .context , "Extra entries in folder `{}`: `{}`" .format (
577
+ folder , folder_set ))
578
+ error += 1
579
+ return error == 0
580
+
581
+
541
582
ERR_COUNT = 0
542
583
543
584
@@ -566,6 +607,13 @@ def check_command(c, cache):
566
607
else :
567
608
raise InvalidCheck ('Invalid number of @{} arguments' .format (c .cmd ))
568
609
610
+ elif c .cmd == 'files' : # check files in given folder
611
+ if len (c .args ) != 2 : # @files <folder path> <file list>
612
+ raise InvalidCheck ("Invalid number of @{} arguments" .format (c .cmd ))
613
+ elif c .negated :
614
+ raise InvalidCheck ("@{} doesn't support negative check" .format (c .cmd ))
615
+ ret = check_files_in_folder (c , cache , c .args [0 ], c .args [1 ])
616
+
569
617
elif c .cmd == 'count' : # count test
570
618
if len (c .args ) == 3 : # @count <path> <pat> <count> = count test
571
619
expected = int (c .args [2 ])
0 commit comments