aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/git_unittest.py
blob: fa756ddfb630db68cce35b2d434edf5bb02126b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
# Copyright 2020 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unit tests for git helper functions."""

import os
import subprocess
import tempfile
import unittest
from unittest import mock

import git


# These are unittests; protected access is OK to a point.
# pylint: disable=protected-access

EXAMPLE_GIT_SHA = "d46d9c1a23118e3943f43fe2dfc9f9c9c0b4aefe"


class HelperFunctionsTest(unittest.TestCase):
    """Test class for updating LLVM hashes of packages."""

    def testIsFullGitSHASuccessCases(self):
        shas = ("a" * 40, EXAMPLE_GIT_SHA)
        for s in shas:
            self.assertTrue(git.IsFullGitSHA(s), s)

    def testIsFullGitSHAFailureCases(self):
        shas = (
            "",
            "A" * 40,
            "g" * 40,
            EXAMPLE_GIT_SHA[1:],
            EXAMPLE_GIT_SHA + "a",
        )
        for s in shas:
            self.assertFalse(git.IsFullGitSHA(s), s)

    @mock.patch.object(os.path, "isdir", return_value=False)
    def testFailedToCreateBranchForInvalidDirectoryPath(self, mock_isdir):
        path_to_repo = "/invalid/path/to/repo"
        branch = "branch-name"

        # Verify the exception is raised when provided an invalid directory
        # path.
        with self.assertRaises(ValueError) as err:
            git.CreateBranch(path_to_repo, branch)

        self.assertEqual(
            str(err.exception),
            "Invalid directory path provided: %s" % path_to_repo,
        )

        mock_isdir.assert_called_once()

    @mock.patch.object(os.path, "isdir", return_value=True)
    @mock.patch.object(subprocess, "check_output", return_value=None)
    def testSuccessfullyCreatedBranch(self, mock_command_output, mock_isdir):
        path_to_repo = "/path/to/repo"
        branch = "branch-name"

        git.CreateBranch(path_to_repo, branch)

        mock_isdir.assert_called_once_with(path_to_repo)

        self.assertEqual(mock_command_output.call_count, 2)

    @mock.patch.object(os.path, "isdir", return_value=False)
    def testFailedToDeleteBranchForInvalidDirectoryPath(self, mock_isdir):
        path_to_repo = "/invalid/path/to/repo"
        branch = "branch-name"

        # Verify the exception is raised on an invalid repo path.
        with self.assertRaises(ValueError) as err:
            git.DeleteBranch(path_to_repo, branch)

        self.assertEqual(
            str(err.exception),
            "Invalid directory path provided: %s" % path_to_repo,
        )

        mock_isdir.assert_called_once()

    @mock.patch.object(os.path, "isdir", return_value=True)
    @mock.patch.object(subprocess, "run", return_value=None)
    def testSuccessfullyDeletedBranch(self, mock_command_output, mock_isdir):
        path_to_repo = "/valid/path/to/repo"
        branch = "branch-name"

        git.DeleteBranch(path_to_repo, branch)

        mock_isdir.assert_called_once_with(path_to_repo)

        self.assertEqual(mock_command_output.call_count, 3)

    @mock.patch.object(os.path, "isdir", return_value=False)
    def testFailedToUploadChangesForInvalidDirectoryPath(self, mock_isdir):
        path_to_repo = "/some/path/to/repo"
        branch = "update-LLVM_NEXT_HASH-a123testhash3"

        # Verify exception is raised when on an invalid repo path.
        with self.assertRaises(ValueError) as err:
            git.UploadChanges(path_to_repo, branch)

        self.assertEqual(
            str(err.exception), "Invalid path provided: %s" % path_to_repo
        )

        mock_isdir.assert_called_once()

    @mock.patch.object(os.path, "isdir", return_value=True)
    @mock.patch.object(subprocess, "check_output")
    @mock.patch.object(tempfile, "NamedTemporaryFile")
    def testSuccessfullyUploadedChangesForReview(
        self, mock_tempfile, mock_commands, mock_isdir
    ):
        path_to_repo = "/some/path/to/repo"
        branch = "branch-name"
        commit_messages = ["Test message"]
        mock_tempfile.return_value.__enter__.return_value.name = "tmp"

        # A test CL generated by `repo upload`.
        mock_commands.side_effect = [
            None,
            (
                "remote: https://chromium-review.googlesource."
                "com/c/chromiumos/overlays/chromiumos-overlay/"
                "+/193147 Fix stdout"
            ),
        ]
        git.CommitChanges(path_to_repo, commit_messages)
        change_list = git.UploadChanges(path_to_repo, branch)

        self.assertEqual(change_list.cl_number, 193147)

        mock_isdir.assert_called_with(path_to_repo)

        expected_command = [
            "git",
            "commit",
            "-F",
            mock_tempfile.return_value.__enter__.return_value.name,
        ]
        self.assertEqual(
            mock_commands.call_args_list[0],
            mock.call(expected_command, cwd=path_to_repo),
        )

        expected_cmd = [
            "repo",
            "upload",
            "--yes",
            "--ne",
            "--no-verify",
            "--br=%s" % branch,
        ]
        self.assertEqual(
            mock_commands.call_args_list[1],
            mock.call(
                expected_cmd,
                stderr=subprocess.STDOUT,
                cwd=path_to_repo,
                encoding="utf-8",
            ),
        )

        self.assertEqual(
            change_list.url,
            "https://chromium-review.googlesource.com/c/chromiumos/overlays/"
            "chromiumos-overlay/+/193147",
        )


if __name__ == "__main__":
    unittest.main()