xref: /aosp_15_r20/external/pigweed/pw_ide/py/activate_tests.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker# Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker#
3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker# the License at
6*61c4878aSAndroid Build Coastguard Worker#
7*61c4878aSAndroid Build Coastguard Worker#     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker#
9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker# the License.
14*61c4878aSAndroid Build Coastguard Worker"""Tests for pw_ide.activate"""
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard Workerimport unittest
17*61c4878aSAndroid Build Coastguard Workerfrom pw_ide.activate import (
18*61c4878aSAndroid Build Coastguard Worker    find_pigweed_json_above,
19*61c4878aSAndroid Build Coastguard Worker    find_pigweed_json_below,
20*61c4878aSAndroid Build Coastguard Worker    pigweed_root,
21*61c4878aSAndroid Build Coastguard Worker)
22*61c4878aSAndroid Build Coastguard Worker
23*61c4878aSAndroid Build Coastguard Workerfrom test_cases import TempDirTestCase
24*61c4878aSAndroid Build Coastguard Worker
25*61c4878aSAndroid Build Coastguard Worker
26*61c4878aSAndroid Build Coastguard Workerclass TestFindPigweedJson(TempDirTestCase):
27*61c4878aSAndroid Build Coastguard Worker    """Test functions that find pigweed.json"""
28*61c4878aSAndroid Build Coastguard Worker
29*61c4878aSAndroid Build Coastguard Worker    def test_find_pigweed_json_above_1_level(self):
30*61c4878aSAndroid Build Coastguard Worker        self.touch_temp_file("pigweed.json")
31*61c4878aSAndroid Build Coastguard Worker        nested_dir = self.temp_dir_path / "nested"
32*61c4878aSAndroid Build Coastguard Worker        nested_dir.mkdir()
33*61c4878aSAndroid Build Coastguard Worker        presumed_root = find_pigweed_json_above(nested_dir)
34*61c4878aSAndroid Build Coastguard Worker        self.assertEqual(presumed_root, self.temp_dir_path)
35*61c4878aSAndroid Build Coastguard Worker
36*61c4878aSAndroid Build Coastguard Worker    def test_find_pigweed_json_above_2_levels(self):
37*61c4878aSAndroid Build Coastguard Worker        self.touch_temp_file("pigweed.json")
38*61c4878aSAndroid Build Coastguard Worker        nested_dir = self.temp_dir_path / "nested" / "again"
39*61c4878aSAndroid Build Coastguard Worker        nested_dir.mkdir(parents=True)
40*61c4878aSAndroid Build Coastguard Worker        presumed_root = find_pigweed_json_above(nested_dir)
41*61c4878aSAndroid Build Coastguard Worker        self.assertEqual(presumed_root, self.temp_dir_path)
42*61c4878aSAndroid Build Coastguard Worker
43*61c4878aSAndroid Build Coastguard Worker    def test_find_pigweed_json_below_1_level(self):
44*61c4878aSAndroid Build Coastguard Worker        nested_dir = self.temp_dir_path / "nested"
45*61c4878aSAndroid Build Coastguard Worker        nested_dir.mkdir()
46*61c4878aSAndroid Build Coastguard Worker        self.touch_temp_file(nested_dir / "pigweed.json")
47*61c4878aSAndroid Build Coastguard Worker        presumed_root = find_pigweed_json_below(self.temp_dir_path)
48*61c4878aSAndroid Build Coastguard Worker        self.assertEqual(presumed_root, nested_dir)
49*61c4878aSAndroid Build Coastguard Worker
50*61c4878aSAndroid Build Coastguard Worker    def test_find_pigweed_json_below_2_level(self):
51*61c4878aSAndroid Build Coastguard Worker        nested_dir = self.temp_dir_path / "nested" / "again"
52*61c4878aSAndroid Build Coastguard Worker        nested_dir.mkdir(parents=True)
53*61c4878aSAndroid Build Coastguard Worker        self.touch_temp_file(nested_dir / "pigweed.json")
54*61c4878aSAndroid Build Coastguard Worker        presumed_root = find_pigweed_json_below(self.temp_dir_path)
55*61c4878aSAndroid Build Coastguard Worker        self.assertEqual(presumed_root, nested_dir)
56*61c4878aSAndroid Build Coastguard Worker
57*61c4878aSAndroid Build Coastguard Worker    def test_pigweed_json_above_is_preferred(self):
58*61c4878aSAndroid Build Coastguard Worker        self.touch_temp_file("pigweed.json")
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Worker        workspace_dir = self.temp_dir_path / "workspace"
61*61c4878aSAndroid Build Coastguard Worker        workspace_dir.mkdir()
62*61c4878aSAndroid Build Coastguard Worker
63*61c4878aSAndroid Build Coastguard Worker        pigweed_dir = workspace_dir / "pigweed"
64*61c4878aSAndroid Build Coastguard Worker        pigweed_dir.mkdir()
65*61c4878aSAndroid Build Coastguard Worker        self.touch_temp_file(pigweed_dir / "pigweed.json")
66*61c4878aSAndroid Build Coastguard Worker
67*61c4878aSAndroid Build Coastguard Worker        presumed_root, _ = pigweed_root(self.temp_dir_path, False)
68*61c4878aSAndroid Build Coastguard Worker        self.assertEqual(presumed_root, self.temp_dir_path)
69*61c4878aSAndroid Build Coastguard Worker
70*61c4878aSAndroid Build Coastguard Worker
71*61c4878aSAndroid Build Coastguard Workerif __name__ == '__main__':
72*61c4878aSAndroid Build Coastguard Worker    unittest.main()
73