Skip to content

Commit 29cdfee

Browse files
authored
Merge pull request #507 from stan-dev/do-cmd-restore-cwd
Restore cwd after do_command error WIP
2 parents 0cbf6a2 + d1beee5 commit 29cdfee

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

.github/workflows/main.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: CmdStanPy
2-
2+
33
on:
44
push:
55
branches:
@@ -14,7 +14,7 @@ on:
1414
description: 'Version to test'
1515
required: false
1616
default: 'latest'
17-
17+
1818
jobs:
1919
get-cmdstan-version:
2020
# get the latest cmdstan version to use as part of the cache key
@@ -81,11 +81,15 @@ jobs:
8181
- name: Show libraries
8282
run: python -m pip freeze
8383

84+
- name: Get system info
85+
uses: kenchan0130/actions-system-info@v1.0.0
86+
id: system-info
87+
8488
- name: CmdStan installation cacheing
8589
uses: actions/cache@v2
8690
with:
8791
path: ~/.cmdstan
88-
key: ${{ runner.os }}-cmdstan-${{ needs.get-cmdstan-version.outputs.version }}-${{ hashFiles('**/install_cmdstan.py') }}
92+
key: ${{ runner.os }}-${{ steps.system-info.outputs.release }}-cmdstan-${{ needs.get-cmdstan-version.outputs.version }}-${{ hashFiles('**/install_cmdstan.py') }}
8993

9094
- name: Install CmdStan (Linux, macOS)
9195
if: matrix.os != 'windows-latest'

cmdstanpy/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,8 +1319,10 @@ def pushd(new_dir: str) -> Iterator[None]:
13191319
"""Acts like pushd/popd."""
13201320
previous_dir = os.getcwd()
13211321
os.chdir(new_dir)
1322-
yield
1323-
os.chdir(previous_dir)
1322+
try:
1323+
yield
1324+
finally:
1325+
os.chdir(previous_dir)
13241326

13251327

13261328
def report_signal(sig: int) -> None:

test/test_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
validate_dir,
4646
windows_short_path,
4747
write_stan_json,
48+
pushd,
4849
)
4950

5051
HERE = os.path.dirname(os.path.abspath(__file__))
@@ -807,6 +808,17 @@ def test_exit(self):
807808
do_command(args, HERE)
808809

809810

811+
class PushdTest(unittest.TestCase):
812+
813+
def test_restore_cwd(self):
814+
"Ensure do_command in a different cwd restores cwd after error."
815+
cwd = os.getcwd()
816+
with self.assertRaises(RuntimeError):
817+
with pushd(os.path.dirname(cwd)):
818+
raise RuntimeError('error')
819+
self.assertEqual(cwd, os.getcwd())
820+
821+
810822
class FlattenTest(unittest.TestCase):
811823
def test_good(self):
812824
array_3d = np.empty((200, 4, 4))

0 commit comments

Comments
 (0)