| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
import os |
| 6 |
import subprocess |
| 7 |
import textwrap |
| 8 |
import uuid |
| 9 |
|
| 10 |
import mozunit |
| 11 |
import pytest |
| 12 |
|
| 13 |
from mozversioncontrol import MissingVCSExtension, get_repository_object |
| 14 |
|
| 15 |
|
| 16 |
def test_push_to_try(repo, monkeypatch): |
| 17 |
if repo.vcs == "src": |
| 18 |
pytest.skip("src repo cannot push") |
| 19 |
|
| 20 |
commit_message = "commit message" |
| 21 |
vcs = get_repository_object(repo.dir) |
| 22 |
|
| 23 |
captured_commands = [] |
| 24 |
captured_inputs = [] |
| 25 |
|
| 26 |
def fake_run(*args, **kwargs): |
| 27 |
cmd = args[0] |
| 28 |
captured_commands.append(cmd) |
| 29 |
if cmd[1] == "var" and cmd[2] in ("GIT_AUTHOR_IDENT", "GIT_COMMITTER_IDENT"): |
| 30 |
return "FooBar <[email protected]> 0 +0000" |
| 31 |
if cmd[1:] == ("rev-parse", "HEAD"): |
| 32 |
return "0987654321098765432109876543210987654321" |
| 33 |
if cmd[1:] == ("fast-import", "--quiet"): |
| 34 |
if input := kwargs.get("input"): |
| 35 |
captured_inputs.append(input) |
| 36 |
return "1234567890123456789012345678901234567890" |
| 37 |
if os.path.basename(cmd[0]).startswith("hg") and cmd[1] == "--version": |
| 38 |
return "version 6.7" |
| 39 |
return "" |
| 40 |
|
| 41 |
def normalize_fake_run(*args, **kwargs): |
| 42 |
if ( |
| 43 |
kwargs.get("text") |
| 44 |
or kwargs.get("universal_newlines") |
| 45 |
or kwargs.get("encoding") |
| 46 |
): |
| 47 |
return fake_run(*args, **kwargs) |
| 48 |
if input := kwargs.get("input"): |
| 49 |
kwargs["input"] = input.decode("utf-8") |
| 50 |
return fake_run(*args, **kwargs).encode("utf-8") |
| 51 |
|
| 52 |
def fake_uuid(): |
| 53 |
return "974284fd-f395-4a15-a9d7-814a71241242" |
| 54 |
|
| 55 |
monkeypatch.setattr(subprocess, "check_output", normalize_fake_run) |
| 56 |
monkeypatch.setattr(subprocess, "check_call", normalize_fake_run) |
| 57 |
monkeypatch.setattr(uuid, "uuid4", fake_uuid) |
| 58 |
|
| 59 |
vcs.push_to_try( |
| 60 |
commit_message, |
| 61 |
{ |
| 62 |
"extra-file": "content", |
| 63 |
"other/extra-file": "content2", |
| 64 |
}, |
| 65 |
) |
| 66 |
tool = vcs._tool |
| 67 |
|
| 68 |
if repo.vcs == "hg": |
| 69 |
expected = [ |
| 70 |
(str(tool), "--version"), |
| 71 |
( |
| 72 |
str(tool), |
| 73 |
"--config", |
| 74 |
"extensions.automv=", |
| 75 |
"addremove", |
| 76 |
os.path.join(vcs.path, "extra-file"), |
| 77 |
os.path.join(vcs.path, "other", "extra-file"), |
| 78 |
), |
| 79 |
(str(tool), "push-to-try", "-m", commit_message), |
| 80 |
(str(tool), "revert", "-a"), |
| 81 |
] |
| 82 |
expected_inputs = [] |
| 83 |
elif repo.vcs == "git": |
| 84 |
expected = [ |
| 85 |
(str(tool), "cinnabar", "--version"), |
| 86 |
(str(tool), "rev-parse", "HEAD"), |
| 87 |
(str(tool), "var", "GIT_AUTHOR_IDENT"), |
| 88 |
(str(tool), "var", "GIT_COMMITTER_IDENT"), |
| 89 |
(str(tool), "fast-import", "--quiet"), |
| 90 |
( |
| 91 |
str(tool), |
| 92 |
"-c", |
| 93 |
"cinnabar.data=never", |
| 94 |
"push", |
| 95 |
"hg::ssh://hg.mozilla.org/try", |
| 96 |
"+1234567890123456789012345678901234567890:refs/heads/branches/default/tip", |
| 97 |
), |
| 98 |
( |
| 99 |
str(tool), |
| 100 |
"update-ref", |
| 101 |
"-m", |
| 102 |
"mach try: push", |
| 103 |
"HEAD", |
| 104 |
"1234567890123456789012345678901234567890", |
| 105 |
"0987654321098765432109876543210987654321", |
| 106 |
), |
| 107 |
( |
| 108 |
str(tool), |
| 109 |
"update-ref", |
| 110 |
"-m", |
| 111 |
"mach try: restore", |
| 112 |
"HEAD", |
| 113 |
"0987654321098765432109876543210987654321", |
| 114 |
"1234567890123456789012345678901234567890", |
| 115 |
), |
| 116 |
] |
| 117 |
expected_inputs = [ |
| 118 |
textwrap.dedent( |
| 119 |
f"""\ |
| 120 |
commit refs/machtry/974284fd-f395-4a15-a9d7-814a71241242 |
| 121 |
mark :1 |
| 122 |
author FooBar <[email protected]> 0 +0000 |
| 123 |
committer FooBar <[email protected]> 0 +0000 |
| 124 |
data {len(commit_message)} |
| 125 |
{commit_message} |
| 126 |
from 0987654321098765432109876543210987654321 |
| 127 |
M 100644 inline extra-file |
| 128 |
data 7 |
| 129 |
content |
| 130 |
M 100644 inline other/extra-file |
| 131 |
data 8 |
| 132 |
content2 |
| 133 |
reset refs/machtry/974284fd-f395-4a15-a9d7-814a71241242 |
| 134 |
from 0000000000000000000000000000000000000000 |
| 135 |
get-mark :1 |
| 136 |
""" |
| 137 |
), |
| 138 |
] |
| 139 |
else: |
| 140 |
assert repo.vcs == "jj" |
| 141 |
expected = [ |
| 142 |
(str(vcs._git._tool), "cinnabar", "--version"), |
| 143 |
( |
| 144 |
str(tool), |
| 145 |
"--ignore-working-copy", |
| 146 |
"operation", |
| 147 |
"log", |
| 148 |
"-n1", |
| 149 |
"--no-graph", |
| 150 |
"-T", |
| 151 |
"id.short(16)", |
| 152 |
), |
| 153 |
(str(tool), "new", "-m", "commit message", "latest((@ ~ empty()) | @-)"), |
| 154 |
(str(tool), "log", "-n0"), |
| 155 |
( |
| 156 |
str(tool), |
| 157 |
"--ignore-working-copy", |
| 158 |
"log", |
| 159 |
"--no-graph", |
| 160 |
"-n1", |
| 161 |
"-r", |
| 162 |
"@", |
| 163 |
"-T", |
| 164 |
"change_id.short()", |
| 165 |
), |
| 166 |
(str(vcs._git._tool), "remote"), |
| 167 |
( |
| 168 |
str(vcs._git._tool), |
| 169 |
"remote", |
| 170 |
"add", |
| 171 |
"mach_tryserver", |
| 172 |
"hg::ssh://hg.mozilla.org/try", |
| 173 |
), |
| 174 |
(str(tool), "git", "import"), |
| 175 |
( |
| 176 |
str(tool), |
| 177 |
"git", |
| 178 |
"push", |
| 179 |
"--remote", |
| 180 |
"mach_tryserver", |
| 181 |
"--change", |
| 182 |
None, |
| 183 |
"--allow-new", |
| 184 |
"--allow-empty-description", |
| 185 |
), |
| 186 |
(str(tool), "operation", "restore", ""), |
| 187 |
(str(tool), "git", "remote", "remove", "mach_tryserver"), |
| 188 |
] |
| 189 |
expected_inputs = [] |
| 190 |
|
| 191 |
for i, value in enumerate(captured_commands): |
| 192 |
assert value == expected[i] |
| 193 |
|
| 194 |
assert len(captured_commands) == len(expected) |
| 195 |
|
| 196 |
for i, value in enumerate(captured_inputs): |
| 197 |
assert value == expected_inputs[i] |
| 198 |
|
| 199 |
assert len(captured_inputs) == len(expected_inputs) |
| 200 |
|
| 201 |
|
| 202 |
def test_push_to_try_missing_extensions(repo, monkeypatch): |
| 203 |
if repo.vcs not in ("git", "jj"): |
| 204 |
return |
| 205 |
|
| 206 |
vcs = get_repository_object(repo.dir) |
| 207 |
|
| 208 |
orig = vcs._run |
| 209 |
|
| 210 |
def cinnabar_raises(*args, **kwargs): |
| 211 |
# Simulate not having git cinnabar |
| 212 |
if args[0] == "cinnabar": |
| 213 |
raise subprocess.CalledProcessError(1, args) |
| 214 |
return orig(*args, **kwargs) |
| 215 |
|
| 216 |
monkeypatch.setattr(vcs, "_run", cinnabar_raises) |
| 217 |
if hasattr(vcs, "_git"): |
| 218 |
monkeypatch.setattr(vcs._git, "_run", cinnabar_raises) |
| 219 |
|
| 220 |
with pytest.raises(MissingVCSExtension): |
| 221 |
vcs.push_to_try("commit message") |
| 222 |
|
| 223 |
|
| 224 |
if __name__ == "__main__": |
| 225 |
mozunit.main() |