Skip to content

Commit d589ad9

Browse files
author
Andrzej Fiedukowicz
committed
Use temp directory instead of cwd for test storage
In `test_36_unichars` we are writing local python file and them import it for testing purposes. So far this was done in a local directory (current working dir), but this is not ideal, as it might not be on a writable filesystem. For that reason it seems better to use a temporary directory, that is much more commonly provided in a writable form. This change makes that shift.
1 parent 0dccf32 commit d589ad9

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

test/grammar/parameter_test.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import contextlib
2+
import pathlib
3+
import sys
4+
import tempfile
25
import unittest
36

47
from tatsu.ngcodegen import codegen
@@ -137,15 +140,15 @@ def test_36_unichars(self):
137140
rule_all('ßÄÖÜäöü', k1="ßäöüÄÖÜ") = 'c' ;
138141
"""
139142

140-
def _trydelete(pymodule):
141-
import os
143+
def _trydelete(pypath, pymodule):
144+
module_with_path = pypath / pymodule
142145

143146
with contextlib.suppress(OSError):
144-
os.unlink(pymodule + '.py') # noqa:PTH108
147+
module_with_path.with_suffix('.py').unlink()
145148
with contextlib.suppress(OSError):
146-
os.unlink(pymodule + '.pyc') # noqa:PTH108
149+
module_with_path.with_suffix('.pyc').unlink()
147150
with contextlib.suppress(OSError):
148-
os.unlink(pymodule + '.pyo') # noqa:PTH108
151+
module_with_path.with_suffix('.pyo').unlink()
149152

150153
def assert_equal(target, value):
151154
self.assertEqual(target, value)
@@ -176,14 +179,19 @@ def rule_all(self, ast, p1, k1):
176179

177180
code = codegen(m)
178181
import codecs
182+
module_name = 'tc36unicharstest'
183+
temp_dir = pathlib.Path(tempfile.mkdtemp()) / module_name
184+
temp_dir.mkdir(exist_ok=True)
185+
py_file_path = temp_dir / f'{module_name}.py'
179186

180-
with codecs.open('tc36unicharstest.py', 'w', 'utf-8') as f:
187+
with codecs.open(py_file_path, 'w', 'utf-8') as f:
181188
f.write(code)
182189
try:
190+
sys.path.append(str(temp_dir))
183191
import tc36unicharstest # pylint: disable=E0401
184192

185193
assert tc36unicharstest
186-
_trydelete('tc36unicharstest')
194+
_trydelete(temp_dir, module_name)
187195
except Exception as e:
188196
self.fail(e)
189197

0 commit comments

Comments
 (0)