1
+ import os
1
2
import re
2
3
from pathlib import Path
3
4
4
5
import pytest
5
6
6
7
from diffpy .labpdfproc .labpdfprocapp import get_args
7
- from diffpy .labpdfproc .tools import known_sources , load_user_metadata , set_output_directory , set_wavelength
8
+ from diffpy .labpdfproc .tools import (
9
+ known_sources ,
10
+ load_user_metadata ,
11
+ set_input_lists ,
12
+ set_output_directory ,
13
+ set_wavelength ,
14
+ )
15
+
16
+ # Use cases can be found here: https://github.com/diffpy/diffpy.labpdfproc/issues/48
17
+
18
+ # This test covers existing single input file, directory, a file list, and multiple files
19
+ # We store absolute path into input_directory and file names into input_file
20
+ params_input = [
21
+ (["good_data.chi" ], ["good_data.chi" ]), # single good file, same directory
22
+ (["input_dir/good_data.chi" ], ["input_dir/good_data.chi" ]), # single good file, input directory
23
+ ( # glob current directory
24
+ ["." ],
25
+ ["good_data.chi" , "good_data.xy" , "good_data.txt" , "unreadable_file.txt" , "binary.pkl" ],
26
+ ),
27
+ ( # glob input directory
28
+ ["./input_dir" ],
29
+ [
30
+ "input_dir/good_data.chi" ,
31
+ "input_dir/good_data.xy" ,
32
+ "input_dir/good_data.txt" ,
33
+ "input_dir/unreadable_file.txt" ,
34
+ "input_dir/binary.pkl" ,
35
+ ],
36
+ ),
37
+ ( # glob list of input directories
38
+ ["." , "./input_dir" ],
39
+ [
40
+ "./good_data.chi" ,
41
+ "./good_data.xy" ,
42
+ "./good_data.txt" ,
43
+ "./unreadable_file.txt" ,
44
+ "./binary.pkl" ,
45
+ "input_dir/good_data.chi" ,
46
+ "input_dir/good_data.xy" ,
47
+ "input_dir/good_data.txt" ,
48
+ "input_dir/unreadable_file.txt" ,
49
+ "input_dir/binary.pkl" ,
50
+ ],
51
+ ),
52
+ ( # file_list.txt list of files provided
53
+ ["input_dir/file_list.txt" ],
54
+ ["good_data.chi" , "good_data.xy" , "good_data.txt" ],
55
+ ),
56
+ ( # file_list_example2.txt list of files provided in different directories
57
+ ["input_dir/file_list_example2.txt" ],
58
+ ["input_dir/good_data.chi" , "good_data.xy" , "input_dir/good_data.txt" ],
59
+ ),
60
+ ]
61
+
62
+
63
+ @pytest .mark .parametrize ("inputs, expected" , params_input )
64
+ def test_set_input_lists (inputs , expected , user_filesystem ):
65
+ base_dir = Path (user_filesystem )
66
+ os .chdir (base_dir )
67
+ expected_paths = [base_dir .resolve () / expected_path for expected_path in expected ]
68
+
69
+ cli_inputs = ["2.5" ] + inputs
70
+ actual_args = get_args (cli_inputs )
71
+ actual_args = set_input_lists (actual_args )
72
+ assert list (actual_args .input_paths ).sort () == expected_paths .sort ()
73
+
74
+
75
+ # This test covers non-existing single input file or directory, in this case we raise an error with message
76
+ params_input_bad = [
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
+ ),
90
+ ]
91
+
92
+
93
+ @pytest .mark .parametrize ("inputs, msg" , params_input_bad )
94
+ def test_set_input_files_bad (inputs , msg , user_filesystem ):
95
+ base_dir = Path (user_filesystem )
96
+ os .chdir (base_dir )
97
+ cli_inputs = ["2.5" ] + inputs
98
+ actual_args = get_args (cli_inputs )
99
+ with pytest .raises (FileNotFoundError , match = msg [0 ]):
100
+ actual_args = set_input_lists (actual_args )
101
+
8
102
9
103
params1 = [
10
104
([], ["." ]),
16
110
17
111
@pytest .mark .parametrize ("inputs, expected" , params1 )
18
112
def test_set_output_directory (inputs , expected , user_filesystem ):
113
+ os .chdir (user_filesystem )
19
114
expected_output_directory = Path (user_filesystem ) / expected [0 ]
20
115
cli_inputs = ["2.5" , "data.xy" ] + inputs
21
116
actual_args = get_args (cli_inputs )
@@ -26,6 +121,7 @@ def test_set_output_directory(inputs, expected, user_filesystem):
26
121
27
122
28
123
def test_set_output_directory_bad (user_filesystem ):
124
+ os .chdir (user_filesystem )
29
125
cli_inputs = ["2.5" , "data.xy" , "--output-directory" , "good_data.chi" ]
30
126
actual_args = get_args (cli_inputs )
31
127
with pytest .raises (FileExistsError ):
0 commit comments