1- use git_iris:: config:: Config ;
21use git_iris:: context:: ChangeType ;
3- use git_iris:: git:: GitRepo ;
42use git2:: Repository ;
53use std:: fs;
64use std:: path:: Path ;
7- use tempfile:: TempDir ;
8-
9- fn setup_git_repo ( ) -> ( TempDir , GitRepo ) {
10- let temp_dir = TempDir :: new ( ) . expect ( "Failed to create temporary directory" ) ;
11- let repo = Repository :: init ( temp_dir. path ( ) ) . expect ( "Failed to initialize repository" ) ;
12-
13- // Configure git user
14- let mut config = repo. config ( ) . expect ( "Failed to get repository config" ) ;
15- config
16- . set_str ( "user.name" , "Test User" )
17- . expect ( "Failed to set user name" ) ;
18- config
19- . set_str ( "user.email" , "[email protected] " ) 20- . expect ( "Failed to set user email" ) ;
21-
22- // Create and commit an initial file
23- let initial_file_path = temp_dir. path ( ) . join ( "initial.txt" ) ;
24- fs:: write ( & initial_file_path, "Initial content" ) . expect ( "Failed to write initial file" ) ;
25-
26- let mut index = repo. index ( ) . expect ( "Failed to get repository index" ) ;
27- index
28- . add_path ( Path :: new ( "initial.txt" ) )
29- . expect ( "Failed to add file to index" ) ;
30- index. write ( ) . expect ( "Failed to write index" ) ;
31-
32- let tree_id = index. write_tree ( ) . expect ( "Failed to write tree" ) ;
33- let tree = repo. find_tree ( tree_id) . expect ( "Failed to find tree" ) ;
34- let signature = repo. signature ( ) . expect ( "Failed to create signature" ) ;
35- repo. commit (
36- Some ( "HEAD" ) ,
37- & signature,
38- & signature,
39- "Initial commit" ,
40- & tree,
41- & [ ] ,
42- )
43- . expect ( "Failed to commit" ) ;
44-
45- // Ensure the default branch is named 'main' for consistency across environments
46- {
47- let head_commit = repo
48- . head ( )
49- . expect ( "Failed to get HEAD" )
50- . peel_to_commit ( )
51- . expect ( "Failed to peel HEAD to commit" ) ;
52- let current_branch = repo
53- . head ( )
54- . ok ( )
55- . and_then ( |h| h. shorthand ( ) . map ( std:: string:: ToString :: to_string) )
56- . unwrap_or_default ( ) ;
57- if current_branch != "main" {
58- // Create or update the 'main' branch pointing to the current HEAD commit
59- repo. branch ( "main" , & head_commit, true )
60- . expect ( "Failed to create 'main' branch" ) ;
61- repo. set_head ( "refs/heads/main" )
62- . expect ( "Failed to set HEAD to 'main' branch" ) ;
63- repo. checkout_head ( Some ( git2:: build:: CheckoutBuilder :: default ( ) . force ( ) ) )
64- . expect ( "Failed to checkout 'main' branch" ) ;
65- }
66- }
675
68- let git_repo = GitRepo :: new ( temp_dir. path ( ) ) . expect ( "Failed to create GitRepo" ) ;
69- ( temp_dir, git_repo)
70- }
6+ // Use our centralized test infrastructure
7+ #[ path = "test_utils.rs" ]
8+ mod test_utils;
9+ use test_utils:: { GitTestHelper , MockDataBuilder , TestAssertions , setup_git_repo} ;
7110
7211#[ tokio:: test]
7312async fn test_get_git_info ( ) {
7413 let ( temp_dir, git_repo) = setup_git_repo ( ) ;
75- let config = Config :: default ( ) ;
14+ let config = MockDataBuilder :: config ( ) ;
7615
7716 let context = git_repo
7817 . get_git_info ( & config)
7918 . await
8019 . expect ( "Failed to get git info" ) ;
8120
21+ // Use centralized assertions
22+ TestAssertions :: assert_commit_context_basics ( & context) ;
23+
8224 // Test branch name
8325 assert ! (
8426 context. branch == "main" || context. branch == "master" ,
@@ -99,15 +41,11 @@ async fn test_get_git_info() {
9941 Some ( "Unknown" . to_string( ) )
10042 ) ;
10143
102- // Create and stage a new file
103- let new_file_path = temp_dir. path ( ) . join ( "new_file.txt" ) ;
104- fs:: write ( & new_file_path, "New content" ) . expect ( "Failed to write new file" ) ;
105- let repo = Repository :: open ( temp_dir. path ( ) ) . expect ( "Failed to open repository" ) ;
106- let mut index = repo. index ( ) . expect ( "Failed to get repository index" ) ;
107- index
108- . add_path ( Path :: new ( "new_file.txt" ) )
109- . expect ( "Failed to add new file to index" ) ;
110- index. write ( ) . expect ( "Failed to write index" ) ;
44+ // Create and stage a new file using helper
45+ let helper = GitTestHelper :: new ( & temp_dir) . expect ( "Failed to create GitTestHelper" ) ;
46+ helper
47+ . create_and_stage_file ( "new_file.txt" , "New content" )
48+ . expect ( "Failed to create and stage file" ) ;
11149
11250 // Create an unstaged file
11351 let unstaged_file_path = temp_dir. path ( ) . join ( "unstaged.txt" ) ;
@@ -131,17 +69,13 @@ async fn test_get_git_info() {
13169#[ tokio:: test]
13270async fn test_commit ( ) {
13371 let ( temp_dir, git_repo) = setup_git_repo ( ) ;
134- let config = Config :: default ( ) ;
72+ let config = MockDataBuilder :: config ( ) ;
13573
136- // Create and stage a new file
137- let new_file_path = temp_dir. path ( ) . join ( "commit_test.txt" ) ;
138- fs:: write ( & new_file_path, "Commit test content" ) . expect ( "Failed to write commit test file" ) ;
139- let repo = Repository :: open ( temp_dir. path ( ) ) . expect ( "Failed to open repository" ) ;
140- let mut index = repo. index ( ) . expect ( "Failed to get repository index" ) ;
141- index
142- . add_path ( Path :: new ( "commit_test.txt" ) )
143- . expect ( "Failed to add commit test file to index" ) ;
144- index. write ( ) . expect ( "Failed to write index" ) ;
74+ // Create and stage a new file using helper
75+ let helper = GitTestHelper :: new ( & temp_dir) . expect ( "Failed to create GitTestHelper" ) ;
76+ helper
77+ . create_and_stage_file ( "commit_test.txt" , "Commit test content" )
78+ . expect ( "Failed to create and stage file" ) ;
14579
14680 // Perform commit
14781 let result = git_repo. commit ( "Test commit message" ) ;
@@ -163,25 +97,22 @@ async fn test_commit() {
16397#[ tokio:: test]
16498async fn test_multiple_staged_files ( ) {
16599 let ( temp_dir, git_repo) = setup_git_repo ( ) ;
166- let config = Config :: default ( ) ;
100+ let config = MockDataBuilder :: config ( ) ;
101+
102+ let helper = GitTestHelper :: new ( & temp_dir) . expect ( "Failed to create GitTestHelper" ) ;
167103
168- // Create and stage multiple files
104+ // Create and stage multiple files using helper
169105 for i in 1 ..=3 {
170- let file_path = temp_dir. path ( ) . join ( format ! ( "file{i}.txt" ) ) ;
171- fs:: write ( & file_path, format ! ( "Content {i}" ) )
172- . expect ( "Failed to write multiple staged file" ) ;
173- let repo = Repository :: open ( temp_dir. path ( ) ) . expect ( "Failed to open repository" ) ;
174- let mut index = repo. index ( ) . expect ( "Failed to get repository index" ) ;
175- index
176- . add_path ( Path :: new ( & format ! ( "file{i}.txt" ) ) )
177- . expect ( "Failed to add multiple staged file to index" ) ;
178- index. write ( ) . expect ( "Failed to write index" ) ;
106+ helper
107+ . create_and_stage_file ( & format ! ( "file{i}.txt" ) , & format ! ( "Content {i}" ) )
108+ . expect ( "Failed to create and stage file" ) ;
179109 }
180110
181111 let context = git_repo
182112 . get_git_info ( & config)
183113 . await
184114 . expect ( "Failed to get git info" ) ;
115+
185116 assert_eq ! ( context. staged_files. len( ) , 3 ) ;
186117 for i in 1 ..=3 {
187118 assert ! (
@@ -196,22 +127,19 @@ async fn test_multiple_staged_files() {
196127#[ tokio:: test]
197128async fn test_modified_file ( ) {
198129 let ( temp_dir, git_repo) = setup_git_repo ( ) ;
199- let config = Config :: default ( ) ;
130+ let config = MockDataBuilder :: config ( ) ;
200131
201- // Modify the initial file
202- let initial_file_path = temp_dir. path ( ) . join ( "initial.txt" ) ;
203- fs:: write ( & initial_file_path, "Modified content" ) . expect ( "Failed to modify file content" ) ;
204- let repo = Repository :: open ( temp_dir. path ( ) ) . expect ( "Failed to open repository" ) ;
205- let mut index = repo. index ( ) . expect ( "Failed to get repository index" ) ;
206- index
207- . add_path ( Path :: new ( "initial.txt" ) )
208- . expect ( "Failed to add modified file to index" ) ;
209- index. write ( ) . expect ( "Failed to write index" ) ;
132+ // Modify the initial file and stage it using helper
133+ let helper = GitTestHelper :: new ( & temp_dir) . expect ( "Failed to create GitTestHelper" ) ;
134+ helper
135+ . create_and_stage_file ( "initial.txt" , "Modified content" )
136+ . expect ( "Failed to modify and stage file" ) ;
210137
211138 let context = git_repo
212139 . get_git_info ( & config)
213140 . await
214141 . expect ( "Failed to get git info" ) ;
142+
215143 assert_eq ! ( context. staged_files. len( ) , 1 ) ;
216144 assert ! (
217145 context
@@ -225,11 +153,13 @@ async fn test_modified_file() {
225153#[ tokio:: test]
226154async fn test_deleted_file ( ) {
227155 let ( temp_dir, git_repo) = setup_git_repo ( ) ;
228- let config = Config :: default ( ) ;
156+ let config = MockDataBuilder :: config ( ) ;
229157
230158 // Delete the initial file
231159 let initial_file_path = temp_dir. path ( ) . join ( "initial.txt" ) ;
232160 fs:: remove_file ( & initial_file_path) . expect ( "Failed to remove initial file" ) ;
161+
162+ // Stage the deletion
233163 let repo = Repository :: open ( temp_dir. path ( ) ) . expect ( "Failed to open repository" ) ;
234164 let mut index = repo. index ( ) . expect ( "Failed to get repository index" ) ;
235165 index
@@ -241,6 +171,7 @@ async fn test_deleted_file() {
241171 . get_git_info ( & config)
242172 . await
243173 . expect ( "Failed to get git info" ) ;
174+
244175 assert_eq ! ( context. staged_files. len( ) , 1 ) ;
245176 assert ! (
246177 context
@@ -254,20 +185,14 @@ async fn test_deleted_file() {
254185#[ tokio:: test]
255186async fn test_binary_file ( ) {
256187 let ( temp_dir, git_repo) = setup_git_repo ( ) ;
257- let config = Config :: default ( ) ;
188+ let config = MockDataBuilder :: config ( ) ;
258189
259- // Create a binary file (a simple PNG file)
190+ // Create a binary file using mock data
191+ let binary_content = MockDataBuilder :: mock_binary_content ( ) ;
260192 let binary_file_path = temp_dir. path ( ) . join ( "image.png" ) ;
261- let binary_content = [
262- 0x89 , 0x50 , 0x4E , 0x47 , 0x0D , 0x0A , 0x1A , 0x0A , 0x00 , 0x00 , 0x00 , 0x0D , 0x49 , 0x48 , 0x44 ,
263- 0x52 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x01 , 0x08 , 0x06 , 0x00 , 0x00 , 0x00 , 0x1F ,
264- 0x15 , 0xC4 , 0x89 , 0x00 , 0x00 , 0x00 , 0x0A , 0x49 , 0x44 , 0x41 , 0x54 , 0x78 , 0x9C , 0x63 , 0x00 ,
265- 0x01 , 0x00 , 0x00 , 0x05 , 0x00 , 0x01 , 0x0D , 0x0A , 0x2D , 0xB4 , 0x00 , 0x00 , 0x00 , 0x00 , 0x49 ,
266- 0x45 , 0x4E , 0x44 , 0xAE , 0x42 , 0x60 , 0x82 ,
267- ] ;
268193 fs:: write ( & binary_file_path, binary_content) . expect ( "Failed to write binary file" ) ;
269194
270- // Stage the binary file
195+ // Stage the binary file (need to use git2 directly for existing files)
271196 let repo = Repository :: open ( temp_dir. path ( ) ) . expect ( "Failed to open repository" ) ;
272197 let mut index = repo. index ( ) . expect ( "Failed to get repository index" ) ;
273198 index
0 commit comments