blob: 7d686530d68b5aa38afcacfa55a48d7f5382b99b [file] [log] [blame]
Simon Glass10ea9c02020-12-28 20:35:07 -07001# SPDX-License-Identifier: GPL-2.0+
2# Copyright 2020 Google LLC
3#
4
5"""Tests for the src_scan module
6
7This includes unit tests for scanning of the source code
8"""
9
10import os
11import shutil
12import tempfile
13import unittest
14from unittest import mock
15
16from dtoc import src_scan
Simon Glass970349a2020-12-28 20:35:08 -070017from patman import test_util
Simon Glass10ea9c02020-12-28 20:35:07 -070018from 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
24class 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 Glass970349a2020-12-28 20:35:08 -070034 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 Glass10ea9c02020-12-28 20:35:07 -070046 scan = src_scan.Scanner(
47 None, True, [None, '', 'tools/dtoc/dtoc_test_scan_drivers.cxx'])
48 scan.scan_drivers()
Simon Glass970349a2020-12-28 20:35:08 -070049 self.assertIn('sandbox_gpio_alias2', scan._driver_aliases)
50 self.assertEqual('sandbox_gpio',
51 scan._driver_aliases['sandbox_gpio_alias2'])
Simon Glass10ea9c02020-12-28 20:35:07 -070052
Simon Glass970349a2020-12-28 20:35:08 -070053 def test_unicode_error(self):
Simon Glass10ea9c02020-12-28 20:35:07 -070054 """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 Glass970349a2020-12-28 20:35:08 -070064 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 Glass10ea9c02020-12-28 20:35:07 -070069
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)