|
1 | 1 | import json |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import patch |
2 | 5 |
|
3 | | -from murfey.util.client import set_default_acquisition_output |
| 6 | +from pytest import mark |
| 7 | + |
| 8 | +from murfey.util.client import read_config, set_default_acquisition_output |
| 9 | + |
| 10 | +test_read_config_params_matrix = ( |
| 11 | + # Environment variable to set | Append to tmp_path |
| 12 | + ( |
| 13 | + "MURFEY_CLIENT_CONFIGURATION", |
| 14 | + "config/murfey-client-config.cfg", |
| 15 | + ), |
| 16 | + ( |
| 17 | + "MURFEY_CLIENT_CONFIG_HOME", |
| 18 | + "config", |
| 19 | + ), |
| 20 | + ( |
| 21 | + "", |
| 22 | + "", |
| 23 | + ), # Test default home directory |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +@mark.parametrize("test_params", test_read_config_params_matrix) |
| 28 | +def test_read_config( |
| 29 | + test_params: tuple[str, str], |
| 30 | + tmp_path, |
| 31 | + mock_client_configuration, |
| 32 | +): |
| 33 | + # Unpack test params |
| 34 | + env_var, partial_path = test_params |
| 35 | + |
| 36 | + # Construct the environment variable and the expected config file path |
| 37 | + env_var_dict: dict[str, str] = {} |
| 38 | + if env_var: |
| 39 | + full_path = tmp_path / partial_path |
| 40 | + env_var_dict[env_var] = str(full_path) |
| 41 | + file_path = full_path if full_path.suffix() else full_path / ".murfey" |
| 42 | + else: |
| 43 | + file_path = Path().home() / ".murfey" |
| 44 | + |
| 45 | + # Make directories all the way to the requested place |
| 46 | + file_path.parent.mkdir(parents=True, exist_ok=True) |
| 47 | + |
| 48 | + # Write the client config fixture to the specified file |
| 49 | + with open(file_path, "w") as file: |
| 50 | + mock_client_configuration.write(file) |
| 51 | + |
| 52 | + # Patch the OS environment variable and run the function |
| 53 | + with patch.dict(os.environ, env_var_dict, clear=False): |
| 54 | + config = read_config() |
| 55 | + |
| 56 | + # Compare returned config with mock one |
| 57 | + assert dict(config["Murfey"]) == dict(mock_client_configuration["Murfey"]) |
4 | 58 |
|
5 | 59 |
|
6 | 60 | def test_set_default_acquisition_output_normal_operation(tmp_path): |
|
0 commit comments