Skip to content

Commit c73c282

Browse files
committed
behavior: if no wavelength is specified, ValueError is raised instead of print statement
1 parent d63b678 commit c73c282

2 files changed

Lines changed: 85 additions & 15 deletions

File tree

src/diffpy/labpdfproc/tools.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,28 @@ def load_wavelength_from_config_file(args):
224224
args : argparse.Namespace
225225
The updated arguments with the updated wavelength and anode type.
226226
"""
227-
global_config = _load_config(Path().home() / "diffpyconfig.json")
228-
local_config = _load_config(Path().cwd() / "diffpyconfig.json")
229-
local_has_data = local_config and "wavelength" in local_config
230-
global_has_data = global_config and "wavelength" in global_config
231-
if not local_has_data and not global_has_data:
232-
print(
227+
228+
if args.wavelength is not None:
229+
return normalize_wavelength(args)
230+
231+
global_config_file = _load_config(Path().home() / "diffpyconfig.json")
232+
local_config_file = _load_config(Path().cwd() / "diffpyconfig.json")
233+
config_file = None
234+
if (
235+
isinstance(local_config_file, dict)
236+
and "wavelength" in local_config_file
237+
):
238+
config_file = local_config_file
239+
elif (
240+
isinstance(global_config_file, dict)
241+
and "wavelength" in global_config_file
242+
):
243+
config_file = global_config_file
244+
if config_file is not None:
245+
args.wavelength = config_file.get("wavelength")
246+
return normalize_wavelength(args)
247+
else:
248+
raise ValueError(
233249
"No configuration file was found containing information "
234250
"about the wavelength or anode type. \n"
235251
"You can add the wavelength or anode type "
@@ -240,13 +256,6 @@ def load_wavelength_from_config_file(args):
240256
"For more information, please refer to www.diffpy.org/"
241257
"diffpy.labpdfproc/examples/toolsexample.html"
242258
)
243-
if args.wavelength is not None:
244-
return normalize_wavelength(args)
245-
config = local_config if local_has_data else global_config
246-
if config:
247-
args.wavelength = config.get("wavelength")
248-
return normalize_wavelength(args)
249-
return args
250259

251260

252261
def set_wavelength(args):

tests/test_tools.py

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,76 @@ def test_load_wavelength_from_config_file_with_local_conf_file(
311311
assert actual_args.wavelength == expected["wavelength"]
312312

313313

314+
@pytest.mark.parametrize(
315+
"local_config, home_config",
316+
[
317+
# C1: no config files exist
318+
# expected: raise ValueError
319+
(None, None),
320+
# C2: local config is empty, no home config
321+
# expected: raise ValueError
322+
({}, None),
323+
# C3: no local config, home config is empty
324+
# expected: raise ValueError
325+
(None, {}),
326+
# C4: both config files are empty
327+
# expected: raise ValueError
328+
({}, {}),
329+
],
330+
)
331+
def test_load_wavelength_from_config_file_without_conf_files_bad(
332+
mocker,
333+
user_filesystem,
334+
local_config,
335+
home_config,
336+
):
337+
# User tries to correct data without specifying wavelength and
338+
# no config files
339+
# with wavelength exist -- expected to raise ValueError
340+
cwd = Path(user_filesystem)
341+
home_dir = cwd / "home_dir"
342+
mocker.patch("pathlib.Path.home", return_value=home_dir)
343+
os.chdir(cwd)
344+
345+
local_config_file = cwd / "diffpyconfig.json"
346+
if local_config_file.exists():
347+
local_config_file.unlink()
348+
home_config_file = home_dir / "diffpyconfig.json"
349+
if home_config_file.exists():
350+
home_config_file.unlink()
351+
352+
if local_config is not None:
353+
with open(local_config_file, "w") as f:
354+
json.dump(local_config, f)
355+
if home_config is not None:
356+
with open(home_config_file, "w") as f:
357+
json.dump(home_config, f)
358+
359+
cli_inputs = ["mud", "data.xy", "2.5"]
360+
actual_args = get_args_cli(cli_inputs)
361+
362+
msg = re.escape(
363+
"No configuration file was found containing information "
364+
"about the wavelength or anode type. \n"
365+
"You can add the wavelength or anode type "
366+
"to a configuration file on the current computer "
367+
"and it will be automatically associated with "
368+
"subsequent diffpy data by default. \n"
369+
"You will only have to do that once. \n"
370+
"For more information, please refer to www.diffpy.org/"
371+
"diffpy.labpdfproc/examples/toolsexample.html"
372+
)
373+
with pytest.raises(ValueError, match=msg):
374+
load_wavelength_from_config_file(actual_args)
375+
376+
314377
@pytest.mark.parametrize(
315378
"inputs, expected",
316379
[
317380
# Test when no config files exist,
318381
# expect to return args without modification.
319382
# This test only checks loading behavior,
320383
# not value validation (which is handled by `set_wavelength`).
321-
# C1: no args
322-
([], {"wavelength": None}),
323384
# C1: wavelength provided
324385
(["--wavelength", "0.25"], {"wavelength": 0.25}),
325386
# C2: anode type provided

0 commit comments

Comments
 (0)