-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathassert.file.bash
More file actions
973 lines (848 loc) · 25.4 KB
/
Copy pathassert.file.bash
File metadata and controls
973 lines (848 loc) · 25.4 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
#!/usr/bin/env bash
##
# @file
# Assertions for files and directories.
#
# shellcheck disable=SC2119,SC2120,SC2044,SC2086
##
# Asserts that a file exists.
#
# Arguments:
# 1. file: Path to check. May be a glob, in which case the first match is
# taken.
##
assert_file_exists() {
local file="${1}"
local f
# The literal path is tried first, because the glob expansion below is
# unquoted and would word-split a path that contains a space.
[ -e "${file}" ] && return 0
for f in ${file}; do
if [ -e "${f}" ]; then
return 0
fi
# Only the first match decides the outcome.
break
done
format_error "File does not exist" "file" "${file}" | flunk
}
##
# Asserts that a file does not exist.
#
# Arguments:
# 1. file: Path to check. May be a glob, in which case the first match is
# taken.
##
assert_file_not_exists() {
local file="${1}"
local f
# The literal path is tried first, because the glob expansion below is
# unquoted and would word-split a path that contains a space.
if [ -e "${file}" ]; then
format_error "File exists, but should not" "file" "${file}" | flunk
return 1
fi
for f in ${file}; do
if [ -e "${f}" ]; then
format_error "File exists, but should not" "file" "${file}" | flunk
return 1
fi
# Only the first match decides the outcome.
break
done
return 0
}
##
# Asserts that a directory exists.
#
# Arguments:
# 1. dir: Path to check. Optional, defaults to the current directory.
##
assert_dir_exists() {
local dir="${1:-$(pwd)}"
if [ -d "${dir}" ]; then
return 0
else
format_error "Directory does not exist" "directory" "${dir}" | flunk
fi
}
##
# Asserts that a directory does not exist.
#
# Arguments:
# 1. dir: Path to check. Optional, defaults to the current directory.
##
assert_dir_not_exists() {
local dir="${1:-$(pwd)}"
if [ -d "${dir}" ]; then
format_error "Directory exists, but should not" "directory" "${dir}" | flunk
else
return 0
fi
}
##
# Asserts that a directory exists and is empty.
#
# Arguments:
# 1. dir: Path to check. Optional, defaults to the current directory.
##
assert_dir_empty() {
local dir="${1:-$(pwd)}"
assert_dir_exists "${dir}" || return 1
if [ -n "$(ls -A "${dir}")" ]; then
format_error "Directory is not empty" "directory" "${dir}" | flunk
else
return 0
fi
}
##
# Asserts that a directory exists and is not empty.
#
# Arguments:
# 1. dir: Path to check. Optional, defaults to the current directory.
##
assert_dir_not_empty() {
local dir="${1:-$(pwd)}"
assert_dir_exists "${dir}" || return 1
if [ -n "$(ls -A "${dir}")" ]; then
return 0
else
format_error "Directory is empty, but should not be" "directory" "${dir}" | flunk
fi
}
##
# Asserts that a symlink exists.
#
# Arguments:
# 1. file: Path to check.
##
assert_symlink_exists() {
local file="${1}"
if [ ! -h "${file}" ] && [ -f "${file}" ]; then
format_error "Regular file exists, but should be a symlink" "file" "${file}" | flunk
elif [ ! -h "${file}" ]; then
format_error "Symlink does not exist" "file" "${file}" | flunk
else
return 0
fi
}
##
# Asserts that a symlink does not exist.
#
# A regular file at the same path satisfies this assertion.
#
# Arguments:
# 1. file: Path to check.
##
assert_symlink_not_exists() {
local file="${1}"
if [ ! -h "${file}" ]; then
return 0
else
format_error "Symlink exists, but should not" "file" "${file}" | flunk
fi
}
##
# Asserts that a file has a permission mode.
#
# The actual mode is masked with the standard umask before comparison, so the
# group and other write bits are ignored.
#
# Arguments:
# 1. file: Path to check.
# 2. perm: Expected three-digit octal mode, such as '644'.
##
assert_file_mode() {
local file="${1}"
local perm="${2}"
local parsed
assert_file_exists "${file}" || return 1
if [ "$(uname)" = "Darwin" ]; then
parsed="$(printf "%.3o\n" $(($(stat -f '0%Lp' "${file}") & ~0022)))"
else
parsed="$(printf "%.3o\n" $(($(stat --printf '0%a' "${file}") & ~0022)))"
fi
if [ "${parsed}" != "${perm}" ]; then
format_error "File does not have the expected permissions" "file" "${file}" "expected" "${perm}" "actual" "${parsed}" | flunk
else
return 0
fi
}
##
# Asserts that a needle matches the contents of a file.
#
# A negated assertion passes for a file that does not exist, which cannot hold
# the needle; a positive one asserts that the file exists first. Only a regular
# file has contents to read, so a directory or a glob is rejected rather than
# handed to 'cat'.
#
# Arguments:
# 1. subject: What the haystack is reported as - 'file'.
# 2. anchor: Where the needle must sit - 'anywhere', 'start' or 'end'.
# 3. negate: '1' to assert that the needle does not match.
# 4. mode: How the needle is read - 'literal', 'regex' or 'format'.
# 5. case_sensitive: '1' to match case-sensitively, '0' to ignore case.
# 6. file: File to search.
# 7. string: String to search for.
##
_file_assert_match() {
if [ "$#" -ne 7 ]; then
flunk "A file and a string are required."
return 1
fi
local negate="${3}"
local file="${6}"
if [ "${negate}" = "1" ]; then
[ ! -f "${file}" ] && return 0
else
assert_file_exists "${file}" || return 1
if [ ! -f "${file}" ]; then
format_error "File is not a regular file" "file" "${file}" | flunk
return 1
fi
fi
local contents
contents="$(cat "${file}")"
_string_assert_match "${1}" "${2}" "${3}" "${4}" "${5}" "${contents}" "${7}" "${file}"
}
##
# Asserts that a file exists and contains a string, ignoring case.
#
# Arguments:
# 1. file: File to search.
# 2. string: String to search for.
##
assert_file_contains() {
_file_assert_match "file" "anywhere" 0 "literal" 0 "$@"
}
##
# Asserts that a file exists and contains a string, case-sensitively.
#
# Arguments:
# 1. file: File to search.
# 2. string: String to search for.
##
assert_file_contains_case() {
_file_assert_match "file" "anywhere" 0 "literal" 1 "$@"
}
##
# Asserts that a file does not contain a string, ignoring case.
#
# A file that does not exist cannot contain the string, so it passes.
#
# Arguments:
# 1. file: File to search.
# 2. string: String to search for.
##
assert_file_not_contains() {
_file_assert_match "file" "anywhere" 1 "literal" 0 "$@"
}
##
# Asserts that a file does not contain a string, case-sensitively.
#
# A file that does not exist cannot contain the string, so it passes.
#
# Arguments:
# 1. file: File to search.
# 2. string: String to search for.
##
assert_file_not_contains_case() {
_file_assert_match "file" "anywhere" 1 "literal" 1 "$@"
}
##
# Asserts that a file exists and matches a regular expression, ignoring case.
#
# Arguments:
# 1. file: File to search.
# 2. string: Extended regular expression to match.
##
assert_file_matches() {
_file_assert_match "file" "anywhere" 0 "regex" 0 "$@"
}
##
# Asserts that a file exists and matches a regular expression, case-sensitively.
#
# Arguments:
# 1. file: File to search.
# 2. string: Extended regular expression to match.
##
assert_file_matches_case() {
_file_assert_match "file" "anywhere" 0 "regex" 1 "$@"
}
##
# Asserts that a file does not match a regular expression, ignoring case.
#
# A file that does not exist cannot match, so it passes.
#
# Arguments:
# 1. file: File to search.
# 2. string: Extended regular expression to match.
##
assert_file_not_matches() {
_file_assert_match "file" "anywhere" 1 "regex" 0 "$@"
}
##
# Asserts that a file does not match a regular expression, case-sensitively.
#
# A file that does not exist cannot match, so it passes.
#
# Arguments:
# 1. file: File to search.
# 2. string: Extended regular expression to match.
##
assert_file_not_matches_case() {
_file_assert_match "file" "anywhere" 1 "regex" 1 "$@"
}
##
# Asserts that a file exists and matches a format string, ignoring case.
#
# Arguments:
# 1. file: File to search.
# 2. string: Format string, see 'string_format_to_regex'.
##
assert_file_matches_format() {
_file_assert_match "file" "anywhere" 0 "format" 0 "$@"
}
##
# Asserts that a file exists and matches a format string, case-sensitively.
#
# Arguments:
# 1. file: File to search.
# 2. string: Format string, see 'string_format_to_regex'.
##
assert_file_matches_format_case() {
_file_assert_match "file" "anywhere" 0 "format" 1 "$@"
}
##
# Asserts that a file does not match a format string, ignoring case.
#
# A file that does not exist cannot match, so it passes.
#
# Arguments:
# 1. file: File to search.
# 2. string: Format string, see 'string_format_to_regex'.
##
assert_file_not_matches_format() {
_file_assert_match "file" "anywhere" 1 "format" 0 "$@"
}
##
# Asserts that a file does not match a format string, case-sensitively.
#
# A file that does not exist cannot match, so it passes.
#
# Arguments:
# 1. file: File to search.
# 2. string: Format string, see 'string_format_to_regex'.
##
assert_file_not_matches_format_case() {
_file_assert_match "file" "anywhere" 1 "format" 1 "$@"
}
##
# Writes the directory names excluded from a recursive content search.
#
# Globals:
# BATS_HELPERS_ASSERT_DIR_EXCLUDE: Additional directory names to exclude from
# the search.
# ASSERT_DIR_EXCLUDE: Deprecated name of the above.
# BATS_HELPERS_DEPRECATION_QUIET: Non-empty suppresses this library's
# deprecation notices.
#
# Outputs:
# STDOUT: One directory name per line, so that a name containing spaces
# survives being read back into an array.
##
_file_dir_exclude_names() {
local -a exclude_dirs=(".git" ".idea" "vendor" "node_modules")
if [ -n "${BATS_HELPERS_ASSERT_DIR_EXCLUDE+x}" ]; then
exclude_dirs+=("${BATS_HELPERS_ASSERT_DIR_EXCLUDE[@]}")
elif [ -n "${ASSERT_DIR_EXCLUDE+x}" ]; then
[ -n "${BATS_HELPERS_DEPRECATION_QUIET-}" ] || echo "Deprecated: 'ASSERT_DIR_EXCLUDE' will be removed in v2.1. Use 'BATS_HELPERS_ASSERT_DIR_EXCLUDE' instead." >&3
exclude_dirs+=("${ASSERT_DIR_EXCLUDE[@]}")
fi
local exclude_dir
for exclude_dir in "${exclude_dirs[@]}"; do
[ -n "${exclude_dir}" ] && printf '%s\n' "${exclude_dir}"
done
return 0
}
##
# Writes the paths of the files of a directory whose contents a needle matches.
#
# Regular files only are searched, and binary files are skipped. The contents
# of each file are matched as one string, so a needle may span lines and the
# '^' and '$' anchors of a regular expression apply to the whole file.
#
# Arguments:
# 1. mode: How the needle is read - 'literal' or 'regex'.
# 2. case_sensitive: '1' to match case-sensitively, '0' to ignore case.
# 3. dir: Directory to search.
# 4. needle: String to search for.
#
# Globals:
# BATS_HELPERS_ASSERT_DIR_EXCLUDE: Additional directory names to exclude from
# the search.
#
# Outputs:
# STDOUT: One matching path per line.
##
_file_dir_match_files() {
local mode="${1}"
local case_sensitive="${2}"
local dir="${3}"
local needle="${4}"
local -a exclude_names
mapfile -t exclude_names < <(_file_dir_exclude_names)
local -a prune=()
local name
for name in "${exclude_names[@]}"; do
[ "${#prune[@]}" -gt 0 ] && prune+=(-o)
prune+=(-name "${name}")
done
local file
local contents
while IFS= read -r -d '' file; do
# Reading a binary file into a Bash string would stop at its first NUL, so
# binaries are told apart first, with the same probe the fixture serialiser
# uses.
if [ -s "${file}" ] && ! LC_ALL=C grep -Iq '^' "${file}"; then
continue
fi
contents="$(cat "${file}")"
if string_match "${contents}" "${needle}" "${mode}" "${case_sensitive}" "anywhere"; then
printf '%s\n' "${file}"
fi
done < <(find "${dir}" -type d \( "${prune[@]}" \) -prune -o -type f -print0)
return 0
}
##
# Asserts that a needle matches the contents of the files of a directory.
#
# A negated assertion passes for a directory that does not exist, which cannot
# hold the needle; a positive one asserts that the directory exists first. The
# files the needle matched are reported on the negated polarity, where they are
# the evidence.
#
# Arguments:
# 1. negate: '1' to assert that the needle matches no file.
# 2. mode: How the needle is read - 'literal' or 'regex'.
# 3. case_sensitive: '1' to match case-sensitively, '0' to ignore case.
# 4. dir: Directory to search.
# 5. needle: String to search for.
#
# Globals:
# BATS_HELPERS_ASSERT_DIR_EXCLUDE: Additional directory names to exclude from
# the search.
##
_file_assert_dir_match() {
if [ "$#" -ne 5 ]; then
flunk "A directory and a string are required."
return 1
fi
local negate="${1}"
local mode="${2}"
local case_sensitive="${3}"
local dir="${4}"
local needle="${5}"
if [ "${negate}" = "1" ]; then
[ ! -d "${dir}" ] && return 0
else
assert_dir_exists "${dir}" || return 1
fi
# An expression that does not compile is an error rather than a mismatch, and
# is checked up front so that it cannot pass silently over a tree with no
# file to fail against.
if [ "${mode}" = "regex" ]; then
local needle_status=0
string_match "" "${needle}" "regex" "${case_sensitive}" "anywhere" || needle_status=$?
if [ "${needle_status}" -eq 2 ]; then
flunk "Invalid regular expression '${needle}'."
return 1
fi
fi
local files
files="$(_file_dir_match_files "${mode}" "${case_sensitive}" "${dir}" "${needle}")"
if [ "${negate}" = "1" ]; then
[ -z "${files}" ] && return 0
else
[ -n "${files}" ] && return 0
fi
##
## Failure report.
##
local noun
noun="$(_string_needle_noun "${mode}")"
local verb="contain"
local verb_third_person="contains"
if [ "${mode}" != "literal" ]; then
verb="match"
verb_third_person="matches"
fi
local title
if [ "${negate}" = "1" ]; then
title="Directory ${verb_third_person} ${noun}, but should not"
else
title="Directory does not ${verb} ${noun}"
fi
# The setting that was in force is only worth naming when the other one would
# have decided the assertion the other way.
local opposite_case=$((1 - case_sensitive))
local opposite_files
opposite_files="$(_file_dir_match_files "${mode}" "${opposite_case}" "${dir}" "${needle}")"
local matched=0
[ -n "${files}" ] && matched=1
local opposite_matched=0
[ -n "${opposite_files}" ] && opposite_matched=1
local case_decided=0
[ "${matched}" -ne "${opposite_matched}" ] && case_decided=1
local -a footer=()
mapfile -t footer < <(_string_match_rows "${mode}" "${case_sensitive}" "${case_decided}")
if [ "${negate}" = "1" ]; then
format_error "${title}" "directory" "${dir}" "${noun}" "${needle}" "${footer[@]}" "files" "${files}" | flunk
return 1
fi
format_error "${title}" "directory" "${dir}" "${noun}" "${needle}" "${footer[@]}" | flunk
}
##
# Asserts that a directory exists and contains a string in one of its files,
# ignoring case.
#
# Binary files are skipped. '.git', '.idea', 'vendor' and 'node_modules' are
# always excluded from the search.
#
# Arguments:
# 1. dir: Directory to search.
# 2. string: String to search for.
#
# Globals:
# BATS_HELPERS_ASSERT_DIR_EXCLUDE: Additional directory names to exclude from
# the search.
##
assert_dir_contains_string() {
_file_assert_dir_match 0 "literal" 0 "$@"
}
##
# Asserts that a directory exists and contains a string in one of its files,
# case-sensitively.
#
# Binary files are skipped. '.git', '.idea', 'vendor' and 'node_modules' are
# always excluded from the search.
#
# Arguments:
# 1. dir: Directory to search.
# 2. string: String to search for.
#
# Globals:
# BATS_HELPERS_ASSERT_DIR_EXCLUDE: Additional directory names to exclude from
# the search.
##
assert_dir_contains_string_case() {
_file_assert_dir_match 0 "literal" 1 "$@"
}
##
# Asserts that a directory does not contain a string in any of its files,
# ignoring case.
#
# Binary files are skipped. '.git', '.idea', 'vendor' and 'node_modules' are
# always excluded from the search. A directory that does not exist cannot
# contain the string, so it passes.
#
# Arguments:
# 1. dir: Directory to search.
# 2. string: String to search for.
#
# Globals:
# BATS_HELPERS_ASSERT_DIR_EXCLUDE: Additional directory names to exclude from
# the search.
##
assert_dir_not_contains_string() {
_file_assert_dir_match 1 "literal" 0 "$@"
}
##
# Asserts that a directory does not contain a string in any of its files,
# case-sensitively.
#
# Binary files are skipped. '.git', '.idea', 'vendor' and 'node_modules' are
# always excluded from the search. A directory that does not exist cannot
# contain the string, so it passes.
#
# Arguments:
# 1. dir: Directory to search.
# 2. string: String to search for.
#
# Globals:
# BATS_HELPERS_ASSERT_DIR_EXCLUDE: Additional directory names to exclude from
# the search.
##
assert_dir_not_contains_string_case() {
_file_assert_dir_match 1 "literal" 1 "$@"
}
##
# Asserts that a directory exists and the contents of one of its files match a
# regular expression, ignoring case.
#
# The expression is matched against file contents rather than file names.
# Binary files are skipped. '.git', '.idea', 'vendor' and 'node_modules' are
# always excluded from the search.
#
# Arguments:
# 1. dir: Directory to search.
# 2. string: Extended regular expression to match.
#
# Globals:
# BATS_HELPERS_ASSERT_DIR_EXCLUDE: Additional directory names to exclude from
# the search.
##
assert_dir_matches() {
_file_assert_dir_match 0 "regex" 0 "$@"
}
##
# Asserts that a directory exists and the contents of one of its files match a
# regular expression, case-sensitively.
#
# The expression is matched against file contents rather than file names.
# Binary files are skipped. '.git', '.idea', 'vendor' and 'node_modules' are
# always excluded from the search.
#
# Arguments:
# 1. dir: Directory to search.
# 2. string: Extended regular expression to match.
#
# Globals:
# BATS_HELPERS_ASSERT_DIR_EXCLUDE: Additional directory names to exclude from
# the search.
##
assert_dir_matches_case() {
_file_assert_dir_match 0 "regex" 1 "$@"
}
##
# Asserts that no file of a directory matches a regular expression, ignoring
# case.
#
# The expression is matched against file contents rather than file names.
# Binary files are skipped. '.git', '.idea', 'vendor' and 'node_modules' are
# always excluded from the search. A directory that does not exist cannot
# match, so it passes.
#
# Arguments:
# 1. dir: Directory to search.
# 2. string: Extended regular expression to match.
#
# Globals:
# BATS_HELPERS_ASSERT_DIR_EXCLUDE: Additional directory names to exclude from
# the search.
##
assert_dir_not_matches() {
_file_assert_dir_match 1 "regex" 0 "$@"
}
##
# Asserts that no file of a directory matches a regular expression,
# case-sensitively.
#
# The expression is matched against file contents rather than file names.
# Binary files are skipped. '.git', '.idea', 'vendor' and 'node_modules' are
# always excluded from the search. A directory that does not exist cannot
# match, so it passes.
#
# Arguments:
# 1. dir: Directory to search.
# 2. string: Extended regular expression to match.
#
# Globals:
# BATS_HELPERS_ASSERT_DIR_EXCLUDE: Additional directory names to exclude from
# the search.
##
assert_dir_not_matches_case() {
_file_assert_dir_match 1 "regex" 1 "$@"
}
##
# Raises the error a comparison command reported instead of comparing files.
#
# 'diff' and 'cmp' both answer "the files differ" with status 1 and report a
# failure of their own with a higher one. Reading the two the same way would let
# an unreadable file satisfy an assertion that the files differ.
#
# Arguments:
# 1. status: Status the comparison command exited with.
# 2. message: What it wrote while exiting.
#
# Returns:
# 0 when the status is a comparison result, non-zero via 'flunk' when it is
# not.
##
_file_compare_failed() {
[ "${1}" -le 1 ] && return 0
flunk "Unable to compare the files. ${2}"
}
##
# Asserts that the contents of two text files are equal.
#
# Arguments:
# 1. file1: First file.
# 2. file2: Second file.
# 3. ignore_spaces: Set to '1' to ignore blank lines and whitespace changes.
# Optional, defaults to '0'. Deprecated, use
# 'assert_files_equal_ignore_spaces' instead.
#
# Globals:
# BATS_HELPERS_DEPRECATION_QUIET: Non-empty suppresses this library's
# deprecation notices.
##
assert_files_equal() {
local ignore_spaces="${3:-0}"
if [ "${ignore_spaces}" = 1 ]; then
[ -n "${BATS_HELPERS_DEPRECATION_QUIET-}" ] || echo "Deprecated: the 'ignore_spaces' argument of 'assert_files_equal' will be removed in v2.1. Use 'assert_files_equal_ignore_spaces' instead." >&3
fi
_file_assert_equal 0 "${ignore_spaces}" "${1}" "${2}"
}
##
# Asserts that two text files are equal, ignoring whitespace.
#
# Arguments:
# 1. file1: First file.
# 2. file2: Second file.
##
assert_files_equal_ignore_spaces() {
_file_assert_equal 0 1 "${1}" "${2}"
}
##
# Asserts that the contents of two text files are not equal.
#
# Arguments:
# 1. file1: First file.
# 2. file2: Second file.
# 3. ignore_spaces: Set to '1' to ignore blank lines and whitespace changes.
# Optional, defaults to '0'. Deprecated, use
# 'assert_files_not_equal_ignore_spaces' instead.
#
# Globals:
# BATS_HELPERS_DEPRECATION_QUIET: Non-empty suppresses this library's
# deprecation notices.
##
assert_files_not_equal() {
local ignore_spaces="${3:-0}"
if [ "${ignore_spaces}" = 1 ]; then
[ -n "${BATS_HELPERS_DEPRECATION_QUIET-}" ] || echo "Deprecated: the 'ignore_spaces' argument of 'assert_files_not_equal' will be removed in v2.1. Use 'assert_files_not_equal_ignore_spaces' instead." >&3
fi
_file_assert_equal 1 "${ignore_spaces}" "${1}" "${2}"
}
##
# Asserts that two text files are not equal, ignoring whitespace.
#
# Arguments:
# 1. file1: First file.
# 2. file2: Second file.
##
assert_files_not_equal_ignore_spaces() {
_file_assert_equal 1 1 "${1}" "${2}"
}
##
# Asserts that two text files are equal, reporting the difference on failure.
#
# Arguments:
# 1. negate: '1' to assert that the files differ.
# 2. ignore_spaces: '1' to ignore blank lines and whitespace changes.
# 3. file1: First file.
# 4. file2: Second file.
##
_file_assert_equal() {
local negate="${1}"
local ignore_spaces="${2}"
local file1="${3}"
local file2="${4}"
local -a diff_opts=(--normal)
[ "${ignore_spaces}" = 1 ] && diff_opts+=(-B -b)
assert_file_exists "${file1}" || return 1
assert_file_exists "${file2}" || return 1
local difference
local compare_status=0
difference="$(diff "${diff_opts[@]}" "${file1}" "${file2}" 2>&1)" || compare_status=$?
# A comparison that could not run is checked before the outcome, so that its
# status is never read as a difference between the files.
_file_compare_failed "${compare_status}" "${difference}" || return 1
if [ "${negate}" = 1 ]; then
[ "${compare_status}" -ne 0 ] && return 0
format_error "Files are equal, but should not be" "file" "${file1}" "other file" "${file2}" | flunk
return 1
fi
[ "${compare_status}" -eq 0 ] && return 0
format_error "Files are not equal" "file" "${file1}" "other file" "${file2}" "difference" "${difference}" | flunk
return 1
}
##
# Asserts that the contents of two binary files are equal.
#
# Arguments:
# 1. file1: First file.
# 2. file2: Second file.
##
assert_binary_files_equal() {
local file1="${1}"
local file2="${2}"
assert_file_exists "${file1}" || return 1
assert_file_exists "${file2}" || return 1
local difference
local compare_status=0
difference="$(cmp "${file1}" "${file2}" 2>&1)" || compare_status=$?
_file_compare_failed "${compare_status}" "${difference}" || return 1
[ "${compare_status}" -eq 0 ] && return 0
format_error "Files are not equal" "file" "${file1}" "other file" "${file2}" "difference" "${difference}" | flunk
}
##
# Asserts that the contents of two binary files are not equal.
#
# Arguments:
# 1. file1: First file.
# 2. file2: Second file.
##
assert_binary_files_not_equal() {
local file1="${1}"
local file2="${2}"
assert_file_exists "${file1}" || return 1
assert_file_exists "${file2}" || return 1
local difference
local compare_status=0
difference="$(cmp "${file1}" "${file2}" 2>&1)" || compare_status=$?
_file_compare_failed "${compare_status}" "${difference}" || return 1
[ "${compare_status}" -ne 0 ] && return 0
format_error "Files are equal, but should not be" "file" "${file1}" "other file" "${file2}" | flunk
}
##
# Asserts that two directories hold the same files with the same contents.
#
# Both directories are walked, so a file present in only one of them fails the
# assertion regardless of which one holds it.
#
# Arguments:
# 1. dir1: First directory.
# 2. dir2: Second directory.
##
assert_dirs_equal() {
local dir1="${1}"
local dir2="${2}"
assert_dir_exists "${dir1}" || return 1
assert_dir_exists "${dir2}" || return 1
_file_dirs_equal_one_way "${dir1}" "${dir2}" || return 1
_file_dirs_equal_one_way "${dir2}" "${dir1}" || return 1
return 0
}
##
# Asserts that every file of one directory has an equal counterpart in another.
#
# Arguments:
# 1. dir: Directory to walk.
# 2. other: Directory the counterpart is expected in.
##
_file_dirs_equal_one_way() {
local dir="${1}"
local other="${2}"
local file
while IFS= read -r -d '' file; do
local counterpart="${other}/${file#"${dir}/"}"
if [ ! -e "${counterpart}" ]; then
format_error "Directory holds a file the other directory does not" "file" "${file}" "directory" "${dir}" "other directory" "${other}" | flunk
return 1
fi
assert_binary_files_equal "${file}" "${counterpart}" || return 1
done < <(find "${dir}/" -type f -print0)
return 0
}