Skip to content

Commit 709b48c

Browse files
committed
tests: change file-not-found to raise FileNotFound error.
1 parent 6124e29 commit 709b48c

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/diffpy/srxplanar/loadimage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def loadImage(self, filename):
8383
filenamefull = found_files[0] if found_files else None
8484

8585
if filenamefull is None or not filenamefull.exists():
86-
print(
87-
f"Warning: file not found: {filename}, "
86+
raise FileNotFoundError(
87+
f"Error: file not found: {filename}, "
8888
f"Please rerun specifying a valid filename."
8989
)
9090
return np.zeros((100, 100))

tests/test_loadimage.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
def test_load_image_cases(input_path, expected, user_filesystem):
2929
base_dir, home_dir, cwd_dir, test_dir = user_filesystem
3030
test_file_dir = Path(__file__).parent
31+
cwd_dir = test_file_dir / "cwd"
32+
home_dir = test_file_dir / "home"
33+
test_dir = test_file_dir / "test"
3134
src_image = test_file_dir.parent / "docs/examples/example.tiff"
3235

3336
# Copy test image into all directories
@@ -44,7 +47,18 @@ def test_load_image_cases(input_path, expected, user_filesystem):
4447
"Cfg", (), {"fliphorizontal": True, "flipvertical": False}
4548
)()
4649
loader = LoadImage(cfg)
47-
actual = loader.loadImage(input_path)
48-
assert isinstance(actual, np.ndarray)
50+
51+
if expected:
52+
# Handle case 1-3 for absolute, cwd, and home directory.
53+
actual = loader.loadImage(input_path)
54+
assert isinstance(actual, np.ndarray)
55+
else:
56+
# Handle Case 4 for missing file.
57+
with pytest.raises(
58+
FileNotFoundError,
59+
match=r"file not found: .*"
60+
r"Please rerun specifying a valid filename\.",
61+
):
62+
loader.loadImage(input_path)
4963
finally:
5064
os.chdir(old_cwd)

0 commit comments

Comments
 (0)