Add a sad Python script for Ouch testing
[ouch.git] / makeshift_testing.py
blobe8130bf717744ce55999d2916e9b31c8cacd269b
1 #!/usr/bin/python3
2 """
3 Little integration testing script while proper integration tests in Rust aren't implemented.
4 """
6 import magic, os, hashlib
8 def make_random_file():
9 with open('test-file', 'wb') as fout:
10 fout.write(os.urandom(2048))
12 def sanity_check_format(format: str):
13 make_random_file()
14 md5sum = hashlib.md5(open('test-file', 'rb').read()).hexdigest()
15 os.system(f"cargo run -- -i test-file -o test-file.{format}")
16 os.remove('test-file')
17 os.system(f"cargo run -- -i test-file.{format}")
18 if md5sum != hashlib.md5(open('test-file', 'rb').read()).hexdigest():
19 print("Something went wrong with tar (de)compression.")
20 os._exit(2)
21 os.remove('test-file')
24 if __name__ == "__main__":
26 # We'll use MIME sniffing through magic numbers to
27 # verify if ouch is actually outputting the file formats
28 # that it should
30 m = magic.open(magic.MAGIC_MIME)
32 try:
33 os.mkdir("testbuilds")
34 except OSError:
35 print ("Could not make testbuilds folder. Exiting.")
36 os._exit(2)
38 os.chdir("testbuilds")
40 m.load()
41 files = [
42 "src.tar",
43 "src.zip",
44 "src.tar.gz",
45 "src.tar.bz",
46 "src.tar.bz2",
47 "src.tar.lz",
48 "src.tar.lzma",
51 expected_mime_types = [
52 "application/x-tar",
53 "application/zip",
54 "application/gzip",
55 "application/x-bzip2",
56 "application/x-bzip2",
57 "application/x-xz",
58 "application/x-xz"
61 for file in files:
62 rv = os.system(f"cargo run -- -i ../src/ -o {file}")
63 if rv != 0:
64 print(f"Failed while compressing {file}")
66 for (file, expected_mime) in zip(files, expected_mime_types):
67 if m.file(file) != expected_mime:
68 print(f"Test failed at file {file}")
69 os._exit(2)
71 for (idx, file) in enumerate(files):
72 rv = os.system(f"cargo run -- -i {file} -o out{idx}/")
73 if rv != 0:
74 print(f"Failed while decompressing {file}")
75 os._exit(2)
77 os.chdir("..")
78 os.system("rm -rf testbuilds")
80 # We'll now verify if ouch is not altering the data it is compressing
81 # and decompressing
83 sanity_check_format("tar")
84 sanity_check_format("tar.gz")
85 sanity_check_format("tar.bz")
86 sanity_check_format("tar.bz2")
87 sanity_check_format("tar.lz")
88 sanity_check_format("tar.lzma")