From c9d2c7197710d0f532a786a02daa5ba994438de2 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 29 Jan 2022 11:05:09 +0000 Subject: Ensure list test exits with appropiate exit code on failure --- tests/test-lz4-list.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test-lz4-list.py b/tests/test-lz4-list.py index b8d844a..39f2253 100644 --- a/tests/test-lz4-list.py +++ b/tests/test-lz4-list.py @@ -5,6 +5,7 @@ import glob import os import tempfile import unittest +import sys SIZES = [3, 11] # Always 2 sizes MIB = 1048576 @@ -278,5 +279,6 @@ def generate_files(): if __name__ == '__main__': cleanup() generate_files() - unittest.main(verbosity=2, exit=False) + ret = unittest.main(verbosity=2, exit=False) cleanup(silent=True) + sys.exit(not ret.result.wasSuccessful()) -- cgit v0.12 From 2f5d65aad165583a2c580e28ce532ac1a7088ca2 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 29 Jan 2022 14:34:31 +0000 Subject: Convert all strings to fstrings && avoid usage of shell for Popen --- tests/test-lz4-list.py | 85 ++++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/tests/test-lz4-list.py b/tests/test-lz4-list.py index 39f2253..39986e3 100644 --- a/tests/test-lz4-list.py +++ b/tests/test-lz4-list.py @@ -9,9 +9,9 @@ import sys SIZES = [3, 11] # Always 2 sizes MIB = 1048576 -LZ4 = os.path.dirname(os.path.realpath(__file__)) + "/../lz4" +LZ4 = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/../lz4") if not os.path.exists(LZ4): - LZ4 = os.path.dirname(os.path.realpath(__file__)) + "/../programs/lz4" + LZ4 = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/../programs/lz4") TEMP = tempfile.gettempdir() @@ -20,26 +20,29 @@ class NVerboseFileInfo(object): self.line = line_in splitlines = line_in.split() if len(splitlines) != 7: - errout("Unexpected line: {}".format(line_in)) + errout(f"Unexpected line: {line_in}") self.frames, self.type, self.block, self.compressed, self.uncompressed, self.ratio, self.filename = splitlines self.exp_unc_size = 0 # Get real file sizes if "concat-all" in self.filename or "2f--content-size" in self.filename: for i in SIZES: - self.exp_unc_size += os.path.getsize("{}/test_list_{}M".format(TEMP, i)) + self.exp_unc_size += os.path.getsize(f"{TEMP}/test_list_{i}M") else: uncompressed_filename = self.filename.split("-")[0] - self.exp_unc_size += os.path.getsize("{}/{}".format(TEMP, uncompressed_filename)) - self.exp_comp_size = os.path.getsize("{}/{}".format(TEMP, self.filename)) + self.exp_unc_size += os.path.getsize(f"{TEMP}/{uncompressed_filename}") + self.exp_comp_size = os.path.getsize(f"{TEMP}/{self.filename}") class TestNonVerbose(unittest.TestCase): @classmethod def setUpClass(self): self.nvinfo_list = [] - for i, line in enumerate(execute("{} --list -m {}/test_list_*.lz4".format(LZ4, TEMP), print_output=True)): - if i > 0: - self.nvinfo_list.append(NVerboseFileInfo(line)) + test_list_files = glob.glob(f"{TEMP}/test_list_*.lz4") + # One of the files has 2 frames so duplicate it in this list to map each frame 1 to a single file + for i, filename in enumerate(test_list_files): + for i, line in enumerate(execute(f"{LZ4} --list -m {filename}", print_output=True)): + if i > 0: + self.nvinfo_list.append(NVerboseFileInfo(line)) def test_frames(self): all_concat_frames = 0 @@ -81,7 +84,7 @@ class TestNonVerbose(unittest.TestCase): def test_ratio(self): for nvinfo in self.nvinfo_list: if "--content-size" in nvinfo.filename: - self.assertEqual(nvinfo.ratio, "{:.2f}%".format(float(nvinfo.exp_comp_size) / float(nvinfo.exp_unc_size) * 100), nvinfo.line) + self.assertEqual(nvinfo.ratio, f"{float(nvinfo.exp_comp_size) / float(nvinfo.exp_unc_size) * 100:.2f}%", nvinfo.line) def test_uncompressed_size(self): for nvinfo in self.nvinfo_list: @@ -113,14 +116,16 @@ class TestVerbose(unittest.TestCase): # we're only really interested in testing the output of the concat-all file. self.vinfo_list = [] start = end = 0 - output = execute("{} --list -m -v {}/test_list_concat-all.lz4 {}/test_list_*M-lz4f-2f--content-size.lz4".format(LZ4, TEMP, TEMP), print_output=True) - for i, line in enumerate(output): - if line.startswith("test_list"): - if start != 0 and end != 0: - self.vinfo_list.append(VerboseFileInfo(output[start:end])) - start = i - if not line: - end = i + test_list_SM_lz4f = glob.glob(f"{TEMP}/test_list_*M-lz4f-2f--content-size.lz4") + for i, filename in enumerate(test_list_SM_lz4f): + output = execute(f"{LZ4} --list -m -v {TEMP}/test_list_concat-all.lz4 {filename}", print_output=True) + for i, line in enumerate(output): + if line.startswith("test_list"): + if start != 0 and end != 0: + self.vinfo_list.append(VerboseFileInfo(output[start:end])) + start = i + if not line: + end = i self.vinfo_list.append(VerboseFileInfo(output[start:end])) # Populate file_frame_map as a reference of the expected info concat_file_list = glob.glob("/tmp/test_list_[!concat]*.lz4") @@ -131,11 +136,11 @@ class TestVerbose(unittest.TestCase): break self.cvinfo = self.vinfo_list[0] self.cvinfo.file_frame_map = concat_file_list - self.cvinfo.compressed_size = os.path.getsize("{}/test_list_concat-all.lz4".format(TEMP)) + self.cvinfo.compressed_size = os.path.getsize(f"{TEMP}/test_list_concat-all.lz4") def test_filename(self): for i, vinfo in enumerate(self.vinfo_list): - self.assertRegex(vinfo.filename, "^test_list_.*({}/{})".format(i + 1, len(self.vinfo_list))) + self.assertRegex(vinfo.filename, f"^test_list_.*({i + 1}/{len(self.vinfo_list)})".format(i + 1, len(self.vinfo_list))) def test_frame_number(self): for vinfo in self.vinfo_list: @@ -170,7 +175,7 @@ class TestVerbose(unittest.TestCase): expected_size = os.path.getsize(self.cvinfo.file_frame_map[i]) self.assertEqual(self.cvinfo.frame_list[i]["compressed"], str(expected_size), self.cvinfo.frame_list[i]["line"]) total += int(self.cvinfo.frame_list[i]["compressed"]) - self.assertEqual(total, self.cvinfo.compressed_size, "Expected total sum ({}) to match {} filesize".format(total, self.cvinfo.filename)) + self.assertEqual(total, self.cvinfo.compressed_size, f"Expected total sum ({total}) to match {self.cvinfo.filename} filesize") def test_uncompressed(self): for i, frame_info in enumerate(self.cvinfo.frame_list): @@ -183,7 +188,7 @@ class TestVerbose(unittest.TestCase): for i, frame_info in enumerate(self.cvinfo.frame_list): if "--content-size" in self.cvinfo.file_frame_map[i]: self.assertEqual(self.cvinfo.frame_list[i]['ratio'], - "{:.2f}%".format(float(self.cvinfo.frame_list[i]['compressed']) / float(self.cvinfo.frame_list[i]['uncompressed']) * 100), + f"{float(self.cvinfo.frame_list[i]['compressed']) / float(self.cvinfo.frame_list[i]['uncompressed']) * 100:.2f}%", self.cvinfo.frame_list[i]["line"]) @@ -192,7 +197,7 @@ def to_human(size): if size < 1024.0: break size /= 1024.0 - return "{:.2f}{}".format(size, unit) + return f"{size:.2f}{unit}" def log(text): @@ -204,12 +209,12 @@ def errout(text, err=1): exit(err) -def execute(command, print_command=True, print_output=False, print_error=True, param_shell=True): +def execute(command, print_command=True, print_output=False, print_error=True): if os.environ.get('QEMU_SYS'): - command = "{} {}".format(os.environ['QEMU_SYS'], command) + command = f"{os.environ['QEMU_SYS']} {command}" if print_command: log("> " + command) - popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=param_shell) + popen = subprocess.Popen(command.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout_lines, stderr_lines = popen.communicate() stderr_lines = stderr_lines.decode("utf-8") stdout_lines = stdout_lines.decode("utf-8") @@ -221,14 +226,14 @@ def execute(command, print_command=True, print_output=False, print_error=True, p if popen.returncode is not None and popen.returncode != 0: if stderr_lines and not print_output and print_error: print(stderr_lines) - errout("Failed to run: {}\n".format(command, stdout_lines + stderr_lines)) + errout(f"Failed to run: {command}, {stdout_lines + stderr_lines}\n") return (stdout_lines + stderr_lines).splitlines() def cleanup(silent=False): - for f in glob.glob("{}/test_list*".format(TEMP)): + for f in glob.glob(f"{TEMP}/test_list*"): if not silent: - log("Deleting {}".format(f)) + log(f"Deleting {f}") os.unlink(f) @@ -244,33 +249,33 @@ def generate_files(): # file format ~ test_list-f.lz4 ~ # Generate LZ4Frames for i in SIZES: - filename = "{}/test_list_{}M".format(TEMP, i) - log("Generating {}".format(filename)) + filename = f"{TEMP}/test_list_{i}M" + log(f"Generating {filename}") datagen(filename, i * MIB) for j in ["--content-size", "-BI", "-BD", "-BX", "--no-frame-crc"]: - lz4file = "{}-lz4f-1f{}.lz4".format(filename, j) - execute("{} {} {} {}".format(LZ4, j, filename, lz4file)) + lz4file = f"{filename}-lz4f-1f{j}.lz4" + execute(f"{LZ4} {j} {filename} {lz4file}") # Generate skippable frames - lz4file = "{}-skip-1f.lz4".format(filename) + lz4file = f"{filename}-skip-1f.lz4" skipsize = i * 1024 skipbytes = bytes([80, 42, 77, 24]) + skipsize.to_bytes(4, byteorder='little', signed=False) with open(lz4file, 'wb') as f: f.write(skipbytes) f.write(os.urandom(skipsize)) # Generate legacy frames - lz4file = "{}-legc-1f.lz4".format(filename) - execute("{} -l {} {}".format(LZ4, filename, lz4file)) + lz4file = f"{filename}-legc-1f.lz4" + execute(f"{LZ4} -l {filename} {lz4file}") # Concatenate --content-size files - file_list = glob.glob("{}/test_list_*-lz4f-1f--content-size.lz4".format(TEMP)) - with open("{}/test_list_{}M-lz4f-2f--content-size.lz4".format(TEMP, sum(SIZES)), 'ab') as outfile: + file_list = glob.glob(f"{TEMP}/test_list_*-lz4f-1f--content-size.lz4") + with open(f"{TEMP}/test_list_{sum(SIZES)}M-lz4f-2f--content-size.lz4", 'ab') as outfile: for fname in file_list: with open(fname, 'rb') as infile: outfile.write(infile.read()) # Concatenate all files - file_list = glob.glob("{}/test_list_*.lz4".format(TEMP)) - with open("{}/test_list_concat-all.lz4".format(TEMP), 'ab') as outfile: + file_list = glob.glob(f"{TEMP}/test_list_*.lz4") + with open(f"{TEMP}/test_list_concat-all.lz4", 'ab') as outfile: for fname in file_list: with open(fname, 'rb') as infile: outfile.write(infile.read()) -- cgit v0.12 From cd4d0814020a44c277559f9f01c4b7da5ea7a8c8 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 29 Jan 2022 16:02:29 +0000 Subject: Correctly use temp folder variable --- tests/test-lz4-list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-lz4-list.py b/tests/test-lz4-list.py index 39986e3..fe11682 100644 --- a/tests/test-lz4-list.py +++ b/tests/test-lz4-list.py @@ -128,7 +128,7 @@ class TestVerbose(unittest.TestCase): end = i self.vinfo_list.append(VerboseFileInfo(output[start:end])) # Populate file_frame_map as a reference of the expected info - concat_file_list = glob.glob("/tmp/test_list_[!concat]*.lz4") + concat_file_list = glob.glob(f"{TEMP}/test_list_[!concat]*.lz4") # One of the files has 2 frames so duplicate it in this list to map each frame 1 to a single file for i, filename in enumerate(concat_file_list): if "2f--content-size" in filename: -- cgit v0.12