|
1 | 1 | import contextlib
|
| 2 | +import pathlib |
| 3 | +import sys |
| 4 | +import tempfile |
2 | 5 | import unittest
|
3 | 6 |
|
4 | 7 | from tatsu.ngcodegen import codegen
|
@@ -137,15 +140,15 @@ def test_36_unichars(self):
|
137 | 140 | rule_all('ßÄÖÜäöü', k1="ßäöüÄÖÜ") = 'c' ;
|
138 | 141 | """
|
139 | 142 |
|
140 |
| - def _trydelete(pymodule): |
141 |
| - import os |
| 143 | + def _trydelete(pypath, pymodule): |
| 144 | + module_with_path = pypath / pymodule |
142 | 145 |
|
143 | 146 | with contextlib.suppress(OSError):
|
144 |
| - os.unlink(pymodule + '.py') # noqa:PTH108 |
| 147 | + module_with_path.with_suffix('.py').unlink() |
145 | 148 | with contextlib.suppress(OSError):
|
146 |
| - os.unlink(pymodule + '.pyc') # noqa:PTH108 |
| 149 | + module_with_path.with_suffix('.pyc').unlink() |
147 | 150 | with contextlib.suppress(OSError):
|
148 |
| - os.unlink(pymodule + '.pyo') # noqa:PTH108 |
| 151 | + module_with_path.with_suffix('.pyo').unlink() |
149 | 152 |
|
150 | 153 | def assert_equal(target, value):
|
151 | 154 | self.assertEqual(target, value)
|
@@ -176,14 +179,19 @@ def rule_all(self, ast, p1, k1):
|
176 | 179 |
|
177 | 180 | code = codegen(m)
|
178 | 181 | 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' |
179 | 186 |
|
180 |
| - with codecs.open('tc36unicharstest.py', 'w', 'utf-8') as f: |
| 187 | + with codecs.open(py_file_path, 'w', 'utf-8') as f: |
181 | 188 | f.write(code)
|
182 | 189 | try:
|
| 190 | + sys.path.append(str(temp_dir)) |
183 | 191 | import tc36unicharstest # pylint: disable=E0401
|
184 | 192 |
|
185 | 193 | assert tc36unicharstest
|
186 |
| - _trydelete('tc36unicharstest') |
| 194 | + _trydelete(temp_dir, module_name) |
187 | 195 | except Exception as e:
|
188 | 196 | self.fail(e)
|
189 | 197 |
|
|
0 commit comments