Skip to content

Commit

Permalink
Added nonyaml file testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lamsoa729 committed Jan 23, 2024
1 parent 76edc1b commit 7c5ad60
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
11 changes: 11 additions & 0 deletions chi_pet/chi_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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.")
Expand Down
39 changes: 36 additions & 3 deletions tests/test_chi_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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']
3 changes: 2 additions & 1 deletion tests/test_chi_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 7c5ad60

Please sign in to comment.