Skip to content

Commit a5bcd4c

Browse files
committed
Add context manager for changing working directories to tests
1 parent ced54d5 commit a5bcd4c

File tree

1 file changed

+56
-37
lines changed

1 file changed

+56
-37
lines changed

tests/test_basic.py

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@
2222
from click.testing import CliRunner
2323
from mkdocs.__main__ import build_command
2424
import git as gitpython
25+
from contextlib import contextmanager
26+
27+
28+
@contextmanager
29+
def working_directory(path):
30+
"""
31+
Temporarily change working directory.
32+
A context manager which changes the working directory to the given
33+
path, and then changes it back to its previous value on exit.
34+
Usage:
35+
```python
36+
# Do something in original directory
37+
with working_directory('/my/new/path'):
38+
# Do something in new directory
39+
# Back to old directory
40+
```
41+
"""
42+
prev_cwd = os.getcwd()
43+
os.chdir(path)
44+
try:
45+
yield
46+
finally:
47+
os.chdir(prev_cwd)
2548

2649

2750
def build_docs_setup(mkdocs_path, output_path):
@@ -63,30 +86,31 @@ def test_project_with_no_commits(tmp_path):
6386
str(testproject_path / "website" / "mkdocs.yml"),
6487
)
6588

66-
cwd = os.getcwd()
67-
os.chdir(str(testproject_path))
89+
with working_directory(str(testproject_path)):
90+
# run 'git init'
91+
gitpython.Repo.init(testproject_path, bare=False)
6892

69-
# run 'git init'
70-
gitpython.Repo.init(testproject_path, bare=False)
93+
result = build_docs_setup(
94+
str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
95+
)
96+
assert result.exit_code == 0, (
97+
"'mkdocs build' command failed. Error: %s" % result.stdout
98+
)
7199

72-
result = build_docs_setup(
73-
str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
74-
)
75-
assert result.exit_code == 0, (
76-
"'mkdocs build' command failed. Error: %s" % result.stdout
77-
)
78100

79-
os.chdir(cwd)
80101

81102

82103
def test_building_empty_site(tmp_path):
83104
"""
84105
Structure:
85106
107+
```
86108
tmp_path/testproject
87109
website/
88110
├── docs/
89-
└── mkdocs.yml"""
111+
└── mkdocs.yml
112+
````
113+
"""
90114
testproject_path = tmp_path / "testproject"
91115

92116
shutil.copytree(
@@ -97,30 +121,30 @@ def test_building_empty_site(tmp_path):
97121
str(testproject_path / "website" / "mkdocs.yml"),
98122
)
99123

100-
cwd = os.getcwd()
101-
os.chdir(str(testproject_path))
124+
with working_directory(str(testproject_path)):
125+
# run 'git init'
126+
gitpython.Repo.init(testproject_path, bare=False)
102127

103-
# run 'git init'
104-
gitpython.Repo.init(testproject_path, bare=False)
128+
result = build_docs_setup(
129+
str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
130+
)
131+
assert result.exit_code == 0, (
132+
"'mkdocs build' command failed. Error: %s" % result.stdout
133+
)
105134

106-
result = build_docs_setup(
107-
str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
108-
)
109-
assert result.exit_code == 0, (
110-
"'mkdocs build' command failed. Error: %s" % result.stdout
111-
)
112-
113-
os.chdir(cwd)
114135

115136

116137
def test_fallback(tmp_path):
117138
"""
118139
Structure:
119140
141+
```
120142
tmp_path/testproject
121143
website/
122144
├── docs/
123-
└── mkdocs.yml"""
145+
└── mkdocs.yml
146+
````
147+
"""
124148
testproject_path = tmp_path / "testproject"
125149

126150
shutil.copytree(
@@ -131,16 +155,11 @@ def test_fallback(tmp_path):
131155
str(testproject_path / "website" / "mkdocs.yml"),
132156
)
133157

134-
cwd = os.getcwd()
135-
os.chdir(str(testproject_path))
136-
137-
print(str(testproject_path))
138-
139-
result = build_docs_setup(
140-
str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
141-
)
142-
assert result.exit_code == 0, (
143-
"'mkdocs build' command failed. Error: %s" % result.stdout
144-
)
158+
with working_directory(str(testproject_path)):
145159

146-
os.chdir(cwd)
160+
result = build_docs_setup(
161+
str(testproject_path / "website/mkdocs.yml"), str(testproject_path / "site")
162+
)
163+
assert result.exit_code == 0, (
164+
"'mkdocs build' command failed. Error: %s" % result.stdout
165+
)

0 commit comments

Comments
 (0)