forked from UCL-COMP0233-2022-2023/Travel-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_check_scripts.py
56 lines (46 loc) · 1.77 KB
/
test_check_scripts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import pytest
from pytest import raises
from testfixtures import tempdir, compare
import yaml
from check_scripts import analyse_files
def read_fixture(expected):
with open(os.path.join(os.path.dirname(__file__),
'fixtures_dir.yaml')) as fixture_file:
fixtures = yaml.load(fixture_file, Loader=yaml.FullLoader)
return fixtures[expected]
def create_files(tree):
"""
Creates a list of filenames from a nested dictionary
Parameters
----------
tree: dict
a dictionrary with nested dictionaries for each directory and
subdirectory. When reaching a file (i.e., something.ext) the
values must be a list with the content of the file.
"""
output = {}
for fname, fcontent in tree.items():
if ".md" in fname:
output[fname] = "\n".join(fcontent)
else:
midoutput = create_files(tree[fname])
output.update({f'{fname}/{iname}': icont for iname, icont in midoutput.items()})
return output
@pytest.mark.parametrize("fixture", read_fixture("tree"))
def test_crate_files(fixture):
assert fixture["expected"] == create_files(fixture["toconvert"])
@pytest.mark.parametrize("fixture", read_fixture("working"))
@tempdir()
def test_analyse(dir, fixture):
for fname, fcontent in create_files(fixture).items():
dir.write(fname, fcontent.encode())
assert analyse_files(dir.path) is None
@pytest.mark.parametrize("fixture", read_fixture("failing"))
@tempdir()
def test_analyse_broken(dir, fixture):
for fname, fcontent in create_files(fixture).items():
dir.write(fname, fcontent.encode())
with raises(ValueError, match=r"Some files are not linked properly:.*"):
analyse_files(dir.path)
# TODO test that there are not extra links?