|
1 | 1 | import argparse
|
| 2 | +import os |
| 3 | +import re |
2 | 4 | from pathlib import Path
|
3 | 5 |
|
4 | 6 | import pytest
|
5 | 7 |
|
6 |
| -from diffpy.labpdfproc.tools import set_output_directory, set_wavelength |
| 8 | +from diffpy.labpdfproc.tools import known_sources, set_output_directory, set_wavelength |
7 | 9 |
|
8 | 10 | params1 = [
|
9 |
| - ([None], [Path.cwd().resolve()]), |
10 |
| - (["."], [Path.cwd().resolve()]), |
11 |
| - (["new_dir"], [Path.cwd().resolve() / "new_dir"]), |
12 |
| - (["existing_dir"], [Path.cwd().resolve() / "existing_dir"]), |
| 11 | + ([None], ["."]), |
| 12 | + (["."], ["."]), |
| 13 | + (["new_dir"], ["new_dir"]), |
| 14 | + (["existing_dir"], ["existing_dir"]), |
13 | 15 | ]
|
14 | 16 |
|
15 | 17 |
|
16 | 18 | @pytest.mark.parametrize("inputs, expected", params1)
|
17 |
| -def test_set_output_directory(inputs, expected): |
| 19 | +def test_set_output_directory(inputs, expected, tmp_path): |
| 20 | + directory = Path(tmp_path) |
| 21 | + os.chdir(directory) |
| 22 | + |
18 | 23 | existing_dir = Path().cwd().resolve() / "existing_dir"
|
19 | 24 | existing_dir.mkdir(parents=True, exist_ok=True)
|
20 | 25 |
|
21 |
| - expected_output_directory = expected[0] |
| 26 | + expected_output_directory = Path.cwd().resolve() / expected[0] |
22 | 27 | actual_args = argparse.Namespace(output_directory=inputs[0])
|
23 | 28 | actual_args.output_directory = set_output_directory(actual_args)
|
24 | 29 | assert actual_args.output_directory == expected_output_directory
|
25 | 30 | assert Path(actual_args.output_directory).exists()
|
26 | 31 | assert Path(actual_args.output_directory).is_dir()
|
27 | 32 |
|
28 | 33 |
|
29 |
| -def test_set_output_directory_bad(): |
| 34 | +def test_set_output_directory_bad(tmp_path): |
| 35 | + directory = Path(tmp_path) |
| 36 | + os.chdir(directory) |
| 37 | + |
30 | 38 | existing_file = Path().cwd().resolve() / "existing_file.py"
|
31 | 39 | existing_file.touch()
|
32 | 40 |
|
@@ -54,14 +62,17 @@ def test_set_wavelength(inputs, expected):
|
54 | 62 |
|
55 | 63 |
|
56 | 64 | params3 = [
|
57 |
| - ([None, "invalid"]), |
58 |
| - ([0, None]), |
59 |
| - ([-1, "Mo"]), |
| 65 | + ( |
| 66 | + [None, "invalid"], |
| 67 | + [f"Anode type not recognized. please rerun specifying an anode_type from {*known_sources, }"], |
| 68 | + ), |
| 69 | + ([0, None], ["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength"]), |
| 70 | + ([-1, "Mo"], ["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength"]), |
60 | 71 | ]
|
61 | 72 |
|
62 | 73 |
|
63 |
| -@pytest.mark.parametrize("inputs", params3) |
64 |
| -def test_set_wavelength_bad(inputs): |
65 |
| - with pytest.raises(ValueError): |
66 |
| - actual_args = argparse.Namespace(wavelength=inputs[0], anode_type=inputs[1]) |
| 74 | +@pytest.mark.parametrize("inputs, msg", params3) |
| 75 | +def test_set_wavelength_bad(inputs, msg): |
| 76 | + actual_args = argparse.Namespace(wavelength=inputs[0], anode_type=inputs[1]) |
| 77 | + with pytest.raises(ValueError, match=re.escape(msg[0])): |
67 | 78 | actual_args.wavelength = set_wavelength(actual_args)
|
0 commit comments