Skip to content

Commit 2a6ee05

Browse files
max-sixtyclaude
andauthored
test(templates): add tests for prefix stripping with built-in Jinja (#903)
Documents that minijinja's built-in `replace` filter and slicing syntax can be used to strip prefixes from branch names in worktree-path templates, addressing the use case in #900 without adding custom filters. Closes #900 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3f0cce8 commit 2a6ee05

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/config/expansion.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,87 @@ mod tests {
608608
);
609609
}
610610

611+
#[test]
612+
fn test_expand_template_strip_prefix() {
613+
let test = test_repo();
614+
let mut vars = HashMap::new();
615+
616+
// Built-in replace filter strips prefix (replaces all occurrences)
617+
vars.insert("branch", "feature/foo");
618+
assert_eq!(
619+
expand_template(
620+
"{{ branch | replace('feature/', '') }}",
621+
&vars,
622+
false,
623+
&test.repo,
624+
"test"
625+
)
626+
.unwrap(),
627+
"foo"
628+
);
629+
630+
// Replace + sanitize for worktree paths
631+
assert_eq!(
632+
expand_template(
633+
"{{ branch | replace('feature/', '') | sanitize }}",
634+
&vars,
635+
false,
636+
&test.repo,
637+
"test"
638+
)
639+
.unwrap(),
640+
"foo"
641+
);
642+
643+
// Branch without prefix passes through unchanged
644+
vars.insert("branch", "main");
645+
assert_eq!(
646+
expand_template(
647+
"{{ branch | replace('feature/', '') }}",
648+
&vars,
649+
false,
650+
&test.repo,
651+
"test"
652+
)
653+
.unwrap(),
654+
"main"
655+
);
656+
657+
// Slicing for prefix-only removal (avoids replacing mid-string)
658+
vars.insert("branch", "feature/nested/feature/deep");
659+
assert_eq!(
660+
expand_template("{{ branch[8:] }}", &vars, false, &test.repo, "test").unwrap(),
661+
"nested/feature/deep"
662+
);
663+
664+
// Conditional slicing for safe prefix removal
665+
assert_eq!(
666+
expand_template(
667+
"{% if branch[:8] == 'feature/' %}{{ branch[8:] }}{% else %}{{ branch }}{% endif %}",
668+
&vars,
669+
false,
670+
&test.repo,
671+
"test"
672+
)
673+
.unwrap(),
674+
"nested/feature/deep"
675+
);
676+
677+
// Conditional passes through non-matching branches
678+
vars.insert("branch", "bugfix/bar");
679+
assert_eq!(
680+
expand_template(
681+
"{% if branch[:8] == 'feature/' %}{{ branch[8:] }}{% else %}{{ branch }}{% endif %}",
682+
&vars,
683+
false,
684+
&test.repo,
685+
"test"
686+
)
687+
.unwrap(),
688+
"bugfix/bar"
689+
);
690+
}
691+
611692
#[test]
612693
fn test_expand_template_sanitize_filter() {
613694
let test = test_repo();

0 commit comments

Comments
 (0)