2020-07-11 15:13:24 -04:00
|
|
|
"""Unit tests: copy_perms"""
|
|
|
|
import os
|
2023-07-10 10:14:33 -04:00
|
|
|
|
2020-07-11 15:13:24 -04:00
|
|
|
import pytest
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
OCTAL = "7654"
|
|
|
|
NON_OCTAL = "9876"
|
2020-07-11 15:13:24 -04:00
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.parametrize("stat_broken", [True, False], ids=["normal", "stat broken"])
|
2020-07-11 15:13:24 -04:00
|
|
|
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)
|
2020-07-11 15:13:24 -04:00
|
|
|
source.chmod(src_mode)
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
dest = tmpdir.join("dest")
|
|
|
|
dest.write("test", ensure=True)
|
2020-07-11 15:13:24 -04:00
|
|
|
dest.chmod(dst_mode)
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
override_stat = ""
|
2020-07-11 15:13:24 -04:00
|
|
|
if stat_broken:
|
2023-07-10 15:43:17 -04:00
|
|
|
override_stat = "function stat() { echo broken; }"
|
2020-07-11 15:13:24 -04:00
|
|
|
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)
|
2020-07-11 15:13:24 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
|
|
|
assert run.out == ""
|
2020-07-11 15:13:24 -04:00
|
|
|
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"])
|
2020-07-11 15:13:24 -04:00
|
|
|
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)
|
2020-07-11 15:13:24 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2020-07-11 15:13:24 -04:00
|
|
|
expected = OCTAL if stat_output == OCTAL else ""
|
2023-07-10 15:43:17 -04:00
|
|
|
assert f"MODE:{expected}\n" in run.out
|