simple-fs 0.12.1

Simple and convenient API for File System access
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
use simple_fs::{ListOptions,SPath, iter_files, list_files};

pub type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;

#[test]
fn test_list_files_one_level_dotted() -> Result<()> {
	// -- Exec
	let res = list_files("./tests-data/", Some(&["./tests-data/*.txt"]), None)?;

	// -- Check
	let res_paths = res.iter().map(|p| p.as_str()).collect::<Vec<_>>();
	assert_eq!(res.len(), 1, "Should have 1 file with *.txt in tests-data");
	assert!(
		res_paths.contains(&"./tests-data/file2.txt"),
		"Should contain file2.txt"
	);
	assert!(
		res_paths.iter().any(|p| p.ends_with("file2.txt")),
		"Should contain file2.txt"
	);

	Ok(())
}

#[test]
fn test_list_files_rel_one_level_dotted() -> Result<()> {
	// NOTE With relative_glob, "*.txt" now works
	// -- Exec
	let res = list_files(
		"./tests-data/",
		Some(&["*.txt"]),
		Some(ListOptions::from_relative_glob(true)),
	)?;

	// -- Check
	let res_paths = res.iter().map(|p| p.as_str()).collect::<Vec<_>>();
	assert_eq!(res.len(), 1, "Should have 1 file with *.txt in tests-data");
	assert!(
		res_paths.iter().any(|p| p.ends_with("file2.txt")),
		"Should contain file2.txt"
	);

	Ok(())
}

#[test]
fn test_list_files_rel_one_level_no_file() -> Result<()> {
	// -- Exec
	let res = list_files(
		"./tests-data/",
		Some(&["*.rs"]),
		Some(ListOptions::from_relative_glob(true)),
	)?;

	// -- Check
	assert_eq!(res.len(), 0, "Should have 0 files with *.rs in tests-data dir");

	Ok(())
}

#[test]
fn test_list_files_one_level_no_file() -> Result<()> {
	// -- Exec
	let res = list_files("./tests-data/", Some(&["./tests-data/*.rs"]), None)?;

	// -- Check
	assert_eq!(res.len(), 0, "Should have 0 files with *.rs in tests-data dir");

	Ok(())
}

#[test]
fn test_list_files_one_file_dotted() -> Result<()> {
	// -- Exec
	let res = list_files("./tests-data", Some(&["./tests-data/file1.md"]), None)?;

	// -- Check
	let res_paths = res.iter().map(|p| p.as_str()).collect::<Vec<_>>();
	assert_eq!(res.len(), 1, "Should have 1 file");
	assert!(res_paths.contains(&"./tests-data/file1.md"), "Should contain file1.md");

	Ok(())
}

#[test]
fn test_list_files_sub_level_dotted() -> Result<()> {
	// -- Exec
	let res = list_files("./tests-data/", Some(&["./tests-data/**/*.md"]), None)?;

	// -- Check
	let res_paths = res.iter().map(|p| p.as_str()).collect::<Vec<_>>();
	assert_md_files_res(&res_paths);

	Ok(())
}

#[test]
fn test_list_files_sub_dir_full_path() -> Result<()> {
	// -- Exec
	let res = list_files("./tests-data/dir1/", Some(&["./tests-data/dir1/**/*.md"]), None)?;

	// -- Check
	let res_paths = res.iter().map(|p| p.as_str()).collect::<Vec<_>>();
	assert_eq!(res_paths.len(), 3, "Should have 3 markdown files in dir1");
	assert!(
		res_paths.contains(&"./tests-data/dir1/file3.md"),
		"Should contain dir1/file3.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/dir1/dir2/file5.md"),
		"Should contain dir1/dir2/file5.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/dir1/dir2/dir3/file7.md"),
		"Should contain dir1/dir2/dir3/file7.md"
	);

	Ok(())
}

/// Here the globs are relative to the base dir given (here `./tests-data/`)
#[test]
fn test_list_files_sub_dir_rel_glob() -> Result<()> {
	// -- Exec
	let res = list_files(
		"./tests-data/",
		Some(&["**/*.md"]),
		Some(ListOptions::from_relative_glob(true)),
	)?;

	// -- Check
	let res_paths = res.iter().map(|p| p.as_str()).collect::<Vec<_>>();
	assert_md_files_res(&res_paths);

	Ok(())
}

