File: cwd-context-manager.patch

package info (click to toggle)
plastex 2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,740 kB
  • sloc: python: 20,923; xml: 18,043; ansic: 46; makefile: 34; sh: 23
file content (186 lines) | stat: -rw-r--r-- 6,420 bytes parent folder | download | duplicates (2)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
Description: Use context managers to manage working directory in test suite
Author: Stuart Prescott <stuart@debian.org>
Forwarded: https://github.com/plastex/plastex/pull/133
diff --git a/unittests/FunctionalPackageResource.py b/unittests/FunctionalPackageResource.py
index b89b54d..eb7fb67 100644
--- a/unittests/FunctionalPackageResource.py
+++ b/unittests/FunctionalPackageResource.py
@@ -19,8 +19,10 @@ def test_package_resource(tmpdir):
 	
 	doc = tex.parse()
 	doc.userdata['working-dir'] = os.path.dirname(__file__)
-	os.chdir(str(tmpdir))
-	Renderer().render(doc)
+
+	with tmpdir.as_cwd():
+		Renderer().render(doc)
+
 	assert tmpdir.join('styles', 'test.css').isfile()
 	assert tmpdir.join('js', 'test.js').isfile()
 	assert 'class="em"' in tmpdir.join('index.html').read()
diff --git a/unittests/Packages/conftest.py b/unittests/Packages/conftest.py
index ad49aa3..7bfe2a0 100644
--- a/unittests/Packages/conftest.py
+++ b/unittests/Packages/conftest.py
@@ -50,8 +50,9 @@ def renderXHTML():
         # Create document file
 
         # Run plastex on the document
-        os.chdir(str(tmpdir))
-        Renderer().render(doc)
+        with tmpdir.as_cwd():
+            Renderer().render(doc)
+
         assert tmpdir.join('index.html').isfile()
 
         # Get output file
diff --git a/unittests/tikzpicture.py b/unittests/tikzpicture.py
index 8137364..7498801 100644
--- a/unittests/tikzpicture.py
+++ b/unittests/tikzpicture.py
@@ -6,6 +6,7 @@ try:
 except ImportError:
     from mock import Mock
 
+import py.path
 from pytest import mark, fixture
 
 from plasTeX.TeX import TeX, TeXDocument
@@ -63,8 +64,6 @@ def document_cd():
 
 
 def test_tikz_basic_setup(monkeypatch, tmpdir, document):
-    cur_dir = os.getcwd()
-    os.chdir(os.path.dirname(__file__))
     mock_call = Mock()
     mock_move = Mock()
     monkeypatch.setattr('subprocess.call', mock_call)
@@ -73,9 +72,10 @@ def test_tikz_basic_setup(monkeypatch, tmpdir, document):
 
     doc = document()
     tikz_tmpdir = doc.userdata['tikzpicture']['tmp_dir']
-    os.chdir(str(tmpdir))
-    renderer = Renderer()
-    renderer.render(doc)
+
+    with tmpdir.as_cwd():
+        renderer = Renderer()
+        renderer.render(doc)
 
     pics = doc.getElementsByTagName('tikzpicture')
     assert pics
@@ -89,23 +89,23 @@ def test_tikz_basic_setup(monkeypatch, tmpdir, document):
     assert mock_move.called
 
     assert 'TikZ picture' in tmpdir.join('index.html').read()
-    os.chdir(cur_dir)
 
 
 def test_tikz_config_options(monkeypatch, tmpdir, document):
-    cur_dir = os.getcwd()
-    os.chdir(os.path.dirname(__file__))
     mock_call = Mock()
     mock_move = Mock()
     monkeypatch.setattr('subprocess.call', mock_call)
     monkeypatch.setattr('os.remove', Mock)
     monkeypatch.setattr('shutil.move', mock_move)
 
-    doc = document(compiler='xelatex', converter='mockconv', template='tikztemplate')
+    with py.path.local(os.path.dirname(__file__)).as_cwd():
+        doc = document(compiler='xelatex', converter='mockconv', template='tikztemplate')
     tikz_tmpdir = doc.userdata['tikzpicture']['tmp_dir']
