-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.rs
More file actions
1032 lines (839 loc) · 30 KB
/
Copy pathintegration_test.rs
File metadata and controls
1032 lines (839 loc) · 30 KB
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
//! Integration Tests for Valence Shell
//!
//! These tests verify that the formally proven properties hold at runtime.
//!
//! ## Properties Tested (matching Lean 4 proofs)
//!
//! 1. `mkdir_rmdir_reversible`: rmdir(mkdir(p, fs)) = fs
//! 2. `createFile_deleteFile_reversible`: deleteFile(createFile(p, fs)) = fs
//! 3. Precondition enforcement (EEXIST, ENOENT, ENOTDIR, ENOTEMPTY)
//! 4. Undo/redo correctness
//! 5. Transaction atomicity
mod fixtures;
use std::fs::{self, OpenOptions};
use std::path::PathBuf;
use std::process::Command;
// ============================================================
// Reversibility Tests
// ============================================================
/// Test: mkdir followed by rmdir returns to initial state
/// Lean theorem: mkdir_rmdir_reversible (FilesystemModel.lean:158)
#[test]
fn test_mkdir_rmdir_reversible() {
let temp = fixtures::test_sandbox("mkdir_rmdir");
let target = temp.path().join("testdir");
// Initial state: directory does not exist
assert!(!target.exists(), "Precondition: path should not exist");
// Create directory
fs::create_dir(&target).unwrap();
assert!(target.exists(), "mkdir should create directory");
assert!(target.is_dir(), "Created path should be a directory");
// Remove directory
fs::remove_dir(&target).unwrap();
// Verify return to initial state
assert!(
!target.exists(),
"rmdir(mkdir(p)) should return to initial state"
);
}
/// Test: touch followed by rm returns to initial state
/// Lean theorem: createFile_deleteFile_reversible (FileOperations.lean)
#[test]
fn test_create_delete_file_reversible() {
let temp = fixtures::test_sandbox("create_delete");
let target = temp.path().join("testfile.txt");
// Initial state
assert!(!target.exists());
// Create file
fs::write(&target, "").unwrap();
assert!(target.exists());
assert!(target.is_file());
// Delete file
fs::remove_file(&target).unwrap();
// Verify return to initial state
assert!(
!target.exists(),
"rm(touch(p)) should return to initial state"
);
}
/// Test: Nested operations can be fully reversed
/// Lean theorem: operationSequenceReversible (FilesystemComposition.lean:129)
#[test]
fn test_operation_sequence_reversible() {
let temp = fixtures::test_sandbox("sequence");
// Create a nested structure
let dir1 = temp.path().join("level1");
let dir2 = temp.path().join("level1/level2");
let file1 = temp.path().join("level1/file.txt");
let file2 = temp.path().join("level1/level2/nested.txt");
// Apply sequence: mkdir a → mkdir a/b → touch a/f → touch a/b/n
fs::create_dir(&dir1).unwrap();
fs::create_dir(&dir2).unwrap();
fs::write(&file1, "content1").unwrap();
fs::write(&file2, "content2").unwrap();
// Verify structure exists
assert!(dir1.is_dir());
assert!(dir2.is_dir());
assert!(file1.is_file());
assert!(file2.is_file());
// Reverse sequence (in reverse order)
fs::remove_file(&file2).unwrap();
fs::remove_file(&file1).unwrap();
fs::remove_dir(&dir2).unwrap();
fs::remove_dir(&dir1).unwrap();
// Verify return to initial state
assert!(
!dir1.exists(),
"Sequence reversal should return to initial state"
);
}
// ============================================================
// Precondition Tests
// ============================================================
/// Test: mkdir fails when path already exists (EEXIST)
/// Coq: MkdirPrecondition requires ~path_exists
#[test]
fn test_mkdir_eexist() {
let temp = fixtures::test_sandbox("eexist");
let target = temp.path().join("existing");
fs::create_dir(&target).unwrap();
// Attempt to create again should fail
let result = fs::create_dir(&target);
assert!(result.is_err(), "mkdir should fail on existing path");
let err = result.unwrap_err();
assert_eq!(
err.kind(),
std::io::ErrorKind::AlreadyExists,
"Error should be EEXIST/AlreadyExists"
);
}
/// Test: mkdir fails when parent doesn't exist (ENOENT)
/// Coq: MkdirPrecondition requires parent_exists
#[test]
fn test_mkdir_enoent() {
let temp = fixtures::test_sandbox("enoent");
let target = temp.path().join("nonexistent/child");
let result = fs::create_dir(&target);
assert!(
result.is_err(),
"mkdir should fail when parent doesn't exist"
);
let err = result.unwrap_err();
assert_eq!(
err.kind(),
std::io::ErrorKind::NotFound,
"Error should be ENOENT/NotFound"
);
}
/// Test: rmdir fails when directory is not empty (ENOTEMPTY)
/// Coq: RmdirPrecondition requires is_empty
#[test]
fn test_rmdir_enotempty() {
let temp = fixtures::test_sandbox("enotempty");
let target = temp.path().join("nonempty");
fs::create_dir(&target).unwrap();
fs::write(target.join("file.txt"), "content").unwrap();
let result = fs::remove_dir(&target);
assert!(result.is_err(), "rmdir should fail on non-empty directory");
// Note: DirectoryNotEmpty is the specific error on most systems
// Some systems return PermissionDenied or Other
let err = result.unwrap_err();
assert!(
matches!(
err.kind(),
std::io::ErrorKind::DirectoryNotEmpty
| std::io::ErrorKind::PermissionDenied
| std::io::ErrorKind::Other
),
"Error should indicate directory not empty, got {:?}",
err.kind()
);
}
/// Test: rmdir fails on file (ENOTDIR)
/// Coq: RmdirPrecondition requires is_directory
#[test]
fn test_rmdir_enotdir() {
let temp = fixtures::test_sandbox("enotdir");
let target = temp.path().join("afile.txt");
fs::write(&target, "content").unwrap();
let result = fs::remove_dir(&target);
assert!(result.is_err(), "rmdir should fail on file");
}
/// Test: rm fails on directory (EISDIR)
/// Coq: DeleteFilePrecondition requires ~is_directory
#[test]
fn test_rm_eisdir() {
let temp = fixtures::test_sandbox("eisdir");
let target = temp.path().join("adir");
fs::create_dir(&target).unwrap();
let result = fs::remove_file(&target);
assert!(result.is_err(), "rm should fail on directory");
}
// ============================================================
// File Content Reversibility Tests
// ============================================================
/// Test: Write file preserves ability to restore original content
/// Lean theorem: writeFileReversible (FileContentOperations.lean)
#[test]
fn test_write_file_reversible() {
let temp = fixtures::test_sandbox("write");
let target = temp.path().join("data.txt");
let original_content = b"original content";
let new_content = b"modified content";
// Create file with original content
fs::write(&target, original_content).unwrap();
// Save original for undo
let saved = fs::read(&target).unwrap();
// Modify file
fs::write(&target, new_content).unwrap();
assert_eq!(fs::read(&target).unwrap(), new_content);
// Restore original (undo)
fs::write(&target, &saved).unwrap();
// Verify restoration
assert_eq!(
fs::read(&target).unwrap(),
original_content,
"Write should be reversible with saved content"
);
}
// ============================================================
// Equivalence Tests
// ============================================================
/// Test: Operations on independent paths don't interfere
/// Lean theorem: mkdirPreservesOtherPaths (FilesystemModel.lean)
#[test]
fn test_operation_independence() {
let temp = fixtures::test_sandbox("independence");
// Create two independent directories
let dir_a = temp.path().join("alpha");
let dir_b = temp.path().join("beta");
fs::create_dir(&dir_a).unwrap();
// Creating dir_b should not affect dir_a
assert!(dir_a.exists());
fs::create_dir(&dir_b).unwrap();
assert!(dir_a.exists(), "Creating beta should not affect alpha");
assert!(dir_b.exists());
// Removing dir_b should not affect dir_a
fs::remove_dir(&dir_b).unwrap();
assert!(dir_a.exists(), "Removing beta should not affect alpha");
assert!(!dir_b.exists());
}
// ============================================================
// Transaction Simulation Tests
// ============================================================
/// Test: Atomic multi-operation rollback
/// Simulates transaction behavior at filesystem level
#[test]
fn test_transaction_rollback_simulation() {
let temp = fixtures::test_sandbox("transaction");
// Record operations for potential rollback
let mut operations: Vec<(&str, PathBuf)> = Vec::new();
// Begin "transaction"
let target1 = temp.path().join("txn_dir1");
let target2 = temp.path().join("txn_dir2");
let target3 = temp.path().join("txn_file.txt");
// Execute operations
fs::create_dir(&target1).unwrap();
operations.push(("mkdir", target1.clone()));
fs::create_dir(&target2).unwrap();
operations.push(("mkdir", target2.clone()));
fs::write(&target3, "transaction data").unwrap();
operations.push(("touch", target3.clone()));
// Verify all exist
assert!(target1.exists());
assert!(target2.exists());
assert!(target3.exists());
// Rollback: reverse all operations
for (op, path) in operations.iter().rev() {
match *op {
"mkdir" => {
fs::remove_dir(path).unwrap();
}
"touch" => {
fs::remove_file(path).unwrap();
}
_ => {}
}
}
// Verify rollback
assert!(
!target1.exists(),
"Rollback should remove all created items"
);
assert!(!target2.exists());
assert!(!target3.exists());
}
// ============================================================
// Edge Cases
// ============================================================
/// Test: Empty file creation and deletion
#[test]
fn test_empty_file() {
let temp = fixtures::test_sandbox("empty");
let target = temp.path().join("empty.txt");
fs::write(&target, "").unwrap();
assert!(target.exists());
assert!(target.is_file());
assert_eq!(fs::metadata(&target).unwrap().len(), 0);
fs::remove_file(&target).unwrap();
assert!(!target.exists());
}
/// Test: Deeply nested path operations
#[test]
fn test_deep_nesting() {
let temp = fixtures::test_sandbox("deep");
// Create deep path
let deep = temp.path().join("a/b/c/d/e/f/g");
fs::create_dir_all(&deep).unwrap();
assert!(deep.exists());
let file = deep.join("deep.txt");
fs::write(&file, "deep content").unwrap();
assert!(file.exists());
// Clean up from deepest
fs::remove_file(&file).unwrap();
// Remove directories from deepest to shallowest
let mut current = deep.clone();
while current != temp.path() {
if current.exists() && fs::read_dir(¤t).unwrap().next().is_none() {
fs::remove_dir(¤t).unwrap();
}
current = current.parent().unwrap().to_path_buf();
}
assert!(
!temp.path().join("a").exists(),
"Deep cleanup should remove all"
);
}
/// Test: Special characters in paths
#[test]
fn test_special_characters() {
let temp = fixtures::test_sandbox("special");
let targets = vec![
"spaces in name",
"unicode_αβγ",
"dots.in.name",
"dash-and-underscore_test",
];
for name in &targets {
let target = temp.path().join(name);
fs::create_dir(&target).unwrap();
assert!(target.exists(), "Should handle special chars: {}", name);
fs::remove_dir(&target).unwrap();
}
}
// ============================================================
// I/O Redirections (Phase 6 M2)
// ============================================================
/// Test: Output redirection via direct Command execution
#[test]
fn test_redirect_output_via_command() {
let temp = fixtures::test_sandbox("redirect_cmd");
// Execute: echo hello (directly via std::process::Command with redirect)
let output_file = temp.path().join("output.txt");
let file = fs::File::create(&output_file).unwrap();
let status = Command::new("echo")
.arg("hello")
.stdout(file)
.status()
.unwrap();
assert!(status.success());
// Verify file was created with correct content
let content = fs::read_to_string(&output_file).unwrap();
assert_eq!(content.trim(), "hello");
}
/// Test: Append redirection via direct Command execution
#[test]
fn test_redirect_append_via_command() {
let temp = fixtures::test_sandbox("redirect_append_cmd");
// Create initial file
let target = temp.path().join("log.txt");
fs::write(&target, "line1\n").unwrap();
// Append via std::process::Command
use std::fs::OpenOptions;
let file = OpenOptions::new()
.create(true)
.append(true)
.open(&target)
.unwrap();
Command::new("echo")
.arg("line2")
.stdout(file)
.status()
.unwrap();
// Verify both lines present
let content = fs::read_to_string(&target).unwrap();
assert!(content.contains("line1"), "Original content should remain");
assert!(content.contains("line2"), "New content should be appended");
}
/// Test: Input redirection via direct Command execution
#[test]
fn test_redirect_input_via_command() {
let temp = fixtures::test_sandbox("redirect_input_cmd");
// Create input file
let input_file = temp.path().join("input.txt");
fs::write(&input_file, "test input data").unwrap();
// Redirect via std::process::Command
let output_file = temp.path().join("output.txt");
let input = fs::File::open(&input_file).unwrap();
let output = fs::File::create(&output_file).unwrap();
Command::new("cat")
.stdin(input)
.stdout(output)
.status()
.unwrap();
// Verify output matches input
let output_content = fs::read_to_string(&output_file).unwrap();
assert_eq!(output_content, "test input data");
}
/// Test: Validation of basic redirection logic
#[test]
fn test_redirect_file_operations() {
let temp = fixtures::test_sandbox("redirect_ops");
// Test file creation
let file1 = temp.path().join("new.txt");
assert!(!file1.exists());
fs::write(&file1, "created").unwrap();
assert!(file1.exists());
// Test file truncation
let original_content = fs::read(&file1).unwrap();
assert_eq!(original_content, b"created");
fs::write(&file1, "truncated").unwrap();
let new_content = fs::read_to_string(&file1).unwrap();
assert_eq!(new_content, "truncated");
// Test reversibility by restoring original
fs::write(&file1, &original_content).unwrap();
let restored = fs::read_to_string(&file1).unwrap();
assert_eq!(restored, "created");
}
/// Test: Append operation tracking
#[test]
fn test_redirect_append_tracking() {
let temp = fixtures::test_sandbox("redirect_append_track");
// Create file
let file = temp.path().join("file.txt");
fs::write(&file, "original").unwrap();
// Record original size
let original_size = fs::metadata(&file).unwrap().len();
assert_eq!(original_size, 8); // "original" = 8 bytes
// Append
use std::io::Write;
let mut f = fs::OpenOptions::new().append(true).open(&file).unwrap();
f.write_all(b" appended").unwrap();
drop(f);
// Verify appended
let content = fs::read_to_string(&file).unwrap();
assert_eq!(content, "original appended");
// Simulate undo: truncate to original size
use std::fs::OpenOptions;
let f = OpenOptions::new().write(true).open(&file).unwrap();
f.set_len(original_size).unwrap();
drop(f);
// Verify truncated back
let restored = fs::read_to_string(&file).unwrap();
assert_eq!(restored, "original");
}
// ============================================================
// Built-in Command Redirections (Phase 6 M2 - Week 2)
// ============================================================
/// Test: pwd output redirection (direct)
#[test]
fn test_builtin_pwd_redirect_direct() {
let temp = fixtures::test_sandbox("pwd_redirect");
// Execute pwd with redirection using direct std::process::Command
let output_file = temp.path().join("pwd_output.txt");
// Simulate: pwd > pwd_output.txt
// Since pwd just prints current dir, we can test the redirection mechanism
let file = fs::File::create(&output_file).unwrap();
let status = Command::new("pwd")
.current_dir(temp.path())
.stdout(file)
.status()
.unwrap();
assert!(status.success(), "pwd should succeed");
// Verify output
let content = fs::read_to_string(&output_file).unwrap();
assert!(
content.contains(&temp.path().to_string_lossy().to_string()),
"Output should contain temp directory path"
);
}
/// Test: ls output redirection (direct)
#[test]
fn test_builtin_ls_redirect_direct() {
let temp = fixtures::test_sandbox("ls_redirect");
// Create test files
fs::create_dir(temp.path().join("testdir")).unwrap();
fs::write(temp.path().join("file.txt"), "").unwrap();
// Execute: ls > listing.txt
let output_file = temp.path().join("listing.txt");
let file = fs::File::create(&output_file).unwrap();
let status = Command::new("ls")
.current_dir(temp.path())
.stdout(file)
.status()
.unwrap();
assert!(status.success(), "ls should succeed");
// Verify listing contains entries
let content = fs::read_to_string(&output_file).unwrap();
assert!(content.contains("testdir"), "Should list testdir");
assert!(content.contains("file.txt"), "Should list file.txt");
}
/// Test: Append redirection behavior
#[test]
fn test_builtin_append_behavior() {
let temp = fixtures::test_sandbox("append_behavior");
// Create initial file
let log = temp.path().join("log.txt");
fs::write(&log, "line1\n").unwrap();
// Append using direct Command
let file = OpenOptions::new()
.create(true)
.append(true)
.open(&log)
.unwrap();
Command::new("echo")
.arg("line2")
.current_dir(temp.path())
.stdout(file)
.status()
.unwrap();
// Verify both lines
let content = fs::read_to_string(&log).unwrap();
assert!(content.contains("line1"), "Original line preserved");
assert!(content.contains("line2"), "New line appended");
}
/// Test: Validation of redirection file creation
#[test]
fn test_redirect_creates_file() {
let temp = fixtures::test_sandbox("redirect_create");
let output = temp.path().join("output.txt");
assert!(!output.exists(), "File should not exist initially");
// Redirect to non-existent file
let file = fs::File::create(&output).unwrap();
Command::new("echo")
.arg("test")
.stdout(file)
.status()
.unwrap();
assert!(output.exists(), "Redirection should create file");
let content = fs::read_to_string(&output).unwrap();
assert_eq!(content.trim(), "test");
}
// ============================================================
// Redirection Undo/Redo Tests (Phase 6 M2 - Undo Integration)
// ============================================================
/// Test: Undo file creation from redirection
#[test]
fn test_redirect_undo_file_created() {
let temp = fixtures::test_sandbox("redirect_undo_create");
let output = temp.path().join("created.txt");
assert!(!output.exists(), "File should not exist initially");
// Simulate: echo hello > created.txt
let file = fs::File::create(&output).unwrap();
Command::new("echo")
.arg("hello")
.current_dir(temp.path())
.stdout(file)
.status()
.unwrap();
assert!(output.exists(), "File should be created");
// Simulate undo by removing the file
fs::remove_file(&output).unwrap();
assert!(!output.exists(), "Undo should remove created file");
}
/// Test: Undo file truncation from redirection
#[test]
fn test_redirect_undo_file_truncated() {
let temp = fixtures::test_sandbox("redirect_undo_truncate");
let target = temp.path().join("existing.txt");
let original_content = "original content";
// Create file with original content
fs::write(&target, original_content).unwrap();
// Save original for undo
let saved_content = fs::read(&target).unwrap();
// Truncate via redirection: echo new > existing.txt
let file = fs::File::create(&target).unwrap();
Command::new("echo")
.arg("new")
.current_dir(temp.path())
.stdout(file)
.status()
.unwrap();
let truncated = fs::read_to_string(&target).unwrap();
assert_ne!(
truncated.trim(),
original_content,
"File should be truncated"
);
// Undo: restore original content
fs::write(&target, &saved_content).unwrap();
let restored = fs::read_to_string(&target).unwrap();
assert_eq!(
restored, original_content,
"Undo should restore original content"
);
}
/// Test: Undo file append from redirection
#[test]
fn test_redirect_undo_file_appended() {
let temp = fixtures::test_sandbox("redirect_undo_append");
let target = temp.path().join("log.txt");
let original_content = "line1\n";
// Create file with original content
fs::write(&target, original_content).unwrap();
// Record original size
let original_size = fs::metadata(&target).unwrap().len();
// Append via redirection: echo line2 >> log.txt
let file = OpenOptions::new()
.create(true)
.append(true)
.open(&target)
.unwrap();
Command::new("echo")
.arg("line2")
.current_dir(temp.path())
.stdout(file)
.status()
.unwrap();
// Verify append happened
let appended = fs::read_to_string(&target).unwrap();
assert!(
appended.len() > original_content.len(),
"File should be larger"
);
assert!(
appended.contains("line2"),
"Appended content should be present"
);
// Undo: truncate to original size
let file = OpenOptions::new().write(true).open(&target).unwrap();
file.set_len(original_size).unwrap();
drop(file);
// Verify undo
let restored = fs::read_to_string(&target).unwrap();
assert_eq!(
restored, original_content,
"Undo should restore original size"
);
}
/// Test: Redo file truncation
#[test]
fn test_redirect_redo_truncate() {
let temp = fixtures::test_sandbox("redirect_redo_truncate");
let target = temp.path().join("file.txt");
fs::write(&target, "original").unwrap();
// Truncate
fs::write(&target, "").unwrap();
assert_eq!(
fs::read_to_string(&target).unwrap(),
"",
"Should be truncated"
);
// Undo (restore)
fs::write(&target, "original").unwrap();
assert_eq!(
fs::read_to_string(&target).unwrap(),
"original",
"Should be restored"
);
// Redo (truncate again)
fs::write(&target, "").unwrap();
assert_eq!(
fs::read_to_string(&target).unwrap(),
"",
"Should be truncated again"
);
}
// ============================================================
// Glob Expansion Tests (Phase 6 M12)
// ============================================================
/// Test: Wildcard glob expansion (*.txt)
#[test]
fn test_glob_wildcard_expansion() {
let temp = fixtures::test_sandbox("glob_wildcard");
// Create test files
fs::write(temp.path().join("file1.txt"), "content1").unwrap();
fs::write(temp.path().join("file2.txt"), "content2").unwrap();
fs::write(temp.path().join("file3.log"), "content3").unwrap();
// Test *.txt pattern via vsh's glob expansion
let matches = vsh::glob::expand_glob("*.txt", temp.path()).unwrap();
let names: Vec<String> = matches.iter().map(|p| p.display().to_string()).collect();
assert!(
names.contains(&"file1.txt".to_string()),
"Should match file1.txt"
);
assert!(
names.contains(&"file2.txt".to_string()),
"Should match file2.txt"
);
assert!(
!names.iter().any(|n| n.contains("file3.log")),
"Should not match .log files"
);
}
/// Test: Question mark glob (file?.txt)
#[test]
fn test_glob_question_mark() {
let temp = fixtures::test_sandbox("glob_question");
// Create test files
fs::write(temp.path().join("file1.txt"), "").unwrap();
fs::write(temp.path().join("file2.txt"), "").unwrap();
fs::write(temp.path().join("file10.txt"), "").unwrap(); // Two chars, shouldn't match
// Test file?.txt pattern via vsh's glob expansion
let matches = vsh::glob::expand_glob("file?.txt", temp.path()).unwrap();
let names: Vec<String> = matches.iter().map(|p| p.display().to_string()).collect();
assert!(
names.contains(&"file1.txt".to_string()),
"Should match single character"
);
assert!(
names.contains(&"file2.txt".to_string()),
"Should match single character"
);
assert!(
!names.contains(&"file10.txt".to_string()),
"Should not match two characters"
);
}
/// Test: Brace expansion (file{1,2,3}.txt)
#[test]
fn test_glob_brace_expansion() {
let temp = fixtures::test_sandbox("glob_brace");
// Create test files
fs::write(temp.path().join("file1.txt"), "").unwrap();
fs::write(temp.path().join("file2.txt"), "").unwrap();
fs::write(temp.path().join("file3.txt"), "").unwrap();
fs::write(temp.path().join("file4.txt"), "").unwrap();
// Test brace expansion via vsh's expand_braces
let expanded = vsh::glob::expand_braces("file{1,2,3}.txt");
assert_eq!(expanded, vec!["file1.txt", "file2.txt", "file3.txt"]);
// Each expanded pattern should match existing files when globbed
for pattern in &expanded {
let matches = vsh::glob::expand_glob(pattern, temp.path()).unwrap();
assert_eq!(
matches.len(),
1,
"Pattern {} should match one file",
pattern
);
}
// file4.txt should not be in the expansion
assert!(
!expanded.contains(&"file4.txt".to_string()),
"Should not include file4.txt"
);
}
/// Test: Empty glob matches return literal (POSIX behavior)
#[test]
fn test_glob_no_matches_literal() {
let temp = fixtures::test_sandbox("glob_no_match");
// Don't create any .xyz files
// Test *.xyz pattern - should pass literal to command
let output = Command::new("echo")
.arg("*.xyz")
.current_dir(temp.path())
.output()
.unwrap();
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("*.xyz"),
"Should pass literal pattern when no matches"
);
}
/// Test: Glob expansion in multiple arguments
#[test]
fn test_glob_multiple_args() {
let temp = fixtures::test_sandbox("glob_multi");
// Create test files
fs::write(temp.path().join("a1.txt"), "").unwrap();
fs::write(temp.path().join("a2.txt"), "").unwrap();
fs::write(temp.path().join("b1.log"), "").unwrap();
fs::write(temp.path().join("b2.log"), "").unwrap();
// Test multiple patterns expanded independently via vsh's glob
let txt_matches = vsh::glob::expand_glob("*.txt", temp.path()).unwrap();
let log_matches = vsh::glob::expand_glob("*.log", temp.path()).unwrap();
let txt_names: Vec<String> = txt_matches
.iter()
.map(|p| p.display().to_string())
.collect();
let log_names: Vec<String> = log_matches
.iter()
.map(|p| p.display().to_string())
.collect();
assert!(
txt_names.contains(&"a1.txt".to_string()),
"Should expand *.txt"
);
assert!(
txt_names.contains(&"a2.txt".to_string()),
"Should expand *.txt"
);
assert!(
log_names.contains(&"b1.log".to_string()),
"Should expand *.log"
);
assert!(
log_names.contains(&"b2.log".to_string()),
"Should expand *.log"
);
}
/// Test: Glob patterns do not expand in quoted strings
#[test]
fn test_glob_no_expansion_in_quotes() {
let temp = fixtures::test_sandbox("glob_quoted");
fs::write(temp.path().join("file.txt"), "").unwrap();
// Test echo "*.txt" (should NOT expand)
let output = Command::new("echo")
.arg("\"*.txt\"") // This would need quote processing in parser
.current_dir(temp.path())
.output()
.unwrap();
let _stdout = String::from_utf8_lossy(&output.stdout);
// Note: This test may need quote handling in parser to work correctly
// For now it documents expected behavior
}
/// Test: Hidden files require explicit dot
#[test]
fn test_glob_hidden_files() {
let temp = fixtures::test_sandbox("glob_hidden");
// Create hidden and visible files
fs::write(temp.path().join(".hidden.txt"), "").unwrap();
fs::write(temp.path().join("visible.txt"), "").unwrap();
// Test *.txt (should NOT match .hidden.txt) via vsh's glob expansion
let matches = vsh::glob::expand_glob("*.txt", temp.path()).unwrap();
let names: Vec<String> = matches.iter().map(|p| p.display().to_string()).collect();
assert!(
names.contains(&"visible.txt".to_string()),
"Should match visible files"
);
assert!(