Skip to content

Commit 1216622

Browse files
committed
tests: Look at the more specific tests first, but don't require specific expectations for all llvm versions.
1 parent 71e0d9d commit 1216622

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

tests/tests.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,13 @@ fn compare_generated_header(header: &PathBuf, builder: Builder) -> Result<(), Er
109109
expectation.pop();
110110
expectation.push("expectations");
111111
expectation.push("tests");
112-
expectation.push(file_name);
113-
expectation.set_extension("rs");
114112

115-
// If the expectation file doesn't exist, see if we have different test
116-
// expectations for different libclang versions.
117-
if !expectation.is_file() {
118-
let file_name = expectation.file_name().unwrap().to_owned();
119-
expectation.pop();
113+
let mut looked_at = vec![];
114+
let mut expectation_file;
115+
116+
// Try more specific expectations first.
117+
{
118+
let mut expectation = expectation.clone();
120119

121120
if cfg!(feature = "testing_only_libclang_4") {
122121
expectation.push("libclang-4");
@@ -144,17 +143,32 @@ fn compare_generated_header(header: &PathBuf, builder: Builder) -> Result<(), Er
144143
}
145144

146145
expectation.push(file_name);
146+
expectation.set_extension("rs");
147+
expectation_file = fs::File::open(&expectation).ok();
148+
looked_at.push(expectation);
149+
}
147150

148-
if !expectation.is_file() {
149-
panic!(
150-
"missing test expectation file and/or 'testing_only_libclang_$VERSION' \
151-
feature for header '{}'; looking for expectation file at '{}'",
152-
header.display(),
153-
expectation.display()
154-
);
155-
}
151+
// Try the default path otherwise.
152+
if expectation_file.is_none() {
153+
expectation.push(file_name);
154+
expectation.set_extension("rs");
155+
expectation_file = fs::File::open(&expectation).ok();
156+
looked_at.push(expectation);
156157
}
157158

159+
let mut expected = String::new();
160+
match expectation_file {
161+
Some(f) => {
162+
BufReader::new(f).read_to_string(&mut expected)?;
163+
}
164+
None => panic!(
165+
"missing test expectation file and/or 'testing_only_libclang_$VERSION' \
166+
feature for header '{}'; looking for expectation file at '{:?}'",
167+
header.display(),
168+
looked_at,
169+
),
170+
};
171+
158172
// We skip the generate() error here so we get a full diff below
159173
let (actual, rustfmt_stderr) = match builder.generate() {
160174
Ok(bindings) => {
@@ -165,13 +179,6 @@ fn compare_generated_header(header: &PathBuf, builder: Builder) -> Result<(), Er
165179
};
166180
println!("{}", rustfmt_stderr);
167181

168-
let mut expected = String::new();
169-
{
170-
if let Ok(expectation_file) = fs::File::open(&expectation) {
171-
BufReader::new(expectation_file).read_to_string(&mut expected)?;
172-
}
173-
}
174-
175182
let (expected, rustfmt_stderr) = rustfmt(expected);
176183
println!("{}", rustfmt_stderr);
177184

@@ -188,7 +195,7 @@ fn compare_generated_header(header: &PathBuf, builder: Builder) -> Result<(), Er
188195
println!("{}", rustfmt_stderr);
189196

190197
println!("diff expected generated");
191-
println!("--- expected: {:?}", expectation);
198+
println!("--- expected: {:?}", looked_at.last().unwrap());
192199
println!("+++ generated from: {:?}", header);
193200

194201
for diff in diff::lines(&expected, &actual) {
@@ -201,7 +208,7 @@ fn compare_generated_header(header: &PathBuf, builder: Builder) -> Result<(), Er
201208

202209
// Overwrite the expectation with actual output.
203210
if env::var_os(OVERWRITE_ENV_VAR).is_some() {
204-
let mut expectation_file = fs::File::create(&expectation)?;
211+
let mut expectation_file = fs::File::create(looked_at.last().unwrap())?;
205212
expectation_file.write_all(actual.as_bytes())?;
206213
}
207214

0 commit comments

Comments
 (0)