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 |
| 17 | from patman import tools |
| 18 | |
| 19 | # This is a test so is allowed to access private things in the module it is |
| 20 | # testing |
| 21 | # pylint: disable=W0212 |
| 22 | |
| 23 | class TestSrcScan(unittest.TestCase): |
| 24 | """Tests for src_scan""" |
| 25 | @classmethod |
| 26 | def setUpClass(cls): |
| 27 | tools.PrepareOutputDir(None) |
| 28 | |
| 29 | @classmethod |
| 30 | def tearDownClass(cls): |
| 31 | tools.FinaliseOutputDir() |
| 32 | |
| 33 | @classmethod |
| 34 | def test_scan_drivers(cls): |
| 35 | """Test running dtoc with additional drivers to scan""" |
| 36 | scan = src_scan.Scanner( |
| 37 | None, True, [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx']) |
| 38 | scan.scan_drivers() |
| 39 | |
| 40 | @classmethod |
| 41 | def test_unicode_error(cls): |
| 42 | """Test running dtoc with an invalid unicode file |
| 43 | |
| 44 | To be able to perform this test without adding a weird text file which |
| 45 | would produce issues when using checkpatch.pl or patman, generate the |
| 46 | file at runtime and then process it. |
| 47 | """ |
| 48 | driver_fn = '/tmp/' + next(tempfile._get_candidate_names()) |
| 49 | with open(driver_fn, 'wb+') as fout: |
| 50 | fout.write(b'\x81') |
| 51 | |
| 52 | src_scan.Scanner(None, True, [driver_fn]) |
| 53 | |
| 54 | def test_driver(self): |
| 55 | """Test the Driver class""" |
| 56 | drv1 = src_scan.Driver('fred') |
| 57 | drv2 = src_scan.Driver('mary') |
| 58 | drv3 = src_scan.Driver('fred') |
| 59 | self.assertEqual("Driver(name='fred')", str(drv1)) |
| 60 | self.assertEqual(drv1, drv3) |
| 61 | self.assertNotEqual(drv1, drv2) |
| 62 | self.assertNotEqual(drv2, drv3) |
| 63 | |
| 64 | def test_scan_dirs(self): |
| 65 | """Test scanning of source directories""" |
| 66 | def add_file(fname): |
| 67 | pathname = os.path.join(indir, fname) |
| 68 | dirname = os.path.dirname(pathname) |
| 69 | os.makedirs(dirname, exist_ok=True) |
| 70 | tools.WriteFile(pathname, '', binary=False) |
| 71 | fname_list.append(pathname) |
| 72 | |
| 73 | try: |
| 74 | indir = tempfile.mkdtemp(prefix='dtoc.') |
| 75 | |
| 76 | fname_list = [] |
| 77 | add_file('fname.c') |
| 78 | add_file('dir/fname2.c') |
| 79 | |
| 80 | # Mock out scan_driver and check that it is called with the |
| 81 | # expected files |
| 82 | with mock.patch.object(src_scan.Scanner, "scan_driver") as mocked: |
| 83 | scan = src_scan.Scanner(indir, True, None) |
| 84 | scan.scan_drivers() |
| 85 | self.assertEqual(2, len(mocked.mock_calls)) |
| 86 | self.assertEqual(mock.call(fname_list[0]), |
| 87 | mocked.mock_calls[0]) |
| 88 | self.assertEqual(mock.call(fname_list[1]), |
| 89 | mocked.mock_calls[1]) |
| 90 | finally: |
| 91 | shutil.rmtree(indir) |