Repository navigation
Expand file tree
/
Copy pathuntar_test.sh
More file actions
73 lines (64 loc) · 1.85 KB
/
Copy pathuntar_test.sh
File metadata and controls
73 lines (64 loc) · 1.85 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
. ./assert.sh
. ./is_command.sh
. ./echoerr.sh
. ./log.sh
. ./mktmpdir.sh
. ./untar.sh
# roundtrip <extension> <command that builds a file named "archive">
#
# If the archive cannot be built the format is skipped rather than failed:
# the compressor is simply not installed here. (Note macOS has no `xz`
# binary but bsdtar links liblzma, so probing the tool directly would skip
# a format that actually works -- build success is the honest test.)
roundtrip() {
ext=$1
maker=$2
d=$(mktmpdir) || return 1
echo untar-fixture >"$d/payload.txt"
if ! (cd "$d" && eval "$maker") >/dev/null 2>&1; then
assert_skip "untar $ext: cannot build archive here"
rm -rf "$d"
return 0
fi
mkdir "$d/out"
mv "$d/archive" "$d/out/archive$ext"
(cd "$d/out" && untar "archive$ext") >/dev/null 2>&1
rc=$?
got=$(cat "$d/out/payload.txt" 2>/dev/null)
rm -rf "$d"
assertEquals "0" "$rc" "untar $ext: returns 0"
assertEquals "untar-fixture" "$got" "untar $ext: contents restored"
}
test_tar() {
roundtrip ".tar" "tar -cf archive payload.txt"
}
test_gz() {
roundtrip ".tar.gz" "tar -czf archive payload.txt"
roundtrip ".tgz" "tar -czf archive payload.txt"
}
test_bz2() {
roundtrip ".tar.bz2" "tar -cjf archive payload.txt"
roundtrip ".tbz" "tar -cjf archive payload.txt"
}
test_xz() {
roundtrip ".tar.xz" "tar -cJf archive payload.txt"
roundtrip ".txz" "tar -cJf archive payload.txt"
}
test_zst() {
roundtrip ".tar.zst" "tar -cf t payload.txt && zstd -q t -o archive"
roundtrip ".tzst" "tar -cf t payload.txt && zstd -q t -o archive"
}
test_zip() {
roundtrip ".zip" "zip -q archive.zip payload.txt && mv archive.zip archive"
}
test_unknown() {
assertFalse "untar some-file.rar" "unknown archive format returns non-zero"
assertFalse "untar noextension" "no extension returns non-zero"
}
test_tar
test_gz
test_bz2
test_xz
test_zst
test_zip
test_unknown