1
0
mirror of https://github.com/TheLocehiliosan/yadm synced 2024-10-27 20:34:27 +00:00
TheLocehiliosan_yadm/test/test_unit_parse_encrypt.py

199 lines
6.0 KiB
Python
Raw Normal View History

"""Unit tests: parse_encrypt"""
import pytest
def test_not_called(runner, paths):
"""Test parse_encrypt (not called)"""
run = run_parse_encrypt(runner, paths, skip_parse=True)
assert run.success
2023-07-10 19:43:17 +00:00
assert run.err == ""
assert "EIF:unparsed" in run.out, "EIF should be unparsed"
assert "EIF_COUNT:1" in run.out, "Only value of EIF should be unparsed"
def test_short_circuit(runner, paths):
"""Test parse_encrypt (short-circuit)"""
run = run_parse_encrypt(runner, paths, twice=True)
assert run.success
2023-07-10 19:43:17 +00:00
assert run.err == ""
assert "PARSE_ENCRYPT_SHORT=parse_encrypt() not reprocessed" in run.out, "parse_encrypt() should short-circuit"
@pytest.mark.parametrize(
2023-07-10 19:43:17 +00:00
"encrypt",
[
("missing"),
("empty"),
],
)
def test_empty(runner, paths, encrypt):
"""Test parse_encrypt (file missing/empty)"""
# write encrypt file
2023-07-10 19:43:17 +00:00
if encrypt == "missing":
assert not paths.encrypt.exists(), "Encrypt should be missing"
else:
2023-07-10 19:43:17 +00:00
paths.encrypt.write("")
assert paths.encrypt.exists(), "Encrypt should exist"
assert paths.encrypt.size() == 0, "Encrypt should be empty"
# run parse_encrypt
run = run_parse_encrypt(runner, paths)
assert run.success
2023-07-10 19:43:17 +00:00
assert run.err == ""
# validate parsing result
2023-07-10 19:43:17 +00:00
assert "EIF_COUNT:0" in run.out, "EIF should be empty"
2019-11-26 22:24:36 +00:00
def create_test_encrypt_data(paths):
"""Generate test data for testing encrypt"""
2023-07-10 19:43:17 +00:00
edata = ""
expected = set()
# empty line
2023-07-10 19:43:17 +00:00
edata += "\n"
# simple comments
2023-07-10 19:43:17 +00:00
edata += "# a simple comment\n"
edata += " # a comment with leading space\n"
# unreferenced directory
2023-07-10 19:43:17 +00:00
paths.work.join("unreferenced").mkdir()
# simple files
2023-07-10 19:43:17 +00:00
edata += "simple_file\n"
edata += "simple.file\n"
paths.work.join("simple_file").write("")
paths.work.join("simple.file").write("")
paths.work.join("simple_file2").write("")
paths.work.join("simple.file2").write("")
expected.add("simple_file")
expected.add("simple.file")
# simple files in directories
2023-07-10 19:43:17 +00:00
edata += "simple_dir/simple_file\n"
paths.work.join("simple_dir/simple_file").write("", ensure=True)
paths.work.join("simple_dir/simple_file2").write("", ensure=True)
expected.add("simple_dir/simple_file")
# paths with spaces
2023-07-10 19:43:17 +00:00
edata += "with space/with space\n"
paths.work.join("with space/with space").write("", ensure=True)
paths.work.join("with space/with space2").write("", ensure=True)
expected.add("with space/with space")
# hidden files
2023-07-10 19:43:17 +00:00
edata += ".hidden\n"
paths.work.join(".hidden").write("")
expected.add(".hidden")
# hidden files in directories
2023-07-10 19:43:17 +00:00
edata += ".hidden_dir/.hidden_file\n"
paths.work.join(".hidden_dir/.hidden_file").write("", ensure=True)
expected.add(".hidden_dir/.hidden_file")
# wildcards
2023-07-10 19:43:17 +00:00
edata += "wild*\n"
paths.work.join("wildcard1").write("", ensure=True)
paths.work.join("wildcard2").write("", ensure=True)
expected.add("wildcard1")
expected.add("wildcard2")
2023-07-10 19:43:17 +00:00
edata += "dirwild*\n"
paths.work.join("dirwildcard/file1").write("", ensure=True)
paths.work.join("dirwildcard/file2").write("", ensure=True)
expected.add("dirwildcard")
# excludes
2023-07-10 19:43:17 +00:00
edata += "exclude*\n"
edata += "ex ex/*\n"
paths.work.join("exclude_file1").write("")
paths.work.join("exclude_file2.ex").write("")
paths.work.join("exclude_file3.ex3").write("")
expected.add("exclude_file1")
expected.add("exclude_file3.ex3")
edata += "!*.ex\n"
edata += "!ex ex/*.txt\n"
paths.work.join("ex ex/file4").write("", ensure=True)
paths.work.join("ex ex/file5.txt").write("", ensure=True)
paths.work.join("ex ex/file6.text").write("", ensure=True)
expected.add("ex ex/file4")
expected.add("ex ex/file6.text")
2019-11-26 22:24:36 +00:00
# double star
2023-07-10 19:43:17 +00:00
edata += "doublestar/**/file*\n"
edata += "!**/file3\n"
paths.work.join("doublestar/a/b/file1").write("", ensure=True)
paths.work.join("doublestar/c/d/file2").write("", ensure=True)
paths.work.join("doublestar/e/f/file3").write("", ensure=True)
paths.work.join("doublestar/g/h/nomatch").write("", ensure=True)
expected.add("doublestar/a/b/file1")
expected.add("doublestar/c/d/file2")
2019-11-26 22:24:36 +00:00
# doublestar/e/f/file3 is excluded
return edata, expected
2023-07-10 19:43:17 +00:00
@pytest.mark.usefixtures("ds1_repo_copy")
2019-11-26 22:24:36 +00:00
def test_file_parse_encrypt(runner, paths):
"""Test parse_encrypt
Test an array of supported features of the encrypt configuration.
"""
# generate test data & expectations
edata, expected = create_test_encrypt_data(paths)
# write encrypt file
2023-07-10 19:43:17 +00:00
print(f"ENCRYPT:\n---\n{edata}---\n")
paths.encrypt.write(edata)
assert paths.encrypt.isfile()
# run parse_encrypt
run = run_parse_encrypt(runner, paths)
assert run.success
2023-07-10 19:43:17 +00:00
assert run.err == ""
2023-07-10 19:43:17 +00:00
assert f"EIF_COUNT:{len(expected)}" in run.out, "EIF count wrong"
for expected_file in expected:
2023-07-10 19:43:17 +00:00
assert f"EIF:{expected_file}\n" in run.out
2023-07-10 19:43:17 +00:00
sorted_expectations = "\n".join([f"EIF:{exp}" for exp in sorted(expected)])
2019-03-24 22:05:11 +00:00
assert sorted_expectations in run.out
2023-07-10 19:43:17 +00:00
def run_parse_encrypt(runner, paths, skip_parse=False, twice=False):
"""Run parse_encrypt
A count of ENCRYPT_INCLUDE_FILES will be reported as EIF_COUNT:X. All
values of ENCRYPT_INCLUDE_FILES will be reported as individual EIF:value
lines.
"""
2023-07-10 19:43:17 +00:00
parse_cmd = "parse_encrypt"
if skip_parse:
2023-07-10 19:43:17 +00:00
parse_cmd = ""
if twice:
2023-07-10 19:43:17 +00:00
parse_cmd = "parse_encrypt; parse_encrypt"
script = f"""
YADM_TEST=1 source {paths.pgm}
YADM_ENCRYPT={paths.encrypt}
export YADM_ENCRYPT
GIT_DIR={paths.repo}
export GIT_DIR
2019-12-12 14:00:10 +00:00
YADM_WORK={paths.work}
export YADM_WORK
{parse_cmd}
export ENCRYPT_INCLUDE_FILES
export PARSE_ENCRYPT_SHORT
env
echo EIF_COUNT:${{#ENCRYPT_INCLUDE_FILES[@]}}
for value in "${{ENCRYPT_INCLUDE_FILES[@]}}"; do
echo "EIF:$value"
done
"""
2023-07-10 19:43:17 +00:00
run = runner(command=["bash"], inp=script)
return run