|
1 | 1 | import argparse
|
2 | 2 | import os
|
3 | 3 | import re
|
4 |
| -from argparse import ArgumentParser |
5 | 4 | from pathlib import Path
|
6 | 5 |
|
7 | 6 | import pytest
|
8 | 7 |
|
| 8 | +from diffpy.labpdfproc.labpdfprocapp import get_args |
9 | 9 | from diffpy.labpdfproc.tools import known_sources, load_user_metadata, set_output_directory, set_wavelength
|
10 | 10 |
|
11 | 11 | params1 = [
|
@@ -80,57 +80,54 @@ def test_set_wavelength_bad(inputs, msg):
|
80 | 80 |
|
81 | 81 |
|
82 | 82 | params5 = [
|
83 |
| - ([None], []), |
| 83 | + ([], []), |
84 | 84 | (
|
85 |
| - [["facility=NSLS II", "beamline=28ID-2", "favorite color=blue"]], |
| 85 | + ["-u", "facility=NSLS II", "beamline=28ID-2", "favorite color=blue"], |
86 | 86 | [["facility", "NSLS II"], ["beamline", "28ID-2"], ["favorite color", "blue"]],
|
87 | 87 | ),
|
88 |
| - ([["x=y=z"]], [["x", "y=z"]]), |
| 88 | + (["-u", "x=y=z"], [["x", "y=z"]]), |
89 | 89 | ]
|
90 | 90 |
|
91 | 91 |
|
92 | 92 | @pytest.mark.parametrize("inputs, expected", params5)
|
93 | 93 | def test_load_user_metadata(inputs, expected):
|
94 |
| - expected_parser = ArgumentParser() |
95 |
| - expected_args = expected_parser.parse_args([]) |
| 94 | + expected_args = get_args(["2.5"]) |
96 | 95 | for expected_pair in expected:
|
97 | 96 | setattr(expected_args, expected_pair[0], expected_pair[1])
|
| 97 | + delattr(expected_args, "user_metadata") |
98 | 98 |
|
99 |
| - actual_parser = ArgumentParser() |
100 |
| - actual_parser.add_argument("--user-metadata") |
101 |
| - actual_args = actual_parser.parse_args(["--user-metadata", inputs[0]]) |
| 99 | + cli_inputs = ["2.5"] + inputs |
| 100 | + actual_args = get_args(cli_inputs) |
102 | 101 | actual_args = load_user_metadata(actual_args)
|
103 | 102 | assert actual_args == expected_args
|
104 | 103 |
|
105 | 104 |
|
106 | 105 | params6 = [
|
107 | 106 | (
|
108 |
| - [["facility=NSLS", "II"]], |
| 107 | + ["-u", "facility=", "NSLS II"], |
109 | 108 | [
|
110 | 109 | "Please provide key-value pairs in the format key=value. "
|
111 | 110 | "For more information, use `labpdfproc --help.`"
|
112 | 111 | ],
|
113 | 112 | ),
|
114 | 113 | (
|
115 |
| - [["favorite", "color=blue"]], |
| 114 | + ["-u", "favorite", "color=blue"], |
116 | 115 | "Please provide key-value pairs in the format key=value. "
|
117 | 116 | "For more information, use `labpdfproc --help.`",
|
118 | 117 | ),
|
119 | 118 | (
|
120 |
| - [["beamline", "=", "28ID-2"]], |
| 119 | + ["-u", "beamline", "=", "28ID-2"], |
121 | 120 | "Please provide key-value pairs in the format key=value. "
|
122 | 121 | "For more information, use `labpdfproc --help.`",
|
123 | 122 | ),
|
124 |
| - ([["facility=NSLS II", "facility=NSLS III"]], ["Please do not specify repeated keys: facility. "]), |
125 |
| - ([["wavelength=2"]], ["Please do not specify repeated keys: wavelength. "]), |
| 123 | + (["-u", "facility=NSLS II", "facility=NSLS III"], "Please do not specify repeated keys: facility. "), |
| 124 | + (["-u", "wavelength=2"], "wavelength is a reserved name. Please rerun using a different key name. "), |
126 | 125 | ]
|
127 | 126 |
|
128 | 127 |
|
129 | 128 | @pytest.mark.parametrize("inputs, msg", params6)
|
130 | 129 | def test_load_user_metadata_bad(inputs, msg):
|
131 |
| - actual_parser = ArgumentParser() |
132 |
| - actual_parser.add_argument("--wavelength") |
133 |
| - actual_parser.add_argument("--user-metadata") |
134 |
| - actual_args = actual_parser.parse_args(["--user-metadata", inputs[0]]) |
| 130 | + cli_inputs = ["2.5"] + inputs |
| 131 | + actual_args = get_args(cli_inputs) |
135 | 132 | with pytest.raises(ValueError, match=msg[0]):
|
136 | 133 | actual_args = load_user_metadata(actual_args)
|
0 commit comments