From 64af88cd6a8eaec7ee46986557d13c90973fd8fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20M=C3=B3ricz?= Date: Sat, 3 Feb 2024 13:49:58 +0100 Subject: [PATCH] chore: various execution_spec/testconv hotfixes (#1098) Co-authored-by: ologbonowiwi <100464352+ologbonowiwi@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- docker.sh | 2 +- testconv/src/http.rs | 5 +- testconv/src/main.rs | 142 ++++++++++++---------------------------- tests/execution_spec.rs | 2 +- 4 files changed, 47 insertions(+), 104 deletions(-) diff --git a/docker.sh b/docker.sh index 56f700e98b..f4ee8f4ff4 100644 --- a/docker.sh +++ b/docker.sh @@ -2,7 +2,7 @@ yj -t < Cargo.toml > Cargo.yaml # Modify Cargo.yaml -yq e 'del(.workspace.members[] | select(. == "cloudflare" or . == "autogen"))' -i Cargo.yaml +yq e 'del(.workspace.members[] | select(. == "cloudflare" or . == "autogen" or . == "testconv"))' -i Cargo.yaml # Convert Cargo.yaml back to TOML yj -yt < Cargo.yaml > Cargo.toml \ No newline at end of file diff --git a/testconv/src/http.rs b/testconv/src/http.rs index 17d3a29280..e0498cbda3 100644 --- a/testconv/src/http.rs +++ b/testconv/src/http.rs @@ -1,6 +1,5 @@ -use std::collections::HashMap; - use derive_setters::Setters; +use indexmap::IndexMap; use serde::{Deserialize, Serialize}; use tailcall::config::Config; @@ -30,7 +29,7 @@ pub struct HttpSpec { pub mock: Vec, #[serde(default)] - pub env: HashMap, + pub env: IndexMap, #[serde(default)] pub expected_upstream_requests: Vec, diff --git a/testconv/src/main.rs b/testconv/src/main.rs index 957caa043f..cf56b8e51b 100644 --- a/testconv/src/main.rs +++ b/testconv/src/main.rs @@ -236,44 +236,46 @@ async fn main() { f.write_all(spec.as_bytes()) .expect("Failed to write execution spec"); - for (i, assert) in old.assert.iter().enumerate() { - let mut f = File::options() - .create(true) - .write(true) - .truncate(true) - .open(snapshots_dir.join(PathBuf::from(format!( - "execution_spec__{}.md_assert_{}.snap", - file_stem, i - )))) - .expect("Failed to open execution snapshot"); - - let mut res = assert.response.to_owned(); - - res.0.headers = res - .0 - .headers - .into_iter() - .map(|(k, v)| (k.to_lowercase(), v.to_owned())) - .collect(); - - if !res.0.headers.contains_key("content-type") { - res.0 + if !has_fail_annotation { + for (i, assert) in old.assert.iter().enumerate() { + let mut f = File::options() + .create(true) + .write(true) + .truncate(true) + .open(snapshots_dir.join(PathBuf::from(format!( + "execution_spec__{}.md_assert_{}.snap", + file_stem, i + )))) + .expect("Failed to open execution snapshot"); + + let mut res = assert.response.to_owned(); + + res.0.headers = res + .0 .headers - .insert("content-type".to_string(), "application/json".to_string()); - } + .into_iter() + .map(|(k, v)| (k.to_lowercase(), v.to_owned())) + .collect(); + + if !res.0.headers.contains_key("content-type") { + res.0 + .headers + .insert("content-type".to_string(), "application/json".to_string()); + } - res.0 - .headers - .sort_by(|a, _, b, _| a.partial_cmp(b).unwrap()); + res.0 + .headers + .sort_by(|a, _, b, _| a.partial_cmp(b).unwrap()); - let snap = format!( - "---\nsource: tests/execution_spec.rs\nexpression: response\n---\n{}\n", - serde_json::to_string_pretty(&res) - .expect("Failed to serialize assert.response"), - ); + let snap = format!( + "---\nsource: tests/execution_spec.rs\nexpression: response\n---\n{}\n", + serde_json::to_string_pretty(&res) + .expect("Failed to serialize assert.response"), + ); - f.write_all(snap.as_bytes()) - .expect("Failed to write exception spec"); + f.write_all(snap.as_bytes()) + .expect("Failed to write exception spec"); + } } if !bad_graphql_skip { @@ -293,70 +295,6 @@ async fn main() { generate_merged_snapshot(&file_stem, &config).await; } }; - } else { - println!("TODO: generate client snap for bad graphql skips") - } - - if has_fail_annotation { - let test = std::process::Command::new("cargo") - .env("INSTA_FORCE_PASS", "1") - .args([ - "test", - "--no-fail-fast", - "-p", - "tailcall", - "--test", - "execution_spec", - "--", - execution_dir.join(&md_path).to_str().unwrap(), - ]) - .output() - .expect("Failed to run cargo test"); - - if !test.status.success() { - panic!( - "Running cargo test (needed for fail annotation conversion) failed:\n{}", - String::from_utf8_lossy(&test.stderr) - ); - } - - let mut patched = 0; - - for i in 0..old.assert.len() { - let old = snapshots_dir.join(PathBuf::from(format!( - "execution_spec__{}.md_assert_{}.snap", - file_stem, i - ))); - - let new = snapshots_dir.join(PathBuf::from(format!( - "execution_spec__{}.md_assert_{}.snap.new", - file_stem, i - ))); - - if new.exists() { - std::fs::rename(new, &old).unwrap(); - - let snap = fs::read_to_string(&old) - .expect("Failed to read back snapshot for patching"); - - let lines = snap - .split('\n') - .filter(|x| !x.starts_with("assertion_line")) - .collect::>() - .join("\n"); - - std::fs::write(&old, lines).expect("Failed to write back patched snapshot"); - - patched += 1; - } - } - - if patched == 0 { - panic!( - "Spec {:?} has a fail annotation but all tests passed.", - path - ); - } } files_already_processed.insert(file_stem); @@ -649,8 +587,14 @@ async fn main() { println!("Running prettier..."); + let prettierrc = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../.prettierrc"); let prettier = std::process::Command::new("prettier") - .args(["-c", ".prettierrc", "--write", "tests/execution/*.md"]) + .args([ + "-c", + prettierrc.to_string_lossy().as_ref(), + "--write", + "tests/execution/*.md", + ]) .output() .expect("Failed to run prettier"); diff --git a/tests/execution_spec.rs b/tests/execution_spec.rs index 1993d0fb80..c38b62002c 100644 --- a/tests/execution_spec.rs +++ b/tests/execution_spec.rs @@ -756,7 +756,7 @@ async fn main() -> anyhow::Result<()> { // Explicitly only run one test if specified in command line args // This is used by testconv to auto-apply the snapshots of unconvertable fail-annotated http specs - let explicit = std::env::args().skip(1).find(|x| x != "--color=auto"); + let explicit = std::env::args().skip(1).find(|x| !x.starts_with("--")); let spec = if let Some(explicit) = explicit { let path = PathBuf::from(&explicit) .canonicalize()