77import os
88import pathlib
99import platform
10- import random
1110import re
1211import shutil
1312import stat
14- import string
1513import tempfile
1614from test import check_present , mark_windows_only , raises_nested
1715from unittest import mock
4341 validate_dir ,
4442 windows_short_path ,
4543)
44+ from cmdstanpy .utils .cmdstan import stanc_path
4645from cmdstanpy .utils .filesystem import temp_inits , temp_single_json
4746
4847HERE = os .path .dirname (os .path .abspath (__file__ ))
@@ -134,6 +133,16 @@ def test_set_path() -> None:
134133 assert os .path .samefile (install_version , os .environ ['CMDSTAN' ])
135134
136135
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+
137146def test_validate_path () -> None :
138147 if 'CMDSTAN' in os .environ :
139148 install_version = os .environ .get ('CMDSTAN' )
@@ -150,20 +159,18 @@ def test_validate_path() -> None:
150159 with pytest .raises (ValueError , match = 'No CmdStan directory' ):
151160 validate_cmdstan_path (path_foo )
152161
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 ()))
163166
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 ()
167174
168175
169176def test_validate_dir () -> None :
@@ -216,7 +223,6 @@ def test_cmdstan_version(caplog: pytest.LogCaptureFixture) -> None:
216223 fake_bin .mkdir (parents = True )
217224 fake_makefile = fake_path / 'makefile'
218225 fake_makefile .touch ()
219- (fake_bin / f'stanc{ EXTENSION } ' ).touch ()
220226 with mock .patch .dict ("os.environ" , CMDSTAN = str (fake_path )):
221227 assert str (fake_path ) == cmdstan_path ()
222228 with open (fake_makefile , 'w' ) as fd :
@@ -230,10 +236,7 @@ def test_cmdstan_version(caplog: pytest.LogCaptureFixture) -> None:
230236 check_present (caplog , ('cmdstanpy' , 'INFO' , expect ))
231237
232238 fake_makefile .unlink ()
233- expect = (
234- 'CmdStan installation {} missing makefile, '
235- 'cannot get version.' .format (fake_path )
236- )
239+ expect = 'No CmdStan installation found.'
237240 with caplog .at_level (logging .INFO ):
238241 cmdstan_version ()
239242 check_present (caplog , ('cmdstanpy' , 'INFO' , expect ))
0 commit comments