2018-07-11 08:50:42 -04:00
|
|
|
"""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 15:43:17 -04: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"
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
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 15:43:17 -04:00
|
|
|
assert run.err == ""
|
|
|
|
assert "PARSE_ENCRYPT_SHORT=parse_encrypt() not reprocessed" in run.out, "parse_encrypt() should short-circuit"
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"encrypt",
|
|
|
|
[
|
|
|
|
("missing"),
|
|
|
|
("empty"),
|
|
|
|
],
|
|
|
|
)
|
2018-07-11 08:50:42 -04:00
|
|
|
def test_empty(runner, paths, encrypt):
|
|
|
|
"""Test parse_encrypt (file missing/empty)"""
|
|
|
|
|
|
|
|
# write encrypt file
|
2023-07-10 15:43:17 -04:00
|
|
|
if encrypt == "missing":
|
|
|
|
assert not paths.encrypt.exists(), "Encrypt should be missing"
|
2018-07-11 08:50:42 -04:00
|
|
|
else:
|
2023-07-10 15:43:17 -04:00
|
|
|
paths.encrypt.write("")
|
|
|
|
assert paths.encrypt.exists(), "Encrypt should exist"
|
|
|
|
assert paths.encrypt.size() == 0, "Encrypt should be empty"
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# run parse_encrypt
|
|
|
|
run = run_parse_encrypt(runner, paths)
|
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# validate parsing result
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "EIF_COUNT:0" in run.out, "EIF should be empty"
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
|
2019-11-26 17:24:36 -05:00
|
|
|
def create_test_encrypt_data(paths):
|
|
|
|
"""Generate test data for testing encrypt"""
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
edata = ""
|
2018-07-11 08:50:42 -04:00
|
|
|
expected = set()
|
|
|
|
|
|
|
|
# empty line
|
2023-07-10 15:43:17 -04:00
|
|
|
edata += "\n"
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# simple comments
|
2023-07-10 15:43:17 -04:00
|
|
|
edata += "# a simple comment\n"
|
|
|
|
edata += " # a comment with leading space\n"
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# unreferenced directory
|
2023-07-10 15:43:17 -04:00
|
|
|
paths.work.join("unreferenced").mkdir()
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# simple files
|
2023-07-10 15:43:17 -04: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")
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# simple files in directories
|
2023-07-10 15:43:17 -04: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")
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# paths with spaces
|
2023-07-10 15:43:17 -04: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")
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# hidden files
|
2023-07-10 15:43:17 -04:00
|
|
|
edata += ".hidden\n"
|
|
|
|
paths.work.join(".hidden").write("")
|
|
|
|
expected.add(".hidden")
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# hidden files in directories
|
2023-07-10 15:43:17 -04:00
|
|
|
edata += ".hidden_dir/.hidden_file\n"
|
|
|
|
paths.work.join(".hidden_dir/.hidden_file").write("", ensure=True)
|
|
|
|
expected.add(".hidden_dir/.hidden_file")
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# wildcards
|
2023-07-10 15:43:17 -04:00
|
|
|
edata += "wild*\n"
|
|
|
|
paths.work.join("wildcard1").write("", ensure=True)
|
|
|
|
paths.work.join("wildcard2").write("", ensure=True)
|
|
|
|
expected.add("wildcard1")
|
|
|
|
expected.add("wildcard2")
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
edata += "dirwild*\n"
|
|
|
|
paths.work.join("dirwildcard/file1").write("", ensure=True)
|
|
|
|
paths.work.join("dirwildcard/file2").write("", ensure=True)
|
2024-11-24 16:56:07 -05:00
|
|
|
expected.add("dirwildcard/file1")
|
|
|
|
expected.add("dirwildcard/file2")
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# excludes
|
2023-07-10 15:43:17 -04: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")
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2019-11-26 17:24:36 -05:00
|
|
|
# double star
|
2023-07-10 15:43:17 -04: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 17:24:36 -05:00
|
|
|
# doublestar/e/f/file3 is excluded
|
|
|
|
|
|
|
|
return edata, expected
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("ds1_repo_copy")
|
2019-11-26 17:24:36 -05: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)
|
|
|
|
|
2018-07-11 08:50:42 -04:00
|
|
|
# write encrypt file
|
2023-07-10 15:43:17 -04:00
|
|
|
print(f"ENCRYPT:\n---\n{edata}---\n")
|
2018-07-11 08:50:42 -04:00
|
|
|
paths.encrypt.write(edata)
|
|
|
|
assert paths.encrypt.isfile()
|
|
|
|
|
|
|
|
# run parse_encrypt
|
|
|
|
run = run_parse_encrypt(runner, paths)
|
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
assert f"EIF_COUNT:{len(expected)}" in run.out, "EIF count wrong"
|
2018-07-11 08:50:42 -04:00
|
|
|
for expected_file in expected:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert f"EIF:{expected_file}\n" in run.out
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
sorted_expectations = "\n".join([f"EIF:{exp}" for exp in sorted(expected)])
|
2019-03-24 18:05:11 -04:00
|
|
|
assert sorted_expectations in run.out
|
|
|
|
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
def run_parse_encrypt(runner, paths, skip_parse=False, twice=False):
|
2018-07-11 08:50:42 -04:00
|
|
|
"""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 15:43:17 -04:00
|
|
|
parse_cmd = "parse_encrypt"
|
2018-07-11 08:50:42 -04:00
|
|
|
if skip_parse:
|
2023-07-10 15:43:17 -04:00
|
|
|
parse_cmd = ""
|
2018-07-11 08:50:42 -04:00
|
|
|
if twice:
|
2023-07-10 15:43:17 -04:00
|
|
|
parse_cmd = "parse_encrypt; parse_encrypt"
|
2018-07-11 08:50:42 -04:00
|
|
|
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 09:00:10 -05:00
|
|
|
YADM_WORK={paths.work}
|
|
|
|
export YADM_WORK
|
2018-07-11 08:50:42 -04:00
|
|
|
{parse_cmd}
|
2024-11-24 16:56:07 -05:00
|
|
|
echo PARSE_ENCRYPT_SHORT=$PARSE_ENCRYPT_SHORT
|
2018-07-11 08:50:42 -04:00
|
|
|
echo EIF_COUNT:${{#ENCRYPT_INCLUDE_FILES[@]}}
|
|
|
|
for value in "${{ENCRYPT_INCLUDE_FILES[@]}}"; do
|
|
|
|
echo "EIF:$value"
|
|
|
|
done
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=["bash"], inp=script)
|
2018-07-11 08:50:42 -04:00
|
|
|
return run
|