Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.

Commit 224403f

Browse files
committed
Improve GitDiff test using temporary git repo infrastructure
- Use TestRepo to create controlled git repository with known changes - Test actual git diff functionality instead of just error handling - Verify returned file changes have correct path and status - Use proper commit range format: HEAD~1..HEAD instead of HEAD^.. - More robust and meaningful test coverage
1 parent 7c5ef5e commit 224403f

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

server/src/ide/test.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,26 +539,52 @@ async fn test_search_function() {
539539

540540
#[tokio::test]
541541
async fn test_gitdiff_function() {
542+
use test_utils::TestRepo;
543+
544+
// Create a temporary git repo with some changes
545+
let temp_repo = TestRepo::new()
546+
.overwrite_and_add("src/main.rs", "fn main() {\n println!(\"Hello\");\n}\n")
547+
.commit("Initial commit")
548+
.overwrite("src/main.rs", "fn main() {\n println!(\"Hello, World!\");\n}\n")
549+
.add("src/main.rs")
550+
.commit("Update greeting")
551+
.create();
552+
542553
let mock_client = MockIpcClient::new();
543554
let mut interpreter = DialectInterpreter::new(mock_client);
544555
interpreter.add_function::<FindDefinitions>();
545556
interpreter.add_function::<FindReferences>();
546557
interpreter.add_function::<crate::ide::Search>();
547558
interpreter.add_function::<crate::ide::GitDiff>();
548559

549-
// Test gitdiff for HEAD (will fail if not in git repo, but tests structure)
560+
// Change to the temp repo directory
561+
let original_dir = std::env::current_dir().unwrap();
562+
std::env::set_current_dir(temp_repo.path()).unwrap();
563+
564+
// Test gitdiff for last commit (HEAD~1 to HEAD)
550565
let program = serde_json::json!({
551566
"gitdiff": {
552-
"range": "HEAD"
567+
"range": "HEAD~1..HEAD"
553568
}
554569
});
555570

556571
let result = interpreter.evaluate(program).await;
557572

558-
// Should either succeed with changes or fail with git error
559-
// This tests the basic structure works
560-
match result {
561-
Ok(_) => {}, // Success - we're in a git repo
562-
Err(_) => {}, // Expected - not in git repo or no commits
573+
// Restore original directory
574+
std::env::set_current_dir(original_dir).unwrap();
575+
576+
// Should succeed and return file changes
577+
assert!(result.is_ok());
578+
let changes = result.unwrap();
579+
580+
// Should be an array with one file change (src/main.rs)
581+
if let serde_json::Value::Array(arr) = changes {
582+
assert_eq!(arr.len(), 1);
583+
if let serde_json::Value::Object(file_change) = &arr[0] {
584+
assert_eq!(file_change.get("path").unwrap(), "src/main.rs");
585+
assert_eq!(file_change.get("status").unwrap(), "Modified");
586+
}
587+
} else {
588+
panic!("Expected array of file changes");
563589
}
564590
}

0 commit comments

Comments
 (0)