tests: Refactor create_empty_file() -> create_empty_tmp_file()

This commit is contained in:
Mike Salvatore 2021-06-09 14:12:34 -04:00
parent cf5b1378f2
commit 67d4f18d65
3 changed files with 23 additions and 29 deletions

View File

@ -21,40 +21,32 @@ def test_expand_vars(patched_home_env):
@pytest.mark.skipif(os.name != "posix", reason="Tests Posix (not Windows) permissions.") @pytest.mark.skipif(os.name != "posix", reason="Tests Posix (not Windows) permissions.")
def test_has_expected_permissions_true_linux(tmpdir, create_empty_file): def test_has_expected_permissions_true_linux(tmpdir, create_empty_tmp_file):
file_name = f"{tmpdir}/test" file_name = create_empty_tmp_file("test")
create_empty_file(file_name)
os.chmod(file_name, 0o754) os.chmod(file_name, 0o754)
assert file_utils.has_expected_permissions(file_name, 0o754) assert file_utils.has_expected_permissions(file_name, 0o754)
@pytest.mark.skipif(os.name != "posix", reason="Tests Posix (not Windows) permissions.") @pytest.mark.skipif(os.name != "posix", reason="Tests Posix (not Windows) permissions.")
def test_has_expected_permissions_false_linux(tmpdir, create_empty_file): def test_has_expected_permissions_false_linux(tmpdir, create_empty_tmp_file):
file_name = f"{tmpdir}/test" file_name = create_empty_tmp_file("test")
create_empty_file(file_name)
os.chmod(file_name, 0o755) os.chmod(file_name, 0o755)
assert not file_utils.has_expected_permissions(file_name, 0o700) assert not file_utils.has_expected_permissions(file_name, 0o700)
@pytest.mark.skipif(os.name == "posix", reason="Tests Windows (not Posix) permissions.") @pytest.mark.skipif(os.name == "posix", reason="Tests Windows (not Posix) permissions.")
def test_has_expected_permissions_true_windows(tmpdir, create_empty_file): def test_has_expected_permissions_true_windows(tmpdir, create_empty_tmp_file):
file_name = f"{tmpdir}/test" file_name = create_empty_tmp_file("test")
create_empty_file(file_name)
subprocess.run(f"echo y| cacls {file_name} /p %USERNAME%:F", shell=True) # noqa: DUO116 subprocess.run(f"echo y| cacls {file_name} /p %USERNAME%:F", shell=True) # noqa: DUO116
assert file_utils.has_expected_permissions(file_name, 2032127) assert file_utils.has_expected_permissions(file_name, 2032127)
@pytest.mark.skipif(os.name == "posix", reason="Tests Windows (not Posix) permissions.") @pytest.mark.skipif(os.name == "posix", reason="Tests Windows (not Posix) permissions.")
def test_has_expected_permissions_false_windows(tmpdir, create_empty_file): def test_has_expected_permissions_false_windows(tmpdir, create_empty_tmp_file):
file_name = f"{tmpdir}/test" file_name = create_empty_tmp_file("test")
create_empty_file(file_name)
subprocess.run(f"echo y| cacls {file_name} /p %USERNAME%:R", shell=True) # noqa: DUO116 subprocess.run(f"echo y| cacls {file_name} /p %USERNAME%:R", shell=True) # noqa: DUO116
assert not file_utils.has_expected_permissions(file_name, 2032127) assert not file_utils.has_expected_permissions(file_name, 2032127)

View File

@ -32,13 +32,12 @@ def linux_island_config_options(create_read_only_linux_file: Callable):
@pytest.fixture @pytest.fixture
def create_read_only_linux_file(tmpdir: str, create_empty_file: Callable) -> Callable: def create_read_only_linux_file(tmpdir: str, create_empty_tmp_file: Callable) -> Callable:
def inner(file_name: str) -> str: def inner(file_name: str) -> str:
new_file = os.path.join(tmpdir, file_name) full_file_path = create_empty_tmp_file(file_name)
create_empty_file(new_file) os.chmod(full_file_path, LINUX_READ_ONLY_BY_USER)
os.chmod(new_file, LINUX_READ_ONLY_BY_USER)
return new_file return full_file_path
return inner return inner
@ -93,14 +92,13 @@ def windows_island_config_options(tmpdir: str, create_read_only_windows_file: Ca
@pytest.fixture @pytest.fixture
def create_read_only_windows_file(tmpdir: str, create_empty_file: Callable) -> Callable: def create_read_only_windows_file(tmpdir: str, create_empty_tmp_file: Callable) -> Callable:
def inner(file_name: str) -> str: def inner(file_name: str) -> str:
new_file = os.path.join(tmpdir, file_name) full_file_path = create_empty_tmp_file(file_name)
create_empty_file(new_file) cmd_to_change_permissions = get_windows_cmd_to_change_permissions(full_file_path, "R")
cmd_to_change_permissions = get_windows_cmd_to_change_permissions(new_file, "R")
subprocess.run(cmd_to_change_permissions, shell=True) # noqa DUO116 subprocess.run(cmd_to_change_permissions, shell=True) # noqa DUO116
return new_file return full_file_path
return inner return inner

View File

@ -1,4 +1,5 @@
import os import os
from collections.abc import Callable
import pytest import pytest
@ -16,9 +17,12 @@ def patched_home_env(monkeypatch, tmpdir):
@pytest.fixture @pytest.fixture
def create_empty_file(): def create_empty_tmp_file(tmpdir: str) -> Callable:
def inner(file_name): def inner(file_name: str):
with open(file_name, "w"): new_file = os.path.join(tmpdir, file_name)
with open(new_file, "w"):
pass pass
return new_file
return inner return inner