From 87440112975b82f3cb2747371ba3cc2ade243146 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 7 Jun 2021 13:17:22 -0400 Subject: [PATCH] island: move set_home_env() to conftest.py so it can be reused --- .../cc/setup/test_island_config_options.py | 36 ++++++++----------- .../unit_tests/monkey_island/conftest.py | 7 ++++ 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py b/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py index 193cc7350..1554980a2 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py +++ b/monkey/tests/unit_tests/monkey_island/cc/setup/test_island_config_options.py @@ -32,28 +32,24 @@ def test_data_dir_uses_default(): assert_data_dir_equals(TEST_CONFIG_FILE_CONTENTS_UNSPECIFIED, DEFAULT_DATA_DIR) -def test_data_dir_expanduser(monkeypatch, tmpdir): - set_home_env(monkeypatch, tmpdir) +def test_data_dir_expanduser(patched_home_env): DATA_DIR_NAME = "test_data_dir" assert_data_dir_equals( - {"data_dir": os.path.join("~", DATA_DIR_NAME)}, os.path.join(tmpdir, DATA_DIR_NAME) + {"data_dir": os.path.join("~", DATA_DIR_NAME)}, + os.path.join(patched_home_env, DATA_DIR_NAME), ) -def test_data_dir_expandvars(monkeypatch, tmpdir): - set_home_env(monkeypatch, tmpdir) +def test_data_dir_expandvars(patched_home_env): DATA_DIR_NAME = "test_data_dir" assert_data_dir_equals( - {"data_dir": os.path.join("$HOME", DATA_DIR_NAME)}, os.path.join(tmpdir, DATA_DIR_NAME) + {"data_dir": os.path.join("$HOME", DATA_DIR_NAME)}, + os.path.join(patched_home_env, DATA_DIR_NAME), ) -def set_home_env(monkeypatch, tmpdir): - monkeypatch.setenv("HOME", str(tmpdir)) - - def assert_data_dir_equals(config_file_contents, expected_data_dir): assert_island_config_option_equals(config_file_contents, "data_dir", expected_data_dir) @@ -85,23 +81,21 @@ def test_crt_path_specified(): ) -def test_crt_path_expanduser(monkeypatch, tmpdir): - set_home_env(monkeypatch, tmpdir) +def test_crt_path_expanduser(patched_home_env): FILE_NAME = "test.crt" assert_ssl_certificate_file_equals( {"ssl_certificate": {"ssl_certificate_file": os.path.join("~", FILE_NAME)}}, - os.path.join(tmpdir, FILE_NAME), + os.path.join(patched_home_env, FILE_NAME), ) -def test_crt_path_expandvars(monkeypatch, tmpdir): - set_home_env(monkeypatch, tmpdir) +def test_crt_path_expandvars(patched_home_env): FILE_NAME = "test.crt" assert_ssl_certificate_file_equals( {"ssl_certificate": {"ssl_certificate_file": os.path.join("$HOME", FILE_NAME)}}, - os.path.join(tmpdir, FILE_NAME), + os.path.join(patched_home_env, FILE_NAME), ) @@ -122,23 +116,21 @@ def test_key_path_specified(): ) -def test_key_path_expanduser(monkeypatch, tmpdir): - set_home_env(monkeypatch, tmpdir) +def test_key_path_expanduser(patched_home_env): FILE_NAME = "test.key" assert_ssl_certificate_key_file_equals( {"ssl_certificate": {"ssl_certificate_key_file": os.path.join("~", FILE_NAME)}}, - os.path.join(tmpdir, FILE_NAME), + os.path.join(patched_home_env, FILE_NAME), ) -def test_key_path_expandvars(monkeypatch, tmpdir): - set_home_env(monkeypatch, tmpdir) +def test_key_path_expandvars(patched_home_env): FILE_NAME = "test.key" assert_ssl_certificate_key_file_equals( {"ssl_certificate": {"ssl_certificate_key_file": os.path.join("$HOME", FILE_NAME)}}, - os.path.join(tmpdir, FILE_NAME), + os.path.join(patched_home_env, FILE_NAME), ) diff --git a/monkey/tests/unit_tests/monkey_island/conftest.py b/monkey/tests/unit_tests/monkey_island/conftest.py index 037bee72b..f45eb3ed6 100644 --- a/monkey/tests/unit_tests/monkey_island/conftest.py +++ b/monkey/tests/unit_tests/monkey_island/conftest.py @@ -6,3 +6,10 @@ import pytest @pytest.fixture(scope="module") def server_configs_dir(data_for_tests_dir): return os.path.join(data_for_tests_dir, "server_configs") + + +@pytest.fixture +def patched_home_env(monkeypatch, tmpdir): + monkeypatch.setenv("HOME", str(tmpdir)) + + return tmpdir