1
- use git_iris:: config:: Config ;
2
1
use git_iris:: context:: ChangeType ;
3
- use git_iris:: git:: GitRepo ;
4
2
use git2:: Repository ;
5
3
use std:: fs;
6
4
use 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
- }
67
5
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} ;
71
10
72
11
#[ tokio:: test]
73
12
async fn test_get_git_info ( ) {
74
13
let ( temp_dir, git_repo) = setup_git_repo ( ) ;
75
- let config = Config :: default ( ) ;
14
+ let config = MockDataBuilder :: config ( ) ;
76
15
77
16
let context = git_repo
78
17
. get_git_info ( & config)
79
18
. await
80
19
. expect ( "Failed to get git info" ) ;
81
20
21
+ // Use centralized assertions
22
+ TestAssertions :: assert_commit_context_basics ( & context) ;
23
+
82
24
// Test branch name
83
25
assert ! (
84
26
context. branch == "main" || context. branch == "master" ,
@@ -99,15 +41,11 @@ async fn test_get_git_info() {
99
41
Some ( "Unknown" . to_string( ) )
100
42
) ;
101
43
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" ) ;
111
49
112
50
// Create an unstaged file
113
51
let unstaged_file_path = temp_dir. path ( ) . join ( "unstaged.txt" ) ;
@@ -131,17 +69,13 @@ async fn test_get_git_info() {
131
69
#[ tokio:: test]
132
70
async fn test_commit ( ) {
133
71
let ( temp_dir, git_repo) = setup_git_repo ( ) ;
134
- let config = Config :: default ( ) ;
72
+ let config = MockDataBuilder :: config ( ) ;
135
73
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" ) ;
145
79
146
80
// Perform commit
147
81
let result = git_repo. commit ( "Test commit message" ) ;
@@ -163,25 +97,22 @@ async fn test_commit() {
163
97
#[ tokio:: test]
164
98
async fn test_multiple_staged_files ( ) {
165
99
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" ) ;
167
103
168
- // Create and stage multiple files
104
+ // Create and stage multiple files using helper
169
105
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" ) ;
179
109
}
180
110
181
111
let context = git_repo
182
112
. get_git_info ( & config)
183
113
. await
184
114
. expect ( "Failed to get git info" ) ;
115
+
185
116
assert_eq ! ( context. staged_files. len( ) , 3 ) ;
186
117
for i in 1 ..=3 {
187
118
assert ! (
@@ -196,22 +127,19 @@ async fn test_multiple_staged_files() {
196
127
#[ tokio:: test]
197
128
async fn test_modified_file ( ) {
198
129
let ( temp_dir, git_repo) = setup_git_repo ( ) ;
199
- let config = Config :: default ( ) ;
130
+ let config = MockDataBuilder :: config ( ) ;
200
131
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" ) ;
210
137
211
138
let context = git_repo
212
139
. get_git_info ( & config)
213
140
. await
214
141
. expect ( "Failed to get git info" ) ;
142
+
215
143
assert_eq ! ( context. staged_files. len( ) , 1 ) ;
216
144
assert ! (
217
145
context
@@ -225,11 +153,13 @@ async fn test_modified_file() {
225
153
#[ tokio:: test]
226
154
async fn test_deleted_file ( ) {
227
155
let ( temp_dir, git_repo) = setup_git_repo ( ) ;
228
- let config = Config :: default ( ) ;
156
+ let config = MockDataBuilder :: config ( ) ;
229
157
230
158
// Delete the initial file
231
159
let initial_file_path = temp_dir. path ( ) . join ( "initial.txt" ) ;
232
160
fs:: remove_file ( & initial_file_path) . expect ( "Failed to remove initial file" ) ;
161
+
162
+ // Stage the deletion
233
163
let repo = Repository :: open ( temp_dir. path ( ) ) . expect ( "Failed to open repository" ) ;
234
164
let mut index = repo. index ( ) . expect ( "Failed to get repository index" ) ;
235
165
index
@@ -241,6 +171,7 @@ async fn test_deleted_file() {
241
171
. get_git_info ( & config)
242
172
. await
243
173
. expect ( "Failed to get git info" ) ;
174
+
244
175
assert_eq ! ( context. staged_files. len( ) , 1 ) ;
245
176
assert ! (
246
177
context
@@ -254,20 +185,14 @@ async fn test_deleted_file() {
254
185
#[ tokio:: test]
255
186
async fn test_binary_file ( ) {
256
187
let ( temp_dir, git_repo) = setup_git_repo ( ) ;
257
- let config = Config :: default ( ) ;
188
+ let config = MockDataBuilder :: config ( ) ;
258
189
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 ( ) ;
260
192
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
- ] ;
268
193
fs:: write ( & binary_file_path, binary_content) . expect ( "Failed to write binary file" ) ;
269
194
270
- // Stage the binary file
195
+ // Stage the binary file (need to use git2 directly for existing files)
271
196
let repo = Repository :: open ( temp_dir. path ( ) ) . expect ( "Failed to open repository" ) ;
272
197
let mut index = repo. index ( ) . expect ( "Failed to get repository index" ) ;
273
198
index
0 commit comments