#[test]
fn test_list_files_absolute_wildcard() -> Result<()> {
	// Get the absolute path to the "tests-data" directory.
	let test_data_abs = SPath::new("./tests-data");
	let test_data_abs_str = test_data_abs.as_str();

	// Construct a glob pattern that should match the "file1.md" file.
	let pattern = format!("{test_data_abs_str}/**/*1.md");

	// -- Exec
	// Execute list_files using the tests-data directory and the wildcard pattern.
	let files = list_files("./tests-data/", Some(&[pattern.as_str()]), None)?;

	// -- Check
	// Check that at least one file's path ends with "file1.md"
	let found = files.iter().any(|p| p.as_str().ends_with("file1.md"));
	assert!(found, "Expected to find file1.md file with wildcard absolute pattern");

	Ok(())
}

#[test]
fn test_list_files_absolute_direct() -> Result<()> {
	// Get the absolute path to "tests-data/file1.md".
	let file_abs = std::fs::canonicalize("tests-data/file1.md")?;
	let file_abs = SPath::from_std_path_buf(file_abs)?;

	// Get the parent directory of the file.
	let parent_dir = file_abs.parent().ok_or("Should have parent dir")?;

	// -- Exec
	// Execute list_files using the parent directory and an exact match glob for the file.
	let files = list_files(parent_dir, Some(&[file_abs.as_str()]), None)?;

	// -- Check
	assert_eq!(files.len(), 1, "Should have exactly one file with exact match");

	let returned_path = files[0].as_str();
	assert_eq!(
		returned_path,
		file_abs.as_str(),
		"The file path should match the absolute file path"
	);

	Ok(())
}

#[test]
fn test_list_files_mixed_absolute_and_relative_globs() -> Result<()> {
	// -- Exec
	// Mix an absolute glob and a relative glob in the same call.
	let abs_pattern = SPath::new("./tests-data/file1.md").canonicalize()?;
	let patterns = [abs_pattern.as_str(), "tests-data/file2.txt"];
	let res = list_files("./", Some(&patterns), None)?;

	// -- Check
	let res_paths: Vec<&str> = res.iter().map(|p| p.as_str()).collect();

	assert_eq!(res.len(), 2, "Expected both files to be found using mixed patterns");
	assert!(
		res_paths.iter().any(|&p| p.ends_with("file1.md")),
		"Should contain file1.md"
	);
	assert!(
		res_paths.iter().any(|&p| p.ends_with("file2.txt")),
		"Should contain file2.txt"
	);
	Ok(())
}

#[test]
fn test_list_files_mixed_absolute_and_relative_globs_with_relative_option() -> Result<()> {
	// -- Exec
	// Mix an absolute glob and a relative glob with the relative_glob option enabled.
	let abs_pattern = SPath::new("./tests-data/file1.md");
	let patterns = ["**/*.txt", abs_pattern.as_str()];
	let res = list_files(
		"./tests-data/",
		Some(&patterns),
		Some(ListOptions::from_relative_glob(true)),
	)?;

	// -- Check
	let res_paths: Vec<&str> = res.iter().map(|p| p.as_str()).collect();
	assert!(
		res.len() >= 6,
		"Expected at least 6 files to be found using mixed patterns with relative_glob option"
	);
	assert!(
		res_paths.iter().any(|&p| p.ends_with("file1.md")),
		"Should contain file1.md"
	);
	assert!(
		res_paths.iter().any(|&p| p.ends_with("file2.txt")),
		"Should contain file2.txt"
	);
	Ok(())
}

#[test]
fn test_list_iter_files_simple_glob_ok() -> Result<()> {
	// -- Exec
	let iter = iter_files("./tests-data/", Some(&["./tests-data/*.md"]), None)?;
	let res: Vec<SPath> = iter.collect();

	// -- Check
	let count = res.len();
	assert_eq!(count, 1, "Expected 1 file matching pattern");
	Ok(())
}

#[test]
fn test_list_iter_files_nested_and_exclude_ok() -> Result<()> {
	// -- Exec
	let excludes = [simple_fs::DEFAULT_EXCLUDE_GLOBS, &["**/.devai", "*.lock", "**/dir2/**"]].concat();
	let iter = iter_files("./tests-data/", Some(&["./tests-data/**/*.md"]), Some(excludes.into()))?;

	// -- Check
	let count = iter.count();
	assert_eq!(count, 5, "Expected 5 files matching pattern after exclusions");
	Ok(())
}

