Skip to content

Commit 5faa2cd

Browse files
committed
Use getpass without env overrides for cache path
1 parent fcc1909 commit 5faa2cd

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

servicex/configuration.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2727
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
import os
29+
from getpass import getuser
2930
import tempfile
3031
from pathlib import Path, PurePath
3132
from typing import List, Optional, Dict
@@ -55,7 +56,7 @@ class Configuration(BaseModel):
5556
config_file: Optional[str] = Field(default=None, exclude=True)
5657

5758
@model_validator(mode="after")
58-
def expand_cache_path(self):
59+
def expand_cache_path(self) -> "Configuration":
5960
"""
6061
Expand the cache path to a full path, and create it if it doesn't exist.
6162
Expand ${USER} to be the user name on the system. Works for windows, too.
@@ -68,12 +69,13 @@ def expand_cache_path(self):
6869

6970
s_path = os.path.expanduser(self.cache_path)
7071

71-
# If they have tried to use the USER or UserName as an expansion, and it has failed, then
72-
# translate it to maintain harmony across platforms.
73-
if "${USER}" in s_path and "UserName" in os.environ:
74-
s_path = s_path.replace("${USER}", os.environ["UserName"])
75-
if "${USER}" in s_path and "USER" in os.environ:
76-
s_path = s_path.replace("${USER}", os.environ["USER"])
72+
if "${USER}" in s_path:
73+
try:
74+
username = getuser()
75+
except Exception: # pragma: no cover - getpass failures are unexpected
76+
username = ""
77+
78+
s_path = s_path.replace("${USER}", username)
7779

7880
p_p = PurePath(s_path)
7981
if len(p_p.parts) > 1 and p_p.parts[1] == "tmp":

tests/test_config.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,46 +27,47 @@
2727
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
import os
2929
from pathlib import Path
30-
from unittest.mock import patch
30+
from unittest.mock import MagicMock, patch
3131
import pytest
3232

3333
from servicex.configuration import Configuration
3434

3535

36+
@patch("servicex.configuration.getuser", return_value="cache_user")
3637
@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.
3940
os.environ["UserName"] = "p_higgs"
4041
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")
4244

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.
4746
os.environ["USER"] = "p_higgs2"
4847
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")
5050

5151
# but what if there is no file at all?
5252
with pytest.raises(NameError):
5353
Configuration.read(config_path="invalid.yaml")
5454

5555

56+
@patch("servicex.configuration.getuser", return_value="cache_user")
5657
@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:
5859

59-
# Windows style user name
60+
# Windows style user name should not override the getpass result.
6061
os.environ["UserName"] = "p_higgs"
6162
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")
6465

65-
# Linux style user name
66+
# Linux style user name should also not override the getpass result.
6667
os.environ["USER"] = "p_higgs"
6768
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")
7071

7172

7273
def test_read_from_home(monkeypatch, tmp_path):

0 commit comments

Comments
 (0)