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