Skip to content

Commit

Permalink
test: add stabliity test for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Aug 24, 2024
1 parent a6c2a03 commit 69bd6b3
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 104 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dprint_plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ dprint-plugin-json = { version = "0.19", features = ["wasm"] }
dprint-plugin-typescript = { version = "0.91", features = ["wasm"] }
insta = { version = "1.38", features = ["glob"] }
malva = { version = "0.9", features = ["config_serde"] }
similar-asserts = "1.5"
238 changes: 136 additions & 102 deletions dprint_plugin/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,98 @@
use anyhow::Error;
use insta::{assert_snapshot, glob, Settings};
use markup_fmt::{detect_language, format_text};
use markup_fmt::{detect_language, format_text, FormatError};
use std::{borrow::Cow, fs, path::Path};

#[test]
fn integration_with_dprint_ts_snapshot() {
fn format_with_dprint_ts(input: &str, path: &Path) -> Result<String, FormatError<Error>> {
let options = Default::default();
format_text(
input,
detect_language(path).unwrap(),
&options,
|code, hints| -> anyhow::Result<Cow<str>> {
let ext = hints.ext;
let additional_config =
dprint_plugin_markup::build_additional_config(hints, &options);
if let Some(syntax) = malva::detect_syntax(&Path::new("file").with_extension(ext)) {
malva::format_text(
code,
syntax,
&serde_json::to_value(additional_config)
.and_then(serde_json::from_value)?,
)
.map(Cow::from)
.map_err(Error::from)
} else if ext == "json" {
dprint_plugin_json::format_text(
&Path::new("file").with_extension(ext),
code,
&dprint_plugin_json::configuration::resolve_config(
additional_config,
&Default::default(),
)
.config,
)
.map(|formatted| {
if let Some(formatted) = formatted {
Cow::from(formatted)
} else {
Cow::from(code)
}
})
} else {
dprint_plugin_typescript::format_text(
&Path::new("file").with_extension(ext),
code.to_owned(),
&dprint_plugin_typescript::configuration::resolve_config(
additional_config,
&Default::default(),
)
.config,
)
.map(|formatted| {
if let Some(formatted) = formatted {
Cow::from(formatted)
} else {
Cow::from(code)
}
})
}
},
)
}

glob!(
"integration/**/*.{html,vue,svelte,astro,jinja,njk,vto}",
|path| {
let input = fs::read_to_string(path).unwrap();
let options = Default::default();

let output = format_text(
&input,
detect_language(path).unwrap(),
&options,
|code, hints| -> anyhow::Result<Cow<str>> {
let ext = hints.ext;
let additional_config =
dprint_plugin_markup::build_additional_config(hints, &options);
if let Some(syntax) =
malva::detect_syntax(&Path::new("file").with_extension(ext))
{
malva::format_text(
code,
syntax,
&serde_json::to_value(additional_config)
.and_then(serde_json::from_value)?,
)
.map(Cow::from)
.map_err(Error::from)
} else if ext == "json" {
dprint_plugin_json::format_text(
&Path::new("file").with_extension(ext),
code,
&dprint_plugin_json::configuration::resolve_config(
additional_config,
&Default::default(),
)
.config,
)
.map(|formatted| {
if let Some(formatted) = formatted {
Cow::from(formatted)
} else {
Cow::from(code)
}
})
} else {
dprint_plugin_typescript::format_text(
&Path::new("file").with_extension(ext),
code.to_owned(),
&dprint_plugin_typescript::configuration::resolve_config(
additional_config,
&Default::default(),
)
.config,
)
.map(|formatted| {
if let Some(formatted) = formatted {
Cow::from(formatted)
} else {
Cow::from(code)
}
})
}
},
)
.map_err(|err| format!("failed to format '{}': {:?}", path.display(), err))
.unwrap();
let output = format_with_dprint_ts(&input, path)
.map_err(|err| format!("failed to format '{}': {:?}", path.display(), err))
.unwrap();

assert!(
output.ends_with('\n'),
"formatted output should contain trailing newline: {}",
path.display()
);

let regression_format = format_with_dprint_ts(&output, path)
.map_err(|err| {
format!(
"syntax error in stability test '{}': {:?}",
path.display(),
err
)
})
.unwrap();
similar_asserts::assert_eq!(
output,
regression_format,
"'{}' format is unstable",
path.display()
);

build_settings(path.parent().unwrap().join("dprint_ts")).bind(|| {
let name = path.file_name().unwrap().to_str().unwrap();
assert_snapshot!(name, output);
Expand All @@ -86,6 +103,45 @@ fn integration_with_dprint_ts_snapshot() {

#[test]
fn integration_with_biome_snapshot() {
fn format_with_biome(input: &str, path: &Path) -> Result<String, FormatError<Error>> {
let options = Default::default();
format_text(
&input,
detect_language(path).unwrap(),
&options,
|code, hints| -> anyhow::Result<Cow<str>> {
let ext = hints.ext;
let additional_config =
dprint_plugin_markup::build_additional_config(hints, &options);
if let Some(syntax) = malva::detect_syntax(&Path::new("file").with_extension(ext)) {
malva::format_text(
code,
syntax,
&serde_json::to_value(additional_config)
.and_then(serde_json::from_value)?,
)
.map(Cow::from)
.map_err(Error::from)
} else {
dprint_plugin_biome::format_text(
&Path::new("file").with_extension(ext),
code,
&serde_json::to_value(additional_config)
.and_then(serde_json::from_value)
.unwrap_or_default(),
)
.map(|formatted| {
if let Some(formatted) = formatted {
Cow::from(formatted)
} else {
Cow::from(code)
}
})
}
},
)
}

glob!(
"integration/**/*.{html,vue,svelte,astro,jinja,njk,vto}",
|path| {
Expand All @@ -97,54 +153,32 @@ fn integration_with_biome_snapshot() {
}

let input = fs::read_to_string(path).unwrap();
let options = Default::default();

let output = format_text(
&input,
detect_language(path).unwrap(),
&options,
|code, hints| -> anyhow::Result<Cow<str>> {
let ext = hints.ext;
let additional_config =
dprint_plugin_markup::build_additional_config(hints, &options);
if let Some(syntax) =
malva::detect_syntax(&Path::new("file").with_extension(ext))
{
malva::format_text(
code,
syntax,
&serde_json::to_value(additional_config)
.and_then(serde_json::from_value)?,
)
.map(Cow::from)
.map_err(Error::from)
} else {
dprint_plugin_biome::format_text(
&Path::new("file").with_extension(ext),
code,
&serde_json::to_value(additional_config)
.and_then(serde_json::from_value)
.unwrap_or_default(),
)
.map(|formatted| {
if let Some(formatted) = formatted {
Cow::from(formatted)
} else {
Cow::from(code)
}
})
}
},
)
.map_err(|err| format!("failed to format '{}': {:?}", path.display(), err))
.unwrap();
let output = format_with_biome(&input, path)
.map_err(|err| format!("failed to format '{}': {:?}", path.display(), err))
.unwrap();

assert!(
output.ends_with('\n'),
"formatted output should contain trailing newline: {}",
path.display()
);

let regression_format = format_with_biome(&output, path)
.map_err(|err| {
format!(
"syntax error in stability test '{}': {:?}",
path.display(),
err
)
})
.unwrap();
similar_asserts::assert_eq!(
output,
regression_format,
"'{}' format is unstable",
path.display()
);

build_settings(path.parent().unwrap().join("biome")).bind(|| {
let name = path.file_name().unwrap().to_str().unwrap();
assert_snapshot!(name, output);
Expand Down

0 comments on commit 69bd6b3

Please sign in to comment.