-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.bats
executable file
·535 lines (498 loc) · 16.2 KB
/
test.bats
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
#!/usr/bin/env bats
# vim: filetype=bash:
#
# GIT-ARCHIVE-ALL
#
# Copyright (c) 2019 Timo Röhling
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
GIT_ARCHIVE_ALL="$(pwd)/git-archive-all"
repo_files()
{
# Creates a list of standard files for a test repository
local var="$1"
local repo="$2"
local prefix="$3"
eval "${var}=(
'${prefix}${repo}_1.txt'
'${prefix}${repo}_2.txt'
'${prefix}${repo}_folder.txt'
'${prefix}${repo}_folder/${repo}_1.txt'
'${prefix}${repo}_folder/${repo}_2.txt'
)"
}
create_repo()
{
# Creates a test repository that will contain
# the standard files and anything passed as
# additional parameter.
local repo="$1"
shift
echo "+++ creating repo $repo"
mkdir "$repo"
local files=()
repo_files files "$repo" "$repo/"
for file in "${files[@]}"
do
[[ "$file" != */ ]] || continue
mkdir -p "$(dirname "$file")"
echo $RANDOM > "$file"
done
for file in "$@"
do
mkdir -p "$(dirname "$repo/$file")"
echo $RANDOM > "$repo/$file"
done
git -C "$repo" init
git -C "$repo" add .
git -C "$repo" commit -m "Initial commit"
}
add_submodule()
{
# Adds a test repository as submodule to another
# test repository.
local repo="$1"
local submodule="$2"
local path="${3:-$submodule}"
echo "+++ adding submodule $submodule to repo $repo"
mkdir -p "$repo/$(dirname "$path")"
git -c protocol.file.allow=always -C "$repo" submodule add ../"$submodule" "$path"
git -C "$repo" add .
git -C "$repo" commit -m "add submodule"
}
run_git_archive_all()
{
echo "+++" "git-archive-all" "$@"
"$GIT_ARCHIVE_ALL" -v "$@"
}
setup()
{
MY_TMPDIR="$(mktemp -d --tmpdir="$BATS_TMPDIR")"
pushd "$MY_TMPDIR" &>/dev/null
}
teardown()
{
popd &>/dev/null
rm -rf "$MY_TMPDIR"
}
sort_array()
{
# Sorts the array. We use 'sort -z' with NUL-terminated strings
# so we can have arbitrary characters in our array elements
local tmpfile="$(mktemp)"
eval 'printf "%s\0" "${'"$1"'[@]}" | sort -z >'"$tmpfile"
eval "$1"'=()'
while read -d $'\0' item
do
eval "$1"'+=("$item")'
done < "$tmpfile"
rm "$tmpfile"
}
filter_array()
{
local array="$1"
local prefix="$2"
local i
eval 'for i in ${!'"$array"'[@]}; do
[[ "${'"$array"'[i]}" == "'"${prefix}"'"* ]] || unset '"$array"'[i]
done'
}
check_tar_content()
{
# Check if the contents of a TAR file matches with the expected
# list of files. File names can have arbitrary characters except
# NUL (and '/' of course).
local tarfile="$1"
shift
local expected_files=("$@")
sort_array expected_files
local actual_files=()
eval "actual_files=($(tar --quoting-style=shell -taf "$tarfile"))"
# tar -t also prints folder names, but we don't want them for
# the file list comparison. Luckily, folder names always end in
# '/', so we can remove them first.
for i in ${!actual_files[@]}
do
[[ "${actual_files[i]}" != */ ]] || unset actual_files[i]
done
sort_array actual_files
local mismatch=0
if [[ "${#expected_files[@]}" == "${#actual_files[@]}" ]]
then
local i
for ((i=0; i < ${#expected_files[@]}; i++))
do
if [[ "${expected_files[i]}" != "${actual_files[i]}" ]]
then
printf>&2" *** MISMATCH %q != %q\n" \
"${expected_files[i]}" "${actual_files[i]}"
mismatch=1
break
fi
done
else
mismatch=1
fi
if [[ "$mismatch" == 1 ]]
then
echo>&2 "*** archive has wrong content"
echo>&2 "*** expected files:"
printf>&2 "%q " "${expected_files[@]}"
echo>&2
echo>&2 "*** actual files:"
printf>&2 "%q " "${actual_files[@]}"
echo>&2
fi
return $mismatch
}
@test "simple repo" {
create_repo alpha
cd alpha
run_git_archive_all -o test.tar
local tar_files
repo_files tar_files alpha
check_tar_content test.tar "${tar_files[@]}"
}
@test "simple repo, archive with prefix" {
create_repo alpha
cd alpha
run_git_archive_all -o test.tar --prefix=prefix/ $(git rev-parse HEAD)
local tar_files
repo_files tar_files alpha prefix/
check_tar_content test.tar "${tar_files[@]}"
}
@test "simple repo, archive with prefix and path spec" {
create_repo alpha
cd alpha
run_git_archive_all -o test.tar --prefix=prefix/ $(git rev-parse HEAD) alpha_folder/
local tar_files
repo_files tar_files alpha prefix/
filter_array tar_files prefix/alpha_folder/
check_tar_content test.tar "${tar_files[@]}"
}
@test "simple repo, fail on missing path spec" {
create_repo alpha
cd alpha
! run_git_archive_all -o test.tar $(git rev-parse HEAD) beta
}
@test "simple repo, file names with non-ASCII characters" {
create_repo alpha "with space.txt" "with"$'\n'"newline.txt" "with"'$'"dollar.txt" "ÄÖÜäöü.txt" "日本人.txt"
cd alpha
run_git_archive_all -o test.tar
local tar_files
repo_files tar_files alpha
tar_files+=("with space.txt" "with"$'\n'"newline.txt" "with"'$'"dollar.txt" "ÄÖÜäöü.txt" "日本人.txt")
check_tar_content test.tar "${tar_files[@]}"
}
@test "simple repo, archive with newline in prefix" {
create_repo alpha
cd alpha
run_git_archive_all -o test.tar --prefix=pre$'\n'fix/ $(git rev-parse HEAD)
local tar_files
repo_files tar_files alpha pre$'\n'fix/
check_tar_content test.tar "${tar_files[@]}"
}
@test "simple repo, add untracked files" {
create_repo alpha
cd alpha
mkdir subdir
touch subdir/untracked1 subdir/untracked2
run_git_archive_all -o test.tar --prefix=prefix/ --add-file=subdir/untracked1 --add-file=subdir/untracked2 $(git rev-parse HEAD)
local tar_files
repo_files tar_files alpha prefix/
tar_files+=("prefix/untracked1" "prefix/untracked2")
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with submodule" {
create_repo alpha
create_repo beta
add_submodule alpha beta
cd alpha
run_git_archive_all -o test.tar $(git rev-parse HEAD)
local tar_files=(.gitmodules)
repo_files tar_files+ alpha
repo_files tar_files+ beta beta/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with submodule, submodule in subfolder" {
create_repo alpha
create_repo beta
add_submodule alpha beta subdir/beta
cd alpha
run_git_archive_all -o test.tar $(git rev-parse HEAD)
local tar_files=(.gitmodules)
repo_files tar_files+ alpha
repo_files tar_files+ beta subdir/beta/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with submodule, gzipped tar archive" {
create_repo alpha
create_repo beta
add_submodule alpha beta
cd alpha
run_git_archive_all -o test.tar.gz $(git rev-parse HEAD)
local tar_files=(.gitmodules)
repo_files tar_files+ alpha
repo_files tar_files+ beta beta/
check_tar_content test.tar.gz "${tar_files[@]}"
file test.tar.gz | grep -q "gzip compressed"
}
@test "repo with submodule, archive with prefix" {
create_repo alpha
create_repo beta
add_submodule alpha beta
cd alpha
run_git_archive_all -o test.tar --prefix=prefix/ $(git rev-parse HEAD)
local tar_files=(prefix/.gitmodules)
repo_files tar_files+ alpha prefix/
repo_files tar_files+ beta prefix/beta/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with submodule, archive with prefix and path spec" {
create_repo alpha
create_repo beta
add_submodule alpha beta
cd alpha
run_git_archive_all -o test.tar --prefix=prefix/ $(git rev-parse HEAD) alpha_folder
local tar_files
repo_files tar_files alpha prefix/
filter_array tar_files prefix/alpha_folder/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with submodule, archive with path spec" {
create_repo alpha
create_repo beta
add_submodule alpha beta
cd alpha
run_git_archive_all -o test.tar $(git rev-parse HEAD) beta/beta_folder
local tar_files
repo_files tar_files beta beta/
filter_array tar_files beta/beta_folder/
check_tar_content test.tar "${tar_files[@]}"
run_git_archive_all -o test.tar HEAD beta
repo_files tar_files beta beta/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with submodule, file names with non-ASCII characters" {
create_repo alpha "with space.txt" "with"$'\n'"newline.txt" "with"'$'"dollar.txt" "ÄÖÜäöü.txt" "日本人.txt"
create_repo beta
add_submodule alpha beta "space umlaut ÄÖÜäöü"
cd alpha
run_git_archive_all -o test.tar $(git rev-parse HEAD)
local tar_files=()
repo_files tar_files+ alpha
repo_files tar_files+ beta "space umlaut ÄÖÜäöü"/
tar_files+=("with space.txt" "with"$'\n'"newline.txt" "with"'$'"dollar.txt" "ÄÖÜäöü.txt" "日本人.txt" .gitmodules)
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with submodule, archive with newline in prefix" {
create_repo alpha
create_repo beta
add_submodule alpha beta
cd alpha
run_git_archive_all -o test.tar --prefix=pre$'\n'fix/ $(git rev-parse HEAD)
local tar_files=(pre$'\n'fix/.gitmodules)
repo_files tar_files+ alpha pre$'\n'fix/
repo_files tar_files+ beta pre$'\n'fix/beta/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with submodule, archive with newline in prefix and path spec" {
create_repo alpha
create_repo beta
add_submodule alpha beta
cd alpha
run_git_archive_all -o test.tar --prefix=pre$'\n'fix/ $(git rev-parse HEAD) beta/beta_folder
local tar_files
repo_files tar_files beta pre$'\n'fix/beta/
filter_array tar_files pre$'\n'fix/beta/beta_folder/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with submodule, older commits" {
create_repo alpha
create_repo beta
add_submodule alpha beta
echo $RANDOM > beta/new_file.txt
git -C beta add .
git -C beta commit -m "new file"
git -C alpha/beta pull origin master
echo $RANDOM > alpha/yet_another_file.txt
git -C alpha add .
git -C alpha commit -m "updated submodule"
cd alpha
local tar_files=()
repo_files tar_files+ alpha
run_git_archive_all -o test.tar --fail-missing HEAD~2
check_tar_content test.tar "${tar_files[@]}"
tar_files+=(.gitmodules)
repo_files tar_files+ beta beta/
run_git_archive_all -o test.tar --fail-missing HEAD~1
check_tar_content test.tar "${tar_files[@]}"
run_git_archive_all -o test.tar --fail-missing HEAD
tar_files+=(beta/new_file.txt yet_another_file.txt)
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with submodule, add untracked files" {
create_repo alpha
create_repo beta
add_submodule alpha beta
cd alpha
touch untracked1
touch beta/untracked2
run_git_archive_all -o test.tar --prefix=prefix/ --add-file=untracked1 --add-file=beta/untracked2 $(git rev-parse HEAD)
local tar_files=("prefix/.gitmodules")
repo_files tar_files+ alpha prefix/
repo_files tar_files+ beta prefix/beta/
tar_files+=("prefix/untracked1" "prefix/untracked2")
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with recursive submodules" {
create_repo alpha
create_repo beta
create_repo gamma
add_submodule beta gamma
add_submodule alpha beta
cd alpha
git -c protocol.file.allow=always submodule update --init --recursive
run_git_archive_all -o test.tar --fail-missing $(git rev-parse HEAD)
local tar_files=(.gitmodules beta/.gitmodules)
repo_files tar_files+ alpha
repo_files tar_files+ beta beta/
repo_files tar_files+ gamma beta/gamma/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with recursive submodules, submodules in subfolders" {
create_repo alpha
create_repo beta
create_repo gamma
add_submodule beta gamma other_subdir/gamma
add_submodule alpha beta subdir/beta
cd alpha
git -c protocol.file.allow=always submodule update --init --recursive
run_git_archive_all -o test.tar --fail-missing $(git rev-parse HEAD)
local tar_files=(.gitmodules subdir/beta/.gitmodules)
repo_files tar_files+ alpha
repo_files tar_files+ beta subdir/beta/
repo_files tar_files+ gamma subdir/beta/other_subdir/gamma/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with recursive submodules, non-recursive archive" {
create_repo alpha
create_repo beta
create_repo gamma
add_submodule beta gamma
add_submodule alpha beta
cd alpha
git -c protocol.file.allow=always submodule update --init --recursive
run_git_archive_all -o test.tar --fail-missing --no-recursive $(git rev-parse HEAD)
local tar_files=(.gitmodules beta/.gitmodules)
repo_files tar_files+ alpha
repo_files tar_files+ beta beta/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with recursive submodules, sub-submodule not initialized" {
create_repo alpha
create_repo beta
create_repo gamma
add_submodule beta gamma
add_submodule alpha beta
cd alpha
! run_git_archive_all -o test.tar --fail-missing $(git rev-parse HEAD)
run_git_archive_all -o test.tar
local tar_files=(.gitmodules beta/.gitmodules)
repo_files tar_files+ alpha
repo_files tar_files+ beta beta/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with recursive submodules, archive with prefix" {
create_repo alpha
create_repo beta
create_repo gamma
add_submodule beta gamma
add_submodule alpha beta
cd alpha
git -c protocol.file.allow=always submodule update --init --recursive
run_git_archive_all -o test.tar --fail-missing --prefix=prefix/ $(git rev-parse HEAD)
local tar_files=(prefix/.gitmodules prefix/beta/.gitmodules)
repo_files tar_files+ alpha prefix/
repo_files tar_files+ beta prefix/beta/
repo_files tar_files+ gamma prefix/beta/gamma/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with recursive submodules, non-recursive archive, path spec is not in result" {
create_repo alpha
create_repo beta
create_repo gamma
add_submodule beta gamma
add_submodule alpha beta
cd alpha
git -c protocol.file.allow=always submodule update --init --recursive
run_git_archive_all -o test.tar --fail-missing --no-recursive $(git rev-parse HEAD) beta/gamma/gamma_folder
local tar_files=()
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with recursive submodules, archive with prefix and pathspec" {
create_repo alpha
create_repo beta
create_repo gamma
add_submodule beta gamma
add_submodule alpha beta
cd alpha
git -c protocol.file.allow=always submodule update --init --recursive
run_git_archive_all -o test.tar --fail-missing --prefix=prefix/ $(git rev-parse HEAD) beta/gamma/gamma_folder
local tar_files
repo_files tar_files gamma prefix/beta/gamma/
filter_array tar_files prefix/beta/gamma/gamma_folder/
check_tar_content test.tar "${tar_files[@]}"
run_git_archive_all -o test.tar --fail-missing --prefix=prefix/ $(git rev-parse HEAD) beta/gamma
repo_files tar_files gamma prefix/beta/gamma/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with recursive submodules, archive with pathspec" {
create_repo alpha
create_repo beta
create_repo gamma
add_submodule beta gamma
add_submodule alpha beta
cd alpha
git -c protocol.file.allow=always submodule update --init --recursive
run_git_archive_all -o test.tar --fail-missing $(git rev-parse HEAD) beta/gamma/gamma_folder
local tar_files
repo_files tar_files gamma beta/gamma/
filter_array tar_files beta/gamma/gamma_folder/
check_tar_content test.tar "${tar_files[@]}"
run_git_archive_all -o test.tar --fail-missing $(git rev-parse HEAD) beta/gamma
repo_files tar_files gamma beta/gamma/
check_tar_content test.tar "${tar_files[@]}"
}
@test "repo with recursive submodules, archive with newline in prefix" {
create_repo alpha
create_repo beta
create_repo gamma
add_submodule beta gamma
add_submodule alpha beta
cd alpha
git -c protocol.file.allow=always submodule update --init --recursive
run_git_archive_all -o test.tar --fail-missing --prefix=pre$'\n'fix/ $(git rev-parse HEAD)
local tar_files=(pre$'\n'fix/.gitmodules pre$'\n'fix/beta/.gitmodules)
repo_files tar_files+ alpha pre$'\n'fix/
repo_files tar_files+ beta pre$'\n'fix/beta/
repo_files tar_files+ gamma pre$'\n'fix/beta/gamma/
check_tar_content test.tar "${tar_files[@]}"
}