Fix /tmp usage in tests

This commit is contained in:
Matthias 2024-06-08 09:42:01 +02:00
parent 2087974520
commit 36ad3bff62
2 changed files with 10 additions and 15 deletions

View File

@ -165,7 +165,6 @@ max-complexity = 12
]
"tests/**/*.py" = [
"S101", # allow assert in tests
"S108", # temp usage ...
"S104", # Possible binding to all interfaces
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes
"S105", # Possible hardcoded password assigned to: "secret"

View File

@ -62,14 +62,12 @@ def test_create_userdata_dir_exists(mocker, tmp_path) -> None:
assert md.call_count == 0
def test_create_userdata_dir_exists_exception(mocker, default_conf, caplog) -> None:
def test_create_userdata_dir_exists_exception(mocker, tmp_path) -> None:
mocker.patch.object(Path, "is_dir", MagicMock(return_value=False))
md = mocker.patch.object(Path, "mkdir", MagicMock())
with pytest.raises(
OperationalException, match=r"Directory `.{1,2}tmp.{1,2}bar` does not exist.*"
):
create_userdata_dir("/tmp/bar", create_dir=False)
with pytest.raises(OperationalException, match=r"Directory `.*.{1,2}bar` does not exist.*"):
create_userdata_dir(f"{tmp_path}/bar", create_dir=False)
assert md.call_count == 0
@ -89,26 +87,24 @@ def test_copy_sample_files(mocker, tmp_path) -> None:
)
def test_copy_sample_files_errors(mocker, default_conf, caplog) -> None:
def test_copy_sample_files_errors(mocker, tmp_path, caplog) -> None:
mocker.patch.object(Path, "is_dir", MagicMock(return_value=False))
mocker.patch.object(Path, "exists", MagicMock(return_value=False))
mocker.patch("shutil.copy", MagicMock())
with pytest.raises(
OperationalException, match=r"Directory `.{1,2}tmp.{1,2}bar` does not exist\."
):
copy_sample_files(Path("/tmp/bar"))
with pytest.raises(OperationalException, match=r"Directory `.*.{1,2}bar` does not exist\."):
copy_sample_files(Path(f"{tmp_path}/bar"))
mocker.patch.object(Path, "is_dir", MagicMock(side_effect=[True, False]))
with pytest.raises(
OperationalException,
match=r"Directory `.{1,2}tmp.{1,2}bar.{1,2}strategies` does not exist\.",
match=r"Directory `.*.{1,2}bar.{1,2}strategies` does not exist\.",
):
copy_sample_files(Path("/tmp/bar"))
copy_sample_files(Path(f"{tmp_path}/bar"))
mocker.patch.object(Path, "is_dir", MagicMock(return_value=True))
mocker.patch.object(Path, "exists", MagicMock(return_value=True))
copy_sample_files(Path("/tmp/bar"))
copy_sample_files(Path(f"{tmp_path}/bar"))
assert log_has_re(r"File `.*` exists already, not deploying sample file\.", caplog)
caplog.clear()
copy_sample_files(Path("/tmp/bar"), overwrite=True)
copy_sample_files(Path(f"{tmp_path}/bar"), overwrite=True)
assert log_has_re(r"File `.*` exists already, overwriting\.", caplog)