diff mbox series

test/py: Rework test_spi.py to assert we found output

Message ID 20250212222407.684579-1-trini@konsulko.com
State Accepted
Commit 3e335ddca6fa7a3d613c082cf83ff7b5bd9ddc29
Delegated to: Tom Rini
Headers show
Series test/py: Rework test_spi.py to assert we found output | expand

Commit Message

Tom Rini Feb. 12, 2025, 10:24 p.m. UTC
When running a newer version of pylint it will complain that page_size
may be used before being assignment. Looking deeper what is going on is
that we could run in to the case where the regex we run for any of the
flash information fails but since we don't have a result, we don't check
it either. In the case of the rest of the numerical values we then have
some assignment (multiplying by some value) and so pylint doesn't
complain. Rework things to assert that each regex has a result and so
failure will stop the test and we won't have any use before assignment.

Signed-off-by: Tom Rini <trini@konsulko.com>
---
Cc: Love Kumar <love.kumar@amd.com>
---
 test/py/tests/test_spi.py | 44 +++++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 23 deletions(-)

Comments

Tom Rini Feb. 21, 2025, 5:38 p.m. UTC | #1
On Wed, 12 Feb 2025 16:24:07 -0600, Tom Rini wrote:

> When running a newer version of pylint it will complain that page_size
> may be used before being assignment. Looking deeper what is going on is
> that we could run in to the case where the regex we run for any of the
> flash information fails but since we don't have a result, we don't check
> it either. In the case of the rest of the numerical values we then have
> some assignment (multiplying by some value) and so pylint doesn't
> complain. Rework things to assert that each regex has a result and so
> failure will stop the test and we won't have any use before assignment.
> 
> [...]

Applied to u-boot/next, thanks!
diff mbox series

Patch

diff --git a/test/py/tests/test_spi.py b/test/py/tests/test_spi.py
index 0abdfa78b761..d57db9178e99 100644
--- a/test/py/tests/test_spi.py
+++ b/test/py/tests/test_spi.py
@@ -119,37 +119,35 @@  def spi_pre_commands(u_boot_console, freq):
         pytest.fail('Not recognized the SPI flash part name')
 
     m = re.search('page size (.+?) Bytes', output)
-    if m:
-        try:
-            page_size = int(m.group(1))
-        except ValueError:
-            pytest.fail('Not recognized the SPI page size')
+    assert m
+    try:
+        page_size = int(m.group(1))
+    except ValueError:
+        pytest.fail('Not recognized the SPI page size')
 
     m = re.search('erase size (.+?) KiB', output)
-    if m:
-        try:
-            erase_size = int(m.group(1))
-        except ValueError:
-            pytest.fail('Not recognized the SPI erase size')
-
+    assert m
+    try:
+        erase_size = int(m.group(1))
         erase_size *= 1024
+    except ValueError:
+        pytest.fail('Not recognized the SPI erase size')
 
     m = re.search('total (.+?) MiB', output)
-    if m:
-        try:
-            total_size = int(m.group(1))
-        except ValueError:
-            pytest.fail('Not recognized the SPI total size')
-
+    assert m
+    try:
+        total_size = int(m.group(1))
         total_size *= 1024 * 1024
+    except ValueError:
+        pytest.fail('Not recognized the SPI total size')
 
     m = re.search('Detected (.+?) with', output)
-    if m:
-        try:
-            flash_part = m.group(1)
-            assert flash_part == part_name
-        except ValueError:
-            pytest.fail('Not recognized the SPI flash part')
+    assert m
+    try:
+        flash_part = m.group(1)
+        assert flash_part == part_name
+    except ValueError:
+        pytest.fail('Not recognized the SPI flash part')
 
     global SPI_DATA
     SPI_DATA = {