Files
gitks/tests/error_test.rs
T
zhenyi 7631e57f69 test(bare): add comprehensive tests for GitBare functionality
- Add test_from_header_valid to verify valid repository header parsing
- Add test_from_header_empty_path to handle empty path scenarios
- Add test_from_header_relative_storage_path to validate absolute paths
- Add test_from_header_relative_path_without_storage for missing storage
- Add test_from_header_nonexistent_repo to check repo existence
- Add test_from_header_path_traversal to prevent directory traversal
- Add test_from_header_not_a_directory for file instead of directory
- Add test_from_header_dir_without_head to verify bare repository format
- Add test_object_format to validate object format detection
- Add test_oid_to_pb to verify OID conversion functionality
- Add test_oid_to_pb_invalid_hex to handle invalid hex input gracefully

test(error): add comprehensive error handling tests

- Add test_error_display_variants to verify error message formatting
- Add test_error_is_debug to
2026-06-04 15:33:33 +08:00

70 lines
2.3 KiB
Rust

use gitks::error::GitError;
#[test]
fn test_error_display_variants() {
let cases: Vec<(GitError, &str)> = vec![
(GitError::NotBareRepository, "not bare"),
(
GitError::CommandFailed {
status_code: Some(1),
stderr: "err".into(),
},
"command failed",
),
(GitError::UnsafeCommand("rm".into()), "unsafe"),
(GitError::ObjectNotFound("abc".into()), "object not found"),
(GitError::RefNotFound("main".into()), "reference not found"),
(GitError::ParseError("bad".into()), "parse error"),
(GitError::Gix("gix err".into()), "gix error"),
(GitError::RepoNotFound, "repository not found"),
(GitError::Internal("oops".into()), "internal error"),
(GitError::NotFound("x".into()), "not found"),
(GitError::InvalidOid("bad".into()), "invalid oid"),
(GitError::Locked("lock".into()), "locked"),
(
GitError::PermissionDenied("denied".into()),
"permission denied",
),
(GitError::AuthFailed("auth".into()), "authentication failed"),
(GitError::PayloadTooLarge("big".into()), "payload too large"),
(GitError::InvalidArgument("arg".into()), "invalid argument"),
];
for (err, keyword) in cases {
let msg = err.to_string();
assert!(
msg.to_lowercase().contains(keyword),
"error '{}' should contain '{}'",
msg,
keyword
);
}
}
#[test]
fn test_error_is_debug() {
let err = GitError::Internal("test".into());
let debug = format!("{:?}", err);
assert!(debug.contains("Internal"));
}
#[test]
fn test_io_error_conversion() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let git_err: GitError = io_err.into();
match git_err {
GitError::Io(_) => {}
other => panic!("expected Io variant, got: {:?}", other),
}
}
#[test]
fn test_boxed_error_conversion() {
let boxed: Box<dyn std::error::Error + Send + Sync> = "test error".into();
let git_err: GitError = boxed.into();
match git_err {
GitError::Gix(msg) => assert!(msg.contains("test error")),
other => panic!("expected Gix variant, got: {:?}", other),
}
}