xref: /aosp_15_r20/external/pigweed/pw_arduino_build/py/file_operations_test.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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
16import os
17import shutil
18import tempfile
19import unittest
20from pathlib import Path
21from parameterized import parameterized  # type: ignore
22
23from pw_arduino_build import file_operations
24
25
26def 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
40def 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
48class TestFileOperations(unittest.TestCase):
49    """Tests to ensure arduino core library source files can be found."""
50
51    def setUp(self):
52        self.test_dir = tempfile.mkdtemp()
53
54    def tearDown(self):
55        shutil.rmtree(self.test_dir)
56
57    @parameterized.expand(
58        [
59            (
60                "sources recursive",
61                file_set(),
62                ["**/*.ino", "**/*.h", "**/*.cpp"],
63                [
64                    "app.ino",
65                    os.path.join("core", "pwm", "pulse.h"),
66                    os.path.join("libraries", "b.cpp"),
67                    os.path.join("libraries", "c.h"),
68                ],
69            ),
70            (
71                "directories recursive",
72                file_set(),
73                ["**"],
74                [
75                    "core",
76                    os.path.join("core", "pwm"),
77                    "libraries",
78                ],
79            ),
80            (
81                "directories one level deep",
82                file_set(),
83                ["*"],
84                [
85                    "core",
86                    "libraries",
87                ],
88            ),
89            (
90                "items one level deep",
91                file_set(),
92                ["*"],
93                [
94                    "app.ino",
95                    "core",
96                    "libraries",
97                ],
98            ),
99        ]
100    )
101    def test_find_files(
102        self, test_case, base_fileset, patterns, expected_results
103    ):
104        """Test find_files on source files and directories."""
105        create_files(self.test_dir, base_fileset)
106        result = file_operations.find_files(
107            self.test_dir,
108            patterns,
109            directories_only=("directories" in test_case),
110        )
111        self.assertSequenceEqual(expected_results, result)
112
113
114if __name__ == '__main__':
115    unittest.main()
116