Skip to content

Commit 9645b9b

Browse files
committed
Move all unit tests from src/ to tests/ directory and add 99 unit tests
- Moved all #[cfg(test)] blocks from src/ modules to corresponding tests/ files - Created new test files: test_cli.rs and test_color_graph.rs - Added comprehensive unit tests for helper functions across all modules - Now have 99 total unit tests organized in tests/ directory - Updated health.rs to make is_git_repo() function public with path parameter - All unit tests properly test extracted helper functions for better coverage - Improved code organization with clear separation between source and tests Unit test breakdown by module: - test_cli.rs: 14 tests (argument parsing) - test_summary.rs: 13 tests (git log parsing, formatting) - test_rename_branch.rs: 11 tests (branch operations) - test_prune_branches.rs: 11 tests (branch protection, deletion) - test_clean_branches.rs: 10 tests (merged branch cleanup) - test_health.rs: 10 tests (repository health checks) - test_what.rs: 8 tests (diff analysis, commit parsing) - test_info.rs: 6 tests (repository information) - test_color_graph.rs: 4 tests (colorized git log) - test_graph.rs: 4 tests (git log formatting) - test_since.rs: 4 tests (commit range analysis) - test_undo.rs: 4 tests (git reset operations)
1 parent 160d4f2 commit 9645b9b

23 files changed

Lines changed: 718 additions & 623 deletions

src/clean_branches.rs

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -86,68 +86,3 @@ pub fn format_deletion_summary(count: usize, dry_run: bool) -> String {
8686
}
8787
}
8888

89-
#[cfg(test)]
90-
mod tests {
91-
use super::*;
92-
93-
#[test]
94-
fn test_get_git_branch_args() {
95-
assert_eq!(get_git_branch_args(), ["branch", "--merged"]);
96-
}
97-
98-
#[test]
99-
fn test_get_protected_branches() {
100-
let protected = get_protected_branches();
101-
assert_eq!(protected, vec!["main", "master", "develop"]);
102-
}
103-
104-
#[test]
105-
fn test_clean_branch_name() {
106-
assert_eq!(clean_branch_name(" feature/test "), "feature/test");
107-
assert_eq!(clean_branch_name("* main"), "main");
108-
assert_eq!(clean_branch_name(" * develop "), "develop");
109-
assert_eq!(clean_branch_name("bugfix/123"), "bugfix/123");
110-
}
111-
112-
#[test]
113-
fn test_is_protected_branch() {
114-
assert!(is_protected_branch("main"));
115-
assert!(is_protected_branch("master"));
116-
assert!(is_protected_branch("develop"));
117-
assert!(!is_protected_branch("feature/test"));
118-
assert!(!is_protected_branch("hotfix/123"));
119-
}
120-
121-
#[test]
122-
fn test_get_git_delete_args() {
123-
assert_eq!(
124-
get_git_delete_args("feature"),
125-
vec!["branch".to_string(), "-d".to_string(), "feature".to_string()]
126-
);
127-
}
128-
129-
#[test]
130-
fn test_format_dry_run_message() {
131-
assert_eq!(
132-
format_dry_run_message("feature/test"),
133-
"(dry run) Would delete: feature/test"
134-
);
135-
}
136-
137-
#[test]
138-
fn test_format_no_branches_message() {
139-
assert_eq!(format_no_branches_message(), "No merged branches to delete.");
140-
}
141-
142-
#[test]
143-
fn test_format_deletion_summary() {
144-
assert_eq!(
145-
format_deletion_summary(3, true),
146-
"🧪 (dry run) 3 branches would be deleted:"
147-
);
148-
assert_eq!(
149-
format_deletion_summary(2, false),
150-
"🧹 Deleted 2 merged branches:"
151-
);
152-
}
153-
}

src/color_graph.rs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -53,59 +53,3 @@ pub fn format_color_git_error(stderr: &str) -> String {
5353
format!("❌ git log failed:\n{stderr}")
5454
}
5555

