test: fs: Use shared generate_file from utils

test_fs/test_erofs.py and test_fs/test_squashfs/sqfs_common.py both
defined a generate_file() helper that writes a file of a given size
filled with 'x'. The two functions were functionally identical and
differed only in parameter names and docstrings.

Move the helper into the existing test/py/utils.py module, which is
the established home for generic test utilities (md5sum_file,
PersistentRandomFile, attempt_to_open_file). Update both call sites
to use it.

Signed-off-by: Aristo Chen <aristo.chen@canonical.com>
Reviewed-by: joaomarcos.costa@bootlin.com
Reviewed-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Aristo Chen
2026-05-25 13:43:21 -06:00
committed by Tom Rini
parent 7bb1917b15
commit e9848e30bd
3 changed files with 22 additions and 29 deletions
+4 -12
View File
@@ -6,19 +6,11 @@ import os
import pytest
import shutil
import subprocess
import utils
EROFS_SRC_DIR = 'erofs_src_dir'
EROFS_IMAGE_NAME = 'erofs.img'
def generate_file(name, size):
"""
Generates a file filled with 'x'.
"""
content = 'x' * size
file = open(name, 'w')
file.write(content)
file.close()
def make_erofs_image(build_dir):
"""
Makes the EROFS images used for the test.
@@ -36,15 +28,15 @@ def make_erofs_image(build_dir):
os.makedirs(root)
# 4096: uncompressed file
generate_file(os.path.join(root, 'f4096'), 4096)
utils.generate_file(os.path.join(root, 'f4096'), 4096)
# 7812: Compressed file
generate_file(os.path.join(root, 'f7812'), 7812)
utils.generate_file(os.path.join(root, 'f7812'), 7812)
# sub-directory with a single file inside
subdir_path = os.path.join(root, 'subdir')
os.makedirs(subdir_path)
generate_file(os.path.join(subdir_path, 'subdir-file'), 100)
utils.generate_file(os.path.join(subdir_path, 'subdir-file'), 100)
# symlink
os.symlink('subdir', os.path.join(root, 'symdir'))
@@ -5,6 +5,7 @@
import os
import shutil
import subprocess
import utils
""" standard test images table: Each table item is a key:value pair
representing the output image name and its respective mksquashfs options.
@@ -66,19 +67,6 @@ def init_standard_table():
for key, value in zip(STANDARD_TABLE.keys(), opts_list):
STANDARD_TABLE[key] = value
def generate_file(file_name, file_size):
""" Generates a file filled with 'x'.
Args:
file_name: the file's name.
file_size: the content's length and therefore the file size.
"""
content = 'x' * file_size
file = open(file_name, 'w')
file.write(content)
file.close()
def generate_sqfs_src_dir(build_dir):
""" Generates the source directory used to make the SquashFS images.
@@ -107,20 +95,20 @@ def generate_sqfs_src_dir(build_dir):
# 4096: minimum block size
file_name = 'f4096'
generate_file(os.path.join(root, file_name), 4096)
utils.generate_file(os.path.join(root, file_name), 4096)
# 5096: minimum block size + 1000 chars (fragment)
file_name = 'f5096'
generate_file(os.path.join(root, file_name), 5096)
utils.generate_file(os.path.join(root, file_name), 5096)
# 1000: less than minimum block size (fragment only)
file_name = 'f1000'
generate_file(os.path.join(root, file_name), 1000)
utils.generate_file(os.path.join(root, file_name), 1000)
# sub-directory with a single file inside
subdir_path = os.path.join(root, 'subdir')
os.makedirs(subdir_path)
generate_file(os.path.join(subdir_path, 'subdir-file'), 100)
utils.generate_file(os.path.join(subdir_path, 'subdir-file'), 100)
# symlink (target: sub-directory)
os.symlink('subdir', os.path.join(root, 'sym'))
+13
View File
@@ -51,6 +51,19 @@ def md5sum_file(fn, max_length=None):
data = fh.read(*params)
return md5sum_data(data)
def generate_file(file_name, file_size):
""" Generates a file filled with 'x'.
Args:
file_name: the file's name.
file_size: the content's length and therefore the file size.
"""
content = 'x' * file_size
file = open(file_name, 'w')
file.write(content)
file.close()
class PersistentRandomFile:
"""Generate and store information about a persistent file containing
random data."""