Simon Glass | 10ea9c0 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # Copyright 2020 Google LLC |
| 3 | # |
| 4 | |
| 5 | """Tests for the src_scan module |
| 6 | |
| 7 | This includes unit tests for scanning of the source code |
| 8 | """ |
| 9 | |
| 10 | import os |
| 11 | import shutil |
| 12 | import tempfile |
| 13 | import unittest |
| 14 | from unittest import mock |
| 15 | |
| 16 | from dtoc import src_scan |
Simon Glass | 970349a | 2020-12-28 20:35:08 -0700 | [diff] [blame^] | 17 | from patman import test_util |
Simon Glass | 10ea9c0 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 18 | from patman import tools |
| 19 | |
| 20 | # This is a test so is allowed to access private things in the module it is |
| 21 | # testing |
| 22 | # pylint: disable=W0212 |
| 23 | |
| 24 | class TestSrcScan(unittest.TestCase): |
| 25 | """Tests for src_scan""" |
| 26 | @classmethod |
| 27 | def setUpClass(cls): |
| 28 | tools.PrepareOutputDir(None) |
| 29 | |
| 30 | @classmethod |
| 31 | def tearDownClass(cls): |
| 32 | tools.FinaliseOutputDir() |
| 33 | |
Simon Glass | 970349a | 2020-12-28 20:35:08 -0700 | [diff] [blame^] | 34 | def test_simple(self): |
| 35 | """Simple test of scanning drivers""" |
| 36 | scan = src_scan.Scanner(None, True, None) |
| 37 | scan.scan_drivers() |
| 38 | self.assertIn('sandbox_gpio', scan._drivers) |
| 39 | self.assertIn('sandbox_gpio_alias', scan._driver_aliases) |
| 40 | self.assertEqual('sandbox_gpio', |
| 41 | scan._driver_aliases['sandbox_gpio_alias']) |
| 42 | self.assertNotIn('sandbox_gpio_alias2', scan._driver_aliases) |
| 43 | |
| 44 | def test_additional(self): |
| 45 | """Test with additional drivers to scan""" |
Simon Glass | 10ea9c0 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 46 | scan = src_scan.Scanner( |
| 47 | None, True, [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx']) |
| 48 | scan.scan_drivers() |
Simon Glass | 970349a | 2020-12-28 20:35:08 -0700 | [diff] [blame^] | 49 | self.assertIn('sandbox_gpio_alias2', scan._driver_aliases) |
| 50 | self.assertEqual('sandbox_gpio', |
| 51 | scan._driver_aliases['sandbox_gpio_alias2']) |
Simon Glass | 10ea9c0 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 52 | |
Simon Glass | 970349a | 2020-12-28 20:35:08 -0700 | [diff] [blame^] | 53 | def test_unicode_error(self): |
Simon Glass | 10ea9c0 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 54 | """Test running dtoc with an invalid unicode file |
| 55 | |
| 56 | To be able to perform this test without adding a weird text file which |
| 57 | would produce issues when using checkpatch.pl or patman, generate the |
| 58 | file at runtime and then process it. |
| 59 | """ |
| 60 | driver_fn = '/tmp/' + next(tempfile._get_candidate_names()) |
| 61 | with open(driver_fn, 'wb+') as fout: |
| 62 | fout.write(b'\x81') |
| 63 | |
Simon Glass | 970349a | 2020-12-28 20:35:08 -0700 | [diff] [blame^] | 64 | scan = src_scan.Scanner(None, True, [driver_fn]) |
| 65 | with test_util.capture_sys_output() as (stdout, _): |
| 66 | scan.scan_drivers() |
| 67 | self.assertRegex(stdout.getvalue(), |
| 68 | r"Skipping file '.*' due to unicode error\s*") |
Simon Glass | 10ea9c0 | 2020-12-28 20:35:07 -0700 | [diff] [blame] | 69 | |
| 70 | def test_driver(self): |
| 71 | """Test the Driver class""" |
| 72 | drv1 = src_scan.Driver('fred') |
| 73 | drv2 = src_scan.Driver('mary') |
| 74 | drv3 = src_scan.Driver('fred') |
| 75 | self.assertEqual("Driver(name='fred')", str(drv1)) |
| 76 | self.assertEqual(drv1, drv3) |
| 77 | self.assertNotEqual(drv1, drv2) |
| 78 | self.assertNotEqual(drv2, drv3) |
| 79 | |
| 80 | def test_scan_dirs(self): |
| 81 | """Test scanning of source directories""" |
| 82 | def add_file(fname): |
| 83 | pathname = os.path.join(indir, fname) |
| 84 | dirname = os.path.dirname(pathname) |
| 85 | os.makedirs(dirname, exist_ok=True) |
| 86 | tools.WriteFile(pathname, '', binary=False) |
| 87 | fname_list.append(pathname) |
| 88 | |
| 89 | try: |
| 90 | indir = tempfile.mkdtemp(prefix='dtoc.') |
| 91 | |
| 92 | fname_list = [] |
| 93 | add_file('fname.c') |
| 94 | add_file('dir/fname2.c') |
| 95 | |
| 96 | # Mock out scan_driver and check that it is called with the |
| 97 | # expected files |
| 98 | with mock.patch.object(src_scan.Scanner, "scan_driver") as mocked: |
| 99 | scan = src_scan.Scanner(indir, True, None) |
| 100 | scan.scan_drivers() |
| 101 | self.assertEqual(2, len(mocked.mock_calls)) |
| 102 | self.assertEqual(mock.call(fname_list[0]), |
| 103 | mocked.mock_calls[0]) |
| 104 | self.assertEqual(mock.call(fname_list[1]), |
| 105 | mocked.mock_calls[1]) |
| 106 | finally: |
| 107 | shutil.rmtree(indir) |