Skip to content

Commit 2405ed8

Browse files
committed
Improve tests for env variable expansion functionality
This includes mocking the environment variables instead of modifying the global environment during unit tests, and adding extra tests for the underlying expansion function in `utils.py`.
1 parent 850f6b0 commit 2405ed8

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

tests/test_config.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -884,10 +884,10 @@ def test_alias_volumes_set(self):
884884
assert v.host_path == '/host/bar'
885885
assert v.options == ['z', 'ro']
886886

887-
def test_volumes_with_env_vars_simple(self):
887+
def test_volumes_with_env_vars_simple(self, monkeypatch):
888888
'''volume definitions can contain environment variables'''
889-
os.environ["TEST_VOL_PATH"] = "/bar/baz"
890-
os.environ["TEST_VOL_PATH2"] = "/moo/doo"
889+
monkeypatch.setenv("TEST_VOL_PATH", "/bar/baz")
890+
monkeypatch.setenv("TEST_VOL_PATH2", "/moo/doo")
891891
with open('.scuba.yml', 'w') as f:
892892
f.write(r'''
893893
image: na
@@ -905,11 +905,11 @@ def test_volumes_with_env_vars_simple(self):
905905
assert v.host_path == '/moo/doo/foo'
906906
assert v.options == []
907907

908-
def test_volumes_with_env_vars_complex(self):
908+
def test_volumes_with_env_vars_complex(self, monkeypatch):
909909
'''complex volume definitions can contain environment variables'''
910-
os.environ["TEST_HOME"] = "/home/testuser"
911-
os.environ["TEST_TMP"] = "/tmp"
912-
os.environ["TEST_MAIL"] = "/var/spool/mail/testuser"
910+
monkeypatch.setenv("TEST_HOME", "/home/testuser")
911+
monkeypatch.setenv("TEST_TMP", "/tmp")
912+
monkeypatch.setenv("TEST_MAIL", "/var/spool/mail/testuser")
913913

914914
with open('.scuba.yml', 'w') as f:
915915
f.write(r'''
@@ -945,10 +945,10 @@ def test_volumes_with_env_vars_complex(self):
945945
assert v.host_path == "/var/spool/mail/testuser"
946946
assert v.options == ['z', 'ro']
947947

948-
def test_volumes_with_invalid_env_vars(self):
948+
def test_volumes_with_invalid_env_vars(self, monkeypatch):
949949
'''Volume definitions cannot include unset env vars'''
950950
# Ensure that the entry does not exist in the environment
951-
os.environ.pop("TEST_VAR1", None)
951+
monkeypatch.delenv("TEST_VAR1", raising=False)
952952
with open('.scuba.yml', 'w') as f:
953953
f.write(r'''
954954
image: na

tests/test_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,26 @@ def test_writeln():
128128
scuba.utils.writeln(s, 'hello')
129129
scuba.utils.writeln(s, 'goodbye')
130130
assert s.getvalue() == 'hello\ngoodbye\n'
131+
132+
def test_expand_env_vars(monkeypatch):
133+
monkeypatch.setenv("MY_VAR", "my favorite variable")
134+
assert scuba.utils.expand_env_vars("This is $MY_VAR") == \
135+
"This is my favorite variable"
136+
assert scuba.utils.expand_env_vars("What is ${MY_VAR}?") == \
137+
"What is my favorite variable?"
138+
139+
def test_expand_missing_env_vars(monkeypatch):
140+
monkeypatch.delenv("MY_VAR", raising=False)
141+
# Verify that a KeyError is raised for unset env variables
142+
with pytest.raises(KeyError) as kerr:
143+
scuba.utils.expand_env_vars("Where is ${MY_VAR}?")
144+
assert kerr.value.args[0] == "MY_VAR"
145+
146+
147+
def test_expand_env_vars_dollars():
148+
# Verify that a ValueError is raised for bare, unescaped '$' characters
149+
with pytest.raises(ValueError):
150+
scuba.utils.expand_env_vars("Just a lonely $")
151+
152+
# Verify that it is possible to get '$' characters in an expanded string
153+
assert scuba.utils.expand_env_vars(r"Just a lonely $$") == "Just a lonely $"

0 commit comments

Comments
 (0)