2424
2525import n3fit
2626from n3fit .io .writer import SuperEncoder
27+ from n3fit .tests .helpers import run_n3fit , run_setupfit
2728from validphys .n3fit_data import replica_mcseed , replica_nnseed , replica_trvlseed
2829from validphys .utils import yaml_safe
2930
@@ -160,7 +161,7 @@ def _auxiliary_performfit(tmp_path, runcard=QUICKNAME, replica=1, timing=True, r
160161 shutil .copy (quickpath , tmp_path )
161162 shutil .copy (weightpath , tmp_path / f"{ weight_name } .weights.h5" )
162163 # run the fit
163- sp . run ( f" { EXE } { quickcard } { replica } " . split ( ), cwd = tmp_path , check = True )
164+ run_n3fit ( quickcard , str ( replica ), cwd = tmp_path , check = True )
164165
165166 # And compare
166167 check_fit_results (tmp_path , runcard , replica , old_json_file , timing = timing , rel_error = rel_error )
@@ -185,7 +186,7 @@ def test_performfit_with_old_theory(tmp_path):
185186 quickcard = "quickcard_old.yml"
186187 quickpath = REGRESSION_FOLDER / quickcard
187188 shutil .copy (quickpath , tmp_path )
188- sp . run ( f" { EXE } { quickcard } 5" . split () , cwd = tmp_path , check = True )
189+ run_n3fit ( quickcard , "5" , cwd = tmp_path , check = True )
189190
190191
191192@pytest .mark .skip (reason = "Still not implemented in parallel mode" )
@@ -198,7 +199,7 @@ def test_hyperopt(tmp_path):
198199 # We just want to ensure that the hyperopt can run, but we need to kill it ourselves
199200 # 60 seconds should be enough
200201 with pytest .raises (sp .TimeoutExpired ):
201- sp . run ( f"{ EXE } { quickcard } { REPLICA } --hyperopt 1000" . split () , cwd = tmp_path , timeout = 60 )
202+ run_n3fit ( quickcard , f"{ REPLICA } --hyperopt 1000" , cwd = tmp_path , timeout = 60 )
202203
203204
204205def test_novalidation (tmp_path , timing = 30 ):
@@ -209,7 +210,7 @@ def test_novalidation(tmp_path, timing=30):
209210 quickpath = REGRESSION_FOLDER / quickcard
210211 shutil .copy (quickpath , tmp_path )
211212 with pytest .raises (sp .TimeoutExpired ):
212- sp . run ( f" { EXE } { quickcard } { REPLICA } " . split () , cwd = tmp_path , timeout = timing )
213+ run_n3fit ( quickcard , REPLICA , cwd = tmp_path , timeout = timing )
213214
214215
215216def test_weirdbasis (tmp_path , timing = 30 ):
@@ -225,7 +226,7 @@ def test_weirdbasis(tmp_path, timing=30):
225226 shutil .copy (quickpath , tmp_path )
226227 # with pytest.raises(sp.TimeoutExpired):
227228 with pytest .raises (sp .CalledProcessError ):
228- sp . run ( f" { EXE } { quickcard } { REPLICA } " . split () , cwd = tmp_path , timeout = timing , check = True )
229+ run_n3fit ( quickcard , REPLICA , cwd = tmp_path , timeout = timing , check = True )
229230
230231
231232@pytest .mark .linux
@@ -239,7 +240,7 @@ def test_multireplica_runs(tmp_path, runcard):
239240 path = tmp_path / name
240241 path .mkdir ()
241242 shutil .copy (quickpath , path )
242- sp . run ( f" { EXE } { quickcard } { replicas } " . split () , cwd = path , check = True )
243+ run_n3fit ( quickcard , replicas , cwd = path , check = True )
243244
244245 for name_1 , option_1 in options .items ():
245246 for name_2 , option_2 in options .items ():
@@ -299,8 +300,8 @@ def test_parallel_against_sequential(tmp_path, rep_from=6, rep_to=8):
299300
300301 # Now run both
301302 for r in range (rep_from , rep_to + 1 ):
302- sp . run ( f" { EXE } { card_sequenti } { r } " . split ( ), cwd = tmp_path , check = True )
303- sp . run ( f"{ EXE } { card_parallel } { rep_from } -r { rep_to } " . split () , cwd = tmp_path , check = True )
303+ run_n3fit ( card_sequenti , str ( r ), cwd = tmp_path , setupfit = ( r == rep_from ) , check = True )
304+ run_n3fit ( card_parallel , f"{ rep_from } -r { rep_to } " , cwd = tmp_path , check = True )
304305
305306 # Loop over all pseudodata files for both fits and load them up
306307 folder_seq = card_sequenti .with_suffix ("" ) / "nnfit"
@@ -335,3 +336,19 @@ def compare_weights(option_1, option_2, file_1, file_2):
335336 weight_name = file_1 [key ].name
336337 err_msg = f"Difference between runs `n3fit { option_1 } ` and `n3fit { option_2 } ` in weights { weight_name } "
337338 assert_allclose (file_1 [key ][:], file_2 [key ][:], rtol = 1e-5 , atol = 1e-5 , err_msg = err_msg )
339+
340+
341+ def test_md5_mismatch_is_detected (tmp_path ):
342+ """vp-setupfit, then tamper with the runcard -> n3fit must refuse to start."""
343+ quickcard = f"{ QUICKNAME } .yml"
344+ weight_name = "weights_pol" if "_pol" in quickcard else "weights"
345+ weightpath = REGRESSION_FOLDER / f"{ weight_name } _1.weights.h5"
346+ shutil .copy (REGRESSION_FOLDER / quickcard , tmp_path )
347+ shutil .copy (weightpath , tmp_path / f"{ weight_name } .weights.h5" )
348+ run_setupfit (quickcard , cwd = tmp_path )
349+
350+ runcard_path = tmp_path / quickcard
351+ runcard_path .write_text (runcard_path .read_text () + "\n # tampered\n " )
352+
353+ with pytest .raises (sp .CalledProcessError ):
354+ run_n3fit (quickcard , REPLICA , cwd = tmp_path , setupfit = False , check = True )
0 commit comments