mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-16 23:31:24 +01:00
113 lines
3 KiB
Rust
113 lines
3 KiB
Rust
use std::{
|
|
path::{Path, PathBuf},
|
|
process::Output,
|
|
};
|
|
|
|
use assert_cmd::Command;
|
|
|
|
type TestError = Box<dyn std::error::Error>;
|
|
type TestResult = Result<(), TestError>;
|
|
|
|
fn fixture_path<P>(name: P) -> PathBuf
|
|
where
|
|
P: AsRef<Path>,
|
|
{
|
|
PathBuf::from("tests/integrations/fixtures/check_config").join(name)
|
|
}
|
|
|
|
fn run<P>(file: P) -> Result<Output, TestError>
|
|
where
|
|
P: AsRef<Path>,
|
|
{
|
|
Command::cargo_bin("grapevine")?
|
|
.args(["check-config", "--log-format=json", "-c"])
|
|
.arg(fixture_path(file))
|
|
.output()
|
|
.map_err(Into::into)
|
|
}
|
|
|
|
macro_rules! make_snapshot_test {
|
|
($name:ident, $description:expr, $fixture_name:expr $(,)?) => {
|
|
#[test]
|
|
fn $name() -> TestResult {
|
|
let output = run($fixture_name)?;
|
|
|
|
let stdout = String::from_utf8(output.stdout)?;
|
|
let stderr = String::from_utf8(output.stderr)?;
|
|
let status_code = output.status.code();
|
|
|
|
insta::with_settings!({
|
|
description => $description,
|
|
omit_expression => true,
|
|
snapshot_suffix => "stdout",
|
|
}, {
|
|
insta::assert_snapshot!(stdout);
|
|
});
|
|
|
|
let stderr_parse = serde_json::Deserializer::from_str(&stderr)
|
|
.into_iter::<serde_json::Value>()
|
|
.collect::<Result<Vec<_>, _>>();
|
|
insta::with_settings!({
|
|
description => $description,
|
|
omit_expression => true,
|
|
snapshot_suffix => "stderr",
|
|
}, {
|
|
if let Ok(stderr_json) = stderr_parse {
|
|
insta::assert_json_snapshot!(stderr_json, {
|
|
".*.timestamp" => "[timestamp]"
|
|
});
|
|
} else {
|
|
insta::assert_snapshot!(stderr);
|
|
}
|
|
});
|
|
|
|
insta::with_settings!({
|
|
description => $description,
|
|
omit_expression => true,
|
|
snapshot_suffix => "status_code",
|
|
}, {
|
|
insta::assert_debug_snapshot!(status_code);
|
|
});
|
|
|
|
Ok(())
|
|
}
|
|
};
|
|
}
|
|
|
|
make_snapshot_test!(valid_config, "A normal config is valid", "valid.toml");
|
|
|
|
make_snapshot_test!(
|
|
minimal_valid_config,
|
|
"A configuration containing only the required keys is valid",
|
|
"minimal-valid.toml",
|
|
);
|
|
|
|
make_snapshot_test!(
|
|
invalid_keys,
|
|
"A config with invalid keys fails",
|
|
"invalid-keys.toml",
|
|
);
|
|
|
|
make_snapshot_test!(
|
|
invalid_values,
|
|
"A config with invalid values fails",
|
|
"invalid-values.toml",
|
|
);
|
|
|
|
make_snapshot_test!(
|
|
overlapping_paths_equal,
|
|
"A config with equal paths fails",
|
|
"equal-paths.toml",
|
|
);
|
|
|
|
make_snapshot_test!(
|
|
overlapping_paths_media,
|
|
"A config with the media path inside the database path fails",
|
|
"media-in-database.toml",
|
|
);
|
|
|
|
make_snapshot_test!(
|
|
overlapping_paths_database,
|
|
"A config with the database path inside the media path fails",
|
|
"database-in-media.toml",
|
|
);
|