1# Copyright 2024 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 pw_ide.activate""" 15 16import unittest 17from pw_ide.activate import ( 18 find_pigweed_json_above, 19 find_pigweed_json_below, 20 pigweed_root, 21) 22 23from test_cases import TempDirTestCase 24 25 26class TestFindPigweedJson(TempDirTestCase): 27 """Test functions that find pigweed.json""" 28 29 def test_find_pigweed_json_above_1_level(self): 30 self.touch_temp_file("pigweed.json") 31 nested_dir = self.temp_dir_path / "nested" 32 nested_dir.mkdir() 33 presumed_root = find_pigweed_json_above(nested_dir) 34 self.assertEqual(presumed_root, self.temp_dir_path) 35 36 def test_find_pigweed_json_above_2_levels(self): 37 self.touch_temp_file("pigweed.json") 38 nested_dir = self.temp_dir_path / "nested" / "again" 39 nested_dir.mkdir(parents=True) 40 presumed_root = find_pigweed_json_above(nested_dir) 41 self.assertEqual(presumed_root, self.temp_dir_path) 42 43 def test_find_pigweed_json_below_1_level(self): 44 nested_dir = self.temp_dir_path / "nested" 45 nested_dir.mkdir() 46 self.touch_temp_file(nested_dir / "pigweed.json") 47 presumed_root = find_pigweed_json_below(self.temp_dir_path) 48 self.assertEqual(presumed_root, nested_dir) 49 50 def test_find_pigweed_json_below_2_level(self): 51 nested_dir = self.temp_dir_path / "nested" / "again" 52 nested_dir.mkdir(parents=True) 53 self.touch_temp_file(nested_dir / "pigweed.json") 54 presumed_root = find_pigweed_json_below(self.temp_dir_path) 55 self.assertEqual(presumed_root, nested_dir) 56 57 def test_pigweed_json_above_is_preferred(self): 58 self.touch_temp_file("pigweed.json") 59 60 workspace_dir = self.temp_dir_path / "workspace" 61 workspace_dir.mkdir() 62 63 pigweed_dir = workspace_dir / "pigweed" 64 pigweed_dir.mkdir() 65 self.touch_temp_file(pigweed_dir / "pigweed.json") 66 67 presumed_root, _ = pigweed_root(self.temp_dir_path, False) 68 self.assertEqual(presumed_root, self.temp_dir_path) 69 70 71if __name__ == '__main__': 72 unittest.main() 73