[PATCH 21/49] dtoc: Move src_scan tests to a separate file

Simon Glass sjg at chromium.org
Tue Dec 29 04:35:07 CET 2020


Move the tests related to scanning into their own class, updating them
to avoid using dtb_platdata as a pass-through.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 tools/dtoc/dtb_platdata.py  |  2 +-
 tools/dtoc/main.py          |  5 +-
 tools/dtoc/src_scan.py      |  4 +-
 tools/dtoc/test_dtoc.py     | 73 -----------------------------
 tools/dtoc/test_src_scan.py | 91 +++++++++++++++++++++++++++++++++++++
 5 files changed, 97 insertions(+), 78 deletions(-)
 create mode 100644 tools/dtoc/test_src_scan.py

diff --git a/tools/dtoc/dtb_platdata.py b/tools/dtoc/dtb_platdata.py
index ad642f30625..b7abaed67ac 100644
--- a/tools/dtoc/dtb_platdata.py
+++ b/tools/dtoc/dtb_platdata.py
@@ -695,7 +695,7 @@ def run_steps(args, dtb_file, include_disabled, output, output_dirs,
     if output and output_dirs and any(output_dirs):
         raise ValueError('Must specify either output or output_dirs, not both')
 
-    scan = src_scan.Scanner(basedir, drivers_additional, warning_disabled)
+    scan = src_scan.Scanner(basedir, warning_disabled, drivers_additional)
     plat = DtbPlatdata(scan, dtb_file, include_disabled)
     scan.scan_drivers()
     plat.scan_dtb()
diff --git a/tools/dtoc/main.py b/tools/dtoc/main.py
index 9d0b3915c0e..b0ad0f3952a 100755
--- a/tools/dtoc/main.py
+++ b/tools/dtoc/main.py
@@ -46,7 +46,8 @@ def run_tests(processes, args):
         args: List of positional args provided to dtoc. This can hold a test
             name to execute (as in 'dtoc -t test_empty_file', for example)
     """
-    import test_dtoc
+    from dtoc import test_src_scan
+    from dtoc import test_dtoc
 
     result = unittest.TestResult()
     sys.argv = [sys.argv[0]]
@@ -55,7 +56,7 @@ def run_tests(processes, args):
     test_util.RunTestSuites(
         result, debug=True, verbosity=1, test_preserve_dirs=False,
         processes=processes, test_name=test_name, toolpath=[],
-        test_class_list=[test_dtoc.TestDtoc,])
+        test_class_list=[test_dtoc.TestDtoc,test_src_scan.TestSrcScan])
 
     return test_util.ReportResult('binman', test_name, result)
 
diff --git a/tools/dtoc/src_scan.py b/tools/dtoc/src_scan.py
index 185a6d9284d..f63c9fc166e 100644
--- a/tools/dtoc/src_scan.py
+++ b/tools/dtoc/src_scan.py
@@ -78,11 +78,11 @@ class Scanner:
             key: Driver alias declared with
                 DM_DRIVER_ALIAS(driver_alias, driver_name)
             value: Driver name declared with U_BOOT_DRIVER(driver_name)
+        _warning_disabled: true to disable warnings about driver names not found
         _drivers_additional (list or str): List of additional drivers to use
             during scanning
-        _warning_disabled: true to disable warnings about driver names not found
     """
-    def __init__(self, basedir, drivers_additional, warning_disabled):
+    def __init__(self, basedir, warning_disabled, drivers_additional):
         """Set up a new Scanner
         """
         if not basedir:
diff --git a/tools/dtoc/test_dtoc.py b/tools/dtoc/test_dtoc.py
index 8e8dd847c10..d961d67b8fc 100755
--- a/tools/dtoc/test_dtoc.py
+++ b/tools/dtoc/test_dtoc.py
@@ -12,18 +12,14 @@ tool.
 import collections
 import glob
 import os
-import shutil
 import struct
-import tempfile
 import unittest
-from unittest import mock
 
 from dtb_platdata import get_value
 from dtb_platdata import tab_to
 from dtoc import dtb_platdata
 from dtoc import fdt
 from dtoc import fdt_util
-from dtoc import src_scan
 from dtoc.src_scan import conv_name_to_c
 from dtoc.src_scan import get_compat_name
 from patman import test_util
@@ -904,44 +900,6 @@ U_BOOT_DRVINFO(spl_test2) = {
         self.assertIn("Unknown command 'invalid-cmd': (use: platdata, struct)",
                       str(exc.exception))
 
-    @staticmethod
-    def test_scan_drivers():
-        """Test running dtoc with additional drivers to scan"""
-        dtb_file = get_dtb_file('dtoc_test_simple.dts')
-        output = tools.GetOutputFilename('output')
-        with test_util.capture_sys_output() as _:
-            dtb_platdata.run_steps(
-                ['struct'], dtb_file, False, output, [], True,
-                [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
-
-    @staticmethod
-    def test_unicode_error():
-        """Test running dtoc with an invalid unicode file
-
-        To be able to perform this test without adding a weird text file which
-        would produce issues when using checkpatch.pl or patman, generate the
-        file at runtime and then process it.
-        """
-        dtb_file = get_dtb_file('dtoc_test_simple.dts')
-        output = tools.GetOutputFilename('output')
-        driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
-        with open(driver_fn, 'wb+') as fout:
-            fout.write(b'\x81')
-
-        with test_util.capture_sys_output() as _:
-            dtb_platdata.run_steps(['struct'], dtb_file, False, output, [],
-                                   True, [driver_fn])
-
-    def test_driver(self):
-        """Test the Driver class"""
-        drv1 = src_scan.Driver('fred')
-        drv2 = src_scan.Driver('mary')
-        drv3 = src_scan.Driver('fred')
-        self.assertEqual("Driver(name='fred')", str(drv1))
-        self.assertEqual(drv1, drv3)
-        self.assertNotEqual(drv1, drv2)
-        self.assertNotEqual(drv2, drv3)
-
     def test_output_conflict(self):
         """Test a conflict between and output dirs and output file"""
         with self.assertRaises(ValueError) as exc:
@@ -969,34 +927,3 @@ U_BOOT_DRVINFO(spl_test2) = {
         self.assertEqual(
             {'dt-structs-gen.h', 'source.dts', 'dt-plat.c', 'source.dtb'},
             leafs)
-
-    def test_scan_dirs(self):
-        """Test scanning of source directories"""
-        def add_file(fname):
-            pathname = os.path.join(indir, fname)
-            dirname = os.path.dirname(pathname)
-            os.makedirs(dirname, exist_ok=True)
-            tools.WriteFile(pathname, '', binary=False)
-            fname_list.append(pathname)
-
-        try:
-            outdir = tools.GetOutputDir()
-            indir = tempfile.mkdtemp(prefix='dtoc.')
-            dtb_file = get_dtb_file('dtoc_test_simple.dts')
-
-            fname_list = []
-            add_file('fname.c')
-            add_file('dir/fname2.c')
-
-            # Mock out scan_driver and check that it is called with the
-            # expected files
-            with mock.patch.object(src_scan.Scanner, "scan_driver")  as mocked:
-                dtb_platdata.run_steps(['all'], dtb_file, False, None, [outdir],
-                                       True, basedir=indir)
-            self.assertEqual(2, len(mocked.mock_calls))
-            self.assertEqual(mock.call(fname_list[0]),
-                             mocked.mock_calls[0])
-            self.assertEqual(mock.call(fname_list[1]),
-                             mocked.mock_calls[1])
-        finally:
-            shutil.rmtree(indir)
diff --git a/tools/dtoc/test_src_scan.py b/tools/dtoc/test_src_scan.py
new file mode 100644
index 00000000000..d25da5760a1
--- /dev/null
+++ b/tools/dtoc/test_src_scan.py
@@ -0,0 +1,91 @@
+# SPDX-License-Identifier: GPL-2.0+
+# Copyright 2020 Google LLC
+#
+
+"""Tests for the src_scan module
+
+This includes unit tests for scanning of the source code
+"""
+
+import os
+import shutil
+import tempfile
+import unittest
+from unittest import mock
+
+from dtoc import src_scan
+from patman import tools
+
+# This is a test so is allowed to access private things in the module it is
+# testing
+# pylint: disable=W0212
+
+class TestSrcScan(unittest.TestCase):
+    """Tests for src_scan"""
+    @classmethod
+    def setUpClass(cls):
+        tools.PrepareOutputDir(None)
+
+    @classmethod
+    def tearDownClass(cls):
+        tools.FinaliseOutputDir()
+
+    @classmethod
+    def test_scan_drivers(cls):
+        """Test running dtoc with additional drivers to scan"""
+        scan = src_scan.Scanner(
+            None, True, [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
+        scan.scan_drivers()
+
+    @classmethod
+    def test_unicode_error(cls):
+        """Test running dtoc with an invalid unicode file
+
+        To be able to perform this test without adding a weird text file which
+        would produce issues when using checkpatch.pl or patman, generate the
+        file at runtime and then process it.
+        """
+        driver_fn = '/tmp/' + next(tempfile._get_candidate_names())
+        with open(driver_fn, 'wb+') as fout:
+            fout.write(b'\x81')
+
+        src_scan.Scanner(None, True, [driver_fn])
+
+    def test_driver(self):
+        """Test the Driver class"""
+        drv1 = src_scan.Driver('fred')
+        drv2 = src_scan.Driver('mary')
+        drv3 = src_scan.Driver('fred')
+        self.assertEqual("Driver(name='fred')", str(drv1))
+        self.assertEqual(drv1, drv3)
+        self.assertNotEqual(drv1, drv2)
+        self.assertNotEqual(drv2, drv3)
+
+    def test_scan_dirs(self):
+        """Test scanning of source directories"""
+        def add_file(fname):
+            pathname = os.path.join(indir, fname)
+            dirname = os.path.dirname(pathname)
+            os.makedirs(dirname, exist_ok=True)
+            tools.WriteFile(pathname, '', binary=False)
+            fname_list.append(pathname)
+
+        try:
+            indir = tempfile.mkdtemp(prefix='dtoc.')
+
+            fname_list = []
+            add_file('fname.c')
+            add_file('dir/fname2.c')
+
+            # Mock out scan_driver and check that it is called with the
+            # expected files
+            with mock.patch.object(src_scan.Scanner, "scan_driver")  as mocked:
+                scan = src_scan.Scanner(indir, True, None)
+                scan.scan_drivers()
+            self.assertEqual(2, len(mocked.mock_calls))
+            self.assertEqual(mock.call(fname_list[0]),
+                             mocked.mock_calls[0])
+            self.assertEqual(mock.call(fname_list[1]),
+                             mocked.mock_calls[1])
+        finally:
+            shutil.rmtree(indir)
-- 
2.29.2.729.g45daf8777d-goog



More information about the U-Boot mailing list