From 7c5ad60289dac53db72546adff70260b3a6b8b4e Mon Sep 17 00:00:00 2001 From: Adam Lamson Date: Tue, 23 Jan 2024 09:49:38 -0500 Subject: [PATCH] Added nonyaml file testing --- chi_pet/chi_parse.py | 11 +++++++++++ tests/test_chi_parse.py | 39 ++++++++++++++++++++++++++++++++++++--- tests/test_chi_run.py | 3 ++- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/chi_pet/chi_parse.py b/chi_pet/chi_parse.py index b6fac3c..31b7745 100644 --- a/chi_pet/chi_parse.py +++ b/chi_pet/chi_parse.py @@ -76,6 +76,7 @@ def parse_chi_options(): opts = parser.parse_args() + # General checking and options if not opts.workdir: opts.workdir = Path.cwd() elif not opts.workdir.exists(): @@ -87,11 +88,21 @@ def parse_chi_options(): with opts.args_file.open('r') as f: opts.args_dict = clib.load_yaml_in_order(f) + else: + opts.args_dict = {} + # Create parsing if opts.command == 'create': if not opts.states: opts.states = list(opts.args_dict.keys()) + if opts.non_yaml: + for ny in opts.non_yaml: + if not ny.exists(): + raise FileNotFoundError( + f"Non-yaml file {ny} does not exist.") + + # Run parsing elif opts.command == 'run': if opts.args_file is None: parser.error("'run' requires the '--args_file' option.") diff --git a/tests/test_chi_parse.py b/tests/test_chi_parse.py index ec1debd..05a2325 100644 --- a/tests/test_chi_parse.py +++ b/tests/test_chi_parse.py @@ -18,10 +18,9 @@ def test_chi_create_argument_parsing(mock_root_dir): root_path = mock_root_dir - mock_args_path = mock_args_file(root_path) # Setup sys.argv = ['chi', 'create', 'param1.yaml', - 'param2.yaml', '-a', f'{mock_args_path}'] + 'param2.yaml'] # Test opts = parse_chi_options() # Assert @@ -30,6 +29,29 @@ def test_chi_create_argument_parsing(mock_root_dir): assert Path(pf) in opts.param_files +def test_chi_create_non_yaml_parsing(mock_root_dir): + root_path = mock_root_dir + ny_path_1 = (root_path / 'mock_non_yaml1.txt') + ny_path_2 = (root_path / 'mock_non_yaml2.txt') + ny_path_1.touch() + ny_path_2.touch() + + # Setup + sys.argv = ['chi', 'create', 'param1.yaml', '-ny', + f'{ny_path_1}', f'{ny_path_2}'] + opts = parse_chi_options() + + assert opts.non_yaml == [ny_path_1, ny_path_2] + # Test that error is thrown + ny_path_2.unlink() + + # Setup + sys.argv = ['chi', 'create', 'param1.yaml', '-ny', + f'{ny_path_1}', f'{ny_path_2}'] + with pytest.raises(FileNotFoundError): + opts = parse_chi_options() + + def test_chi_run_argument_parsing(mock_root_dir): root_path = mock_root_dir mock_args_path = mock_args_file(root_path) @@ -43,6 +65,17 @@ def test_chi_run_argument_parsing(mock_root_dir): with pytest.raises(SystemExit): opts = parse_chi_options() -# TODO Add test for non-yaml files # TODO Add test for sim states + + +# def test_chi_run_state(mock_root_dir): +# root_path = mock_root_dir +# mock_args_path = mock_args_file(root_path) + +# (root_path / 'sim.touch1').touch() + +# # Setup +# sys.argv = ['chi', 'run', '-a', f'{mock_args_path}', '--use-sim-states'] +# opts = parse_chi_options() +# assert opts.states == ['touch1'] diff --git a/tests/test_chi_run.py b/tests/test_chi_run.py index cdd4b4d..26fd70d 100644 --- a/tests/test_chi_run.py +++ b/tests/test_chi_run.py @@ -33,7 +33,7 @@ def test_chi_run_touch(mock_leaf_dir, mock_run_opts): assert (mock_leaf_path / 'mock_output2.txt').exists() -def test_chi_run_only_one_touch(mock_leaf_dir, mock_run_opts): +def test_chi_run_only_one_touch_with_opts(mock_leaf_dir, mock_run_opts): """Test chi_run.""" mock_leaf_path = mock_leaf_dir # Create and modify run optoins @@ -50,4 +50,5 @@ def test_chi_run_only_one_touch(mock_leaf_dir, mock_run_opts): assert (mock_leaf_path / 'mock_output1.txt').exists() assert not (mock_leaf_path / 'mock_output2.txt').exists() + # TODO NEXT TEST test run args to make sure it always returns you to the right directory