1# 2# Copyright (C) 2023 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16"""Unit tests for fileutils.""" 17 18import contextlib 19import unittest 20from pathlib import Path 21from tempfile import TemporaryDirectory 22 23import fileutils 24 25 26class ResolveCommandLinePathsTest(unittest.TestCase): 27 """Unit tests for resolve_command_line_paths.""" 28 29 def test_empty_paths(self) -> None: 30 """Tests that an empty argument returns an empty list.""" 31 self.assertListEqual([], fileutils.resolve_command_line_paths([])) 32 33 def test_absolute_paths(self) -> None: 34 """Tests that absolute paths are resolved correctly.""" 35 with TemporaryDirectory() as temp_dir_str: 36 temp_dir = Path(temp_dir_str) 37 a = temp_dir / "a" 38 b = temp_dir / "external" / "b" 39 a.mkdir() 40 b.mkdir(parents=True) 41 self.assertListEqual( 42 [a, b], 43 fileutils.resolve_command_line_paths( 44 [str(a), str(b), "/does/not/exist"] 45 ), 46 ) 47 48 def test_relative_paths(self) -> None: 49 """Tests that relative paths are resolved correctly.""" 50 with TemporaryDirectory() as temp_dir_str: 51 # Make this absolute so the CWD change later doesn't break it. 52 temp_dir = Path(temp_dir_str).resolve() 53 external = temp_dir / "external" 54 external.mkdir() 55 a = external / "a" 56 a.mkdir() 57 58 working_dir = temp_dir / "cwd" 59 working_dir.mkdir() 60 b = working_dir / "b" 61 b.mkdir() 62 with contextlib.chdir(working_dir): 63 self.assertListEqual( 64 [a, working_dir, b], 65 fileutils.resolve_command_line_paths( 66 [ 67 # These will all be resolved as absolute paths and returned. 68 "../external/a", 69 ".", 70 "b", 71 # This one doesn't exist. It will be pruned from the result. 72 "c", 73 ] 74 ), 75 ) 76 77 78class FindTreeContainingTest(unittest.TestCase): 79 """Unit tests for find_tree_containing.""" 80 81 def setUp(self) -> None: 82 self._temp_dir = TemporaryDirectory() 83 self.temp_dir = Path(self._temp_dir.name) 84 self.repo_tree = self.temp_dir / "tree" 85 (self.repo_tree / ".repo").mkdir(parents=True) 86 87 def tearDown(self) -> None: 88 self._temp_dir.cleanup() 89 90 def test_cwd_is_in_tree(self) -> None: 91 """Tests that the root is found when the CWD is in the same tree.""" 92 (self.repo_tree / "external/a").mkdir(parents=True) 93 (self.repo_tree / "external/b").mkdir(parents=True) 94 95 with contextlib.chdir(self.repo_tree / "external/a"): 96 self.assertEqual( 97 fileutils.find_tree_containing(self.repo_tree / "external/b"), 98 self.repo_tree, 99 ) 100 101 def test_cwd_is_in_other_tree(self) -> None: 102 """Tests that the root is found when the CWD is in another tree.""" 103 tree_a = self.temp_dir / "a" 104 (tree_a / ".repo").mkdir(parents=True) 105 (tree_a / "external/a").mkdir(parents=True) 106 107 tree_b = self.temp_dir / "b" 108 (tree_b / ".repo").mkdir(parents=True) 109 (tree_b / "external/b").mkdir(parents=True) 110 111 with contextlib.chdir(tree_a / "external/a"): 112 self.assertEqual( 113 fileutils.find_tree_containing(tree_b / "external/b"), tree_b 114 ) 115 116 def test_no_root(self) -> None: 117 """Tests that an error is raised when no tree is found.""" 118 with self.assertRaises(FileNotFoundError): 119 fileutils.find_tree_containing(self.temp_dir) 120 121 122if __name__ == "__main__": 123 unittest.main(verbosity=2) 124