blob: 1fc16011c973fe6c3e6b5f58e60b063ca099d1db (
plain)
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
|
;;; test-custom-file-buffer-move-buffer-and-file.el --- Tests for cj/move-buffer-and-file -*- lexical-binding: t; -*-
;;; Commentary:
;; Tests for the cj/--move-buffer-and-file function from custom-file-buffer.el
;;
;; This is the internal (non-interactive) implementation that moves both the
;; current buffer and its visited file to a new directory. It handles trailing
;; slashes, preserves file content, updates the visited-file-name, and clears
;; the modified flag. The interactive wrapper cj/move-buffer-and-file handles
;; user prompting and delegates to this implementation.
;;; Code:
(require 'ert)
(require 'testutil-general)
;; Add modules directory to load path
(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
;; Stub dependencies before loading the module
(defvar cj/custom-keymap (make-sparse-keymap)
"Stub keymap for testing.")
;; Stub ps-print package
(provide 'ps-print)
;; Now load the actual production module
(require 'custom-file-buffer)
;;; Setup and Teardown
(defun test-move-buffer-and-file-setup ()
"Setup for move-buffer-and-file tests."
(cj/create-test-base-dir))
(defun test-move-buffer-and-file-teardown ()
"Teardown for move-buffer-and-file tests."
;; Kill all buffers visiting files in test directory
(dolist (buf (buffer-list))
(when (buffer-file-name buf)
(when (string-prefix-p cj/test-base-dir (buffer-file-name buf))
(with-current-buffer buf
(set-buffer-modified-p nil))
(kill-buffer buf))))
(cj/delete-test-base-dir))
;;; Normal Cases
(ert-deftest test-move-buffer-and-file-simple-move-should-succeed ()
"Should move file and buffer to new directory successfully."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir))
(content "Test content"))
(with-temp-file source-file
(insert content))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-updates-buffer-file-name ()
"Should update buffer-file-name to new location."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (string= (buffer-file-name) target-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-preserves-content ()
"Should preserve file content after move."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(content "Original content\nWith multiple lines\n"))
(with-temp-file source-file
(insert content))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (string= (buffer-string) content))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-preserves-buffer-name ()
"Should preserve buffer name after move."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "myfile.txt" source-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(should (string= (buffer-name) "myfile.txt"))
(cj/--move-buffer-and-file target-dir)
(should (string= (buffer-name) "myfile.txt"))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-clears-modified-flag ()
"Should clear buffer modified flag after move."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(insert "modification")
(should (buffer-modified-p))
(cj/--move-buffer-and-file target-dir)
(should-not (buffer-modified-p))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-returns-t-on-success ()
"Should return t on successful move."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(should (eq t (cj/--move-buffer-and-file target-dir)))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-deletes-source-file ()
"Should delete source file after move."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-creates-target-file ()
"Should create file in target directory."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
;;; Boundary Cases - Path Handling
(ert-deftest test-move-buffer-and-file-trailing-slash-should-strip ()
"Should handle directory with trailing slash."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file (concat target-dir "/"))
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-trailing-backslash-should-strip ()
"Should handle directory with trailing backslash."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file (concat target-dir "\\"))
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-no-trailing-slash-should-work ()
"Should work with directory without trailing slash."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-deeply-nested-target ()
"Should move to deeply nested target directory."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "a/b/c/d/target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-relative-path-should-work ()
"Should resolve relative paths relative to file's directory."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
;; Use "../target" to go up from source/ to target/
(cj/--move-buffer-and-file "../target")
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
;;; Boundary Cases - Character Encoding
(ert-deftest test-move-buffer-and-file-unicode-filename ()
"Should handle Unicode characters in filename."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test-café.txt" source-dir))
(target-file (expand-file-name "test-café.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-unicode-directory ()
"Should handle Unicode characters in directory name."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target-ñoño"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-emoji-in-filename ()
"Should handle emoji in filename."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test-🎉-file.txt" source-dir))
(target-file (expand-file-name "test-🎉-file.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-rtl-characters ()
"Should handle RTL text in filename."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test-مرحبا.txt" source-dir))
(target-file (expand-file-name "test-مرحبا.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-spaces-in-filename ()
"Should handle spaces in filename."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test file with spaces.txt" source-dir))
(target-file (expand-file-name "test file with spaces.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-special-chars-in-filename ()
"Should handle special characters in filename."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test[file]-(1).txt" source-dir))
(target-file (expand-file-name "test[file]-(1).txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
;;; Boundary Cases - File Naming
(ert-deftest test-move-buffer-and-file-hidden-file ()
"Should handle hidden files (starting with dot)."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name ".hidden" source-dir))
(target-file (expand-file-name ".hidden" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-no-extension ()
"Should handle files without extensions."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "README" source-dir))
(target-file (expand-file-name "README" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-multiple-dots-in-name ()
"Should handle multiple dots in filename."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "my.file.name.test.txt" source-dir))
(target-file (expand-file-name "my.file.name.test.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-single-char-filename ()
"Should handle single character filenames."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "x" source-dir))
(target-file (expand-file-name "x" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-very-long-filename ()
"Should handle very long filenames."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(long-name (concat (make-string 200 ?x) ".txt"))
(source-file (expand-file-name long-name source-dir))
(target-file (expand-file-name long-name target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-very-long-path ()
"Should handle very long paths."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((long-dir (make-string 100 ?x))
(source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory long-dir))
(long-filename (concat (make-string 100 ?y) ".txt"))
(source-file (expand-file-name long-filename source-dir))
(target-file (expand-file-name long-filename target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
;;; Boundary Cases - File Content
(ert-deftest test-move-buffer-and-file-empty-file ()
"Should move empty file successfully."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "empty.txt" source-dir))
(target-file (expand-file-name "empty.txt" target-dir)))
(with-temp-file source-file)
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(should (= 0 (buffer-size)))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-large-file ()
"Should move large file successfully."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "large.txt" source-dir))
(large-content (make-string 100000 ?x)))
(with-temp-file source-file
(insert large-content))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (string= (buffer-string) large-content))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-binary-file ()
"Should move binary-like content successfully."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "binary.dat" source-dir))
(target-file (expand-file-name "binary.dat" target-dir))
(binary-content (string 0 1 2 3 255 254 253)))
(with-temp-file source-file
(set-buffer-multibyte nil)
(insert binary-content))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-preserves-newlines ()
"Should preserve different newline types."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "newlines.txt" source-dir))
(content "Line 1\nLine 2\n\nLine 4\n"))
(with-temp-file source-file
(insert content))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (string= (buffer-string) content))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-preserves-encoding ()
"Should preserve UTF-8 encoded content."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "utf8.txt" source-dir))
(content "Hello 世界 مرحبا Привет"))
(with-temp-file source-file
(insert content))
(find-file source-file)
(cj/--move-buffer-and-file target-dir)
(should (string= (buffer-string) content))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
;;; Boundary Cases - Buffer State
(ert-deftest test-move-buffer-and-file-with-unsaved-changes ()
"Should handle buffer with unsaved changes."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir))
(original "original"))
(with-temp-file source-file
(insert original))
(find-file source-file)
(insert " modified")
(should (buffer-modified-p))
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (buffer-modified-p))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-with-multiple-windows ()
"Should work when buffer is displayed in multiple windows."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(delete-other-windows)
(split-window)
(other-window 1)
(switch-to-buffer (get-file-buffer source-file))
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(kill-buffer (current-buffer))
(delete-other-windows))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-preserves-point-position ()
"Should preserve point position in buffer."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(content "Line 1\nLine 2\nLine 3\n"))
(with-temp-file source-file
(insert content))
(find-file source-file)
(goto-char (point-min))
(forward-line 1)
(let ((original-point (point)))
(cj/--move-buffer-and-file target-dir)
(should (= (point) original-point)))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-preserves-mark ()
"Should preserve mark in buffer."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(content "Line 1\nLine 2\nLine 3\n"))
(with-temp-file source-file
(insert content))
(find-file source-file)
(goto-char (point-min))
(set-mark (point))
(forward-line 2)
(let ((original-mark (mark)))
(cj/--move-buffer-and-file target-dir)
(should (= (mark) original-mark)))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
;;; Error Cases - Buffer Issues
(ert-deftest test-move-buffer-and-file-non-file-buffer-returns-nil ()
"Should return nil when buffer not visiting a file."
(test-move-buffer-and-file-setup)
(unwind-protect
(let ((target-dir (cj/create-test-subdirectory "target")))
(with-temp-buffer
(rename-buffer "non-file-buffer" t)
(let ((result (cj/--move-buffer-and-file target-dir)))
(should-not result))))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-scratch-buffer-returns-nil ()
"Should return nil for scratch buffer."
(test-move-buffer-and-file-setup)
(unwind-protect
(let ((target-dir (cj/create-test-subdirectory "target")))
(with-current-buffer "*scratch*"
(let ((result (cj/--move-buffer-and-file target-dir)))
(should-not result))))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-killed-buffer-should-error ()
"Should error when operating on killed buffer."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(buf nil))
(with-temp-file source-file
(insert "content"))
(setq buf (find-file source-file))
(kill-buffer buf)
(should-error
(with-current-buffer buf
(cj/--move-buffer-and-file target-dir))))
(test-move-buffer-and-file-teardown)))
;;; Error Cases - Directory Issues
(ert-deftest test-move-buffer-and-file-nonexistent-target-should-error ()
"Should error when target directory doesn't exist."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(source-file (expand-file-name "test.txt" source-dir))
(nonexistent-dir (expand-file-name "nonexistent" cj/test-base-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(should-error (cj/--move-buffer-and-file nonexistent-dir))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-target-is-file-not-dir-should-error ()
"Should error when target is a file, not directory."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "notadir.txt" cj/test-base-dir)))
(with-temp-file target-file
(insert "I'm a file"))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(should-error (cj/--move-buffer-and-file target-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-nil-directory-should-error ()
"Should error when directory is nil."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(source-file (expand-file-name "test.txt" source-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(should-error (cj/--move-buffer-and-file nil))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-empty-string-directory-should-error ()
"Should error when directory is empty string."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(source-file (expand-file-name "test.txt" source-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(should-error (cj/--move-buffer-and-file ""))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
;;; Error Cases - Permission Issues
(ert-deftest test-move-buffer-and-file-no-read-permission-source-should-error ()
"Should error when source file is not readable."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(set-file-modes source-file #o000)
(should-error (cj/--move-buffer-and-file target-dir))
(set-file-modes source-file #o644)
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-no-write-permission-target-should-error ()
"Should error when target directory is not writable."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir)))
(with-temp-file source-file
(insert "content"))
(set-file-modes target-dir #o555)
(find-file source-file)
(should-error (cj/--move-buffer-and-file target-dir))
(set-file-modes target-dir #o755)
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-no-delete-permission-source-should-error ()
"Should error when source directory doesn't allow deletion."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(set-file-modes source-dir #o555)
(should-error (cj/--move-buffer-and-file target-dir))
(set-file-modes source-dir #o755)
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
;;; Error Cases - File Conflicts
(ert-deftest test-move-buffer-and-file-target-exists-should-overwrite ()
"Should overwrite existing file when ok-if-exists is t."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir))
(new-content "New content")
(old-content "Old content"))
(with-temp-file target-file
(insert old-content))
(with-temp-file source-file
(insert new-content))
(find-file source-file)
(cj/--move-buffer-and-file target-dir t)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(revert-buffer t t)
(should (string= (buffer-string) new-content))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-target-exists-should-error-if-not-ok ()
"Should error when target exists and ok-if-exists is nil."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir)))
(with-temp-file target-file
(insert "existing"))
(with-temp-file source-file
(insert "new"))
(find-file source-file)
(should-error (cj/--move-buffer-and-file target-dir nil))
;; Source should still exist since move failed
(should (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-interactive-prompts-if-target-exists ()
"Should prompt user when called interactively and target exists."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir))
(prompted nil))
(with-temp-file target-file
(insert "existing"))
(with-temp-file source-file
(insert "new"))
(find-file source-file)
;; Mock yes-or-no-p to capture that it was called
(cl-letf (((symbol-function 'yes-or-no-p)
(lambda (prompt)
(setq prompted t)
t))
((symbol-function 'read-directory-name)
(lambda (&rest _) target-dir)))
(call-interactively #'cj/move-buffer-and-file)
(should prompted))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-interactive-no-prompt-if-target-missing ()
"Should not prompt when called interactively if target doesn't exist."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(prompted nil))
(with-temp-file source-file
(insert "new"))
(find-file source-file)
;; Mock yes-or-no-p to capture if it was called
(cl-letf (((symbol-function 'yes-or-no-p)
(lambda (prompt)
(setq prompted t)
t))
((symbol-function 'read-directory-name)
(lambda (&rest _) target-dir)))
(call-interactively #'cj/move-buffer-and-file)
(should-not prompted))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-source-deleted-during-operation-should-error ()
"Should error if source file is deleted during operation."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(delete-file source-file)
(should-error (cj/--move-buffer-and-file target-dir))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
;;; Error Cases - Edge Cases
(ert-deftest test-move-buffer-and-file-symlink-source-should-handle ()
"Should handle symbolic link as source."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(real-file (expand-file-name "real.txt" source-dir))
(symlink (expand-file-name "link.txt" source-dir))
(target-file (expand-file-name "link.txt" target-dir)))
(with-temp-file real-file
(insert "content"))
(make-symbolic-link real-file symlink)
(find-file symlink)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(ert-deftest test-move-buffer-and-file-read-only-buffer-should-still-work ()
"Should work even if buffer is read-only."
(test-move-buffer-and-file-setup)
(unwind-protect
(let* ((source-dir (cj/create-test-subdirectory "source"))
(target-dir (cj/create-test-subdirectory "target"))
(source-file (expand-file-name "test.txt" source-dir))
(target-file (expand-file-name "test.txt" target-dir)))
(with-temp-file source-file
(insert "content"))
(find-file source-file)
(read-only-mode 1)
(cj/--move-buffer-and-file target-dir)
(should (file-exists-p target-file))
(should-not (file-exists-p source-file))
(kill-buffer (current-buffer)))
(test-move-buffer-and-file-teardown)))
(provide 'test-custom-file-buffer-move-buffer-and-file)
;;; test-custom-file-buffer-move-buffer-and-file.el ends here
|