5454BERN_EXE = os .path .join (DATAFILES_PATH , 'bernoulli' + EXTENSION )
5555
5656
57- class CmdStanPathTest (unittest . TestCase ):
57+ class CmdStanPathTest (CustomTestCase ):
5858 def test_default_path (self ):
59- cur_value = None
6059 if 'CMDSTAN' in os .environ :
61- cur_value = os .environ ['CMDSTAN' ]
62- try :
63- if 'CMDSTAN' in os .environ :
64- self .assertEqual (cmdstan_path (), os .environ ['CMDSTAN' ])
65- path = os .environ ['CMDSTAN' ]
66- del os .environ ['CMDSTAN' ]
60+ self .assertPathsEqual (cmdstan_path (), os .environ ['CMDSTAN' ])
61+ path = os .environ ['CMDSTAN' ]
62+ with self .modified_environ ('CMDSTAN' ):
6763 self .assertFalse ('CMDSTAN' in os .environ )
6864 set_cmdstan_path (path )
69- self .assertEqual (cmdstan_path (), path )
70- self .assertTrue ('CMDSTAN' in os .environ )
71- else :
72- cmdstan_dir = os .path .expanduser (
73- os .path .join ('~' , _DOT_CMDSTAN )
74- )
75- install_version = os .path .join (
76- cmdstan_dir , get_latest_cmdstan (cmdstan_dir )
77- )
78- self .assertTrue (
79- os .path .samefile (cmdstan_path (), install_version )
80- )
65+ self .assertPathsEqual (cmdstan_path (), path )
8166 self .assertTrue ('CMDSTAN' in os .environ )
82- finally :
83- if cur_value is not None :
84- os .environ ['CMDSTAN' ] = cur_value
85- else :
86- if 'CMDSTAN' in os .environ :
87- del os .environ ['CMDSTAN' ]
67+ else :
68+ cmdstan_dir = os .path .expanduser (os .path .join ('~' , _DOT_CMDSTAN ))
69+ install_version = os .path .join (
70+ cmdstan_dir , get_latest_cmdstan (cmdstan_dir )
71+ )
72+ self .assertTrue (os .path .samefile (cmdstan_path (), install_version ))
73+ self .assertTrue ('CMDSTAN' in os .environ )
8874
8975 def test_non_spaces_location (self ):
9076 with tempfile .TemporaryDirectory (
9177 prefix = "cmdstan_tests" , dir = _TMPDIR
9278 ) as tmpdir :
9379 good_path = os .path .join (tmpdir , 'good_dir' )
80+ os .mkdir (good_path )
9481 with SanitizedOrTmpFilePath (good_path ) as (pth , is_changed ):
95- self .assertEqual (pth , good_path )
82+ self .assertPathsEqual (pth , good_path )
9683 self .assertFalse (is_changed )
9784
9885 # prepare files for test
@@ -117,19 +104,20 @@ def test_non_spaces_location(self):
117104 self .assertFalse (os .path .exists (stan_copied ))
118105
119106 # cleanup after test
107+ shutil .rmtree (good_path , ignore_errors = True )
120108 shutil .rmtree (bad_path , ignore_errors = True )
121109
122110 def test_set_path (self ):
123111 if 'CMDSTAN' in os .environ :
124- self .assertEqual (cmdstan_path (), os .environ ['CMDSTAN' ])
112+ self .assertPathsEqual (cmdstan_path (), os .environ ['CMDSTAN' ])
125113 else :
126114 cmdstan_dir = os .path .expanduser (os .path .join ('~' , _DOT_CMDSTAN ))
127115 install_version = os .path .join (
128116 cmdstan_dir , get_latest_cmdstan (cmdstan_dir )
129117 )
130118 set_cmdstan_path (install_version )
131- self .assertEqual (install_version , cmdstan_path ())
132- self .assertEqual (install_version , os .environ ['CMDSTAN' ])
119+ self .assertPathsEqual (install_version , cmdstan_path ())
120+ self .assertPathsEqual (install_version , os .environ ['CMDSTAN' ])
133121
134122 def test_validate_path (self ):
135123 if 'CMDSTAN' in os .environ :
@@ -209,29 +197,27 @@ def test_cmdstan_version(self):
209197 fake_bin = os .path .join (fake_path , 'bin' )
210198 os .makedirs (fake_bin )
211199 Path (os .path .join (fake_bin , 'stanc' + EXTENSION )).touch ()
212- os .environ ['CMDSTAN' ] = fake_path
213- self .assertTrue (fake_path == cmdstan_path ())
214- expect = (
215- 'CmdStan installation {} missing makefile, '
216- 'cannot get version.' .format (fake_path )
217- )
218- with LogCapture () as log :
219- logging .getLogger ()
220- cmdstan_version ()
221- log .check_present (('cmdstanpy' , 'INFO' , expect ))
222- fake_makefile = os .path .join (fake_path , 'makefile' )
223- with open (fake_makefile , 'w' ) as fd :
224- fd .write ('... CMDSTAN_VERSION := dont_need_no_mmp\n \n ' )
225- expect = (
226- 'Cannot parse version, expected "<major>.<minor>.<patch>", '
227- 'found: "dont_need_no_mmp".'
228- )
229- with LogCapture () as log :
230- logging .getLogger ()
231- cmdstan_version ()
232- log .check_present (('cmdstanpy' , 'INFO' , expect ))
233- # cleanup
234- del os .environ ['CMDSTAN' ]
200+ with self .modified_environ (CMDSTAN = fake_path ):
201+ self .assertTrue (fake_path == cmdstan_path ())
202+ expect = (
203+ 'CmdStan installation {} missing makefile, '
204+ 'cannot get version.' .format (fake_path )
205+ )
206+ with LogCapture () as log :
207+ logging .getLogger ()
208+ cmdstan_version ()
209+ log .check_present (('cmdstanpy' , 'INFO' , expect ))
210+ fake_makefile = os .path .join (fake_path , 'makefile' )
211+ with open (fake_makefile , 'w' ) as fd :
212+ fd .write ('... CMDSTAN_VERSION := dont_need_no_mmp\n \n ' )
213+ expect = (
214+ 'Cannot parse version, expected "<major>.<minor>.<patch>", '
215+ 'found: "dont_need_no_mmp".'
216+ )
217+ with LogCapture () as log :
218+ logging .getLogger ()
219+ cmdstan_version ()
220+ log .check_present (('cmdstanpy' , 'INFO' , expect ))
235221 cmdstan_path ()
236222
237223
0 commit comments