-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_environ.py
More file actions
61 lines (52 loc) · 2.45 KB
/
test_environ.py
File metadata and controls
61 lines (52 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
from ..syntax import macros, test, test_raises, the # noqa: F401
from ..test.fixtures import session, testset
import os
from ..environ import override
def runtests():
with testset("environ.override"):
# Basic override and restore
os.environ["_UNPYTHONIC_TEST_VAR"] = "original"
with override(_UNPYTHONIC_TEST_VAR="overridden"):
test[the[os.environ["_UNPYTHONIC_TEST_VAR"]] == "overridden"]
test[the[os.environ["_UNPYTHONIC_TEST_VAR"]] == "original"]
del os.environ["_UNPYTHONIC_TEST_VAR"]
# Adding a variable that didn't exist before
key = "_UNPYTHONIC_TEST_NEW"
if key in os.environ:
del os.environ[key]
with override(**{key: "added"}):
test[the[os.environ[key]] == "added"]
test[key not in os.environ]
# Multiple overrides at once
os.environ["_UNPYTHONIC_TEST_A"] = "a_orig"
os.environ["_UNPYTHONIC_TEST_B"] = "b_orig"
with override(_UNPYTHONIC_TEST_A="a_new", _UNPYTHONIC_TEST_B="b_new"):
test[the[os.environ["_UNPYTHONIC_TEST_A"]] == "a_new"]
test[the[os.environ["_UNPYTHONIC_TEST_B"]] == "b_new"]
test[the[os.environ["_UNPYTHONIC_TEST_A"]] == "a_orig"]
test[the[os.environ["_UNPYTHONIC_TEST_B"]] == "b_orig"]
del os.environ["_UNPYTHONIC_TEST_A"]
del os.environ["_UNPYTHONIC_TEST_B"]
# Nested overrides (same-thread; RLock allows this)
os.environ["_UNPYTHONIC_TEST_VAR"] = "level0"
with override(_UNPYTHONIC_TEST_VAR="level1"):
test[the[os.environ["_UNPYTHONIC_TEST_VAR"]] == "level1"]
with override(_UNPYTHONIC_TEST_VAR="level2"):
test[the[os.environ["_UNPYTHONIC_TEST_VAR"]] == "level2"]
test[the[os.environ["_UNPYTHONIC_TEST_VAR"]] == "level1"]
test[the[os.environ["_UNPYTHONIC_TEST_VAR"]] == "level0"]
del os.environ["_UNPYTHONIC_TEST_VAR"]
# Restore on exception
os.environ["_UNPYTHONIC_TEST_VAR"] = "before"
try:
with override(_UNPYTHONIC_TEST_VAR="during"):
test[the[os.environ["_UNPYTHONIC_TEST_VAR"]] == "during"]
raise RuntimeError("boom")
except RuntimeError:
pass
test[the[os.environ["_UNPYTHONIC_TEST_VAR"]] == "before"]
del os.environ["_UNPYTHONIC_TEST_VAR"]
if __name__ == '__main__': # pragma: no cover
with session(__file__):
runtests()