56-
#[cfg(test)]
57-
mod tests {
58-
use super::*;
59-
use std::process::{Output, ExitStatus};
60-
61-
#[test]
62-
fn test_get_color_git_log_args() {
63-
let args = get_color_git_log_args();
64-
assert_eq!(args[0], "log");
65-
assert_eq!(args[1], "--oneline");
66-
assert_eq!(args[2], "--graph");
67-
assert_eq!(args[3], "--decorate");
68-
assert_eq!(args[4], "--all");
69-
assert_eq!(args[5], "--color=always");
70-
assert!(args[6].contains("--pretty=format:"));
71-
assert!(args[6].contains("%C(auto)"));
72-
}
73-
74-
#[test]
75-
fn test_format_color_git_error() {
76-
assert_eq!(
77-
format_color_git_error("not a git repository"),
78-
"❌ git log failed:\nnot a git repository"
79-
);
80-
assert_eq!(
81-
format_color_git_error("permission denied"),
82-
"❌ git log failed:\npermission denied"
83-
);
84-
}
85-
86-
#[test]
87-
fn test_is_command_successful() {
88-
use std::os::unix::process::ExitStatusExt;
89-
90-
let success_output = Output {
91-
status: ExitStatus::from_raw(0),
92-
stdout: vec![],
93-
stderr: vec![],
94-
};
95-
assert!(is_command_successful(&success_output));
96-
97-
let failure_output = Output {
98-
status: ExitStatus::from_raw(256), // Exit code 1
99-
stdout: vec![],
100-
stderr: vec![],
101-
};
102-
assert!(!is_command_successful(&failure_output));
103-
}
104-
105-
#[test]
106-
fn test_convert_output_to_string() {
107-
assert_eq!(convert_output_to_string(b"hello world"), "hello world");
108-
assert_eq!(convert_output_to_string(b""), "");
109-
assert_eq!(convert_output_to_string(b"git log output"), "git log output");
110-
}
111-
}

src/graph.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,3 @@ pub fn format_git_error(stderr: &str) -> String {
2525
format!("❌ git log failed:\n{stderr}")
2626
}
2727

28-
#[cfg(test)]
29-
mod tests {
30-
use super::*;
31-
32-
#[test]
33-
fn test_get_git_log_args() {
34-
assert_eq!(
35-
get_git_log_args(),
36-
["log", "--oneline", "--graph", "--decorate", "--all"]
37-
);
38-
}
39-
40-
#[test]
41-
fn test_format_git_error() {
42-
assert_eq!(
43-
format_git_error("not a git repository"),
44-
"❌ git log failed:\nnot a git repository"
45-
);
46-
assert_eq!(
47-
format_git_error("unknown revision"),
48-
"❌ git log failed:\nunknown revision"
49-
);
50-
}
51-
}

