···13131414# task files for tracking already run commands
1515/.task
1616+1717+# Local cache files for testing
1818+/.cache
+3
Taskfile.yml
···6060 commit:
6161 desc: Commit changes using custom script
6262 cmd: "{{.BIN}}/commit.sh"
6363+ test:full:
6464+ desc: Run comparisons against official tools
6565+ cmd: "{{.BIN}}/test.sh {{.CLI_ARGS}}"
+209
bin/test.sh
···11+#!/usr/bin/env bash
22+set -euo pipefail
33+44+# Test functionality of cmprss by comparing it with the official tools
55+66+CACHE_DIR="${PRJ_ROOT}/.cache"
77+88+# Create a tmp directory and cd into it
99+tmpdir() {
1010+ mkdir -p "$CACHE_DIR"
1111+ local dir=$(mktemp --directory --tmpdir="$CACHE_DIR")
1212+ cd "$dir"
1313+}
1414+1515+# Compare the two files/directories and exit with an error if they are different
1616+compare() {
1717+ local file1="$1"
1818+ local file2="$2"
1919+ if ! diff -qr "$file1" "$file2" >/dev/null; then
2020+ echo "Diff Detected: $file1 $file2"
2121+ exit 1
2222+ fi
2323+}
2424+2525+# Compare the two archive sizes to check if they are similar
2626+# TODO: This doesn't work anywhere in the file because we're compressing random data
2727+# So the algos will always fail to compress things very well
2828+compare_size() {
2929+ local file1="$1"
3030+ local file2="$2"
3131+ # Use $3 as the max difference or 100 bytes
3232+ local max_diff=${3:-100}
3333+ local size1=$(stat -c %s "$file1")
3434+ local size2=$(stat -c %s "$file2")
3535+ if [ $((size1 - size2)) -gt $max_diff ]; then
3636+ echo "Size difference too large: $file1:$size1 $file2:$size2"
3737+ exit 1
3838+ fi
3939+}
4040+4141+# Create a random file with the given size
4242+random_file() {
4343+ local size="$1"
4444+ local file="$2"
4545+ dd if=/dev/urandom of="$file" bs=1 count="$size" 2>/dev/null
4646+}
4747+4848+# Create a random directory with the given size
4949+random_dir() {
5050+ local size="$1"
5151+ local dir="$2"
5252+ mkdir -p "$dir"
5353+ for i in $(seq 1 "$size"); do
5454+ random_file 1024 "$dir/$i"
5555+ done
5656+}
5757+5858+# Run cmprss using cargo to test the current version
5959+cmprss() {
6060+ cargo run --release --quiet -- "$@"
6161+}
6262+6363+test_tar() {
6464+ tmpdir
6565+ echo "Testing tar in $PWD"
6666+ echo "Creating random data"
6767+ random_dir 100 dir
6868+ echo "Compressing with tar and cmprss"
6969+ tar -cf tar_dir.tar dir
7070+ cmprss tar dir cmprss_dir.tar
7171+ # Compare the two archives
7272+ # We don't care if the archives are slightly different, just that they decompress the same
7373+ #compare tar_dir.tar cmprss_dir.tar
7474+ # Decompress the 4 variations
7575+ echo "Decompressing"
7676+ mkdir -p tar_tar
7777+ cd tar_tar
7878+ tar -xf ../tar_dir.tar
7979+ cd ..
8080+ mkdir -p tar_cmprss
8181+ cd tar_cmprss
8282+ cmprss tar --extract ../tar_dir.tar
8383+ cd ..
8484+ mkdir -p cmprss_tar
8585+ cd cmprss_tar
8686+ tar -xf ../cmprss_dir.tar
8787+ cd ..
8888+ mkdir -p cmprss_cmprss
8989+ cd cmprss_cmprss
9090+ cmprss tar --extract ../cmprss_dir.tar
9191+ cd ..
9292+9393+ echo "Comparing the decompressed files"
9494+ compare dir tar_tar/dir
9595+ compare dir tar_cmprss/dir
9696+ compare dir cmprss_tar/dir
9797+ compare dir cmprss_cmprss/dir
9898+ echo "No errors detected"
9999+}
100100+101101+# Test gzip using the provided compression level
102102+test_gzip_level() {
103103+ tmpdir
104104+ echo "Testing gzip level $1 in $PWD"
105105+ echo "Creating random data"
106106+ random_file 1000000 file
107107+ echo "Compressing with gzip and cmprss"
108108+ gzip -$1 -c file >gzip_file.gz
109109+ cmprss gzip --level $1 file cmprss_file.gz
110110+ # Compare the two archives
111111+ # The archives may have slight variations (versioning or whatever) so we
112112+ # only compare the sizes to make sure they are similar
113113+ compare_size gzip_file.gz cmprss_file.gz
114114+ # Decompress the 4 variations
115115+ echo "Decompressing"
116116+ gzip -c -d gzip_file.gz >gzip_gzip
117117+ gzip -c -d cmprss_file.gz >cmprss_gzip
118118+ cmprss gzip --extract cmprss_file.gz cmprss_cmprss
119119+ cmprss gzip --extract gzip_file.gz gzip_cmprss
120120+ echo "Comparing the decompressed files"
121121+ compare file gzip_gzip
122122+ compare file gzip_cmprss
123123+ compare file cmprss_cmprss
124124+ compare file cmprss_gzip
125125+ echo "No errors detected"
126126+}
127127+128128+test_gzip() {
129129+ test_gzip_level 1
130130+ test_gzip_level 6 # Default
131131+ test_gzip_level 9
132132+}
133133+134134+# Test xz using the provided compression level
135135+test_xz_level() {
136136+ tmpdir
137137+ echo "Testing xz level $1 in $PWD"
138138+ echo "Creating random data"
139139+ random_file 1000000 file
140140+ echo "Compressing with xz and cmprss"
141141+ xz -$1 --stdout file >xz_file.xz
142142+ cmprss xz --level $1 file cmprss_file.xz --progress=off
143143+ # Compare the two archives
144144+ # The archives may have slight variations (versioning or whatever) so we
145145+ # only compare the sizes to make sure they are similar
146146+ compare_size xz_file.xz cmprss_file.xz
147147+ # Decompress the 4 variations
148148+ echo "Decompressing"
149149+ xz --stdout --decompress xz_file.xz >xz_xz
150150+ xz --stdout --decompress cmprss_file.xz >xz_cmprss
151151+ cmprss xz --extract cmprss_file.xz cmprss_cmprss --progress=off
152152+ cmprss xz --extract xz_file.xz cmprss_xz --progress=off
153153+ echo "Comparing the decompressed files"
154154+ compare file xz_xz
155155+ compare file xz_cmprss
156156+ compare file cmprss_cmprss
157157+ compare file cmprss_xz
158158+ echo "No errors detected"
159159+}
160160+161161+test_xz() {
162162+ test_xz_level 0 # No compression
163163+ test_xz_level 1
164164+ test_xz_level 6 # Default
165165+ test_xz_level 9
166166+}
167167+168168+# Test bzip2 using the provided compression level
169169+test_bzip2_level() {
170170+ tmpdir
171171+ echo "Testing bzip2 level $1 in $PWD"
172172+ echo "Creating random data"
173173+ random_file 1000000 file
174174+ echo "Compressing with bzip2 and cmprss"
175175+ bzip2 -$1 --stdout file >bzip2_file.bz2
176176+ cmprss bzip2 --level $1 file cmprss_file.bz2 --progress=off
177177+ # Compare the two archives
178178+ # The archives may have slight variations (versioning or whatever) so we
179179+ # only compare the sizes to make sure they are similar
180180+ compare_size bzip2_file.bz2 cmprss_file.bz2
181181+ # Decompress the 4 variations
182182+ echo "Decompressing"
183183+ bzip2 --stdout --decompress bzip2_file.bz2 >bzip2_bzip2
184184+ bzip2 --stdout --decompress cmprss_file.bz2 >cmprss_bzip2
185185+ cmprss bzip2 --extract cmprss_file.bz2 cmprss_cmprss --progress=off
186186+ cmprss bzip2 --extract bzip2_file.bz2 bzip2_cmprss --progress=off
187187+ echo "Comparing the decompressed files"
188188+ compare file bzip2_bzip2
189189+ compare file bzip2_cmprss
190190+ compare file cmprss_cmprss
191191+ compare file cmprss_bzip2
192192+ echo "No errors detected"
193193+}
194194+195195+test_bzip2() {
196196+ test_bzip2_level 1
197197+ test_bzip2_level 6
198198+ test_bzip2_level 9 # Default
199199+}
200200+201201+# Run all the tests if no arguments are given
202202+if [ $# -eq 0 ]; then
203203+ set -- tar gzip xz bzip2
204204+fi
205205+206206+# Run the tests given on the command line
207207+for test in "$@"; do
208208+ test_"$test"
209209+done
+8
flake.nix
···148148149149 # Code coverage
150150 cargo-tarpaulin
151151+152152+ # For running tests
153153+ diffutils
154154+ # Official tools
155155+ bzip2
156156+ gnutar
157157+ gzip
158158+ xz
151159 ];
152160153161 # Many tools read this to find the sources for rust stdlib