#[test]
fn test_list_files_with_negative_glob() -> Result<()> {
	// -- Exec
	// Include all markdown files but exclude those in dir2
	let res = list_files(
		"./tests-data/",
		Some(&["./tests-data/**/*.md", "!./tests-data/**/dir2/**"]),
		None,
	)?;

	// -- Check
	let res_paths = res.iter().map(|p| p.as_str()).collect::<Vec<_>>();
	assert_eq!(res.len(), 5, "Should have 5 markdown files (excluding dir2)");

	assert!(res_paths.contains(&"./tests-data/file1.md"), "Should contain file1.md");
	assert!(
		res_paths.contains(&"./tests-data/dir1/file3.md"),
		"Should contain dir1/file3.md"
	);
	assert!(
		!res_paths.contains(&"./tests-data/dir1/dir2/file5.md"),
		"Should not contain dir1/dir2/file5.md"
	);
	assert!(
		!res_paths.contains(&"./tests-data/dir1/dir2/dir3/file7.md"),
		"Should not contain dir1/dir2/dir3/file7.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/another-dir/notes.md"),
		"Should contain another-dir/notes.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/another-dir/sub-dir/example.md"),
		"Should contain another-dir/sub-dir/example.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/another-dir/sub-dir/deep-folder/final.md"),
		"Should contain another-dir/sub-dir/deep-folder/final.md"
	);

	Ok(())
}

#[test]
fn test_list_files_with_multiple_negative_globs() -> Result<()> {
	// -- Exec
	// Include all markdown files but exclude multiple patterns
	let res = list_files(
		"./tests-data/",
		Some(&[
			"./tests-data/**/*.md",       // Include all markdown files
			"!./tests-data/**/dir2/**",   // Exclude dir2 files
			"!./tests-data/**/*final.md", // Exclude final.md files
		]),
		None,
	)?;

	// -- Check
	let res_paths = res.iter().map(|p| p.as_str()).collect::<Vec<_>>();
	assert_eq!(res.len(), 4, "Should have 4 markdown files after multiple exclusions");

	assert!(res_paths.contains(&"./tests-data/file1.md"), "Should contain file1.md");
	assert!(
		res_paths.contains(&"./tests-data/dir1/file3.md"),
		"Should contain dir1/file3.md"
	);
	assert!(
		!res_paths.contains(&"./tests-data/dir1/dir2/file5.md"),
		"Should not contain dir1/dir2/file5.md"
	);
	assert!(
		!res_paths.contains(&"./tests-data/dir1/dir2/dir3/file7.md"),
		"Should not contain dir1/dir2/dir3/file7.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/another-dir/notes.md"),
		"Should contain another-dir/notes.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/another-dir/sub-dir/example.md"),
		"Should contain another-dir/sub-dir/example.md"
	);
	assert!(
		!res_paths.contains(&"./tests-data/another-dir/sub-dir/deep-folder/final.md"),
		"Should not contain another-dir/sub-dir/deep-folder/final.md"
	);

	Ok(())
}

#[test]
fn test_list_files_with_only_negative_globs() -> Result<()> {
	// -- Exec
	// Only use negative patterns (should default to ** for includes)
	let res = list_files(
		"./tests-data/",
		Some(&[
			"!./tests-data/**/*.txt", // Exclude all txt files
		]),
		None,
	)?;

	// -- Check
	let res_paths = res.iter().map(|p| p.as_str()).collect::<Vec<_>>();
	assert!(res.len() >= 7, "Should have at least 7 files after excluding txt files");

	// Ensure no txt files are included
	assert!(
		!res_paths.iter().any(|p| p.ends_with(".txt")),
		"Should not contain any .txt files"
	);

	// Check for md files as a sanity check
	assert!(res_paths.iter().any(|p| p.ends_with(".md")), "Should contain .md files");

	Ok(())
}

#[test]
fn test_list_files_relative_negative_glob() -> Result<()> {
	// -- Exec
	// Use relative globs with negative patterns
	let res = list_files(
		"./tests-data/",
		Some(&[
			"**/*.md",     // Include all markdown files
			"!**/dir2/**", // Exclude dir2 files (using relative glob)
		]),
		Some(ListOptions::from_relative_glob(true)),
	)?;

	// -- Check
	let res_paths = res.iter().map(|p| p.as_str()).collect::<Vec<_>>();
	assert_eq!(res.len(), 5, "Should have 5 markdown files (excluding dir2)");

	assert!(res_paths.contains(&"./tests-data/file1.md"), "Should contain file1.md");
	assert!(
		res_paths.contains(&"./tests-data/dir1/file3.md"),
		"Should contain dir1/file3.md"
	);
	assert!(
		!res_paths.contains(&"./tests-data/dir1/dir2/file5.md"),
		"Should not contain dir1/dir2/file5.md"
	);
	assert!(
		!res_paths.contains(&"./tests-data/dir1/dir2/dir3/file7.md"),
		"Should not contain dir1/dir2/dir3/file7.md"
	);

	Ok(())
}

