From 10ea9c0b059c37e6b2026fe1334d1d57c465984d Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 28 Dec 2020 20:35:07 -0700 Subject: dtoc: Move src_scan tests to a separate file 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 --- tools/dtoc/test_src_scan.py | 91 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tools/dtoc/test_src_scan.py (limited to 'tools/dtoc/test_src_scan.py') diff --git a/tools/dtoc/test_src_scan.py b/tools/dtoc/test_src_scan.py new file mode 100644 index 0000000000..d25da5760a --- /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) -- cgit v1.2.3 From 970349a96dac3ad46c33851b1a773bfe3f1d4b33 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 28 Dec 2020 20:35:08 -0700 Subject: dtoc: Tidy up src_scan tests Some of these tests don't actually check anything. Add a few more checks to complete the tests. Also add a simple scan test that does the basics. Signed-off-by: Simon Glass --- tools/dtoc/test_src_scan.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'tools/dtoc/test_src_scan.py') diff --git a/tools/dtoc/test_src_scan.py b/tools/dtoc/test_src_scan.py index d25da5760a..7d686530d6 100644 --- a/tools/dtoc/test_src_scan.py +++ b/tools/dtoc/test_src_scan.py @@ -14,6 +14,7 @@ import unittest from unittest import mock from dtoc import src_scan +from patman import test_util from patman import tools # This is a test so is allowed to access private things in the module it is @@ -30,15 +31,26 @@ class TestSrcScan(unittest.TestCase): def tearDownClass(cls): tools.FinaliseOutputDir() - @classmethod - def test_scan_drivers(cls): - """Test running dtoc with additional drivers to scan""" + def test_simple(self): + """Simple test of scanning drivers""" + scan = src_scan.Scanner(None, True, None) + scan.scan_drivers() + self.assertIn('sandbox_gpio', scan._drivers) + self.assertIn('sandbox_gpio_alias', scan._driver_aliases) + self.assertEqual('sandbox_gpio', + scan._driver_aliases['sandbox_gpio_alias']) + self.assertNotIn('sandbox_gpio_alias2', scan._driver_aliases) + + def test_additional(self): + """Test with additional drivers to scan""" scan = src_scan.Scanner( None, True, [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx']) scan.scan_drivers() + self.assertIn('sandbox_gpio_alias2', scan._driver_aliases) + self.assertEqual('sandbox_gpio', + scan._driver_aliases['sandbox_gpio_alias2']) - @classmethod - def test_unicode_error(cls): + def test_unicode_error(self): """Test running dtoc with an invalid unicode file To be able to perform this test without adding a weird text file which @@ -49,7 +61,11 @@ class TestSrcScan(unittest.TestCase): with open(driver_fn, 'wb+') as fout: fout.write(b'\x81') - src_scan.Scanner(None, True, [driver_fn]) + scan = src_scan.Scanner(None, True, [driver_fn]) + with test_util.capture_sys_output() as (stdout, _): + scan.scan_drivers() + self.assertRegex(stdout.getvalue(), + r"Skipping file '.*' due to unicode error\s*") def test_driver(self): """Test the Driver class""" -- cgit v1.2.3