diff --git a/cdisc_rules_engine/utilities/utils.py b/cdisc_rules_engine/utilities/utils.py index 4704c6005..eb6b47952 100644 --- a/cdisc_rules_engine/utilities/utils.py +++ b/cdisc_rules_engine/utilities/utils.py @@ -442,3 +442,12 @@ def replace_nan_values_in_df(df, columns): if mask.any(): df.loc[mask, col] = None return df + + +def validate_dataset_files_exist(dataset_path: tuple[str], logger, ctx): + non_existing_files: list[str] = [ + path for path in dataset_path if not os.path.exists(path) + ] + if non_existing_files: + logger.error(f'Files {", ".join(non_existing_files)} are not found') + ctx.exit(2) diff --git a/core.py b/core.py index 5a2242de9..52c44fc44 100644 --- a/core.py +++ b/core.py @@ -26,6 +26,7 @@ from cdisc_rules_engine.utilities.utils import ( generate_report_filename, get_rules_cache_key, + validate_dataset_files_exist, ) from cdisc_rules_engine.enums.dataformat_types import DataFormatTypes from scripts.list_dataset_metadata_handler import list_dataset_metadata_handler @@ -393,6 +394,8 @@ def validate( logger = logging.getLogger("validator") load_dotenv() + validate_dataset_files_exist(dataset_path, logger, ctx) + if raw_report is True: if not (len(output_format) == 1 and output_format[0] == ReportTypes.JSON.value): logger.error( diff --git a/tests/QARegressionTests/test_Issues/test_CoreIssue1204.py b/tests/QARegressionTests/test_Issues/test_CoreIssue1204.py new file mode 100644 index 000000000..24ec2349c --- /dev/null +++ b/tests/QARegressionTests/test_Issues/test_CoreIssue1204.py @@ -0,0 +1,40 @@ +import subprocess +import pytest +from conftest import get_python_executable + + +@pytest.mark.regression +def test_non_existing_dataset_shows_helpful_error(): + """Test that the engine displays a helpful error message if dataset files are not found""" + command = [ + get_python_executable(), + "-m", + "core", + "validate", + "-s", + "sdtmig", + "-v", + "3.4", + "-dp", + "ds.json", + "-dp", + "ds2.json", + ] + + process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + + stderr_text = stderr.decode() + stdout_text = stdout.decode() + + assert process.returncode == 2, "Expected non-zero exit code" + assert ( + "FileNotFoundError" not in stderr_text + ), "Should not show FileNotFoundError crash" + assert ( + "Failed to execute script" not in stderr_text + ), "Should not show script execution failure" + assert "Files ds.json, ds2.json are not found" in stderr_text, ( + f"Expected helpful error message about non-existing dataset files. " + f"stderr: {stderr_text}, stdout: {stdout_text}" + )