|
27 | 27 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | import os |
29 | 29 | from pathlib import Path |
30 | | -from unittest.mock import patch |
| 30 | +from unittest.mock import MagicMock, patch |
31 | 31 | import pytest |
32 | 32 |
|
33 | 33 | from servicex.configuration import Configuration |
34 | 34 |
|
35 | 35 |
|
| 36 | +@patch("servicex.configuration.getuser", return_value="cache_user") |
36 | 37 | @patch("servicex.configuration.tempfile.gettempdir", return_value="./mytemp") |
37 | | -def test_config_read(tempdir): |
38 | | - # Windows style user name |
| 38 | +def test_config_read(tempdir: MagicMock, getuser_mock: MagicMock) -> None: |
| 39 | + # Windows style user name should not override the getpass result. |
39 | 40 | os.environ["UserName"] = "p_higgs" |
40 | 41 | c = Configuration.read(config_path="tests/example_config.yaml") |
41 | | - assert c.cache_path == "mytemp/servicex_p_higgs" |
| 42 | + assert c.cache_path == "mytemp/servicex_cache_user" |
| 43 | + os.environ.pop("UserName") |
42 | 44 |
|
43 | | - # Reset environment |
44 | | - del os.environ["UserName"] |
45 | | - |
46 | | - # Linux style user name |
| 45 | + # Linux style user name should also not override the getpass result. |
47 | 46 | os.environ["USER"] = "p_higgs2" |
48 | 47 | c = Configuration.read(config_path="tests/example_config.yaml") |
49 | | - assert c.cache_path == "mytemp/servicex_p_higgs2" |
| 48 | + assert c.cache_path == "mytemp/servicex_cache_user" |
| 49 | + os.environ.pop("USER") |
50 | 50 |
|
51 | 51 | # but what if there is no file at all? |
52 | 52 | with pytest.raises(NameError): |
53 | 53 | Configuration.read(config_path="invalid.yaml") |
54 | 54 |
|
55 | 55 |
|
| 56 | +@patch("servicex.configuration.getuser", return_value="cache_user") |
56 | 57 | @patch("servicex.configuration.tempfile.gettempdir", return_value="./mytemp") |
57 | | -def test_default_cache_path(tempdir): |
| 58 | +def test_default_cache_path(tempdir: MagicMock, getuser_mock: MagicMock) -> None: |
58 | 59 |
|
59 | | - # Windows style user name |
| 60 | + # Windows style user name should not override the getpass result. |
60 | 61 | os.environ["UserName"] = "p_higgs" |
61 | 62 | c = Configuration.read(config_path="tests/example_config_no_cache_path.yaml") |
62 | | - assert c.cache_path == "mytemp/servicex_p_higgs" |
63 | | - del os.environ["UserName"] |
| 63 | + assert c.cache_path == "mytemp/servicex_cache_user" |
| 64 | + os.environ.pop("UserName") |
64 | 65 |
|
65 | | - # Linux style user name |
| 66 | + # Linux style user name should also not override the getpass result. |
66 | 67 | os.environ["USER"] = "p_higgs" |
67 | 68 | c = Configuration.read(config_path="tests/example_config_no_cache_path.yaml") |
68 | | - assert c.cache_path == "mytemp/servicex_p_higgs" |
69 | | - del os.environ["USER"] |
| 69 | + assert c.cache_path == "mytemp/servicex_cache_user" |
| 70 | + os.environ.pop("USER") |
70 | 71 |
|
71 | 72 |
|
72 | 73 | def test_read_from_home(monkeypatch, tmp_path): |
|
0 commit comments