-    os.chdir(str(tmpdir))
-    renderer = Renderer()
-    renderer.render(doc)
+
+    with tmpdir.as_cwd():
+        renderer = Renderer()
+        renderer.render(doc)
+
     pics = doc.getElementsByTagName('tikzpicture')
     assert pics
     tex_path = os.path.join(tikz_tmpdir, pics[0].id + '.tex')
@@ -115,11 +115,8 @@ def test_tikz_config_options(monkeypatch, tmpdir, document):
 
     assert 'xelatex' in mock_call.call_args_list[0][0][0]
     assert 'mockconv' in mock_call.call_args_list[1][0][0]
-    os.chdir(cur_dir)
 
 def test_tikzcd_basic_setup(monkeypatch, tmpdir, document_cd):
-    cur_dir = os.getcwd()
-    os.chdir(os.path.dirname(__file__))
     mock_call = Mock()
     mock_move = Mock()
     monkeypatch.setattr('subprocess.call', mock_call)
@@ -128,9 +125,10 @@ def test_tikzcd_basic_setup(monkeypatch, tmpdir, document_cd):
 
     doc = document_cd()
     tikz_tmpdir = doc.userdata['tikzcd']['tmp_dir']
-    os.chdir(str(tmpdir))
-    renderer = Renderer()
-    renderer.render(doc)
+
+    with tmpdir.as_cwd():
+        renderer = Renderer()
+        renderer.render(doc)
 
     pics = doc.getElementsByTagName('tikzcd')
     assert pics
@@ -144,22 +142,22 @@ def test_tikzcd_basic_setup(monkeypatch, tmpdir, document_cd):
     assert mock_move.called
 
     assert 'Commutative diagram' in tmpdir.join('index.html').read()
-    os.chdir(cur_dir)
 
 def test_tikzcd_config_options(monkeypatch, tmpdir, document_cd):
-    cur_dir = os.getcwd()
-    os.chdir(os.path.dirname(__file__))
     mock_call = Mock()
     mock_move = Mock()
     monkeypatch.setattr('subprocess.call', mock_call)
     monkeypatch.setattr('os.remove', Mock)
     monkeypatch.setattr('shutil.move', mock_move)
 
-    doc = document_cd(compiler='xelatex', converter='mockconv', template='tikzcdtemplate')
+    with py.path.local(os.path.dirname(__file__)).as_cwd():
+        doc = document_cd(compiler='xelatex', converter='mockconv', template='tikzcdtemplate')
     tikz_tmpdir = doc.userdata['tikzcd']['tmp_dir']
-    os.chdir(str(tmpdir))
-    renderer = Renderer()
-    renderer.render(doc)
+
+    with tmpdir.as_cwd():
+        renderer = Renderer()
+        renderer.render(doc)
+
     pics = doc.getElementsByTagName('tikzcd')
     assert pics
     tex_path = os.path.join(tikz_tmpdir, pics[0].id + '.tex')
@@ -169,10 +167,8 @@ def test_tikzcd_config_options(monkeypatch, tmpdir, document_cd):
 
     assert 'xelatex' in mock_call.call_args_list[0][0][0]
     assert 'mockconv' in mock_call.call_args_list[1][0][0]
-    os.chdir(cur_dir)
 
 def test_functional(tmpdir):
-    cur_dir = os.getcwd()
     tmpdir.join('test.tex').write(r"""
     \documentclass{article} 
     \usepackage{tikz, tikz-cd}
@@ -186,8 +182,8 @@ def test_functional(tmpdir):
             A \\rar & B
     \end{tikzcd}
     \end{document}""")
-    os.chdir(str(tmpdir))
-    subprocess.call(
+    with tmpdir.as_cwd():
+        subprocess.call(
             ['plastex', '--renderer', 'HTML5', 'test.tex'])
     assert os.path.isdir(str(tmpdir.join('test')))
     assert os.path.isfile(str(tmpdir.join('test', 'index.html')))
@@ -195,5 +191,3 @@ def test_functional(tmpdir):
     svgs = soup.findAll('object')
     for svg in svgs:
             assert os.path.isfile(str(tmpdir.join('test', svg.attrs['data'])))
-
-    os.chdir(cur_dir)