Add @files command
This commit is contained in:
parent
fe37f37e4b
commit
65a7047ee4
@ -110,6 +110,9 @@ There are a number of supported commands:
|
|||||||
|
|
||||||
* `@has-dir PATH` checks for the existence of the given directory.
|
* `@has-dir PATH` checks for the existence of the given directory.
|
||||||
|
|
||||||
|
* `@files FOLDER_PATH [ENTRIES]`, checks that `FOLDER_PATH` contains exactly
|
||||||
|
`[ENTRIES]`.
|
||||||
|
|
||||||
All conditions can be negated with `!`. `@!has foo/type.NoSuch.html`
|
All conditions can be negated with `!`. `@!has foo/type.NoSuch.html`
|
||||||
checks if the given file does not exist, for example.
|
checks if the given file does not exist, for example.
|
||||||
|
|
||||||
@ -321,12 +324,15 @@ class CachedFiles(object):
|
|||||||
else:
|
else:
|
||||||
return self.last_path
|
return self.last_path
|
||||||
|
|
||||||
|
def get_absolute_path(self, path):
|
||||||
|
return os.path.join(self.root, path)
|
||||||
|
|
||||||
def get_file(self, path):
|
def get_file(self, path):
|
||||||
path = self.resolve_path(path)
|
path = self.resolve_path(path)
|
||||||
if path in self.files:
|
if path in self.files:
|
||||||
return self.files[path]
|
return self.files[path]
|
||||||
|
|
||||||
abspath = os.path.join(self.root, path)
|
abspath = self.get_absolute_path(path)
|
||||||
if not(os.path.exists(abspath) and os.path.isfile(abspath)):
|
if not(os.path.exists(abspath) and os.path.isfile(abspath)):
|
||||||
raise FailedCheck('File does not exist {!r}'.format(path))
|
raise FailedCheck('File does not exist {!r}'.format(path))
|
||||||
|
|
||||||
@ -340,7 +346,7 @@ class CachedFiles(object):
|
|||||||
if path in self.trees:
|
if path in self.trees:
|
||||||
return self.trees[path]
|
return self.trees[path]
|
||||||
|
|
||||||
abspath = os.path.join(self.root, path)
|
abspath = self.get_absolute_path(path)
|
||||||
if not(os.path.exists(abspath) and os.path.isfile(abspath)):
|
if not(os.path.exists(abspath) and os.path.isfile(abspath)):
|
||||||
raise FailedCheck('File does not exist {!r}'.format(path))
|
raise FailedCheck('File does not exist {!r}'.format(path))
|
||||||
|
|
||||||
@ -356,7 +362,7 @@ class CachedFiles(object):
|
|||||||
|
|
||||||
def get_dir(self, path):
|
def get_dir(self, path):
|
||||||
path = self.resolve_path(path)
|
path = self.resolve_path(path)
|
||||||
abspath = os.path.join(self.root, path)
|
abspath = self.get_absolute_path(path)
|
||||||
if not(os.path.exists(abspath) and os.path.isdir(abspath)):
|
if not(os.path.exists(abspath) and os.path.isdir(abspath)):
|
||||||
raise FailedCheck('Directory does not exist {!r}'.format(path))
|
raise FailedCheck('Directory does not exist {!r}'.format(path))
|
||||||
|
|
||||||
@ -538,6 +544,41 @@ def get_nb_matching_elements(cache, c, regexp, stop_at_first):
|
|||||||
return check_tree_text(cache.get_tree(c.args[0]), pat, c.args[2], regexp, stop_at_first)
|
return check_tree_text(cache.get_tree(c.args[0]), pat, c.args[2], regexp, stop_at_first)
|
||||||
|
|
||||||
|
|
||||||
|
def check_files_in_folder(c, cache, folder, files):
|
||||||
|
files = files.strip()
|
||||||
|
if not files.startswith('[') or not files.endswith(']'):
|
||||||
|
raise InvalidCheck("Expected list as second argument of @{} (ie '[]')".format(c.cmd))
|
||||||
|
|
||||||
|
folder = cache.get_absolute_path(folder)
|
||||||
|
|
||||||
|
# First we create a set of files to check if there are duplicates.
|
||||||
|
files = shlex.split(files[1:-1].replace(",", ""))
|
||||||
|
files_set = set()
|
||||||
|
for file in files:
|
||||||
|
if file in files_set:
|
||||||
|
raise InvalidCheck("Duplicated file `{}` in @{}".format(file, c.cmd))
|
||||||
|
files_set.add(file)
|
||||||
|
folder_set = set([f for f in os.listdir(folder) if f != "." and f != ".."])
|
||||||
|
|
||||||
|
# Then we remove entries from both sets (we clone `folder_set` so we can iterate it while
|
||||||
|
# removing its elements).
|
||||||
|
for entry in set(folder_set):
|
||||||
|
if entry in files_set:
|
||||||
|
files_set.remove(entry)
|
||||||
|
folder_set.remove(entry)
|
||||||
|
|
||||||
|
error = 0
|
||||||
|
if len(files_set) != 0:
|
||||||
|
print_err(c.lineno, c.context, "Entries not found in folder `{}`: `{}`".format(
|
||||||
|
folder, files_set))
|
||||||
|
error += 1
|
||||||
|
if len(folder_set) != 0:
|
||||||
|
print_err(c.lineno, c.context, "Extra entries in folder `{}`: `{}`".format(
|
||||||
|
folder, folder_set))
|
||||||
|
error += 1
|
||||||
|
return error == 0
|
||||||
|
|
||||||
|
|
||||||
ERR_COUNT = 0
|
ERR_COUNT = 0
|
||||||
|
|
||||||
|
|
||||||
@ -566,6 +607,13 @@ def check_command(c, cache):
|
|||||||
else:
|
else:
|
||||||
raise InvalidCheck('Invalid number of @{} arguments'.format(c.cmd))
|
raise InvalidCheck('Invalid number of @{} arguments'.format(c.cmd))
|
||||||
|
|
||||||
|
elif c.cmd == 'files': # check files in given folder
|
||||||
|
if len(c.args) != 2: # @files <folder path> <file list>
|
||||||
|
raise InvalidCheck("Invalid number of @{} arguments".format(c.cmd))
|
||||||
|
elif c.negated:
|
||||||
|
raise InvalidCheck("@{} doesn't support negative check".format(c.cmd))
|
||||||
|
ret = check_files_in_folder(c, cache, c.args[0], c.args[1])
|
||||||
|
|
||||||
elif c.cmd == 'count': # count test
|
elif c.cmd == 'count': # count test
|
||||||
if len(c.args) == 3: # @count <path> <pat> <count> = count test
|
if len(c.args) == 3: # @count <path> <pat> <count> = count test
|
||||||
expected = int(c.args[2])
|
expected = int(c.args[2])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user