#[test]
fn test_list_files_with_nonexistent_folder_glob_relative() -> Result<()> {
	// -- Exec
	// Mix a glob for a non-existent folder with a valid glob using relative_glob option
	let res = list_files(
		"./tests-data/",
		Some(&["nonexistent-folder/**/*.rs", "./**/*.md"]),
		Some(ListOptions::from_relative_glob(true)),
	)?;

	// -- Check
	let res_paths = res.iter().map(|p| p.as_str()).collect::<Vec<_>>();
	assert_eq!(
		res.len(),
		7,
		"Should have 7 markdown files despite non-existent folder glob"
	);
	assert!(res_paths.contains(&"./tests-data/file1.md"), "Should contain file1.md");
	assert!(
		res_paths.contains(&"./tests-data/dir1/file3.md"),
		"Should contain dir1/file3.md"
	);

	Ok(())
}

#[test]
fn test_list_files_with_combined_exclusion_methods() -> Result<()> {
	// -- Exec
	// Combine both ListOptions exclude_globs and negative patterns in include_globs
	let list_options = ListOptions::default()
		.with_exclude_globs(&["**/deep-folder/**"]) // Exclude deep-folder files via ListOptions
		.with_relative_glob(); // Use relative glob mode

	let res = list_files(
		"./tests-data/",
		Some(&[
			"**/*.md",     // Include all markdown files
			"!**/dir2/**", // Exclude dir2 files via negative pattern
		]),
		Some(list_options),
	)?;

	// -- Check
	let res_paths = res.iter().map(|p| p.as_str()).collect::<Vec<_>>();
	assert_eq!(res.len(), 4, "Should have 4 markdown files after combined exclusions");

	// Files that should be included
	assert!(res_paths.contains(&"./tests-data/file1.md"), "Should contain file1.md");
	assert!(
		res_paths.contains(&"./tests-data/dir1/file3.md"),
		"Should contain dir1/file3.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/another-dir/notes.md"),
		"Should contain another-dir/notes.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/another-dir/sub-dir/example.md"),
		"Should contain another-dir/sub-dir/example.md"
	);

	// Files that should be excluded by negative pattern
	assert!(
		!res_paths.contains(&"./tests-data/dir1/dir2/file5.md"),
		"Should not contain dir1/dir2/file5.md (excluded by negative pattern)"
	);
	assert!(
		!res_paths.contains(&"./tests-data/dir1/dir2/dir3/file7.md"),
		"Should not contain dir1/dir2/dir3/file7.md (excluded by negative pattern)"
	);

	// Files that should be excluded by ListOptions
	assert!(
		!res_paths.contains(&"./tests-data/another-dir/sub-dir/deep-folder/final.md"),
		"Should not contain another-dir/sub-dir/deep-folder/final.md (excluded by ListOptions)"
	);

	Ok(())
}

// region:    --- Support

/// Reusable function for checking markdown files in test-data directory
fn assert_md_files_res(res_paths: &[&str]) {
	assert_eq!(res_paths.len(), 7, "Should have 7 markdown files in total");
	assert!(res_paths.contains(&"./tests-data/file1.md"), "Should contain file1.md");
	assert!(
		res_paths.contains(&"./tests-data/dir1/file3.md"),
		"Should contain dir1/file3.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/dir1/dir2/file5.md"),
		"Should contain dir1/dir2/file5.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/dir1/dir2/dir3/file7.md"),
		"Should contain dir1/dir2/dir3/file7.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/another-dir/notes.md"),
		"Should contain another-dir/notes.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/another-dir/sub-dir/deep-folder/final.md"),
		"Should contain another-dir/sub-dir/deep-folder/final.md"
	);
	assert!(
		res_paths.contains(&"./tests-data/another-dir/sub-dir/example.md"),
		"Should contain another-dir/sub-dir/example.md"
	);
}

// endregion: --- Support