Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_arduino_build / py / file_operations_test.py
1 # Copyright 2020 The Pigweed Authors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 # use this file except in compliance with the License. You may obtain a copy of
5 # the License at
6 #
7 #     https://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations under
13 # the License.
14 """Tests for file_operations module."""
15
16 import os
17 import shutil
18 import tempfile
19 import unittest
20 from pathlib import Path
21 from parameterized import parameterized  # type: ignore
22
23 from pw_arduino_build import file_operations
24
25
26 def file_set():
27     return [
28         "app.ino",
29         "core/asm.S",
30         "core/asm.s",
31         "core/pwm/pulse.c",
32         "core/pwm/pulse.h",
33         "libraries/a.c",
34         "libraries/b.cpp",
35         "libraries/c.cc",
36         "libraries/c.h",
37     ]
38
39
40 def create_files(root_dir, file_names):
41     for file_name in file_names:
42         folder_path = Path(root_dir) / Path(os.path.dirname(file_name))
43         folder_path.mkdir(parents=True, exist_ok=True)
44         file_path = Path(root_dir) / Path(file_name)
45         file_path.touch(exist_ok=True)
46
47
48 class TestFileOperations(unittest.TestCase):
49     """Tests to ensure arduino core library source files can be found."""
50     def setUp(self):
51         self.test_dir = tempfile.mkdtemp()
52
53     def tearDown(self):
54         shutil.rmtree(self.test_dir)
55
56     @parameterized.expand([
57         (
58             "sources recursive", file_set(), ["**/*.ino", "**/*.h", "**/*.cpp"],
59             [
60                 "app.ino",
61                 os.path.join("core", "pwm", "pulse.h"),
62                 os.path.join("libraries", "b.cpp"),
63                 os.path.join("libraries", "c.h"),
64             ]
65         ),
66         (
67             "directories recursive", file_set(), ["**"],
68             [
69                 "core",
70                 os.path.join("core", "pwm"),
71                 "libraries",
72             ]
73         ),
74         (
75             "directories one level deep", file_set(), ["*"],
76             [
77                 "core",
78                 "libraries",
79             ]
80         ),
81         (
82             "items one level deep", file_set(), ["*"],
83             [
84                 "app.ino",
85                 "core",
86                 "libraries",
87             ]
88         )
89     ]) # yapf: disable
90     def test_find_files(self, test_case, base_fileset, patterns,
91                         expected_results):
92         """Test find_files on source files and directories."""
93         create_files(self.test_dir, base_fileset)
94         result = file_operations.find_files(self.test_dir,
95                                             patterns,
96                                             directories_only=("directories"
97                                                               in test_case))
98         self.assertSequenceEqual(expected_results, result)
99
100
101 if __name__ == '__main__':
102     unittest.main()