use std::path ::PathBuf;
fn check_compile_fails( code : &str, features : &[ &str ] ) -> bool
{
let manifest_dir = env!( "CARGO_MANIFEST_DIR" );
let target_root = PathBuf::from( manifest_dir ).join( "target" ).join( "feature_gate_tests" );
let src_dir = target_root.join( format!( "src_{}", std::process::id() ) );
std::fs::create_dir_all( src_dir.join( "src" ) ).expect( "create temp src dir" );
let features_str = features
.iter()
.map( |f| format!( "\"{f}\"" ) )
.collect ::< Vec< _ > >()
.join( ", " );
let cargo_toml = format!(
"[package]\n\
name = \"feature_gate_check\"\n\
version = \"0.1.0\"\n\
edition = \"2021\"\n\
\n\
[dependencies]\n\
collection_tools = {{ path = \"{manifest_dir}\", default-features = false, features = [{features_str}] }}\n",
);
std::fs::write( src_dir.join( "Cargo.toml" ), &cargo_toml ).expect( "write temp Cargo.toml" );
std::fs::write( src_dir.join( "src/main.rs" ), code ).expect( "write temp main.rs" );
let cargo = std::env ::var( "CARGO" ).unwrap_or_else( |_| "cargo".to_owned() );
let output = std::process ::Command ::new( &cargo )
.args
( [
"check",
"--manifest-path",
&src_dir.join( "Cargo.toml" ).to_string_lossy(),
] )
.env( "CARGO_TARGET_DIR", target_root.join( "build" ) )
.output()
.expect( "cargo check invocation failed" );
let _ = std::fs ::remove_dir_all( &src_dir );
!output.status.success()
}
#[ test ]
fn strict_macros_absent_without_collection_constructors()
{
assert!(
check_compile_fails(
"fn main() { let _ = collection_tools ::vec![ 1, 2, 3 ]; }",
&[ "enabled" ],
),
"collection_tools::vec! must not compile without collection_constructors feature"
);
}
#[ test ]
fn strict_macros_absent_with_only_into_feature()
{
assert!(
check_compile_fails(
"fn main() { let _ = collection_tools ::vec![ 1, 2, 3 ]; }",
&[ "enabled", "collection_into_constructors" ],
),
"collection_tools::vec! must not compile without collection_constructors, even when collection_into_constructors is on"
);
}
#[ test ]
fn into_macros_absent_with_only_strict_feature()
{
assert!(
check_compile_fails(
"fn main() { let _: std::collections::HashMap< &str, i32 > = collection_tools ::into_hmap!{ \"a\" => 1 }; }",
&[ "enabled", "collection_constructors" ],
),
"collection_tools::into_hmap! must not compile without collection_into_constructors, even when collection_constructors is on"
);
}