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"""Tests for the git_utils module.""" 17from pathlib import Path 18import pytest 19from pytest_mock import MockerFixture 20 21from git_utils import tree_uses_pore 22 23 24@pytest.fixture(name="repo_tree") 25def fixture_repo_tree(tmp_path: Path) -> Path: 26 """Fixture for a repo tree.""" 27 (tmp_path / ".repo").write_text("") 28 (tmp_path / "external/foobar").mkdir(parents=True) 29 return tmp_path 30 31 32@pytest.fixture(name="pore_tree") 33def fixture_pore_tree(repo_tree: Path) -> Path: 34 """Fixture for a pore tree.""" 35 (repo_tree / ".pore").write_text("") 36 return repo_tree 37 38 39def test_tree_uses_pore_fast_path(tmp_path: Path, mocker: MockerFixture) -> None: 40 """Tests that the fast-path does not recurse.""" 41 which_mock = mocker.patch("shutil.which") 42 which_mock.return_value = None 43 path_parent_mock = mocker.patch("pathlib.Path.parent") 44 assert not tree_uses_pore(tmp_path) 45 path_parent_mock.assert_not_called() 46 47 48def test_tree_uses_pore_identifies_pore_trees(pore_tree: Path, mocker: MockerFixture) -> None: 49 """Tests that a pore tree is correctly identified.""" 50 which_mock = mocker.patch("shutil.which") 51 which_mock.return_value = Path("pore") 52 assert tree_uses_pore(pore_tree) 53 54 55def test_tree_uses_pore_identifies_repo_trees(repo_tree: Path, mocker: MockerFixture) -> None: 56 """Tests that a repo tree is correctly identified.""" 57 which_mock = mocker.patch("shutil.which") 58 which_mock.return_value = Path("pore") 59 assert not tree_uses_pore(repo_tree) 60