|
6 | 6 | import json |
7 | 7 | import logging |
8 | 8 | import os |
| 9 | +import pathlib |
9 | 10 | import platform |
10 | 11 | import random |
11 | 12 | import shutil |
12 | 13 | import stat |
13 | 14 | import string |
14 | 15 | import tempfile |
15 | 16 | import unittest |
16 | | -from pathlib import Path |
17 | 17 | from test import CustomTestCase |
18 | 18 |
|
19 | 19 | import numpy as np |
@@ -137,18 +137,21 @@ def test_validate_path(self): |
137 | 137 | path_foo = os.path.abspath(os.path.join('releases', 'foo')) |
138 | 138 | with self.assertRaisesRegex(ValueError, 'No CmdStan directory'): |
139 | 139 | validate_cmdstan_path(path_foo) |
| 140 | + |
140 | 141 | folder_name = ''.join( |
141 | 142 | random.choice(string.ascii_letters) for _ in range(10) |
142 | 143 | ) |
143 | 144 | while os.path.exists(folder_name): |
144 | 145 | folder_name = ''.join( |
145 | 146 | random.choice(string.ascii_letters) for _ in range(10) |
146 | 147 | ) |
147 | | - os.makedirs(folder_name) |
148 | | - path_test = os.path.abspath(folder_name) |
| 148 | + folder = pathlib.Path(folder_name) |
| 149 | + folder.mkdir(parents=True) |
| 150 | + (folder / "makefile").touch() |
| 151 | + |
149 | 152 | with self.assertRaisesRegex(ValueError, 'missing binaries'): |
150 | | - validate_cmdstan_path(path_test) |
151 | | - shutil.rmtree(folder_name) |
| 153 | + validate_cmdstan_path(str(folder.absolute())) |
| 154 | + shutil.rmtree(folder) |
152 | 155 |
|
153 | 156 | def test_validate_dir(self): |
154 | 157 | with tempfile.TemporaryDirectory( |
@@ -193,32 +196,31 @@ def test_cmdstan_version(self): |
193 | 196 | with tempfile.TemporaryDirectory( |
194 | 197 | prefix="cmdstan_tests", dir=_TMPDIR |
195 | 198 | ) as tmpdir: |
196 | | - tdir = os.path.join(tmpdir, 'tmpdir_xxx') |
197 | | - os.makedirs(tdir) |
198 | | - fake_path = os.path.join(tdir, 'cmdstan-2.22.0') |
199 | | - os.makedirs(os.path.join(fake_path)) |
200 | | - fake_bin = os.path.join(fake_path, 'bin') |
201 | | - os.makedirs(fake_bin) |
202 | | - Path(os.path.join(fake_bin, 'stanc' + EXTENSION)).touch() |
203 | | - with self.modified_environ(CMDSTAN=fake_path): |
204 | | - self.assertTrue(fake_path == cmdstan_path()) |
| 199 | + tdir = pathlib.Path(tmpdir) / 'tmpdir_xxx' |
| 200 | + fake_path = tdir / 'cmdstan-2.22.0' |
| 201 | + fake_bin = fake_path / 'bin' |
| 202 | + fake_bin.mkdir(parents=True) |
| 203 | + fake_makefile = fake_path / 'makefile' |
| 204 | + fake_makefile.touch() |
| 205 | + (fake_bin / f'stanc{EXTENSION}').touch() |
| 206 | + with self.modified_environ(CMDSTAN=str(fake_path)): |
| 207 | + self.assertTrue(str(fake_path) == cmdstan_path()) |
| 208 | + with open(fake_makefile, 'w') as fd: |
| 209 | + fd.write('... CMDSTAN_VERSION := dont_need_no_mmp\n\n') |
205 | 210 | expect = ( |
206 | | - 'CmdStan installation {} missing makefile, ' |
207 | | - 'cannot get version.'.format(fake_path) |
| 211 | + 'Cannot parse version, expected "<major>.<minor>.<patch>", ' |
| 212 | + 'found: "dont_need_no_mmp".' |
208 | 213 | ) |
209 | 214 | with LogCapture() as log: |
210 | | - logging.getLogger() |
211 | 215 | cmdstan_version() |
212 | 216 | log.check_present(('cmdstanpy', 'INFO', expect)) |
213 | | - fake_makefile = os.path.join(fake_path, 'makefile') |
214 | | - with open(fake_makefile, 'w') as fd: |
215 | | - fd.write('... CMDSTAN_VERSION := dont_need_no_mmp\n\n') |
| 217 | + |
| 218 | + fake_makefile.unlink() |
216 | 219 | expect = ( |
217 | | - 'Cannot parse version, expected "<major>.<minor>.<patch>", ' |
218 | | - 'found: "dont_need_no_mmp".' |
| 220 | + 'CmdStan installation {} missing makefile, ' |
| 221 | + 'cannot get version.'.format(fake_path) |
219 | 222 | ) |
220 | 223 | with LogCapture() as log: |
221 | | - logging.getLogger() |
222 | 224 | cmdstan_version() |
223 | 225 | log.check_present(('cmdstanpy', 'INFO', expect)) |
224 | 226 | cmdstan_path() |
|
0 commit comments