Skip to content

Commit 79c45af

Browse files
changed output directory and wavelength tests to use get_args
1 parent cbf7a46 commit 79c45af

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from diffpy.utils.scattering_objects.diffraction_objects import XQUANTITIES, Diffraction_object
99

1010

11-
def get_args():
11+
def get_args(override_cli_inputs=None):
1212
p = ArgumentParser()
1313
p.add_argument("mud", help="Value of mu*D for your " "sample. Required.", type=float)
1414
p.add_argument("-i", "--input-file", help="The filename of the " "datafile to load.")
@@ -58,7 +58,7 @@ def get_args():
5858
action="store_true",
5959
help="Outputs will not overwrite existing file unless --force is specified.",
6060
)
61-
args = p.parse_args()
61+
args = p.parse_args(override_cli_inputs)
6262
return args
6363

6464

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,74 @@
1-
import argparse
2-
import os
31
import re
42
from pathlib import Path
53

64
import pytest
75

6+
from diffpy.labpdfproc.labpdfprocapp import get_args
87
from diffpy.labpdfproc.tools import known_sources, set_output_directory, set_wavelength
98

109
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"]),
1514
]
1615

1716

1817
@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]
2221

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)
2824
actual_args.output_directory = set_output_directory(actual_args)
2925
assert actual_args.output_directory == expected_output_directory
3026
assert Path(actual_args.output_directory).exists()
3127
assert Path(actual_args.output_directory).is_dir()
3228

3329

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)
4233
with pytest.raises(FileExistsError):
4334
actual_args.output_directory = set_output_directory(actual_args)
4435
assert Path(actual_args.output_directory).exists()
4536
assert not Path(actual_args.output_directory).is_dir()
4637

4738

4839
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]),
5344
]
5445

5546

5647
@pytest.mark.parametrize("inputs, expected", params2)
5748
def test_set_wavelength(inputs, expected):
5849
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
6254

6355

6456
params3 = [
6557
(
66-
[None, "invalid"],
58+
["-a", "invalid"],
6759
[f"Anode type not recognized. Please rerun specifying an anode_type from {*known_sources, }."],
6860
),
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+
),
7166
]
7267

7368

7469
@pytest.mark.parametrize("inputs, msg", params3)
7570
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)
7773
with pytest.raises(ValueError, match=re.escape(msg[0])):
7874
actual_args.wavelength = set_wavelength(actual_args)

0 commit comments

Comments
 (0)