Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cdisc_rules_engine/utilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 3 additions & 0 deletions core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
40 changes: 40 additions & 0 deletions tests/QARegressionTests/test_Issues/test_CoreIssue1204.py
Original file line number Diff line number Diff line change
@@ -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}"
)
Loading