7
7
import os
8
8
import pathlib
9
9
import platform
10
- import random
11
10
import re
12
11
import shutil
13
12
import stat
14
- import string
15
13
import tempfile
16
14
from test import check_present , mark_windows_only , raises_nested
17
15
from unittest import mock
43
41
validate_dir ,
44
42
windows_short_path ,
45
43
)
44
+ from cmdstanpy .utils .cmdstan import stanc_path
46
45
from cmdstanpy .utils .filesystem import temp_inits , temp_single_json
47
46
48
47
HERE = os .path .dirname (os .path .abspath (__file__ ))
@@ -134,6 +133,16 @@ def test_set_path() -> None:
134
133
assert os .path .samefile (install_version , os .environ ['CMDSTAN' ])
135
134
136
135
136
+ @contextlib .contextmanager
137
+ def temporary_cmdstan_path (path : str ) -> None :
138
+ prev = cmdstan_path ()
139
+ try :
140
+ set_cmdstan_path (path )
141
+ yield
142
+ finally :
143
+ set_cmdstan_path (prev )
144
+
145
+
137
146
def test_validate_path () -> None :
138
147
if 'CMDSTAN' in os .environ :
139
148
install_version = os .environ .get ('CMDSTAN' )
@@ -150,20 +159,18 @@ def test_validate_path() -> None:
150
159
with pytest .raises (ValueError , match = 'No CmdStan directory' ):
151
160
validate_cmdstan_path (path_foo )
152
161
153
- folder_name = '' .join (
154
- random .choice (string .ascii_letters ) for _ in range (10 )
155
- )
156
- while os .path .exists (folder_name ):
157
- folder_name = '' .join (
158
- random .choice (string .ascii_letters ) for _ in range (10 )
159
- )
160
- folder = pathlib .Path (folder_name )
161
- folder .mkdir (parents = True )
162
- (folder / "makefile" ).touch ()
162
+ with tempfile .TemporaryDirectory () as tmpdir :
163
+ folder = pathlib .Path (tmpdir )
164
+ with pytest .raises (ValueError , match = 'missing makefile' ):
165
+ validate_cmdstan_path (str (folder .absolute ()))
163
166
164
- with pytest .raises (ValueError , match = 'missing binaries' ):
165
- validate_cmdstan_path (str (folder .absolute ()))
166
- shutil .rmtree (folder )
167
+ (folder / "makefile" ).touch ()
168
+ with temporary_cmdstan_path (str (folder .absolute ())):
169
+ with pytest .raises (
170
+ ValueError ,
171
+ match = 'stanc executable not found in CmdStan installation' ,
172
+ ):
173
+ stanc_path ()
167
174
168
175
169
176
def test_validate_dir () -> None :
@@ -216,7 +223,6 @@ def test_cmdstan_version(caplog: pytest.LogCaptureFixture) -> None:
216
223
fake_bin .mkdir (parents = True )
217
224
fake_makefile = fake_path / 'makefile'
218
225
fake_makefile .touch ()
219
- (fake_bin / f'stanc{ EXTENSION } ' ).touch ()
220
226
with mock .patch .dict ("os.environ" , CMDSTAN = str (fake_path )):
221
227
assert str (fake_path ) == cmdstan_path ()
222
228
with open (fake_makefile , 'w' ) as fd :
@@ -230,10 +236,7 @@ def test_cmdstan_version(caplog: pytest.LogCaptureFixture) -> None:
230
236
check_present (caplog , ('cmdstanpy' , 'INFO' , expect ))
231
237
232
238
fake_makefile .unlink ()
233
- expect = (
234
- 'CmdStan installation {} missing makefile, '
235
- 'cannot get version.' .format (fake_path )
236
- )
239
+ expect = 'No CmdStan installation found.'
237
240
with caplog .at_level (logging .INFO ):
238
241
cmdstan_version ()
239
242
check_present (caplog , ('cmdstanpy' , 'INFO' , expect ))
0 commit comments