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"""End-to-end tests for external_updater."""
17import subprocess
18from pathlib import Path
19
20from .treebuilder import TreeBuilder
21
22
23class TestCheck:
24    """Tests for `external_updater check`."""
25
26    def check(self, updater_cmd: list[str], paths: list[Path]) -> str:
27        """Runs `external_updater check` with the given arguments.
28
29        Returns:
30            The output of the command.
31        """
32        return subprocess.run(
33            updater_cmd + ["check"] + [str(p) for p in paths],
34            check=True,
35            capture_output=True,
36            text=True,
37        ).stdout
38
39    def test_git_up_to_date(
40        self, tree_builder: TreeBuilder, updater_cmd: list[str]
41    ) -> None:
42        """Tests that up-to-date projects are identified."""
43        tree = tree_builder.repo_tree("tree")
44        a = tree.project("platform/external/foo", "external/foo")
45        a.upstream.commit(
46            "Add README.md.",
47            update_files={
48                "README.md": "Hello, world!\n",
49            },
50        )
51        tree.create_manifest_repo()
52        a.initial_import()
53        tree.init_and_sync()
54        output = self.check(updater_cmd, [a.local.path])
55        current_version = a.upstream.head()
56        assert output == (
57            f"Checking {a.local.path}...\n"
58            f"Current version: {current_version}\n"
59            f"Latest version: {current_version}\n"
60            "Up to date.\n"
61        )
62
63    def test_git_out_of_date(
64        self, tree_builder: TreeBuilder, updater_cmd: list[str]
65    ) -> None:
66        """Tests that out-of-date projects are identified."""
67        tree = tree_builder.repo_tree("tree")
68        a = tree.project("platform/external/foo", "external/foo")
69        a.upstream.commit(
70            "Add README.md.",
71            update_files={
72                "README.md": "Hello, world!\n",
73            },
74        )
75        tree.create_manifest_repo()
76        a.initial_import()
77        current_version = a.upstream.head()
78        tree.init_and_sync()
79        a.upstream.commit(
80            "Update the project.",
81            update_files={"README.md": "This project is deprecated.\n"},
82        )
83        output = self.check(updater_cmd, [a.local.path])
84        latest_version = a.upstream.head()
85        assert output == (
86            f"Checking {a.local.path}...\n"
87            f"Current version: {current_version}\n"
88            f"Latest version: {latest_version}\n"
89            "Out of date!\n"
90        )
91