Skip to content

Commit f277c4c

Browse files
committed
Add unit tests on setup_reproduce and setup_manifests_unchanged.
1 parent fc3af4b commit f277c4c

2 files changed

Lines changed: 73 additions & 5 deletions

File tree

src/model_config_tests/exp_test_helper.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ def setup_manifests_unchanged(self):
128128
f"{'='*10}STDOUT{'='*10}\n {setup_result.stdout}\n"
129129
f"{'='*10}STDERR{'='*10}\n {setup_result.stderr}\n"
130130
)
131-
elif "error" in setup_result.stderr.lower():
132-
warnings.warn(
133-
f"Payu setup existed safely with errors in stderr:\n{setup_result.stderr}. Proceeding to modification check."
134-
)
131+
135132
result = sp.run(
136133
["git", "diff", "--name-only", "manifests/"],
137134
capture_output=True,

tests/test_exp_test_helper.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import shutil
22
import subprocess
33
from pathlib import Path
4-
from unittest.mock import Mock, patch
4+
from unittest.mock import MagicMock, Mock, patch
55

66
import pytest
77
import yaml
@@ -556,3 +556,74 @@ def test_experiments_check_experiment_error(tmp_path):
556556
)
557557
with pytest.raises(RuntimeError, match=error_msg):
558558
exps.check_experiment("error_exp")
559+
560+
561+
@patch("subprocess.run")
562+
def test_setup_reproduce_error(mock_run, exp):
563+
"""Test that payu setup --repro fails raises an error and return to original work directory"""
564+
# Mock the payu setup --repro to fail
565+
mock_result = MagicMock()
566+
mock_result.returncode = 1
567+
mock_result.stderr = "MD5 mismatch"
568+
mock_result.stdout = "Check manifest"
569+
mock_run.return_value = mock_result
570+
571+
with pytest.raises(RuntimeError) as excinfo:
572+
exp.setup_reproduce()
573+
574+
assert (
575+
f"Failed to run payu setup with --reproduce. Error: {mock_result.stderr}"
576+
in str(excinfo.value)
577+
)
578+
assert f"Full output: {mock_result.stdout}" in str(excinfo.value)
579+
580+
# assert returning to the original work directory
581+
assert Path.cwd() == exp.control_path
582+
583+
584+
@patch("subprocess.run")
585+
def test_setup_manifests_unchanged_fail_setup(mock_run, exp):
586+
"""Test that an error is raised when payu setup fails in setup_manifests_unchanged()"""
587+
# Mock the payu setup --repro to fail with unchanged manifests
588+
mock_result = MagicMock()
589+
mock_result.returncode = 1
590+
mock_result.stderr = "Setup failed"
591+
mock_result.stdout = "Payu setup output"
592+
mock_run.return_value = mock_result
593+
594+
with pytest.raises(RuntimeError) as excinfo:
595+
exp.setup_manifests_unchanged()
596+
597+
assert "Error during payu setup." in str(excinfo.value)
598+
assert f"{'='*10}STDOUT{'='*10}\n {mock_result.stdout}\n" in str(excinfo.value)
599+
600+
# assert returning to the original work directory
601+
assert Path.cwd() == exp.control_path
602+
603+
604+
@patch("subprocess.run")
605+
def test_setup_manifests_unchanged_show_changes(mock_run, exp):
606+
"""Test that when manifests are changed, the `git diff` results are printed to stdout"""
607+
# Mock the `payu setup` succeed first
608+
setup_success = MagicMock(returncode=0, stdout="Payu setup succeeded")
609+
610+
# Then mock the `git diff --name-only` to show which files are changed
611+
git_diff_name_only = MagicMock(returncode=1, stdout="manifests/input.yaml")
612+
613+
# Mock the `git diff` to show the detailed changes in the file
614+
git_diff_output = (
615+
"--- a/manifests/input.yaml\n+++ b/manifests/input.yaml\n+new line\n-old line"
616+
)
617+
git_diff_run = MagicMock(returncode=0, stdout=git_diff_output)
618+
619+
# Run these mocks in sequence
620+
mock_run.side_effect = [setup_success, git_diff_name_only, git_diff_run]
621+
622+
with pytest.raises(RuntimeError) as excinfo:
623+
exp.setup_manifests_unchanged()
624+
625+
assert "Modifications are detected in file:\n" in str(excinfo.value)
626+
assert git_diff_output in str(excinfo.value)
627+
628+
# assert returning to the original work directory
629+
assert Path.cwd() == exp.control_path

0 commit comments

Comments
 (0)