• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (C) 2024 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 git_utils."""
17import os
18import unittest
19from contextlib import ExitStack
20from pathlib import Path
21from subprocess import CalledProcessError
22from tempfile import TemporaryDirectory
23
24import git_utils
25
26from .gitrepo import GitRepo
27
28
29class GitRepoTestCase(unittest.TestCase):
30    """Common base for tests that operate on a git repo."""
31
32    def setUp(self) -> None:
33        # Local test runs will probably pass without this since the caller
34        # almost certainly has git configured, but the bots that run the tests
35        # may not. **Do not** use `git config --global` for this, since that
36        # will modify the caller's config during local testing.
37        self._original_env = os.environ.copy()
38        os.environ["GIT_AUTHOR_NAME"] = "Testy McTestFace"
39        os.environ["GIT_AUTHOR_EMAIL"] = "test@example.com"
40        os.environ["GIT_COMMITTER_NAME"] = os.environ["GIT_AUTHOR_NAME"]
41        os.environ["GIT_COMMITTER_EMAIL"] = os.environ["GIT_AUTHOR_EMAIL"]
42
43        with ExitStack() as stack:
44            temp_dir = TemporaryDirectory()  # pylint: disable=consider-using-with
45            stack.enter_context(temp_dir)
46            self.addCleanup(stack.pop_all().close)
47            self.repo = GitRepo(Path(temp_dir.name) / "repo")
48
49    def tearDown(self) -> None:
50        # This isn't trivially `os.environ = self._original_env` because
51        # os.environ isn't actually a dict, it's an os._Environ, and there isn't
52        # a good way to construct a new one of those.
53        os.environ.clear()
54        os.environ.update(self._original_env)
55
56class IsAncestorTest(GitRepoTestCase):
57    """Tests for git_utils.is_ancestor."""
58
59    def test_if_commit_is_its_own_ancestor(self) -> None:
60        """Tests that False is returned when both commits are the same."""
61        self.repo.init()
62        self.repo.commit("Initial commit.", allow_empty=True)
63        initial_commit = self.repo.head()
64        assert not git_utils.is_ancestor(self.repo.path, initial_commit, initial_commit)
65
66    def test_is_ancestor(self) -> None:
67        """Tests that True is returned when the ref is an ancestor."""
68        self.repo.init()
69        self.repo.commit("Initial commit.", allow_empty=True)
70        initial_commit = self.repo.head()
71        self.repo.commit("Second commit.", allow_empty=True)
72        second_commit = self.repo.head()
73        git_utils.is_ancestor(self.repo.path, initial_commit, second_commit)
74
75    def test_is_not_ancestor(self) -> None:
76        """Tests that False is returned when the ref is not an ancestor."""
77        self.repo.init()
78        self.repo.commit("Initial commit.", allow_empty=True)
79        initial_commit = self.repo.head()
80        self.repo.commit("Second commit.", allow_empty=True)
81        second_commit = self.repo.head()
82        assert not git_utils.is_ancestor(self.repo.path, second_commit, initial_commit)
83
84    def test_error(self) -> None:
85        """Tests that an error is raised when git encounters an error."""
86        self.repo.init()
87        with self.assertRaises(CalledProcessError):
88            git_utils.is_ancestor(self.repo.path, "not-a-ref", "not-a-ref")
89
90
91class GetMostRecentTagTest(GitRepoTestCase):
92    """Tests for git_utils.get_most_recent_tag."""
93
94    def test_find_tag_on_correct_branch(self) -> None:
95        """Tests that only tags on the given branch are found."""
96        self.repo.init("main")
97        self.repo.commit("Initial commit.", allow_empty=True)
98        self.repo.tag("v1.0.0")
99        self.repo.switch_to_new_branch("release-2.0")
100        self.repo.commit("Second commit.", allow_empty=True)
101        self.repo.tag("v2.0.0")
102        self.assertEqual(
103            git_utils.get_most_recent_tag(self.repo.path, "main"), "v1.0.0"
104        )
105
106    def test_no_tags(self) -> None:
107        """Tests that None is returned when the repo has no tags."""
108        self.repo.init("main")
109        self.repo.commit("Initial commit.", allow_empty=True)
110        self.assertIsNone(git_utils.get_most_recent_tag(self.repo.path, "main"))
111
112    def test_no_describing_tags(self) -> None:
113        """Tests that None is returned when no tags describe the ref."""
114        self.repo.init("main")
115        self.repo.commit("Initial commit.", allow_empty=True)
116        self.repo.switch_to_new_branch("release-2.0")
117        self.repo.commit("Second commit.", allow_empty=True)
118        self.repo.tag("v2.0.0")
119        self.assertIsNone(git_utils.get_most_recent_tag(self.repo.path, "main"))
120
121
122class DiffTest(GitRepoTestCase):
123    """Tests for git_utils.diff."""
124    def test_git_diff_added_filter(self) -> None:
125        self.repo.init("main")
126        self.repo.commit(
127            "Add README.md", update_files={"README.md": "Hello, world!"}
128        )
129        first_commit = self.repo.head()
130        self.repo.commit(
131            "Add OWNERS", update_files={"OWNERS": "nobody"}
132        )
133        diff = git_utils.diff(self.repo.path, 'A', first_commit)
134        self.assertIn('OWNERS', diff)
135
136
137if __name__ == "__main__":
138    unittest.main(verbosity=2)
139