src/health.rs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn run() {
1212
println!();
1313

1414
// Check if we're in a git repository
15-
if !is_git_repo() {
15+
if !is_git_repo(&std::env::current_dir().unwrap_or_else(|_| ".".into())) {
1616
println!("{} Not in a Git repository", red.apply_to("✗"));
1717
return;
1818
}
@@ -36,9 +36,10 @@ pub fn run() {
3636
println!("{}", bold.apply_to("Health check complete!"));
3737
}
3838

39-
fn is_git_repo() -> bool {
39+
pub fn is_git_repo(path: &std::path::Path) -> bool {
4040
Command::new("git")
4141
.args(["rev-parse", "--git-dir"])
42+
.current_dir(path)
4243
.output()
4344
.map(|output| output.status.success())
4445
.unwrap_or(false)
@@ -167,30 +168,4 @@ fn check_uncommitted_changes(green: &Style, yellow: &Style, _red: &Style) {
167168
}
168169
}
169170

170-
#[cfg(test)]
171-
mod tests {
172-
use super::*;
173171

174-
#[test]
175-
fn test_is_git_repo_returns_false_for_non_git_dir() {
176-
// This test creates a temporary directory that's not a git repo
177-
// and verifies that is_git_repo() correctly returns false
178-
let temp_dir = std::env::temp_dir();
179-
let original_dir = std::env::current_dir().unwrap();
180-
181-
// Change to temp directory (should not be a git repo)
182-
std::env::set_current_dir(&temp_dir).unwrap();
183-
184-
// Test - this might fail if temp dir is somehow in a git repo
185-
// So let's just test the basic functionality
186-
let result = is_git_repo();
187-
188-
// Restore original directory
189-
std::env::set_current_dir(original_dir).unwrap();
190-
191-
// The result depends on whether temp dir is in a git repo or not
192-
// This test mainly ensures the function doesn't panic
193-
// We don't assert a specific value since temp dir might be in git repo
194-
let _ = result;
195-
}
196-
}

src/info.rs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -79,45 +79,4 @@ pub fn format_tracking_branch(tracking_raw: &str) -> String {
7979
}
8080
}
8181

82-
#[cfg(test)]
83-
mod tests {
84-
use super::*;
8582

86-
#[test]
87-
fn test_extract_repo_name() {
88-
assert_eq!(extract_repo_name("/path/to/my-repo"), "my-repo");
89-
assert_eq!(extract_repo_name("/home/user/git-x"), "git-x");
90-
assert_eq!(extract_repo_name("relative-path"), "relative-path");
91-
assert_eq!(extract_repo_name(""), "unknown");
92-
}
93-
94-
#[test]
95-
fn test_parse_ahead_behind_counts() {
96-
assert_eq!(
97-
parse_ahead_behind_counts("3 2"),
98-
("3".to_string(), "2".to_string())
99-
);
100-
assert_eq!(
101-
parse_ahead_behind_counts("0 0"),
102-
("0".to_string(), "0".to_string())
103-
);
104-
assert_eq!(
105-
parse_ahead_behind_counts("5"),
106-
("5".to_string(), "0".to_string())
107-
);
108-
assert_eq!(
109-
parse_ahead_behind_counts(""),
110-
("0".to_string(), "0".to_string())
111-
);
112-
}
113-
114-
#[test]
115-
fn test_format_tracking_branch() {
116-
assert_eq!(format_tracking_branch("origin/main"), "origin/main");
117-
assert_eq!(format_tracking_branch(""), "(no upstream)");
118-
assert_eq!(
119-
format_tracking_branch("upstream/develop"),
120-
"upstream/develop"
121-
);
122-
}
123-
}

src/prune_branches.rs

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -115,81 +115,4 @@ pub fn format_no_branches_to_prune_message() -> &'static str {
115115
"✅ No merged branches to prune."
116116
}
117117

118-
#[cfg(test)]
119-
mod tests {
120-
use super::*;
121118

122-
#[test]
123-
fn test_get_default_protected_branches() {
124-
assert_eq!(get_default_protected_branches(), vec!["main", "master", "develop"]);
125-
}
126-
127-
#[test]
128-
fn test_parse_except_branches() {
129-
assert_eq!(
130-
parse_except_branches("feature,hotfix"),
131-
vec!["feature".to_string(), "hotfix".to_string()]
132-
);
133-
assert_eq!(
134-
parse_except_branches(" branch1 , branch2 "),
135-
vec!["branch1".to_string(), "branch2".to_string()]
136-
);
137-
assert_eq!(parse_except_branches("single"), vec!["single".to_string()]);
138-
assert_eq!(parse_except_branches(""), Vec::<String>::new());
139-
}
140-
141-
#[test]
142-
fn test_get_all_protected_branches() {
143-
let default_only = get_all_protected_branches(None);
144-
assert_eq!(default_only, vec!["main", "master", "develop"]);
145-
146-
let with_except = get_all_protected_branches(Some("feature,hotfix"));
147-
assert_eq!(with_except, vec!["main", "master", "develop", "feature", "hotfix"]);
148-
}
149-
150-
#[test]
151-
fn test_clean_git_branch_name() {
152-
assert_eq!(clean_git_branch_name(" feature/test "), "feature/test");
153-
assert_eq!(clean_git_branch_name("* main"), "main");
154-
assert_eq!(clean_git_branch_name("develop"), "develop");
155-
}
156-
157-
#[test]
158-
fn test_is_branch_protected() {
159-
let protected = vec!["main".to_string(), "develop".to_string()];
160-
161-
assert!(is_branch_protected("main", "current", &protected));
162-
assert!(is_branch_protected("develop", "current", &protected));
163-
assert!(is_branch_protected("current", "current", &protected));
164-
assert!(!is_branch_protected("feature", "current", &protected));
165-
}
166-
167-
#[test]
168-
fn test_get_git_branch_delete_args() {
169-
assert_eq!(
170-
get_git_branch_delete_args("feature"),
171-
["branch".to_string(), "-d".to_string(), "feature".to_string()]
172-
);
173-
}
174-
175-
#[test]
176-
fn test_format_branch_deleted_message() {
177-
assert_eq!(
178-
format_branch_deleted_message("feature/test"),
179-
"🧹 Deleted merged branch 'feature/test'"
180-
);
181-
}
182-
183-
#[test]
184-
fn test_format_branch_delete_failed_message() {
185-
assert_eq!(
186-
format_branch_delete_failed_message("feature/test"),
187-
"⚠️ Failed to delete branch 'feature/test'"
188-
);
189-
}
190-
191-
#[test]
192-
fn test_format_no_branches_to_prune_message() {
193-
assert_eq!(format_no_branches_to_prune_message(), "✅ No merged branches to prune.");
194-
}
195-
}

0 commit comments

Comments
 (0)