island: Refactor duplicate code in IslandConfigOptions data_dir tests

This commit is contained in:
Mike Salvatore 2021-06-01 03:46:28 -04:00
parent c761dba030
commit 2cd665ff11
1 changed files with 35 additions and 27 deletions

View File

@ -18,11 +18,41 @@ TEST_CONFIG_FILE_CONTENTS_UNSPECIFIED = {}
TEST_CONFIG_FILE_CONTENTS_NO_STARTMONGO = {"mongodb": {}}
def test_island_config_options__data_dir():
options = IslandConfigOptions(TEST_CONFIG_FILE_CONTENTS_SPECIFIED)
assert options.data_dir == "/tmp"
options = IslandConfigOptions(TEST_CONFIG_FILE_CONTENTS_UNSPECIFIED)
assert options.data_dir == DEFAULT_DATA_DIR
def test_island_config_options__data_dir_specified():
assert_island_config_options_data_dir_equals(TEST_CONFIG_FILE_CONTENTS_SPECIFIED, "/tmp")
def test_island_config_options__data_dir_uses_default():
assert_island_config_options_data_dir_equals(
TEST_CONFIG_FILE_CONTENTS_UNSPECIFIED, DEFAULT_DATA_DIR
)
def test_island_config_options__data_dir_expanduser(monkeypatch, tmpdir):
set_home_env(monkeypatch, tmpdir)
DATA_DIR_NAME = "test_data_dir"
assert_island_config_options_data_dir_equals(
{"data_dir": f"~/{DATA_DIR_NAME}"}, os.path.join(tmpdir, DATA_DIR_NAME)
)
def test_island_config_options__data_dir_expandvars(monkeypatch, tmpdir):
set_home_env(monkeypatch, tmpdir)
DATA_DIR_NAME = "test_data_dir"
assert_island_config_options_data_dir_equals(
{"data_dir": f"$HOME/{DATA_DIR_NAME}"}, os.path.join(tmpdir, DATA_DIR_NAME)
)
def set_home_env(monkeypatch, tmpdir):
monkeypatch.setenv("HOME", str(tmpdir))
def assert_island_config_options_data_dir_equals(config_file_contents, expected_data_dir):
options = IslandConfigOptions(config_file_contents)
assert options.data_dir == expected_data_dir
def test_island_config_options__log_level():
@ -39,25 +69,3 @@ def test_island_config_options__mongodb():
assert options.start_mongodb == DEFAULT_START_MONGO_DB
options = IslandConfigOptions(TEST_CONFIG_FILE_CONTENTS_NO_STARTMONGO)
assert options.start_mongodb == DEFAULT_START_MONGO_DB
def set_home_env(monkeypatch, tmpdir):
monkeypatch.setenv("HOME", str(tmpdir))
def test_island_config_options__data_dir_expanduser(monkeypatch, tmpdir):
set_home_env(monkeypatch, tmpdir)
DATA_DIR_NAME = "test_data_dir"
options = IslandConfigOptions({"data_dir": f"~/{DATA_DIR_NAME}"})
assert options.data_dir == os.path.join(tmpdir, DATA_DIR_NAME)
def test_island_config_options__data_dir_expandvars(monkeypatch, tmpdir):
set_home_env(monkeypatch, tmpdir)
DATA_DIR_NAME = "test_data_dir"
options = IslandConfigOptions({"data_dir": f"$HOME/{DATA_DIR_NAME}"})
assert options.data_dir == os.path.join(tmpdir, DATA_DIR_NAME)