Skip to content

Commit 2b14038

Browse files
authored
Merge pull request #530 from stan-dev/test-issues-29
Fix tests and src_info for 2.29
2 parents db939f4 + 0a2539f commit 2b14038

3 files changed

Lines changed: 38 additions & 20 deletions

File tree

cmdstanpy/model.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ def __init__(
161161
self._compiler_options.add_include_path(path)
162162

163163
# try to detect models w/out parameters, needed for sampler
164-
if not cmdstan_version_before(2, 27) and cmdstan_version_before(
165-
2, 29
166-
):
164+
if not cmdstan_version_before(
165+
2, 27
166+
): # unknown end of version range
167167
model_info = self.src_info()
168168
if 'parameters' in model_info:
169-
self._fixed_param = len(model_info['parameters']) == 0
169+
self._fixed_param |= len(model_info['parameters']) == 0
170170

171171
if exe_file is not None:
172172
self._exe_file = os.path.realpath(os.path.expanduser(exe_file))
@@ -275,19 +275,25 @@ def src_info(self) -> Dict[str, Any]:
275275
return result
276276
try:
277277
cmd = (
278-
[os.path.join('.', 'bin', 'stanc' + EXTENSION)]
278+
[os.path.join(cmdstan_path(), 'bin', 'stanc' + EXTENSION)]
279279
# handle include-paths, allow-undefined etc
280280
+ self._compiler_options.compose_stanc()
281281
+ [
282282
'--info',
283283
self.stan_file,
284284
]
285285
)
286-
sout = io.StringIO()
287-
do_command(cmd=cmd, cwd=cmdstan_path(), fd_out=sout)
288-
result = json.loads(sout.getvalue())
286+
proc = subprocess.run(
287+
cmd, capture_output=True, text=True, check=True
288+
)
289+
result = json.loads(proc.stdout)
289290
return result
290-
except (ValueError, RuntimeError) as e:
291+
except (
292+
ValueError,
293+
RuntimeError,
294+
OSError,
295+
subprocess.CalledProcessError,
296+
) as e:
291297
get_logger().debug(e)
292298
return result
293299

test/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""The global configuration for the test suite"""
2+
import atexit
23
import os
4+
import shutil
35
import subprocess
46

57
import pytest
@@ -13,7 +15,16 @@
1315

1416
@pytest.fixture(scope='session', autouse=True)
1517
def cleanup_test_files():
18+
19+
import cmdstanpy
20+
21+
# see https://github.com/pytest-dev/pytest/issues/5502
22+
atexit.unregister(cmdstanpy._cleanup_tmpdir)
23+
1624
yield
25+
26+
shutil.rmtree(cmdstanpy._TMPDIR, ignore_errors=True)
27+
1728
subprocess.Popen(
1829
['git', 'clean', '-fX', DATAFILES_PATH],
1930
stdout=subprocess.DEVNULL,

test/test_sample.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,17 +1157,18 @@ def test_diagnose_divergences(self):
11571157
]
11581158
fit = CmdStanMCMC(runset)
11591159
# TODO - use cmdstan test files instead
1160-
expected = '\n'.join(
1161-
[
1162-
'Checking sampler transitions treedepth.',
1163-
'424 of 1000 (42%) transitions hit the maximum '
1164-
'treedepth limit of 8, or 2^8 leapfrog steps.',
1165-
'Trajectories that are prematurely terminated '
1166-
'due to this limit will result in slow exploration.',
1167-
'For optimal performance, increase this limit.',
1168-
]
1169-
)
1170-
self.assertIn(expected, fit.diagnose().replace('\r\n', '\n'))
1160+
expected = [
1161+
'Checking sampler transitions treedepth.',
1162+
'424 of 1000',
1163+
'treedepth limit of 8, or 2^8 leapfrog steps.',
1164+
'Trajectories that are prematurely terminated '
1165+
'due to this limit will result in slow exploration.',
1166+
'For optimal performance, increase this limit.',
1167+
]
1168+
1169+
diagnose = fit.diagnose()
1170+
for e in expected:
1171+
self.assertIn(e, diagnose)
11711172

11721173
def test_validate_bad_run(self):
11731174
exe = os.path.join(DATAFILES_PATH, 'bernoulli' + EXTENSION)

0 commit comments

Comments
 (0)