diff options
author | Gabriel <gabriel.stedman@zynstra.com> | 2022-01-29 14:34:31 (GMT) |
---|---|---|
committer | Gabriel <gabriel.stedman@zynstra.com> | 2022-01-29 14:58:59 (GMT) |
commit | 2f5d65aad165583a2c580e28ce532ac1a7088ca2 (patch) | |
tree | e62a146f5c3c429f1a900a35ead0b9070d836485 /tests | |
parent | c9d2c7197710d0f532a786a02daa5ba994438de2 (diff) | |
download | lz4-2f5d65aad165583a2c580e28ce532ac1a7088ca2.zip lz4-2f5d65aad165583a2c580e28ce532ac1a7088ca2.tar.gz lz4-2f5d65aad165583a2c580e28ce532ac1a7088ca2.tar.bz2 |
Convert all strings to fstrings && avoid usage of shell for Popen
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-lz4-list.py | 85 |
1 files 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<frametype>-<no_frames>f<create-args>.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()) |