-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_decorators.py
More file actions
64 lines (46 loc) · 1.89 KB
/
test_decorators.py
File metadata and controls
64 lines (46 loc) · 1.89 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
62
63
64
"""Tests for our tests helpers. 8-}."""
import os
import sys
from cli_test_helpers import ArgvContext, EnvironContext, RandomDirectoryContext
def test_argv_context():
"""
Does ArgvContext set new argvs and reset old ones correctly?
"""
old = sys.argv
new = ["Alice", "Bob", "Chris", "Daisy"]
assert sys.argv == old
with ArgvContext(*new):
assert sys.argv == new, (
"sys.argv wasn't correctly changed by the contextmanager"
)
assert sys.argv == old, "sys.argv wasn't correctly reset"
def test_environ_context():
"""
Does EnvironContext set new environ values and reset old ones correctly?
"""
old_environ = os.environ
old_path = os.getenv("PATH")
assert os.environ == old_environ
assert os.getenv("PATH") is not None, "Invalid test setup"
assert os.getenv("FOO") is None, "Invalid test setup"
with EnvironContext(PATH=None, FOO="my foo value"):
assert os.getenv("PATH") is None, (
"os.environ[PATH] wasn't removed by the contextmanager"
)
assert os.getenv("FOO") == "my foo value", (
"os.environ[FOO] wasn't set by the contextmanager"
)
assert os.environ == old_environ, "object os.environ was not restored"
assert os.getenv("PATH") == old_path, "env var PATH was not restored"
assert os.getenv("FOO") is None, "env var FOO was not cleared"
def test_random_directory_context():
"""
In a directory context, are we effectively in a different location?
"""
before_dir = os.getcwd()
with RandomDirectoryContext() as random_dir:
new_dir = os.getcwd()
assert new_dir == random_dir, "Doesn't behave like TemporaryDirectory"
assert new_dir != before_dir, "Context not in a different file system location"
after_dir = os.getcwd()
assert after_dir == before_dir, "Execution directory not restored to original"