Skip to content

Commit 7e68789

Browse files
committed
all tests passing, much simplified. Cannot yet handle lists from file
1 parent 8285277 commit 7e68789

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@
4949
"input_dir/binary.pkl",
5050
],
5151
),
52-
( # list of files provided (we skip if encountering missing files)
53-
["good_data.chi", "good_data.xy", "unreadable_file.txt", "missing_file.txt"],
54-
["good_data.chi", "good_data.xy", "unreadable_file.txt"],
55-
),
56-
( # list of files provided (with invalid files and files in different directories)
57-
["input_dir/good_data.chi", "good_data.chi", "missing_file.txt"],
58-
["input_dir/good_data.chi", "good_data.chi"],
59-
),
6052
( # file_list.txt list of files provided
6153
["input_dir/file_list.txt"],
6254
["good_data.chi", "good_data.xy", "good_data.txt"],
@@ -77,22 +69,24 @@ def test_set_input_lists(inputs, expected, user_filesystem):
7769
cli_inputs = ["2.5"] + inputs
7870
actual_args = get_args(cli_inputs)
7971
actual_args = set_input_lists(actual_args)
80-
assert actual_args.input_directory == expected_paths
81-
82-
83-
# This test is for existing single input file or directory absolute path not in cwd
84-
# Here we are in user_filesystem/input_dir, testing for a file or directory in user_filesystem
85-
params_input_not_cwd = [
86-
(["good_data.chi"], ["good_data.chi"]),
87-
(["."], ["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"]),
88-
]
72+
assert list(actual_args.input_directory).sort() == expected_paths.sort()
8973

9074

9175
# This test covers non-existing single input file or directory, in this case we raise an error with message
9276
params_input_bad = [
93-
(["non_existing_file.xy"], "Please specify at least one valid input file or directory."),
94-
(["./input_dir/non_existing_file.xy"], "Please specify at least one valid input file or directory."),
95-
(["./non_existing_dir"], "Please specify at least one valid input file or directory."),
77+
(
78+
["non_existing_file.xy"],
79+
"Cannot find non_existing_file.xy. Please specify valid input file(s) or directories.",
80+
),
81+
(
82+
["./input_dir/non_existing_file.xy"],
83+
"Cannot find ./input_dir/non_existing_file.xy. Please specify valid input file(s) or directories.",
84+
),
85+
(["./non_existing_dir"], "Cannot find ./non_existing_dir. Please specify valid input file(s) or directories."),
86+
( # list of files provided (with missing files)
87+
["good_data.chi", "good_data.xy", "unreadable_file.txt", "missing_file.txt"],
88+
"Cannot find missing_file.txt. Please specify valid input file(s) or directories.",
89+
),
9690
]
9791

9892

@@ -102,7 +96,7 @@ def test_set_input_files_bad(inputs, msg, user_filesystem):
10296
os.chdir(base_dir)
10397
cli_inputs = ["2.5"] + inputs
10498
actual_args = get_args(cli_inputs)
105-
with pytest.raises(ValueError, match=msg[0]):
99+
with pytest.raises(FileNotFoundError, match=msg[0]):
106100
actual_args = set_input_lists(actual_args)
107101

108102

src/diffpy/labpdfproc/tools.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,38 @@ def set_output_directory(args):
2929

3030

3131
def set_input_lists(args):
32+
"""
33+
Set input directory and files.
34+
35+
It takes cli inputs, checks if they are files or directories and creates
36+
a list of files to be processed which is stored in the args Namespace.
37+
38+
Parameters
39+
----------
40+
args argparse.Namespace
41+
the arguments from the parser
42+
43+
Returns
44+
-------
45+
args argparse.Namespace
46+
47+
"""
48+
49+
input_paths = []
50+
for input in args.input:
51+
input_path = Path(input).resolve()
52+
if input_path.exists():
53+
if input_path.is_file():
54+
input_paths.append(input_path)
55+
elif input_path.is_dir():
56+
input_files = input_path.glob("*")
57+
input_files = [file.resolve() for file in input_files if file.is_file()]
58+
input_paths.extend(input_files)
59+
else:
60+
raise FileNotFoundError(f"Cannot find {input}. Please specify valid input file(s) or directories.")
61+
else:
62+
raise FileNotFoundError(f"Cannot find {input}")
63+
setattr(args, "input_directory", input_paths)
3264
return args
3365

3466

0 commit comments

Comments
 (0)