diff options
author | Dan Albert <danalbert@google.com> | 2023-03-30 17:50:19 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-03-30 17:50:19 +0000 |
commit | 6dc6895661bb06d52a3a3a5b8a4bc7b1b650c2e6 (patch) | |
tree | 37eaf285256dcc5c2fc5a5c6ec804bc44f09aeea | |
parent | a3fa00756688a36fb9818af129e3662fb6664a62 (diff) | |
parent | 5c351f372d7aa186cceb21a8058bcee343b01122 (diff) | |
download | external_updater-6dc6895661bb06d52a3a3a5b8a4bc7b1b650c2e6.tar.gz |
Fix repo trees, add fast path. am: 242574e9d9 am: 44becffcf9 am: 5c351f372d
Original change: https://android-review.googlesource.com/c/platform/tools/external_updater/+/2513908
Change-Id: I5cca38670d0d68034ac004c4e8c510f1bcbdac58
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) |