Skip to content

Commit cf30c83

Browse files
committed
[yaml runner] sort module imports, allow bypassing test file generation using '*' in skip.yml
1 parent 399ca4e commit cf30c83

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

yaml_test_runner/src/generator.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use regex::Regex;
2525
use semver::Version;
2626
use serde::Deserialize;
2727
use std::{
28-
borrow::Borrow,
2928
collections::{BTreeMap, HashSet},
3029
fs,
3130
fs::{File, OpenOptions},
@@ -170,16 +169,11 @@ impl<'a> YamlTests<'a> {
170169

171170
/// Whether the test should be skipped
172171
fn skip_test(&self, name: &str) -> bool {
173-
if self.skip.tests.contains_key(self.path.as_str()) {
174-
let tests = self.skip.tests.get(self.path.as_str());
175-
176-
return match tests {
177-
Some(t) => t.contains(name.to_string().borrow()),
178-
None => true,
179-
};
172+
if let Some(tests) = self.skip.tests.get(&self.path) {
173+
tests.iter().any(|n| n == name || n == "*")
174+
} else {
175+
false
180176
}
181-
182-
false
183177
}
184178

185179
fn fn_impls(
@@ -490,13 +484,13 @@ pub fn generate_tests_from_yaml(
490484
}
491485
}
492486

493-
write_mod_files(&generated_dir)?;
487+
write_mod_files(&generated_dir, true)?;
494488

495489
Ok(())
496490
}
497491

498492
/// Writes a mod.rs file in each generated directory
499-
fn write_mod_files(generated_dir: &PathBuf) -> Result<(), failure::Error> {
493+
fn write_mod_files(generated_dir: &PathBuf, toplevel: bool) -> Result<(), failure::Error> {
500494
if !generated_dir.exists() {
501495
fs::create_dir(generated_dir)?;
502496
}
@@ -505,30 +499,34 @@ fn write_mod_files(generated_dir: &PathBuf) -> Result<(), failure::Error> {
505499
let mut mods = vec![];
506500
for path in paths {
507501
if let Ok(entry) = path {
508-
let file_type = entry.file_type().unwrap();
509502
let path = entry.path();
510503
let name = path.file_stem().unwrap().to_string_lossy();
511504

512-
let is_tests_common_dir =
513-
name.as_ref() == "common" && path.parent().unwrap().file_name().unwrap() == "tests";
514-
515-
if name.as_ref() != "mod" {
516-
if is_tests_common_dir {
517-
mods.push("#[macro_use]".to_string());
518-
}
519-
505+
if name != "mod" {
520506
mods.push(format!(
521507
"pub mod {};",
522508
path.file_stem().unwrap().to_string_lossy()
523509
));
524510
}
525511

526-
if file_type.is_dir() && !is_tests_common_dir {
527-
write_mod_files(&entry.path())?;
512+
if path.is_dir() && !(toplevel && name == "common") {
513+
write_mod_files(&entry.path(), false)?;
528514
}
529515
}
530516
}
531517

518+
// Make sure we have a stable output
519+
mods.sort();
520+
521+
if toplevel {
522+
// The "common" module must appear first so that its macros are parsed before the
523+
// compiler visits other modules, otherwise we'll have "macro not found" errors.
524+
mods.retain(|name| name != "pub mod common;");
525+
mods.insert(0, "#[macro_use]".into());
526+
mods.insert(1, "pub mod common;".into());
527+
mods.insert(2, "".into());
528+
}
529+
532530
let mut path = generated_dir.clone();
533531
path.push("mod.rs");
534532
let mut file = File::create(&path)?;
@@ -562,6 +560,14 @@ fn write_test_file(
562560
relative_path: &Path,
563561
generated_dir: &PathBuf,
564562
) -> Result<(), failure::Error> {
563+
if test.skip_test("*") {
564+
info!(
565+
r#"skipping all tests in {} because it's included in skip.yml"#,
566+
test.path,
567+
);
568+
return Ok(());
569+
}
570+
565571
let mut path = test_file_path(relative_path)?;
566572
path = generated_dir.join(path);
567573
path.set_extension("rs");

yaml_test_runner/src/step/do.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl ApiCall {
291291
// arguments for the API call
292292
for (k, v) in hash.iter() {
293293
match k.as_str().unwrap() {
294-
"body" => body = Self::generate_body(endpoint, v),
294+
"body" => body = Self::generate_body(endpoint, v)?,
295295
"ignore" => {
296296
ignore = match v.as_i64() {
297297
Some(i) => Some(i as u16),
@@ -853,9 +853,9 @@ impl ApiCall {
853853
/// When reading a body from the YAML test, it'll be converted to a Yaml variant,
854854
/// usually a Hash. To get the JSON representation back requires converting
855855
/// back to JSON
856-
fn generate_body(endpoint: &ApiEndpoint, v: &Yaml) -> Option<Tokens> {
856+
fn generate_body(endpoint: &ApiEndpoint, v: &Yaml) -> Result<Option<Tokens>, failure::Error> {
857857
match v {
858-
Yaml::Null => None,
858+
Yaml::Null => Ok(None),
859859
Yaml::String(s) => {
860860
let json = {
861861
let json = replace_set(s);
@@ -884,10 +884,10 @@ impl ApiCall {
884884
quote! { JsonBody::from(json!(#ident)) }
885885
})
886886
.collect();
887-
Some(quote!(.body(vec![#(#values),*])))
887+
Ok(Some(quote!(.body(vec![#(#values),*]))))
888888
} else {
889889
let ident = syn::Ident::from(json);
890-
Some(quote!(.body(json!{#ident})))
890+
Ok(Some(quote!(.body(json!{#ident}))))
891891
}
892892
}
893893
_ => {
@@ -898,7 +898,7 @@ impl ApiCall {
898898
}
899899

900900
if endpoint.supports_nd_body() {
901-
let values: Vec<serde_json::Value> = serde_yaml::from_str(&s).unwrap();
901+
let values: Vec<serde_json::Value> = serde_yaml::from_str(&s)?;
902902
let json: Vec<Tokens> = values
903903
.iter()
904904
.map(|value| {
@@ -915,15 +915,15 @@ impl ApiCall {
915915
}
916916
})
917917
.collect();
918-
Some(quote!(.body(vec![ #(#json),* ])))
918+
Ok(Some(quote!(.body(vec![ #(#json),* ]))))
919919
} else {
920-
let value: serde_json::Value = serde_yaml::from_str(&s).unwrap();
921-
let mut json = serde_json::to_string_pretty(&value).unwrap();
920+
let value: serde_json::Value = serde_yaml::from_str(&s)?;
921+
let mut json = serde_json::to_string_pretty(&value)?;
922922
json = replace_set(json);
923923
json = replace_i64(json);
924924
let ident = syn::Ident::from(json);
925925

926-
Some(quote!(.body(json!{#ident})))
926+
Ok(Some(quote!(.body(json!{#ident}))))
927927
}
928928
}
929929
}

0 commit comments

Comments
 (0)