-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathexec_unit_tests.rs
More file actions
231 lines (209 loc) · 7.25 KB
/
Copy pathexec_unit_tests.rs
File metadata and controls
231 lines (209 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2017 Google Inc.
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
/// ! This file contains what would be normally be unit tests for `find::matchers::exec`.
/// ! But as the tests require running an external executable, they need to be run
/// ! as integration tests so we can ensure that our testing-commandline binary
/// ! has been built.
use std::env;
use std::fs::File;
use std::io::Read;
use tempfile::Builder;
use common::test_helpers::{
fix_up_slashes, get_dir_entry_for, path_to_testing_commandline, FakeDependencies,
};
use findutils::find::matchers::exec::SingleExecMatcher;
use findutils::find::matchers::Matcher;
mod common;
#[test]
fn matching_executes_code() {
let temp_dir = Builder::new()
.prefix("matching_executes_code")
.tempdir()
.unwrap();
let temp_dir_path = temp_dir.path().to_string_lossy();
let abbbc = get_dir_entry_for("test_data/simple", "abbbc");
let matcher = SingleExecMatcher::new(
&path_to_testing_commandline(),
&[temp_dir_path.as_ref(), "abc", "{}", "xyz"],
false,
false,
)
.expect("Failed to create matcher");
let deps = FakeDependencies::new();
assert!(matcher.matches(&abbbc, &mut deps.new_matcher_io()));
let mut f = File::open(temp_dir.path().join("1.txt")).expect("Failed to open output file");
let mut s = String::new();
f.read_to_string(&mut s)
.expect("failed to read output file");
assert_eq!(
s,
fix_up_slashes(&format!(
"cwd={}\nargs=\nabc\ntest_data/simple/abbbc\nxyz\n",
env::current_dir().unwrap().to_string_lossy()
))
);
}
#[test]
fn matching_executes_code_in_files_directory() {
let temp_dir = Builder::new()
.prefix("matching_executes_code_in_files_directory")
.tempdir()
.unwrap();
let temp_dir_path = temp_dir.path().to_string_lossy();
let abbbc = get_dir_entry_for("test_data/simple", "abbbc");
let matcher = SingleExecMatcher::new(
&path_to_testing_commandline(),
&[temp_dir_path.as_ref(), "abc", "{}", "xyz"],
true,
false,
)
.expect("Failed to create matcher");
let deps = FakeDependencies::new();
assert!(matcher.matches(&abbbc, &mut deps.new_matcher_io()));
let mut f = File::open(temp_dir.path().join("1.txt")).expect("Failed to open output file");
let mut s = String::new();
f.read_to_string(&mut s)
.expect("failed to read output file");
assert_eq!(
s,
fix_up_slashes(&format!(
"cwd={}/test_data/simple\nargs=\nabc\n./abbbc\nxyz\n",
env::current_dir().unwrap().to_string_lossy()
))
);
}
#[test]
fn matching_embedded_filename() {
let temp_dir = Builder::new()
.prefix("matching_embedded_filename")
.tempdir()
.unwrap();
let temp_dir_path = temp_dir.path().to_string_lossy();
let abbbc = get_dir_entry_for("test_data/simple", "abbbc");
let matcher = SingleExecMatcher::new(
&path_to_testing_commandline(),
&[temp_dir_path.as_ref(), "abc{}x{}yz"],
false,
false,
)
.expect("Failed to create matcher");
let deps = FakeDependencies::new();
assert!(matcher.matches(&abbbc, &mut deps.new_matcher_io()));
let mut f = File::open(temp_dir.path().join("1.txt")).expect("Failed to open output file");
let mut s = String::new();
f.read_to_string(&mut s)
.expect("failed to read output file");
assert_eq!(
s,
fix_up_slashes(&format!(
"cwd={}\nargs=\nabctest_data/simple/abbbcxtest_data/simple/abbbcyz\n",
env::current_dir().unwrap().to_string_lossy()
))
);
}
#[test]
/// Running "find . -execdir whatever \;" failed with a No such file or directory error.
/// It's now fixed, and this is a regression test to check that it stays fixed.
fn execdir_in_current_directory() {
let temp_dir = Builder::new()
.prefix("execdir_in_current_directory")
.tempdir()
.unwrap();
let temp_dir_path = temp_dir.path().to_string_lossy();
let current_dir_entry = get_dir_entry_for(".", "");
let matcher = SingleExecMatcher::new(
&path_to_testing_commandline(),
&[temp_dir_path.as_ref(), "abc", "{}", "xyz"],
true,
false,
)
.expect("Failed to create matcher");
let deps = FakeDependencies::new();
assert!(matcher.matches(¤t_dir_entry, &mut deps.new_matcher_io()));
let mut f = File::open(temp_dir.path().join("1.txt")).expect("Failed to open output file");
let mut s = String::new();
f.read_to_string(&mut s)
.expect("failed to read output file");
assert_eq!(
s,
fix_up_slashes(&format!(
"cwd={}\nargs=\nabc\n./.\nxyz\n",
env::current_dir().unwrap().to_string_lossy()
))
);
}
#[test]
/// Regression test for "find / -execdir whatever \;"
fn execdir_in_root_directory() {
let temp_dir = Builder::new()
.prefix("execdir_in_root_directory")
.tempdir()
.unwrap();
let temp_dir_path = temp_dir.path().to_string_lossy();
let cwd = env::current_dir().expect("no current directory");
let root_dir = cwd
.ancestors()
.last()
.expect("current directory has no root");
let root_dir_entry = get_dir_entry_for(root_dir.to_str().unwrap(), "");
let matcher = SingleExecMatcher::new(
&path_to_testing_commandline(),
&[temp_dir_path.as_ref(), "abc", "{}", "xyz"],
true,
false,
)
.expect("Failed to create matcher");
let deps = FakeDependencies::new();
assert!(matcher.matches(&root_dir_entry, &mut deps.new_matcher_io()));
let mut f = File::open(temp_dir.path().join("1.txt")).expect("Failed to open output file");
let mut s = String::new();
f.read_to_string(&mut s)
.expect("failed to read output file");
assert_eq!(
s,
fix_up_slashes(&format!(
"cwd={}\nargs=\nabc\n{}\nxyz\n",
root_dir.to_string_lossy(),
root_dir.to_string_lossy(),
))
);
}
#[test]
fn matching_fails_if_executable_fails() {
let temp_dir = Builder::new()
.prefix("matching_fails_if_executable_fails")
.tempdir()
.unwrap();
let temp_dir_path = temp_dir.path().to_string_lossy();
let abbbc = get_dir_entry_for("test_data/simple", "abbbc");
let matcher = SingleExecMatcher::new(
&path_to_testing_commandline(),
&[
temp_dir_path.as_ref(),
"--exit_with_failure",
"abc",
"{}",
"xyz",
],
true,
false,
)
.expect("Failed to create matcher");
let deps = FakeDependencies::new();
assert!(!matcher.matches(&abbbc, &mut deps.new_matcher_io()));
let mut f = File::open(temp_dir.path().join("1.txt")).expect("Failed to open output file");
let mut s = String::new();
f.read_to_string(&mut s)
.expect("failed to read output file");
assert_eq!(
s,
fix_up_slashes(&format!(
"cwd={}/test_data/simple\nargs=\n--exit_with_failure\nabc\n.\
/abbbc\nxyz\n",
env::current_dir().unwrap().to_string_lossy()
))
);
}