diff options
author | Dan Albert <danalbert@google.com> | 2023-03-31 00:24:49 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-03-31 00:24:49 +0000 |
commit | b426447829879649ce1be5cb1d9495f66a2669c1 (patch) | |
tree | 37eaf285256dcc5c2fc5a5c6ec804bc44f09aeea | |
parent | f022a1bc24eb394c9737e49ebe08ab6088f6e083 (diff) | |
parent | 6dc6895661bb06d52a3a3a5b8a4bc7b1b650c2e6 (diff) | |
download | external_updater-b426447829879649ce1be5cb1d9495f66a2669c1.tar.gz |
Fix repo trees, add fast path. am: 242574e9d9 am: 44becffcf9 am: 5c351f372d am: 6dc6895661
Original change: https://android-review.googlesource.com/c/platform/tools/external_updater/+/2513908
Change-Id: If17f86ddd84fb5a8a9668a22a5376f735f270d3c
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | git_utils.py | 8 | ||||
-rw-r--r-- | test_git_utils.py | 59 |
2 files changed, 66 insertions, 1 deletions
diff --git a/git_utils.py b/git_utils.py index 75a2a24..c8e50b6 100644 --- a/git_utils.py +++ b/git_utils.py @@ -15,6 +15,7 @@ import datetime import re +import shutil import subprocess from pathlib import Path @@ -202,7 +203,12 @@ def tree_uses_pore(proj_path: Path) -> bool: https://github.com/jmgao/pore """ - if proj_path == proj_path.root: + if shutil.which("pore") is None: + # Fast path for users that don't have pore installed, since that's almost + # everyone. + return False + + if proj_path == Path(proj_path.root): return False if (proj_path / ".pore").exists(): return True diff --git a/test_git_utils.py b/test_git_utils.py new file mode 100644 index 0000000..c9d948a --- /dev/null +++ b/test_git_utils.py @@ -0,0 +1,59 @@ +# +# Copyright (C) 2023 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the 'License'); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an 'AS IS' BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +"""Tests for the git_utils module.""" +from pathlib import Path +import pytest +from pytest_mock import MockerFixture + +from git_utils import tree_uses_pore + + +@pytest.fixture(name="repo_tree") +def fixture_repo_tree(tmp_path: Path) -> Path: + """Fixture for a repo tree.""" + (tmp_path / ".repo").write_text("") + (tmp_path / "external/foobar").mkdir(parents=True) + return tmp_path + + +@pytest.fixture(name="pore_tree") +def fixture_pore_tree(repo_tree: Path) -> Path: + """Fixture for a pore tree.""" + (repo_tree / ".pore").write_text("") + return repo_tree + + +def test_tree_uses_pore_fast_path(tmp_path: Path, mocker: MockerFixture) -> None: + """Tests that the fast-path does not recurse.""" + which_mock = mocker.patch("shutil.which") + which_mock.return_value = None + path_parent_mock = mocker.patch("pathlib.Path.parent") + assert not tree_uses_pore(tmp_path) + path_parent_mock.assert_not_called() + + +def test_tree_uses_pore_identifies_pore_trees(pore_tree: Path, mocker: MockerFixture) -> None: + """Tests that a pore tree is correctly identified.""" + which_mock = mocker.patch("shutil.which") + which_mock.return_value = Path("pore") + assert tree_uses_pore(pore_tree) + + +def test_tree_uses_pore_identifies_repo_trees(repo_tree: Path, mocker: MockerFixture) -> None: + """Tests that a repo tree is correctly identified.""" + which_mock = mocker.patch("shutil.which") + which_mock.return_value = Path("pore") + assert not tree_uses_pore(repo_tree) |