use super::handle;
use crate::bin_cli::args::Commands;
use crate::bin_cli::dispatch::DispatchContext;
use crate::test_support::{CwdGuard, PathWithoutToolGuard, RealCargoGuard};
const FIXTURE_SOURCE: &str = "pub fn compute_widget_totals(\n \
alpha: String,\n bravo: String,\n charlie: String,\n delta: String,\n echo: String,\n \
foxtrot: String,\n golf: String,\n hotel: String,\n india: String,\n juliett: String,\n\
) -> String {\n \
format!(\"{alpha}{bravo}{charlie}{delta}{echo}{foxtrot}{golf}{hotel}{india}{juliett}\")\n\
}\n";
const FIXTURE_CARGO_TOML: &str = "[package]\nname = \"test-lib\"\nversion = \"0.1.0\"\nedition = \"2024\"\n";
const FIXTURE_ALEF_TOML: &str = r#"
[workspace]
languages = ["python"]
[[crates]]
name = "test-lib"
sources = ["src/lib.rs"]
version_from = "Cargo.toml"
[crates.python]
module_name = "test_lib"
[crates.python.stubs]
output = "packages/python/test_lib"
"#;
fn write_fixture(root: &std::path::Path) {
std::fs::create_dir_all(root.join("src")).expect("create fixture src directory");
std::fs::write(root.join("src/lib.rs"), FIXTURE_SOURCE).expect("write fixture source");
std::fs::write(root.join("Cargo.toml"), FIXTURE_CARGO_TOML).expect("write fixture Cargo.toml");
std::fs::write(root.join("alef.toml"), FIXTURE_ALEF_TOML).expect("write fixture alef.toml");
}
fn all_command() -> Commands {
Commands::All {
clean: false,
clobber_create_once_seeds: false,
strict: false,
skip_frb: false,
skip_snippet_validation: false,
skip_compile: true,
}
}
#[test]
fn all_writes_rustfmt_toml_before_bindings_so_the_first_run_is_already_formatted() {
if !crate::cli::pipeline::is_tool_available("rustfmt") {
return;
}
let temp = tempfile::tempdir().expect("tempdir");
let root = temp.path().canonicalize().unwrap_or_else(|_| temp.path().to_path_buf());
write_fixture(&root);
assert!(
!root.join("rustfmt.toml").exists(),
"fixture setup: this run must start with no committed rustfmt.toml, or it is not \
exercising a project's first `alef all` run at all"
);
let _cargo_guard = RealCargoGuard::acquire();
let _no_poly = PathWithoutToolGuard::exclude("poly");
let _cwd = CwdGuard::enter(&root);
let context = DispatchContext {
config_path: root.join("alef.toml"),
crate_filter: Vec::new(),
};
handle(all_command(), &context).expect("alef all must succeed against a plain python fixture");
let generated_path = root.join("crates/test-lib-py/src/lib.rs");
let generated = std::fs::read_to_string(&generated_path)
.unwrap_or_else(|e| panic!("read generated bindings at {}: {e}", generated_path.display()));
let longest_line = generated.lines().map(str::len).max().unwrap_or(0);
assert!(
longest_line > 100,
"longest generated line was {longest_line} columns (expected > 100) -- this fixture is \
built so a correctly max_width=120-formatted file needs more than 100 columns \
somewhere; a shorter result means rustfmt's default max_width=100 was used instead of \
the project's own rustfmt.toml, got:\n{generated}"
);
assert!(
longest_line <= 120,
"longest generated line was {longest_line} columns (expected <= 120) -- the project's \
own rustfmt.toml sets max_width = 120, so nothing in a correctly formatted file may \
exceed it; a much longer line means the bindings landed unformatted (alef #465), got:\n{generated}"
);
}