Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Refactor test helpers to reduce boilerplate

Maximilian Roosd76848f7bd80
diff --git a/src/commands/list/json_output.rs b/src/commands/list/json_output.rs
index 9f573ea74..62f1d0b81 100644
--- a/src/commands/list/json_output.rs
+++ b/src/commands/list/json_output.rs
@@ -577,7 +577,11 @@ mod tests {
#[test]
fn test_upstream_to_json_with_remote() {
- let upstream = UpstreamStatus::from_parts(Some("origin".to_string()), 3, 2);
+ let upstream = UpstreamStatus {
+ remote: Some("origin".to_string()),
+ ahead: 3,
+ behind: 2,
+ };
let branch = Some("feature".to_string());
let json = upstream_to_json(&upstream, &branch);
assert!(json.is_some());
@@ -590,7 +594,11 @@ mod tests {
#[test]
fn test_upstream_to_json_no_remote() {
- let upstream = UpstreamStatus::from_parts(None, 0, 0);
+ let upstream = UpstreamStatus {
+ remote: None,
+ ahead: 0,
+ behind: 0,
+ };
let branch = Some("feature".to_string());
let json = upstream_to_json(&upstream, &branch);
assert!(json.is_none());
@@ -598,7 +606,11 @@ mod tests {
#[test]
fn test_upstream_to_json_no_branch() {
- let upstream = UpstreamStatus::from_parts(Some("origin".to_string()), 1, 0);
+ let upstream = UpstreamStatus {
+ remote: Some("origin".to_string()),
+ ahead: 1,
+ behind: 0,
+ };
let branch = None;
let json = upstream_to_json(&upstream, &branch);
assert!(json.is_some());
diff --git a/src/commands/list/layout.rs b/src/commands/list/layout.rs
index ef96e71fc..a33256a2f 100644
--- a/src/commands/list/layout.rs
+++ b/src/commands/list/layout.rs
@@ -1211,7 +1211,11 @@ mod tests {
would_merge_add: None,
is_ancestor: None,
is_orphan: None,
- upstream: Some(UpstreamStatus::from_parts(Some("origin".to_string()), 4, 2)),
+ upstream: Some(UpstreamStatus {
+ remote: Some("origin".to_string()),
+ ahead: 4,
+ behind: 2,
+ }),
pr_status: None,
url: None,
url_active: None,
diff --git a/src/commands/list/model.rs b/src/commands/list/model.rs
index 5923a5364..6b7591a70 100644
--- a/src/commands/list/model.rs
+++ b/src/commands/list/model.rs
@@ -297,15 +297,6 @@ impl UpstreamStatus {
behind: self.behind,
})
}
-
- #[cfg(test)]
- pub(crate) fn from_parts(remote: Option<String>, ahead: usize, behind: usize) -> Self {
- Self {
- remote,
- ahead,
- behind,
- }
- }
}
/// Unified item for displaying worktrees and branches in the same table
@@ -1979,7 +1970,11 @@ mod tests {
#[test]
fn test_upstream_status_active_with_remote() {
- let status = UpstreamStatus::from_parts(Some("origin".to_string()), 3, 2);
+ let status = UpstreamStatus {
+ remote: Some("origin".to_string()),
+ ahead: 3,
+ behind: 2,
+ };
let active = status.active().unwrap();
assert_eq!(active.remote, "origin");
assert_eq!(active.ahead, 3);
@@ -1988,7 +1983,11 @@ mod tests {
#[test]
fn test_upstream_status_active_no_remote() {
- let status = UpstreamStatus::from_parts(None, 0, 0);
+ let status = UpstreamStatus {
+ remote: None,
+ ahead: 0,
+ behind: 0,
+ };
assert!(status.active().is_none());
}
diff --git a/src/main.rs b/src/main.rs
index f133d3480..4a0ea809b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1773,10 +1773,8 @@ fn main() {
let _ = output::print(format_with_gutter(&chain_text, None));
} else if msg.contains('\n') {
// Multiline error without context - this shouldn't happen if all
- // errors have proper context. Fail in tests, log in production.
- if cfg!(test) {
- panic!("Multiline error without context: {msg}");
- }
+ // errors have proper context. Catch in debug builds, log in release.
+ debug_assert!(false, "Multiline error without context: {msg}");
log::warn!("Multiline error without context: {msg}");
let _ = output::print(error_message("Command failed"));
let _ = output::print(format_with_gutter(&msg, None));
diff --git a/src/md_help.rs b/src/md_help.rs
index 0239d6ce6..163846b84 100644
--- a/src/md_help.rs
+++ b/src/md_help.rs
@@ -30,12 +30,6 @@ fn help_table_skin() -> MadSkin {
skin
}
-/// Render markdown in help text to ANSI without prose wrapping
-#[cfg(test)]
-fn render_markdown_in_help(help: &str) -> String {
- render_markdown_in_help_with_width(help, None)
-}
-
/// Render markdown in help text to ANSI with minimal styling (green headers only)
///
/// If `width` is provided, prose text is wrapped to that width. Tables, code blocks,
@@ -385,6 +379,11 @@ fn colorize_status_symbols(text: &str) -> String {
mod tests {
use super::*;
+ /// Test helper: render markdown without prose wrapping
+ fn render_markdown_in_help(help: &str) -> String {
+ render_markdown_in_help_with_width(help, None)
+ }
+
#[test]
fn test_render_inline_formatting_strips_links() {
assert_eq!(render_inline_formatting("[text](url)"), "text");
diff --git a/src/styling/mod.rs b/src/styling/mod.rs
index 8fc6061af..769917db6 100644
--- a/src/styling/mod.rs
+++ b/src/styling/mod.rs
@@ -84,9 +84,6 @@ pub fn fix_dim_after_color_reset(s: &str) -> String {
s.replace("\x1b[39m\x1b[2m", "\x1b[0m\x1b[2m")
}
-// Re-export for tests
-#[cfg(test)]
-use format::wrap_text_at_width;
// ============================================================================
// Tests
// ============================================================================
@@ -228,14 +225,14 @@ command = "npm install"
// Word-wrapping tests
#[test]
fn test_wrap_text_no_wrapping_needed() {
- let result = super::wrap_text_at_width("short line", 50);
+ let result = super::format::wrap_text_at_width("short line", 50);
assert_eq!(result, vec!["short line"]);
}
#[test]
fn test_wrap_text_at_word_boundary() {
let text = "This is a very long line that needs to be wrapped at word boundaries";
- let result = super::wrap_text_at_width(text, 30);
+ let result = super::format::wrap_text_at_width(text, 30);
// Should wrap at word boundaries
assert!(result.len() > 1);
@@ -261,14 +258,14 @@ command = "npm install"
#[test]
fn test_wrap_text_single_long_word() {
// A single word longer than max_width should still be included
- let result = super::wrap_text_at_width("verylongwordthatcannotbewrapped", 10);
+ let result = super::format::wrap_text_at_width("verylongwordthatcannotbewrapped", 10);
assert_eq!(result.len(), 1);
assert_eq!(result[0], "verylongwordthatcannotbewrapped");
}
#[test]
fn test_wrap_text_empty_input() {
- let result = super::wrap_text_at_width("", 50);
+ let result = super::format::wrap_text_at_width("", 50);
assert_eq!(result, vec![""]);
}
@@ -276,7 +273,7 @@ command = "npm install"
fn test_wrap_text_unicode() {
// Unicode characters should be handled correctly by width
let text = "This line has emoji 🎉 and should wrap correctly when needed";
- let result = super::wrap_text_at_width(text, 30);
+ let result = super::format::wrap_text_at_width(text, 30);
// Should wrap
assert!(result.len() > 1);