use super::*;
const MANIFEST: &str = "[package]\nname = \"demo\"\nedition = \"2024\"\n\n\
[dependencies]\naxum = \"0.8.9\"\nrahti = \"0.0.7\"\n\n\
[build-dependencies]\nrahti-build = \"0.0.7\"\n";
#[test]
fn a_dependency_lands_in_the_dependency_table() {
let [(name, line), _] = t::sea_orm_dependencies(Backend::Sqlite);
let out = with_dependency(MANIFEST, name, &line).expect("an amended manifest");
let dependencies = out
.split("[build-dependencies]")
.next()
.expect("the first table");
assert!(dependencies.contains("sea-orm ="), "{out}");
assert!(out.contains("sqlx-sqlite"), "{out}");
assert!(out.contains("axum = \"0.8.9\""), "{out}");
assert!(out.contains("rahti = \"0.0.7\""), "{out}");
assert!(out.contains("[build-dependencies]\nrahti-build"), "{out}");
assert!(out.ends_with('\n'));
}
#[test]
fn a_dependency_that_is_there_is_not_added_again() {
let [(name, line), _] = t::sea_orm_dependencies(Backend::Sqlite);
let once = with_dependency(MANIFEST, name, &line).expect("an amended manifest");
assert_eq!(with_dependency(&once, name, &line), None);
}
#[test]
fn a_longer_name_is_not_the_dependency() {
let manifest = "[dependencies]\nsea-orm-migration = \"2\"\n";
assert!(!declares(manifest, "sea-orm"));
assert!(declares(manifest, "sea-orm-migration"));
}
#[test]
fn a_dependency_table_counts_as_declared() {
assert!(declares(
"[dependencies.sea-orm]\nversion = \"2\"\n",
"sea-orm"
));
}
#[test]
fn a_sub_table_ends_the_dependency_table() {
let manifest = "[dependencies]\naxum = \"0.8.9\"\n\n\
[dependencies.rahti]\nversion = \"0.0.7\"\n";
let out = with_dependency(manifest, "sea-orm", "sea-orm = \"2\"").expect("an amended manifest");
let before = out.split("[dependencies.rahti]").next().expect("the table");
assert!(before.contains("sea-orm = \"2\""), "{out}");
}
#[test]
fn a_manifest_without_the_table_gains_one() {
let out = with_dependency("[package]\nname = \"demo\"\n", "sea-orm", "sea-orm = \"2\"")
.expect("an amended manifest");
assert!(out.contains("[dependencies]\nsea-orm = \"2\""), "{out}");
}
#[test]
fn the_ws_feature_is_folded_into_a_bare_version() {
let out = with_ws_feature(MANIFEST)
.expect("a readable manifest")
.expect("an amended manifest");
assert!(
out.contains("rahti = { version = \"0.0.7\", features = [\"ws\"] }"),
"{out}"
);
assert!(out.contains("rahti-build = \"0.0.7\""), "{out}");
}
#[test]
fn the_ws_feature_is_folded_into_a_path_dependency() {
let manifest = "[dependencies]\nrahti = { path = \"/checkout/crates/rahti\" }\n";
let out = with_ws_feature(manifest)
.expect("a readable manifest")
.expect("an amended manifest");
assert!(
out.contains("rahti = { path = \"/checkout/crates/rahti\", features = [\"ws\"] }"),
"{out}"
);
}
#[test]
fn an_existing_feature_list_keeps_what_is_in_it() {
let manifest = "[dependencies]\nrahti = { version = \"0.0.7\", features = [\"other\"] }\n";
let out = with_ws_feature(manifest)
.expect("a readable manifest")
.expect("an amended manifest");
assert!(out.contains("\"ws\""), "{out}");
assert!(out.contains("\"other\""), "{out}");
}
#[test]
fn the_ws_feature_is_not_added_twice() {
let manifest = "[dependencies]\nrahti = { version = \"0.0.7\", features = [\"ws\"] }\n";
assert_eq!(
with_ws_feature(manifest).expect("a readable manifest"),
None
);
}
#[test]
fn an_unreadable_dependency_is_refused_rather_than_mangled() {
assert!(with_ws_feature("[dependencies]\naxum = \"0.8.9\"\n").is_err());
assert!(with_ws_feature("[dependencies.rahti]\nversion = \"0.0.7\"\n").is_err());
assert!(
with_ws_feature("[dependencies]\nrahti = {\n version = \"0.0.7\",\n}\n").is_err(),
"a dependency spread over several lines is not a value to rewrite"
);
}
#[test]
fn the_connection_string_is_added_above_what_is_there() {
let existing = "AUTH_SECRET=\"abc\"\n";
let out = with_database_url(existing, Backend::Postgres).expect("an amended file");
assert!(out.contains("DATABASE_URL=\"postgres://"), "{out}");
assert!(out.ends_with(existing), "{out}");
assert_eq!(with_database_url(&out, Backend::Postgres), None);
}
#[test]
fn an_existing_connection_string_is_left_alone() {
let env = "DATABASE_URL=\"postgres://elsewhere/db\"\n";
assert_eq!(with_database_url(env, Backend::Sqlite), None);
}