[PATCH v2 1/2] tests/avocado: Factor file_truncate() helper out

Philippe Mathieu-Daudé posted 2 patches 3 years ago
Maintainers: Subbaraya Sundeep <sundeep.lkml@gmail.com>, Peter Maydell <peter.maydell@linaro.org>, Cleber Rosa <crosa@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Wainer dos Santos Moschetta <wainersm@redhat.com>, Beraldo Leal <bleal@redhat.com>
[PATCH v2 1/2] tests/avocado: Factor file_truncate() helper out
Posted by Philippe Mathieu-Daudé 3 years ago
Factor file_truncate() helper out of image_pow2ceil_expand()
for reuse.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 tests/avocado/boot_linux_console.py | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/tests/avocado/boot_linux_console.py b/tests/avocado/boot_linux_console.py
index 8c1d981586..8a598be966 100644
--- a/tests/avocado/boot_linux_console.py
+++ b/tests/avocado/boot_linux_console.py
@@ -30,15 +30,16 @@
 def pow2ceil(x):
     return 1 if x == 0 else 2**(x - 1).bit_length()
 
+def file_truncate(path, size):
+    if size != os.path.getsize(path):
+        with open(path, 'ab+') as fd:
+            fd.truncate(size)
+
 """
 Expand file size to next power of 2
 """
-def image_pow2ceil_expand(path):
-        size = os.path.getsize(path)
-        size_aligned = pow2ceil(size)
-        if size != size_aligned:
-            with open(path, 'ab+') as fd:
-                fd.truncate(size_aligned)
+def image_pow2ceil_expand(path, size):
+    file_truncate(path, pow2ceil(size))
 
 class LinuxKernelTest(QemuSystemTest):
     KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
-- 
2.38.1


Re: [PATCH v2 1/2] tests/avocado: Factor file_truncate() helper out
Posted by Cédric Le Goater 3 years ago
On 1/20/23 14:43, Philippe Mathieu-Daudé wrote:
> Factor file_truncate() helper out of image_pow2ceil_expand()
> for reuse.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   tests/avocado/boot_linux_console.py | 13 +++++++------
>   1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/tests/avocado/boot_linux_console.py b/tests/avocado/boot_linux_console.py
> index 8c1d981586..8a598be966 100644
> --- a/tests/avocado/boot_linux_console.py
> +++ b/tests/avocado/boot_linux_console.py
> @@ -30,15 +30,16 @@
>   def pow2ceil(x):
>       return 1 if x == 0 else 2**(x - 1).bit_length()
>   
> +def file_truncate(path, size):
> +    if size != os.path.getsize(path):
> +        with open(path, 'ab+') as fd:
> +            fd.truncate(size)
> +
>   """
>   Expand file size to next power of 2
>   """
> -def image_pow2ceil_expand(path):
> -        size = os.path.getsize(path)
> -        size_aligned = pow2ceil(size)
> -        if size != size_aligned:
> -            with open(path, 'ab+') as fd:
> -                fd.truncate(size_aligned)
> +def image_pow2ceil_expand(path, size):

The image_pow2ceil_expand() callers should be changed to add 'size' argument.

C.


> +    file_truncate(path, pow2ceil(size))
>   
>   class LinuxKernelTest(QemuSystemTest):
>       KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '