summaryrefslogtreecommitdiff
path: root/tests/test_utils.py
blob: b7c975418db33e024b712cb1b7a956bc94497697 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import pytest
from pytest import LogCaptureFixture
from pathlib import Path
from logging import INFO
from pyssg.utils import (get_expanded_path, get_checksum, copy_file, create_dir,
                         get_dir_structure, get_file_list)


# $PYSSG_HOME is the only env var set
#   in the project settings that resemble a path
@pytest.mark.parametrize('path, expected_expanded', [
    ('$PYSSG_HOME', '/tmp/pyssg'),
    ('$PYSSG_HOME/', '/tmp/pyssg'),
    ('/test$PYSSG_HOME/', '/test/tmp/pyssg'),
    ('/test/$PYSSG_HOME/', '/test/tmp/pyssg'),
    ('/test/$PYSSG_HOME/test', '/test/tmp/pyssg/test')
])
def test_path_expansion(path: str, expected_expanded: str) -> None:
    expanded: str = get_expanded_path(path)
    assert expanded == expected_expanded


@pytest.mark.parametrize('path', [
    ('$'),
    ('$NON_EXISTENT_VARIABLE'),
    ('/path/to/something/$'),
    ('/path/to/something/$NON_EXISTENT_VARIABLE')
])
def test_path_expansion_failure(path: str) -> None:
    with pytest.raises(SystemExit) as system_exit:
        get_expanded_path(path)
    assert system_exit.type == SystemExit
    assert system_exit.value.code == 1


def test_checksum(test_dir: str, simple_yaml: str) -> None:
    path: str = f'{test_dir}/io_files/{simple_yaml}'
    simple_yaml_checksum: str = 'd4f0a3ed56fd530d3ea485dced25534c'
    checksum: str = get_checksum(path)
    assert checksum == simple_yaml_checksum


def test_copy_file(tmp_path: Path, caplog: LogCaptureFixture) -> None:
    src: Path = tmp_path/'src'
    dst: Path = tmp_path/'dst'
    src.mkdir()
    dst.mkdir()
    src_file: Path = src/'tmp_file.txt'
    dst_file: Path = dst/'tmp_file.txt'
    src_file.write_text('something')
    inf: tuple[str, int, str] = ('pyssg.utils',
                                 INFO,
                                 f'copied file "{src_file}" to "{dst_file}"')
    copy_file(str(src_file), str(dst_file))
    assert caplog.record_tuples[-1] == inf


def test_copy_file_failure(tmp_path: Path, caplog: LogCaptureFixture) -> None:
    src: Path = tmp_path/'src'
    dst: Path = tmp_path/'dst'
    src.mkdir()
    dst.mkdir()
    src_file: Path = src/'tmp_file.txt'
    dst_file: Path = dst/'tmp_file.txt'
    src_file.write_text('something')
    dst_file.write_text('something')
    inf: tuple[str, int, str] = ('pyssg.utils',
                                 INFO,
                                 f'file "{dst_file}" already exists, ignoring')
    copy_file(str(src_file), str(dst_file))
    assert caplog.record_tuples[-1] == inf


def test_create_dir(tmp_path: Path, caplog: LogCaptureFixture) -> None:
    path: Path = tmp_path/'new_dir'
    inf: tuple[str, int, str] = ('pyssg.utils',
                                 INFO,
                                 f'created directory "{path}"')
    assert path.exists() is False
    create_dir(str(path), False, False)
    assert path.exists() is True
    assert caplog.record_tuples[-1] == inf


def test_create_dir_failure(tmp_path: Path, caplog: LogCaptureFixture) -> None:
    path: Path = tmp_path/'new_dir'
    inf: tuple[str, int, str] = ('pyssg.utils',
                                 INFO,
                                 f'directory "{path}" exists, ignoring')
    path.mkdir()
    create_dir(str(path), False, False)
    assert caplog.record_tuples[-1] == inf


def test_create_dirs(tmp_path: Path, caplog: LogCaptureFixture) -> None:
    path: Path = tmp_path/'new_dir'
    sub_path: Path = path/'sub_dir'
    inf: tuple[str, int, str] = ('pyssg.utils',
                                 INFO,
                                 f'created directory "{sub_path}"')
    assert path.exists() is False
    assert sub_path.exists() is False
    create_dir(str(sub_path), True, False)
    assert path.exists() is True
    assert sub_path.exists() is True
    assert caplog.record_tuples[-1] == inf


def test_create_dirs_failure(tmp_path: Path, caplog: LogCaptureFixture) -> None:
    path: Path = tmp_path/'new_dir'
    sub_path: Path = path/'sub_dir'
    inf: tuple[str, int, str] = ('pyssg.utils',
                                 INFO,
                                 f'directory "{sub_path}" exists, ignoring')
    path.mkdir()
    sub_path.mkdir()
    create_dir(str(sub_path), True, False)
    assert caplog.record_tuples[-1] == inf


@pytest.mark.parametrize('exclude, exp_dir_str', [
    ([], ['second/s1', 'first/f1/f2']),
    (['f2'], ['second/s1', 'first/f1']),
    (['f1'], ['second/s1', 'first']),
    (['second'], ['first/f1/f2']),
    (['s1', 'f2'], ['second', 'first/f1']),
    (['s1', 'f1'], ['second', 'first']),
    (['s1', 'first'], ['second'])
])
def test_dir_structure(tmp_dir_structure: Path,
                       exclude: list[str],
                       exp_dir_str: list[str]) -> None:
    dir_str: list[str] = get_dir_structure(str(tmp_dir_structure), exclude)
    # order doesn't matter
    assert sorted(dir_str) == sorted(exp_dir_str)


@pytest.mark.parametrize('exts, exclude_dirs, exp_flist', [
    (('txt',), [], ['f0.txt', 'second/f4.txt',
                    'second/s1/f5.txt', 'first/f1.txt',
                    'first/f1/f2.txt', 'first/f1/f2/f3.txt']),
    (('txt', 'html'), [], ['f0.html', 'f0.txt',
                           'second/f4.txt', 'second/f4.html',
                           'second/s1/f5.html', 'second/s1/f5.txt',
                           'first/f1.html', 'first/f1.txt',
                           'first/f1/f2.txt', 'first/f1/f2.html',
                           'first/f1/f2/f3.txt', 'first/f1/f2/f3.html']),
    (('md',), [], ['f0.md', 'second/f4.md',
                   'second/s1/f5.md', 'first/f1.md',
                   'first/f1/f2.md', 'first/f1/f2/f3.md']),
    (('md',), ['first'], ['f0.md', 'second/f4.md', 'second/s1/f5.md']),
    (('md',), ['first', 's1'], ['f0.md', 'second/f4.md']),
    (('md',), ['f2', 's1'], ['f0.md', 'second/f4.md',
                             'first/f1.md', 'first/f1/f2.md',]),
])
def test_file_list(tmp_dir_structure: Path,
                   exts: tuple[str],
                   exclude_dirs: list[str],
                   exp_flist: list[str]) -> None:
    flist: list[str] = get_file_list(str(tmp_dir_structure), exts, exclude_dirs)
    # order doesn't matter
    assert sorted(flist) == sorted(exp_flist)