diff --git a/makeshift_testing.py b/makeshift_testing.py new file mode 100755 index 0000000..e8130bf --- /dev/null +++ b/makeshift_testing.py @@ -0,0 +1,88 @@ +#!/usr/bin/python3 +""" +Little integration testing script while proper integration tests in Rust aren't implemented. +""" + +import magic, os, hashlib + +def make_random_file(): + with open('test-file', 'wb') as fout: + fout.write(os.urandom(2048)) + +def sanity_check_format(format: str): + make_random_file() + md5sum = hashlib.md5(open('test-file', 'rb').read()).hexdigest() + os.system(f"cargo run -- -i test-file -o test-file.{format}") + os.remove('test-file') + os.system(f"cargo run -- -i test-file.{format}") + if md5sum != hashlib.md5(open('test-file', 'rb').read()).hexdigest(): + print("Something went wrong with tar (de)compression.") + os._exit(2) + os.remove('test-file') + + +if __name__ == "__main__": + + # We'll use MIME sniffing through magic numbers to + # verify if ouch is actually outputting the file formats + # that it should + + m = magic.open(magic.MAGIC_MIME) + + try: + os.mkdir("testbuilds") + except OSError: + print ("Could not make testbuilds folder. Exiting.") + os._exit(2) + + os.chdir("testbuilds") + + m.load() + files = [ + "src.tar", + "src.zip", + "src.tar.gz", + "src.tar.bz", + "src.tar.bz2", + "src.tar.lz", + "src.tar.lzma", + ] + + expected_mime_types = [ + "application/x-tar", + "application/zip", + "application/gzip", + "application/x-bzip2", + "application/x-bzip2", + "application/x-xz", + "application/x-xz" + ] + + for file in files: + rv = os.system(f"cargo run -- -i ../src/ -o {file}") + if rv != 0: + print(f"Failed while compressing {file}") + + for (file, expected_mime) in zip(files, expected_mime_types): + if m.file(file) != expected_mime: + print(f"Test failed at file {file}") + os._exit(2) + + for (idx, file) in enumerate(files): + rv = os.system(f"cargo run -- -i {file} -o out{idx}/") + if rv != 0: + print(f"Failed while decompressing {file}") + os._exit(2) + + os.chdir("..") + os.system("rm -rf testbuilds") + + # We'll now verify if ouch is not altering the data it is compressing + # and decompressing + + sanity_check_format("tar") + sanity_check_format("tar.gz") + sanity_check_format("tar.bz") + sanity_check_format("tar.bz2") + sanity_check_format("tar.lz") + sanity_check_format("tar.lzma") \ No newline at end of file diff --git a/src/extension.rs b/src/extension.rs index 49915e1..35a4396 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -50,7 +50,7 @@ impl Extension { "tar" => Ok(Tar), "gz" => Ok(Gzip), "bz" | "bz2" => Ok(Bzip), - "lz" | "lzma" => Ok(Lzma), + "xz" | "lz" | "lzma" => Ok(Lzma), other => Err(error::Error::UnknownExtensionError(other.into())), }; @@ -109,7 +109,7 @@ fn extension_from_os_str(ext: &OsStr) -> Result "tar" => Ok(Tar), "gz" => Ok(Gzip), "bz" | "bz2" => Ok(Bzip), - "lzma" | "lz" => Ok(Lzma), + "xz" | "lzma" | "lz" => Ok(Lzma), other => Err(error::Error::UnknownExtensionError(other.into())), } }