@@ -77,3 +77,258 @@ fn extract_package_name(cargo_toml_path: &Path) -> Option<String> {
77
77
. as_str ( )
78
78
. map ( |s| s. to_string ( ) )
79
79
}
80
+
81
+ #[ cfg( test) ]
82
+ mod tests {
83
+ use super :: * ;
84
+ use std:: fs:: { self , File } ;
85
+ use std:: io:: Write ;
86
+ use std:: path:: PathBuf ;
87
+ use tempfile:: TempDir ;
88
+
89
+ // Helper function to create a Cargo.toml file with a specific package name
90
+ fn create_cargo_toml ( path : & Path , package_name : & str ) -> std:: io:: Result < ( ) > {
91
+ let mut file = File :: create ( path) ?;
92
+ writeln ! (
93
+ file,
94
+ r#"[package]
95
+ name = "{}"
96
+ version = "0.1.0"
97
+ edition = "2021"
98
+ "# ,
99
+ package_name
100
+ ) ?;
101
+ Ok ( ( ) )
102
+ }
103
+
104
+ #[ test]
105
+ fn test_get_crate_for_file_direct_parent ( ) {
106
+ // Create a temporary directory with a specific crate structure
107
+ let temp_dir = TempDir :: new ( ) . expect ( "Failed to create temp directory" ) ;
108
+ let crate_root = temp_dir. path ( ) . join ( "my-crate" ) ;
109
+ fs:: create_dir_all ( & crate_root) . expect ( "Failed to create crate directory" ) ;
110
+
111
+ // Create Cargo.toml in the crate root
112
+ let cargo_toml_path = crate_root. join ( "Cargo.toml" ) ;
113
+ create_cargo_toml ( & cargo_toml_path, "my-awesome-crate" )
114
+ . expect ( "Failed to create Cargo.toml" ) ;
115
+
116
+ // Create a source file in the same directory
117
+ let source_file_path = crate_root. join ( "lib.rs" ) ;
118
+ File :: create ( & source_file_path) . expect ( "Failed to create source file" ) ;
119
+
120
+ let crate_name = get_crate_for_file ( & source_file_path. to_string_lossy ( ) ) ;
121
+
122
+ assert_eq ! (
123
+ crate_name, "my-awesome-crate" ,
124
+ "Should identify the correct crate name from direct parent"
125
+ ) ;
126
+ }
127
+
128
+ #[ test]
129
+ fn test_get_crate_for_file_nested_directory ( ) {
130
+ // Create a temporary directory with a nested structure
131
+ let temp_dir = TempDir :: new ( ) . expect ( "Failed to create temp directory" ) ;
132
+ let crate_root = temp_dir. path ( ) . join ( "my-crate" ) ;
133
+ let src_dir = crate_root. join ( "src" ) ;
134
+ let module_dir = src_dir. join ( "module" ) ;
135
+ fs:: create_dir_all ( & module_dir) . expect ( "Failed to create nested directories" ) ;
136
+
137
+ // Create Cargo.toml in the crate root
138
+ let cargo_toml_path = crate_root. join ( "Cargo.toml" ) ;
139
+ create_cargo_toml ( & cargo_toml_path, "nested-crate" ) . expect ( "Failed to create Cargo.toml" ) ;
140
+
141
+ // Create a source file in the nested directory
142
+ let source_file_path = module_dir. join ( "mod.rs" ) ;
143
+ File :: create ( & source_file_path) . expect ( "Failed to create source file" ) ;
144
+
145
+ let crate_name = get_crate_for_file ( & source_file_path. to_string_lossy ( ) ) ;
146
+
147
+ assert_eq ! (
148
+ crate_name, "nested-crate" ,
149
+ "Should identify the correct crate name from nested directory"
150
+ ) ;
151
+ }
152
+
153
+ #[ test]
154
+ fn test_get_crate_for_file_multiple_cargo_tomls ( ) {
155
+ // Create a temporary directory with nested crates
156
+ let temp_dir = TempDir :: new ( ) . expect ( "Failed to create temp directory" ) ;
157
+ let workspace_dir = temp_dir. path ( ) . join ( "workspace" ) ;
158
+ let parent_crate_dir = workspace_dir. join ( "parent-crate" ) ;
159
+ let child_crate_dir = parent_crate_dir. join ( "child-crate" ) ;
160
+ let child_src_dir = child_crate_dir. join ( "src" ) ;
161
+
162
+ fs:: create_dir_all ( & parent_crate_dir) . expect ( "Failed to create parent crate directory" ) ;
163
+ fs:: create_dir_all ( & child_src_dir) . expect ( "Failed to create child crate src directory" ) ;
164
+
165
+ // Create Cargo.toml in both crate directories
166
+ let parent_cargo_toml = parent_crate_dir. join ( "Cargo.toml" ) ;
167
+ create_cargo_toml ( & parent_cargo_toml, "parent-crate" )
168
+ . expect ( "Failed to create parent Cargo.toml" ) ;
169
+
170
+ let child_cargo_toml = child_crate_dir. join ( "Cargo.toml" ) ;
171
+ create_cargo_toml ( & child_cargo_toml, "child-crate" )
172
+ . expect ( "Failed to create child Cargo.toml" ) ;
173
+
174
+ // Create a source file in the child crate
175
+ let source_file_path = child_src_dir. join ( "lib.rs" ) ;
176
+ File :: create ( & source_file_path) . expect ( "Failed to create source file" ) ;
177
+
178
+ let crate_name = get_crate_for_file ( & source_file_path. to_string_lossy ( ) ) ;
179
+
180
+ assert_eq ! (
181
+ crate_name, "child-crate" ,
182
+ "Should identify the closest crate"
183
+ ) ;
184
+ }
185
+
186
+ #[ test]
187
+ fn test_get_crate_for_file_no_cargo_toml ( ) {
188
+ // Create a temporary directory with no Cargo.toml
189
+ let temp_dir = TempDir :: new ( ) . expect ( "Failed to create temp directory" ) ;
190
+ let src_dir = temp_dir. path ( ) . join ( "src" ) ;
191
+ fs:: create_dir_all ( & src_dir) . expect ( "Failed to create src directory" ) ;
192
+
193
+ // Create a source file
194
+ let source_file_path = src_dir. join ( "orphan.rs" ) ;
195
+ File :: create ( & source_file_path) . expect ( "Failed to create source file" ) ;
196
+
197
+ let crate_name = get_crate_for_file ( & source_file_path. to_string_lossy ( ) ) ;
198
+
199
+ assert_eq ! (
200
+ crate_name, "unknown-crate" ,
201
+ "Should return unknown-crate when no Cargo.toml is found"
202
+ ) ;
203
+ }
204
+
205
+ #[ test]
206
+ fn test_get_crate_for_file_invalid_cargo_toml ( ) {
207
+ // Create a temporary directory with an invalid Cargo.toml
208
+ let temp_dir = TempDir :: new ( ) . expect ( "Failed to create temp directory" ) ;
209
+ let crate_dir = temp_dir. path ( ) . join ( "invalid-crate" ) ;
210
+ fs:: create_dir_all ( & crate_dir) . expect ( "Failed to create crate directory" ) ;
211
+
212
+ // Create an invalid Cargo.toml (missing package name)
213
+ let cargo_toml_path = crate_dir. join ( "Cargo.toml" ) ;
214
+ let mut file = File :: create ( & cargo_toml_path) . expect ( "Failed to create Cargo.toml" ) ;
215
+ writeln ! (
216
+ file,
217
+ r#"
218
+ [package]
219
+ version = "0.1.0"
220
+ edition = "2021"
221
+ "#
222
+ )
223
+ . expect ( "Failed to write to Cargo.toml" ) ;
224
+
225
+ // Create a source file
226
+ let source_file_path = crate_dir. join ( "lib.rs" ) ;
227
+ File :: create ( & source_file_path) . expect ( "Failed to create source file" ) ;
228
+
229
+ let crate_name = get_crate_for_file ( & source_file_path. to_string_lossy ( ) ) ;
230
+
231
+ assert_eq ! (
232
+ crate_name, "unknown-crate" ,
233
+ "Should return unknown-crate when Cargo.toml is invalid"
234
+ ) ;
235
+ }
236
+
237
+ #[ test]
238
+ fn test_get_crate_for_file_workspace_member ( ) {
239
+ // Create a temporary directory with a workspace structure
240
+ let temp_dir = TempDir :: new ( ) . expect ( "Failed to create temp directory" ) ;
241
+ let workspace_dir = temp_dir. path ( ) . join ( "workspace" ) ;
242
+ fs:: create_dir_all ( & workspace_dir) . expect ( "Failed to create workspace directory" ) ;
243
+
244
+ // Create workspace Cargo.toml
245
+ let workspace_cargo_toml = workspace_dir. join ( "Cargo.toml" ) ;
246
+ let mut file =
247
+ File :: create ( & workspace_cargo_toml) . expect ( "Failed to create workspace Cargo.toml" ) ;
248
+ writeln ! (
249
+ file,
250
+ r#"
251
+ [workspace]
252
+ members = ["member1", "member2"]
253
+ "#
254
+ )
255
+ . expect ( "Failed to write to workspace Cargo.toml" ) ;
256
+
257
+ // Create member1 crate
258
+ let member1_dir = workspace_dir. join ( "member1" ) ;
259
+ let member1_src_dir = member1_dir. join ( "src" ) ;
260
+ fs:: create_dir_all ( & member1_src_dir) . expect ( "Failed to create member1 src directory" ) ;
261
+
262
+ // Create member1 Cargo.toml
263
+ let member1_cargo_toml = member1_dir. join ( "Cargo.toml" ) ;
264
+ create_cargo_toml ( & member1_cargo_toml, "workspace-member1" )
265
+ . expect ( "Failed to create member1 Cargo.toml" ) ;
266
+
267
+ // Create a source file in member1
268
+ let source_file_path = member1_src_dir. join ( "lib.rs" ) ;
269
+ File :: create ( & source_file_path) . expect ( "Failed to create source file" ) ;
270
+
271
+ let crate_name = get_crate_for_file ( & source_file_path. to_string_lossy ( ) ) ;
272
+
273
+ assert_eq ! (
274
+ crate_name, "workspace-member1" ,
275
+ "Should identify the workspace member crate"
276
+ ) ;
277
+ }
278
+
279
+ #[ test]
280
+ fn test_get_crate_for_file_non_existent_file ( ) {
281
+ // Test with a non-existent file path
282
+ let crate_name = get_crate_for_file ( "/path/to/non/existent/file.rs" ) ;
283
+
284
+ assert_eq ! (
285
+ crate_name, "unknown-crate" ,
286
+ "Should return unknown-crate for non-existent file"
287
+ ) ;
288
+ }
289
+
290
+ #[ test]
291
+ fn test_get_crate_for_file_with_special_chars ( ) {
292
+ // Create a temporary directory with spaces and special characters
293
+ let temp_dir = TempDir :: new ( ) . expect ( "Failed to create temp directory" ) ;
294
+ let crate_dir = temp_dir. path ( ) . join ( "my crate with spaces" ) ;
295
+ fs:: create_dir_all ( & crate_dir) . expect ( "Failed to create crate directory with spaces" ) ;
296
+
297
+ // Create Cargo.toml
298
+ let cargo_toml_path = crate_dir. join ( "Cargo.toml" ) ;
299
+ create_cargo_toml ( & cargo_toml_path, "special-chars-crate" )
300
+ . expect ( "Failed to create Cargo.toml" ) ;
301
+
302
+ // Create a source file
303
+ let source_file_path = crate_dir. join ( "special file.rs" ) ;
304
+ File :: create ( & source_file_path) . expect ( "Failed to create source file with spaces" ) ;
305
+
306
+ let crate_name = get_crate_for_file ( & source_file_path. to_string_lossy ( ) ) ;
307
+
308
+ assert_eq ! (
309
+ crate_name, "special-chars-crate" ,
310
+ "Should handle paths with spaces correctly"
311
+ ) ;
312
+ }
313
+
314
+ #[ test]
315
+ fn test_get_crate_for_file_with_directory_not_file ( ) {
316
+ // Create a temporary directory structure
317
+ let temp_dir = TempDir :: new ( ) . expect ( "Failed to create temp directory" ) ;
318
+ let crate_dir = temp_dir. path ( ) . join ( "directory-test" ) ;
319
+ let src_dir = crate_dir. join ( "src" ) ;
320
+ fs:: create_dir_all ( & src_dir) . expect ( "Failed to create directories" ) ;
321
+
322
+ // Create Cargo.toml
323
+ let cargo_toml_path = crate_dir. join ( "Cargo.toml" ) ;
324
+ create_cargo_toml ( & cargo_toml_path, "directory-crate" )
325
+ . expect ( "Failed to create Cargo.toml" ) ;
326
+
327
+ let crate_name = get_crate_for_file ( & src_dir. to_string_lossy ( ) ) ;
328
+
329
+ assert_eq ! (
330
+ crate_name, "directory-crate" ,
331
+ "Should work with directory paths"
332
+ ) ;
333
+ }
334
+ }
0 commit comments