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