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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from pathlib import Path
from textwrap import dedent

import pytest
from testlib.fixtures.file_helper import (
    CopyFile,
    CopyTemplate,
    CopyTree,
    File,
    Symlink,
    AssetSymlink,
    merge_file_declaration,
    with_files,
)


@with_files({"test_file_1": File("this is some test content")})
def test_file_creates(files: Path):
    file_name = "test_file_1"
    file_content = "this is some test content"

    target_path = files / file_name
    assert target_path.exists()
    assert target_path.read_text() == file_content


@with_files({"test_file_2": File("asdf")})
def test_no_file_leakage(files: Path):
    file_name = "test_file_2"
    file_content = "asdf"

    some_other_file_name = "test_file_1"

    assert (files / file_name).exists()
    assert (files / file_name).read_text() == file_content
    # Check for no leakage from other tests
    assert not (files / some_other_file_name).exists()


@with_files({"copy_file_test.txt": CopyFile("assets/test_file_helper/copy_file_test.txt")})
def test_copy_file_no_name(files: Path):
    target_path = files / "copy_file_test.txt"

    assert target_path.exists()

    assert target_path.read_text() == "some amazing content\n"


@with_files({"new_name.txt": CopyFile("assets/test_file_helper/copy_file_test.txt")})
def test_copy_file_with_name(files: Path):
    assert (files / "new_name.txt").exists()
    assert not (files / "copy_file_test.txt").exists()


@with_files(
    {
        "target.txt": CopyTemplate(
            "assets/test_file_helper/copy_file_template.template",
            {"some_key": "abc", "other_key": 123, "key_in_braces": "in_braces"},
        )
    }
)
def test_template(files: Path):
    assert (files / "target.txt").exists()

    expected_content = dedent("""
        some_key is set to abc
        more of abc
        other_key is 123
        @not_a_key@
        @in_braces@
    """)

    actual_content = (files / "target.txt").read_text()
    assert actual_content == expected_content


@with_files(
    {
        "target.txt": CopyTemplate(
            "assets/test_file_helper/copy_file_template.template",
            {"some_key": "abc", "key_in_braces": "in_braces"},
        )
    }
)
@pytest.mark.xfail(raises=KeyError)
def test_template_missing_key(files: Path):
    # Empty, because the initialization of the CopyTemplate fails. Caught and tested for by the xfail mark
    ...


@with_files(
    {
        "target.txt": CopyTemplate(
            "assets/test_file_helper/copy_file_template.template",
            {
                "some_key": "Eragon",
                "key_in_braces": "in_braces",
                "other_key": "Arthur Leywin",
                "the beginning": "after the end",
            },
        )
    }
)
@pytest.mark.xfail(raises=ExceptionGroup)
def test_template_too_many_keys(files: Path):
    # Empty, because the initialization of the CopyTemplate fails. Caught and tested for by the xfail mark
    ...


@with_files({"some_folder": CopyTree("assets/test_file_helper/test_folder")})
def test_copy_tree(files: Path):
    files = files / "some_folder"
    assert (files / "a.txt").exists()
    assert (files / "b.txt").exists()
    assert (files / "sub_test").exists()
    assert (files / "sub_test" / "c.txt").exists()


@with_files(
    {
        "a.txt": File("Hello World"),
        "assembler.txt": File("reinforced iron plates\n"),
        "folder_1": {
            "empty.nix": File(""),
            "folder_2": {"not_empty.py": File("print('Arrruuuuuuuuuuuu')")},
        },
    }
)
def test_files_sub_dirs(files: Path):
    f = files / "a.txt"
    assert f.exists()
    assert f.read_text() == "Hello World"

    f = files / "assembler.txt"
    assert f.exists()

    fldr = files / "folder_1"
    assert fldr.exists()
    f = fldr / "empty.nix"
    assert f.exists()

    fldr /= "folder_2"
    assert fldr.exists()

    f = fldr / "not_empty.py"
    assert f.exists()


@with_files({"not_empty": {"a.txt": File("Hello World")}, "empty": {}})
def test_creates_empty_directory(files: Path):
    assert (files / "not_empty").exists()
    assert (files / "empty").exists()
    assert (files / "empty").is_dir()


@with_files({"a.sh": File("echo test", mode=0o477)})
def test_mode_setting(files: Path):
    file = files / "a.sh"
    assert file.exists()
    assert file.stat().st_mode & 0o477 == 0o477


@with_files({"a": AssetSymlink("assets/test_file_helper/copy_file_test.txt")})
def test_asset_symlink(files: Path):
    file = files / "a"
    assert file.exists(follow_symlinks=False)

    assert file.is_symlink()

    assert file.readlink().exists()

    assert file.read_text() == "some amazing content\n"


@with_files({"a": AssetSymlink("assets/test_file_helper/test_folder")})
def test_dir_symlink(files: Path):
    folder = files / "a"
    assert folder.exists(follow_symlinks=False)

    assert folder.is_symlink()

    assert folder.readlink().exists()

    assert folder.readlink().is_dir()

    assert (folder / "a.txt").exists()


@with_files({"a": AssetSymlink("assets/test_file_helper/this_does_not_exist")})
def test_invalid_asset_symlink(files: Path):
    link = files / "a"
    assert link.exists(follow_symlinks=False)
    assert link.is_symlink()
    assert not link.readlink().exists()


@pytest.mark.xfail(raises=ValueError)
@with_files({"a": AssetSymlink("/absolute/assets/test_file_helper/this_does_not_exist")})
def test_absolute_asset_symlink(files: Path):
    pass


@with_files({"tg": File("Hello World"), "folder": {"link": Symlink("../tg")}})
def test_file_symlink(files: Path):
    assert (files / "tg").exists()
    link = files / "folder" / "link"
    assert link.exists(follow_symlinks=False)
    assert link.is_symlink()
    # check that this is actually relative and not an absolute path
    assert str(link.readlink()) == "../tg"


@with_files({"test-file.txt": File("a")}, {"test-file.txt": File("b")})
def test_multiple_file_sets(files: Path):
    file = files / "test-file.txt"
    assert file.exists()
    assert file.read_text() == "a" or file.read_text() == "b"


def test_merge_fd_merges_correctly():
    fa = File("a")
    fc = File("c")
    fd = File("d")
    ff = File("f")
    fd1 = {"a": fa, "b": {"c": fc}}

    fd2 = {"b": {"d": fd}, "e": {"f": ff}}

    expected = {"a": fa, "b": {"c": fc, "d": fd}, "e": {"f": ff}}

    result = merge_file_declaration(fd1, fd2)
    assert result == expected


def test_merge_fd_throws_on_conflict():
    fd1 = {"a": {"b": File("b")}}
    fd2 = {"a": File("a")}

    with pytest.raises(
        ValueError,
        match=r"\('Cannot merge files; got two different values for the same path', 'a'\)",
    ):
        merge_file_declaration(fd1, fd2)


def test_merge_fd_throws_on_conflict_with_full_path():
    fd1 = {"a": {"b": {"c": {"d": File("d")}}}}
    fd2 = {"a": {"b": {"c": {"d": File("different file")}}}}

    with pytest.raises(
        ValueError,
        match=r"\('Cannot merge files; got two different values for the same path', 'a/b/c/d'\)",
    ):
        merge_file_declaration(fd1, fd2)