1
0
Fork 0
mirror of synced 2024-11-18 15:15:35 -05:00
yadm/test/test_unit_copy_perms.py

54 lines
1.5 KiB
Python
Raw Permalink Normal View History

"""Unit tests: copy_perms"""
import os
2023-07-10 10:14:33 -04:00
import pytest
2023-07-10 15:43:17 -04:00
OCTAL = "7654"
NON_OCTAL = "9876"
2023-07-10 15:43:17 -04:00
@pytest.mark.parametrize("stat_broken", [True, False], ids=["normal", "stat broken"])
def test_copy_perms(runner, yadm, tmpdir, stat_broken):
"""Test function copy_perms"""
src_mode = 0o754
dst_mode = 0o644
2023-07-10 15:43:17 -04:00
source = tmpdir.join("source")
source.write("test", ensure=True)
source.chmod(src_mode)
2023-07-10 15:43:17 -04:00
dest = tmpdir.join("dest")
dest.write("test", ensure=True)
dest.chmod(dst_mode)
2023-07-10 15:43:17 -04:00
override_stat = ""
if stat_broken:
2023-07-10 15:43:17 -04:00
override_stat = "function stat() { echo broken; }"
script = f"""
YADM_TEST=1 source {yadm}
{override_stat}
copy_perms "{source}" "{dest}"
"""
2023-07-10 15:43:17 -04:00
run = runner(command=["bash"], inp=script)
assert run.success
2023-07-10 15:43:17 -04:00
assert run.err == ""
assert run.out == ""
expected = dst_mode if stat_broken else src_mode
assert oct(os.stat(dest).st_mode)[-3:] == oct(expected)[-3:]
2023-07-10 15:43:17 -04:00
@pytest.mark.parametrize("stat_output", [OCTAL, NON_OCTAL], ids=["octal", "non-octal"])
def test_get_mode(runner, yadm, stat_output):
"""Test function get_mode"""
script = f"""
YADM_TEST=1 source {yadm}
function stat() {{ echo {stat_output}; }}
mode=$(get_mode abc)
echo "MODE:$mode"
"""
2023-07-10 15:43:17 -04:00
run = runner(command=["bash"], inp=script)
assert run.success
2023-07-10 15:43:17 -04:00
assert run.err == ""
expected = OCTAL if stat_output == OCTAL else ""
2023-07-10 15:43:17 -04:00
assert f"MODE:{expected}\n" in run.out