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

View File

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

View File

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