use super::*;
const CMD_SETUP_TEMPLATE: &str = include_str!("../../../backends/go/templates/cmd_setup_main.go.jinja");
fn const_assignment_column(line: &str) -> Option<usize> {
let trimmed = line.strip_prefix('\t')?;
if !trimmed.starts_with(|c: char| c.is_ascii_alphabetic()) {
return None;
}
let equals = trimmed.find('=')?;
trimmed[..equals].split_whitespace().count().eq(&1).then_some(equals)
}
fn version_ident_const_block(template: &str) -> Vec<&str> {
let mut lines = template.lines().skip_while(|line| !line.starts_with("const ("));
lines.next();
lines.take_while(|line| !line.starts_with(')')).collect()
}
#[test]
fn cmd_setup_template_const_block_is_gofmt_aligned() {
let columns: Vec<(usize, &str)> = version_ident_const_block(CMD_SETUP_TEMPLATE)
.into_iter()
.filter_map(|line| const_assignment_column(line).map(|column| (column, line)))
.collect();
assert!(
columns.len() >= 5,
"expected the cmd/setup const block to be found in the template; got {} aligned lines",
columns.len()
);
let (first_column, first_line) = columns[0];
for (column, line) in &columns {
assert_eq!(
*column, first_column,
"cmd_setup_main.go.jinja's const block must be gofmt-aligned:\n {first_line}\n {line}"
);
}
assert!(
columns.iter().any(|(_, line)| line.contains("versionIdent")),
"versionIdent must be one of the aligned const lines"
);
}
#[test]
fn sync_go_cmd_setup_version_ident_preserves_gofmt_alignment() {
let rendered = CMD_SETUP_TEMPLATE.replace("{{ version_ident }}", "1_15_5");
let before = rendered
.lines()
.find(|line| line.contains("versionIdent"))
.expect("template declares versionIdent")
.to_string();
let updated = sync_go_cmd_setup_version_ident(&rendered, "1_15_6").expect("value changed, so a rewrite happened");
let after = updated
.lines()
.find(|line| line.contains("versionIdent"))
.expect("versionIdent survives the rewrite")
.to_string();
assert_eq!(
after,
before.replace("1_15_5", "1_15_6"),
"only the quoted value may change; the alignment padding around `=` must be replayed \
verbatim, or the regenerated cmd/setup/main.go fails `gofmt -l`"
);
assert_eq!(
const_assignment_column(&after),
const_assignment_column(&before),
"the `=` column must not move"
);
}
#[test]
fn sync_go_cmd_setup_version_ident_is_idempotent_on_current_content() {
let rendered = CMD_SETUP_TEMPLATE.replace("{{ version_ident }}", "1_15_6");
assert_eq!(
sync_go_cmd_setup_version_ident(&rendered, "1_15_6"),
None,
"content already carrying the target identifier must be left untouched"
);
}