| 1 |
# |
| 2 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
# you may not use this file except in compliance with the License. |
| 4 |
# You may obtain a copy of the License at |
| 5 |
# |
| 6 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
# |
| 8 |
# Unless required by applicable law or agreed to in writing, software |
| 9 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
# See the License for the specific language governing permissions and |
| 12 |
# limitations under the License. |
| 13 |
# |
| 14 |
# Authors: Tristan Van Berkom <[email protected]> |
| 15 |
# Jonathan Maw <[email protected]> |
| 16 |
# William Salmon <[email protected]> |
| 17 |
# |
| 18 |
|
| 19 |
# Pylint doesn't play well with fixtures and dependency injection from pytest |
| 20 |
# pylint: disable=redefined-outer-name |
| 21 |
|
| 22 |
import os |
| 23 |
import subprocess |
| 24 |
import shutil |
| 25 |
|
| 26 |
import pytest |
| 27 |
|
| 28 |
from buildstream import Node |
| 29 |
from buildstream.exceptions import ErrorDomain |
| 30 |
from buildstream.plugin import CoreWarnings |
| 31 |
from buildstream._testing import cli # pylint: disable=unused-import |
| 32 |
from buildstream._testing import generate_project, generate_element, load_yaml |
| 33 |
from buildstream._testing import create_repo |
| 34 |
from buildstream import _yaml |
| 35 |
|
| 36 |
from tests.testutils.site import HAVE_GIT, HAVE_OLD_GIT |
| 37 |
|
| 38 |
DATA_DIR = os.path.join( |
| 39 |
os.path.dirname(os.path.realpath(__file__)), |
| 40 |
"git", |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
def generate_project_with_git(project, config): |
| 45 |
config["plugins"] = [{"origin": "pip", "package-name": "buildstream-plugins", "sources": ["git"]}] |
| 46 |
generate_project(project, config) |
| 47 |
|
| 48 |
|
| 49 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 50 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 51 |
def test_fetch_bad_ref(cli, tmpdir, datafiles): |
| 52 |
project = str(datafiles) |
| 53 |
|
| 54 |
# Create the repo from 'repofiles' subdir |
| 55 |
repo = create_repo("git", str(tmpdir)) |
| 56 |
repo.create(os.path.join(project, "repofiles")) |
| 57 |
|
| 58 |
# Write out our test target with a bad ref |
| 59 |
element = {"kind": "import", "sources": [repo.source_config(ref="5")]} |
| 60 |
generate_element(project, "target.bst", element) |
| 61 |
|
| 62 |
# Assert that fetch raises an error here |
| 63 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 64 |
result.assert_main_error(ErrorDomain.STREAM, None) |
| 65 |
result.assert_task_error(ErrorDomain.SOURCE, None) |
| 66 |
|
| 67 |
|
| 68 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 69 |
@pytest.mark.skipif(HAVE_OLD_GIT, reason="old git cannot clone a shallow repo to stage the source") |
| 70 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 71 |
def test_fetch_shallow(cli, tmpdir, datafiles): |
| 72 |
project = str(datafiles) |
| 73 |
workspacedir = os.path.join(str(tmpdir), "workspace") |
| 74 |
|
| 75 |
# Create the repo from 'repofiles' subdir |
| 76 |
repo = create_repo("git", str(tmpdir)) |
| 77 |
repo.create(os.path.join(project, "repofiles")) |
| 78 |
first_commit = repo.latest_commit() |
| 79 |
repo.add_commit() |
| 80 |
repo.add_tag("tag") |
| 81 |
|
| 82 |
ref = "tag-0-g" + repo.latest_commit() |
| 83 |
|
| 84 |
element = {"kind": "import", "sources": [repo.source_config(ref=ref)]} |
| 85 |
generate_element(project, "target.bst", element) |
| 86 |
|
| 87 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 88 |
result.assert_success() |
| 89 |
result = cli.run(project=project, args=["workspace", "open", "--directory", workspacedir, "target.bst"]) |
| 90 |
result.assert_success() |
| 91 |
|
| 92 |
assert subprocess.call(["git", "show", "tag"], cwd=workspacedir) == 0 |
| 93 |
assert subprocess.call(["git", "show", first_commit], cwd=workspacedir) != 0 |
| 94 |
|
| 95 |
|
| 96 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 97 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 98 |
def test_submodule_fetch_checkout(cli, tmpdir, datafiles): |
| 99 |
project = str(datafiles) |
| 100 |
checkoutdir = os.path.join(str(tmpdir), "checkout") |
| 101 |
|
| 102 |
# Create the submodule first from the 'subrepofiles' subdir |
| 103 |
subrepo = create_repo("git", str(tmpdir), "subrepo") |
| 104 |
subrepo.create(os.path.join(project, "subrepofiles")) |
| 105 |
|
| 106 |
# Create the repo from 'repofiles' subdir |
| 107 |
repo = create_repo("git", str(tmpdir)) |
| 108 |
repo.create(os.path.join(project, "repofiles")) |
| 109 |
|
| 110 |
# Add a submodule pointing to the one we created |
| 111 |
ref = repo.add_submodule("subdir", "file://" + subrepo.repo) |
| 112 |
|
| 113 |
# Write out our test target |
| 114 |
element = {"kind": "import", "sources": [repo.source_config(ref=ref)]} |
| 115 |
generate_element(project, "target.bst", element) |
| 116 |
|
| 117 |
# Fetch, build, checkout |
| 118 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 119 |
result.assert_success() |
| 120 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 121 |
result.assert_success() |
| 122 |
result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir]) |
| 123 |
result.assert_success() |
| 124 |
|
| 125 |
# Assert we checked out both files at their expected location |
| 126 |
assert os.path.exists(os.path.join(checkoutdir, "file.txt")) |
| 127 |
assert os.path.exists(os.path.join(checkoutdir, "subdir", "ponyfile.txt")) |
| 128 |
|
| 129 |
|
| 130 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 131 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 132 |
def test_recursive_submodule_fetch_checkout(cli, tmpdir, datafiles): |
| 133 |
project = str(datafiles) |
| 134 |
checkoutdir = os.path.join(str(tmpdir), "checkout") |
| 135 |
|
| 136 |
# Create a submodule from the 'othersubrepofiles' subdir |
| 137 |
subsubrepo = create_repo("git", str(tmpdir), "subsubrepo") |
| 138 |
subsubrepo.create(os.path.join(project, "othersubrepofiles")) |
| 139 |
|
| 140 |
# Create another submodule from the 'subrepofiles' subdir |
| 141 |
subrepo = create_repo("git", str(tmpdir), "subrepo") |
| 142 |
subrepo.create(os.path.join(project, "subrepofiles")) |
| 143 |
|
| 144 |
# Create the repo from 'repofiles' subdir |
| 145 |
repo = create_repo("git", str(tmpdir)) |
| 146 |
repo.create(os.path.join(project, "repofiles")) |
| 147 |
|
| 148 |
# Configure submodules |
| 149 |
subrepo.add_submodule("subdir", "file://" + subsubrepo.repo) |
| 150 |
ref = repo.add_submodule("subdir", "file://" + subrepo.repo) |
| 151 |
|
| 152 |
# Write out our test target |
| 153 |
element = {"kind": "import", "sources": [repo.source_config(ref=ref)]} |
| 154 |
generate_element(project, "target.bst", element) |
| 155 |
|
| 156 |
# Fetch, build, checkout |
| 157 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 158 |
result.assert_success() |
| 159 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 160 |
result.assert_success() |
| 161 |
result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir]) |
| 162 |
result.assert_success() |
| 163 |
|
| 164 |
# Assert we checked out all files at their expected location |
| 165 |
assert os.path.exists(os.path.join(checkoutdir, "file.txt")) |
| 166 |
assert os.path.exists(os.path.join(checkoutdir, "subdir", "ponyfile.txt")) |
| 167 |
assert os.path.exists(os.path.join(checkoutdir, "subdir", "subdir", "unicornfile.txt")) |
| 168 |
|
| 169 |
|
| 170 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 171 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 172 |
def test_submodule_fetch_source_enable_explicit(cli, tmpdir, datafiles): |
| 173 |
project = str(datafiles) |
| 174 |
checkoutdir = os.path.join(str(tmpdir), "checkout") |
| 175 |
|
| 176 |
# Create the submodule first from the 'subrepofiles' subdir |
| 177 |
subrepo = create_repo("git", str(tmpdir), "subrepo") |
| 178 |
subrepo.create(os.path.join(project, "subrepofiles")) |
| 179 |
|
| 180 |
# Create the repo from 'repofiles' subdir |
| 181 |
repo = create_repo("git", str(tmpdir)) |
| 182 |
repo.create(os.path.join(project, "repofiles")) |
| 183 |
|
| 184 |
# Add a submodule pointing to the one we created |
| 185 |
ref = repo.add_submodule("subdir", "file://" + subrepo.repo) |
| 186 |
|
| 187 |
# Write out our test target |
| 188 |
element = {"kind": "import", "sources": [repo.source_config_extra(ref=ref, checkout_submodules=True)]} |
| 189 |
generate_element(project, "target.bst", element) |
| 190 |
|
| 191 |
# Fetch, build, checkout |
| 192 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 193 |
result.assert_success() |
| 194 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 195 |
result.assert_success() |
| 196 |
result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir]) |
| 197 |
result.assert_success() |
| 198 |
|
| 199 |
# Assert we checked out both files at their expected location |
| 200 |
assert os.path.exists(os.path.join(checkoutdir, "file.txt")) |
| 201 |
assert os.path.exists(os.path.join(checkoutdir, "subdir", "ponyfile.txt")) |
| 202 |
|
| 203 |
|
| 204 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 205 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 206 |
def test_submodule_fetch_source_disable(cli, tmpdir, datafiles): |
| 207 |
project = str(datafiles) |
| 208 |
checkoutdir = os.path.join(str(tmpdir), "checkout") |
| 209 |
|
| 210 |
# Create the submodule first from the 'subrepofiles' subdir |
| 211 |
subrepo = create_repo("git", str(tmpdir), "subrepo") |
| 212 |
subrepo.create(os.path.join(project, "subrepofiles")) |
| 213 |
|
| 214 |
# Create the repo from 'repofiles' subdir |
| 215 |
repo = create_repo("git", str(tmpdir)) |
| 216 |
repo.create(os.path.join(project, "repofiles")) |
| 217 |
|
| 218 |
# Add a submodule pointing to the one we created |
| 219 |
ref = repo.add_submodule("subdir", "file://" + subrepo.repo) |
| 220 |
|
| 221 |
# Write out our test target |
| 222 |
element = {"kind": "import", "sources": [repo.source_config_extra(ref=ref, checkout_submodules=False)]} |
| 223 |
generate_element(project, "target.bst", element) |
| 224 |
|
| 225 |
# Fetch, build, checkout |
| 226 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 227 |
result.assert_success() |
| 228 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 229 |
result.assert_success() |
| 230 |
result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir]) |
| 231 |
result.assert_success() |
| 232 |
|
| 233 |
# Assert we checked out both files at their expected location |
| 234 |
assert os.path.exists(os.path.join(checkoutdir, "file.txt")) |
| 235 |
assert not os.path.exists(os.path.join(checkoutdir, "subdir", "ponyfile.txt")) |
| 236 |
|
| 237 |
|
| 238 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 239 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 240 |
def test_submodule_fetch_submodule_does_override(cli, tmpdir, datafiles): |
| 241 |
project = str(datafiles) |
| 242 |
checkoutdir = os.path.join(str(tmpdir), "checkout") |
| 243 |
|
| 244 |
# Create the submodule first from the 'subrepofiles' subdir |
| 245 |
subrepo = create_repo("git", str(tmpdir), "subrepo") |
| 246 |
subrepo.create(os.path.join(project, "subrepofiles")) |
| 247 |
|
| 248 |
# Create the repo from 'repofiles' subdir |
| 249 |
repo = create_repo("git", str(tmpdir)) |
| 250 |
repo.create(os.path.join(project, "repofiles")) |
| 251 |
|
| 252 |
# Add a submodule pointing to the one we created |
| 253 |
ref = repo.add_submodule("subdir", "file://" + subrepo.repo, checkout=True) |
| 254 |
|
| 255 |
# Write out our test target |
| 256 |
element = {"kind": "import", "sources": [repo.source_config_extra(ref=ref, checkout_submodules=False)]} |
| 257 |
generate_element(project, "target.bst", element) |
| 258 |
|
| 259 |
# Fetch, build, checkout |
| 260 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 261 |
result.assert_success() |
| 262 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 263 |
result.assert_success() |
| 264 |
result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir]) |
| 265 |
result.assert_success() |
| 266 |
|
| 267 |
# Assert we checked out both files at their expected location |
| 268 |
assert os.path.exists(os.path.join(checkoutdir, "file.txt")) |
| 269 |
assert os.path.exists(os.path.join(checkoutdir, "subdir", "ponyfile.txt")) |
| 270 |
|
| 271 |
|
| 272 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 273 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 274 |
def test_submodule_fetch_submodule_individual_checkout(cli, tmpdir, datafiles): |
| 275 |
project = str(datafiles) |
| 276 |
checkoutdir = os.path.join(str(tmpdir), "checkout") |
| 277 |
|
| 278 |
# Create the submodule first from the 'subrepofiles' subdir |
| 279 |
subrepo = create_repo("git", str(tmpdir), "subrepo") |
| 280 |
subrepo.create(os.path.join(project, "subrepofiles")) |
| 281 |
|
| 282 |
# Create another submodule from the 'othersubrepofiles' subdir |
| 283 |
other_subrepo = create_repo("git", str(tmpdir), "othersubrepo") |
| 284 |
other_subrepo.create(os.path.join(project, "othersubrepofiles")) |
| 285 |
|
| 286 |
# Create the repo from 'repofiles' subdir |
| 287 |
repo = create_repo("git", str(tmpdir)) |
| 288 |
repo.create(os.path.join(project, "repofiles")) |
| 289 |
|
| 290 |
# Add a submodule pointing to the one we created |
| 291 |
repo.add_submodule("subdir", "file://" + subrepo.repo, checkout=False) |
| 292 |
ref = repo.add_submodule("othersubdir", "file://" + other_subrepo.repo) |
| 293 |
|
| 294 |
# Write out our test target |
| 295 |
element = {"kind": "import", "sources": [repo.source_config_extra(ref=ref, checkout_submodules=True)]} |
| 296 |
generate_element(project, "target.bst", element) |
| 297 |
|
| 298 |
# Fetch, build, checkout |
| 299 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 300 |
result.assert_success() |
| 301 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 302 |
result.assert_success() |
| 303 |
result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir]) |
| 304 |
result.assert_success() |
| 305 |
|
| 306 |
# Assert we checked out files at their expected location |
| 307 |
assert os.path.exists(os.path.join(checkoutdir, "file.txt")) |
| 308 |
assert not os.path.exists(os.path.join(checkoutdir, "subdir", "ponyfile.txt")) |
| 309 |
assert os.path.exists(os.path.join(checkoutdir, "othersubdir", "unicornfile.txt")) |
| 310 |
|
| 311 |
|
| 312 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 313 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 314 |
def test_submodule_fetch_submodule_individual_checkout_explicit(cli, tmpdir, datafiles): |
| 315 |
project = str(datafiles) |
| 316 |
checkoutdir = os.path.join(str(tmpdir), "checkout") |
| 317 |
|
| 318 |
# Create the submodule first from the 'subrepofiles' subdir |
| 319 |
subrepo = create_repo("git", str(tmpdir), "subrepo") |
| 320 |
subrepo.create(os.path.join(project, "subrepofiles")) |
| 321 |
|
| 322 |
# Create another submodule from the 'othersubrepofiles' subdir |
| 323 |
other_subrepo = create_repo("git", str(tmpdir), "othersubrepo") |
| 324 |
other_subrepo.create(os.path.join(project, "othersubrepofiles")) |
| 325 |
|
| 326 |
# Create the repo from 'repofiles' subdir |
| 327 |
repo = create_repo("git", str(tmpdir)) |
| 328 |
repo.create(os.path.join(project, "repofiles")) |
| 329 |
|
| 330 |
# Add a submodule pointing to the one we created |
| 331 |
repo.add_submodule("subdir", "file://" + subrepo.repo, checkout=False) |
| 332 |
ref = repo.add_submodule("othersubdir", "file://" + other_subrepo.repo, checkout=True) |
| 333 |
|
| 334 |
# Write out our test target |
| 335 |
element = {"kind": "import", "sources": [repo.source_config_extra(ref=ref, checkout_submodules=True)]} |
| 336 |
generate_element(project, "target.bst", element) |
| 337 |
|
| 338 |
# Fetch, build, checkout |
| 339 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 340 |
result.assert_success() |
| 341 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 342 |
result.assert_success() |
| 343 |
result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir]) |
| 344 |
result.assert_success() |
| 345 |
|
| 346 |
# Assert we checked out files at their expected location |
| 347 |
assert os.path.exists(os.path.join(checkoutdir, "file.txt")) |
| 348 |
assert not os.path.exists(os.path.join(checkoutdir, "subdir", "ponyfile.txt")) |
| 349 |
assert os.path.exists(os.path.join(checkoutdir, "othersubdir", "unicornfile.txt")) |
| 350 |
|
| 351 |
|
| 352 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 353 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "project-override")) |
| 354 |
def test_submodule_fetch_project_override(cli, tmpdir, datafiles): |
| 355 |
project = str(datafiles) |
| 356 |
checkoutdir = os.path.join(str(tmpdir), "checkout") |
| 357 |
|
| 358 |
# Create the submodule first from the 'subrepofiles' subdir |
| 359 |
subrepo = create_repo("git", str(tmpdir), "subrepo") |
| 360 |
subrepo.create(os.path.join(project, "subrepofiles")) |
| 361 |
|
| 362 |
# Create the repo from 'repofiles' subdir |
| 363 |
repo = create_repo("git", str(tmpdir)) |
| 364 |
repo.create(os.path.join(project, "repofiles")) |
| 365 |
|
| 366 |
# Add a submodule pointing to the one we created |
| 367 |
ref = repo.add_submodule("subdir", "file://" + subrepo.repo) |
| 368 |
|
| 369 |
# Write out our test target |
| 370 |
element = {"kind": "import", "sources": [repo.source_config(ref=ref)]} |
| 371 |
generate_element(project, "target.bst", element) |
| 372 |
|
| 373 |
# Fetch, build, checkout |
| 374 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 375 |
result.assert_success() |
| 376 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 377 |
result.assert_success() |
| 378 |
result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir]) |
| 379 |
result.assert_success() |
| 380 |
|
| 381 |
# Assert we checked out both files at their expected location |
| 382 |
assert os.path.exists(os.path.join(checkoutdir, "file.txt")) |
| 383 |
assert not os.path.exists(os.path.join(checkoutdir, "subdir", "ponyfile.txt")) |
| 384 |
|
| 385 |
|
| 386 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 387 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 388 |
def test_submodule_track_ignore_inconsistent(cli, tmpdir, datafiles): |
| 389 |
project = str(datafiles) |
| 390 |
|
| 391 |
# Create the repo from 'repofiles' subdir |
| 392 |
repo = create_repo("git", str(tmpdir)) |
| 393 |
ref = repo.create(os.path.join(project, "repofiles")) |
| 394 |
|
| 395 |
# Write out our test target |
| 396 |
element = {"kind": "import", "sources": [repo.source_config(ref=ref)]} |
| 397 |
generate_element(project, "target.bst", element) |
| 398 |
|
| 399 |
# Now add a .gitmodules file with an inconsistent submodule, |
| 400 |
# we are calling this inconsistent because the file was created |
| 401 |
# but `git submodule add` was never called, so there is no reference |
| 402 |
# associated to the submodule. |
| 403 |
# |
| 404 |
repo.add_file(os.path.join(project, "inconsistent-submodule", ".gitmodules")) |
| 405 |
|
| 406 |
# Fetch should work, we're not yet at the offending ref |
| 407 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 408 |
result.assert_success() |
| 409 |
|
| 410 |
# Track to update to the offending commit |
| 411 |
result = cli.run(project=project, args=["source", "track", "target.bst"]) |
| 412 |
result.assert_success() |
| 413 |
|
| 414 |
# Fetch after track will encounter an inconsistent submodule without any ref |
| 415 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 416 |
result.assert_success() |
| 417 |
|
| 418 |
# Assert that we are just fine without it, and emit a warning to the user. |
| 419 |
assert "Ignoring inconsistent submodule" in result.stderr |
| 420 |
|
| 421 |
|
| 422 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 423 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 424 |
def test_submodule_track_no_ref_or_track(cli, tmpdir, datafiles): |
| 425 |
project = str(datafiles) |
| 426 |
|
| 427 |
# Create the repo from 'repofiles' subdir |
| 428 |
repo = create_repo("git", str(tmpdir)) |
| 429 |
repo.create(os.path.join(project, "repofiles")) |
| 430 |
|
| 431 |
# Write out our test target |
| 432 |
gitsource = repo.source_config(ref=None) |
| 433 |
gitsource.pop("track") |
| 434 |
element = {"kind": "import", "sources": [gitsource]} |
| 435 |
generate_element(project, "target.bst", element) |
| 436 |
|
| 437 |
# Track will encounter an inconsistent submodule without any ref |
| 438 |
result = cli.run(project=project, args=["show", "target.bst"]) |
| 439 |
result.assert_main_error(ErrorDomain.SOURCE, "missing-track-and-ref") |
| 440 |
result.assert_task_error(None, None) |
| 441 |
|
| 442 |
|
| 443 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 444 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 445 |
@pytest.mark.parametrize("fail", ["warn", "error"]) |
| 446 |
def test_ref_not_in_track(cli, tmpdir, datafiles, fail): |
| 447 |
project = str(datafiles) |
| 448 |
|
| 449 |
# Make the warning an error if we're testing errors |
| 450 |
if fail == "error": |
| 451 |
generate_project_with_git(project, config={"fatal-warnings": [CoreWarnings.REF_NOT_IN_TRACK]}) |
| 452 |
|
| 453 |
# Create the repo from 'repofiles', create a branch without latest commit |
| 454 |
repo = create_repo("git", str(tmpdir)) |
| 455 |
ref = repo.create(os.path.join(project, "repofiles")) |
| 456 |
|
| 457 |
gitsource = repo.source_config(ref=ref) |
| 458 |
|
| 459 |
# Overwrite the track value to the added branch |
| 460 |
gitsource["track"] = "foo" |
| 461 |
|
| 462 |
# Write out our test target |
| 463 |
element = {"kind": "import", "sources": [gitsource]} |
| 464 |
generate_element(project, "target.bst", element) |
| 465 |
|
| 466 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 467 |
|
| 468 |
# Assert a warning or an error depending on what we're checking |
| 469 |
if fail == "error": |
| 470 |
result.assert_main_error(ErrorDomain.STREAM, None) |
| 471 |
result.assert_task_error(ErrorDomain.PLUGIN, CoreWarnings.REF_NOT_IN_TRACK) |
| 472 |
else: |
| 473 |
result.assert_success() |
| 474 |
assert "ref-not-in-track" in result.stderr |
| 475 |
|
| 476 |
|
| 477 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 478 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 479 |
@pytest.mark.parametrize("fail", ["warn", "error"]) |
| 480 |
def test_unlisted_submodule(cli, tmpdir, datafiles, fail): |
| 481 |
project = str(datafiles) |
| 482 |
|
| 483 |
# Make the warning an error if we're testing errors |
| 484 |
if fail == "error": |
| 485 |
generate_project_with_git(project, config={"fatal-warnings": ["git:unlisted-submodule"]}) |
| 486 |
|
| 487 |
# Create the submodule first from the 'subrepofiles' subdir |
| 488 |
subrepo = create_repo("git", str(tmpdir), "subrepo") |
| 489 |
subrepo.create(os.path.join(project, "subrepofiles")) |
| 490 |
|
| 491 |
# Create the repo from 'repofiles' subdir |
| 492 |
repo = create_repo("git", str(tmpdir)) |
| 493 |
repo.create(os.path.join(project, "repofiles")) |
| 494 |
|
| 495 |
# Add a submodule pointing to the one we created |
| 496 |
ref = repo.add_submodule("subdir", "file://" + subrepo.repo) |
| 497 |
|
| 498 |
# Create the source, and delete the explicit configuration |
| 499 |
# of the submodules. |
| 500 |
# |
| 501 |
# We expect this to cause an unlisted submodule warning |
| 502 |
# after the source has been fetched. |
| 503 |
# |
| 504 |
gitsource = repo.source_config(ref=ref) |
| 505 |
del gitsource["submodules"] |
| 506 |
|
| 507 |
# Write out our test target |
| 508 |
element = {"kind": "import", "sources": [gitsource]} |
| 509 |
generate_element(project, "target.bst", element) |
| 510 |
|
| 511 |
# The warning or error is reported during fetch. There should be no |
| 512 |
# error with `bst show`. |
| 513 |
result = cli.run(project=project, args=["show", "target.bst"]) |
| 514 |
result.assert_success() |
| 515 |
assert "git:unlisted-submodule" not in result.stderr |
| 516 |
|
| 517 |
# We will notice this directly in fetch, as it will try to fetch |
| 518 |
# the submodules it discovers as a result of fetching the primary repo. |
| 519 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 520 |
|
| 521 |
# Assert a warning or an error depending on what we're checking |
| 522 |
if fail == "error": |
| 523 |
result.assert_main_error(ErrorDomain.STREAM, None) |
| 524 |
result.assert_task_error(ErrorDomain.PLUGIN, "git:unlisted-submodule") |
| 525 |
else: |
| 526 |
result.assert_success() |
| 527 |
assert "git:unlisted-submodule" in result.stderr |
| 528 |
|
| 529 |
# Verify that `bst show` will still not error out after fetching. |
| 530 |
result = cli.run(project=project, args=["show", "target.bst"]) |
| 531 |
result.assert_success() |
| 532 |
assert "git:unlisted-submodule" not in result.stderr |
| 533 |
|
| 534 |
|
| 535 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 536 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 537 |
@pytest.mark.parametrize("fail", ["warn", "error"]) |
| 538 |
def test_track_unlisted_submodule(cli, tmpdir, datafiles, fail): |
| 539 |
project = str(datafiles) |
| 540 |
|
| 541 |
# Make the warning an error if we're testing errors |
| 542 |
if fail == "error": |
| 543 |
generate_project_with_git(project, config={"fatal-warnings": ["git:unlisted-submodule"]}) |
| 544 |
|
| 545 |
# Create the submodule first from the 'subrepofiles' subdir |
| 546 |
subrepo = create_repo("git", str(tmpdir), "subrepo") |
| 547 |
subrepo.create(os.path.join(project, "subrepofiles")) |
| 548 |
|
| 549 |
# Create the repo from 'repofiles' subdir |
| 550 |
repo = create_repo("git", str(tmpdir)) |
| 551 |
ref = repo.create(os.path.join(project, "repofiles")) |
| 552 |
|
| 553 |
# Add a submodule pointing to the one we created, but use |
| 554 |
# the original ref, let the submodules appear after tracking |
| 555 |
repo.add_submodule("subdir", "file://" + subrepo.repo) |
| 556 |
|
| 557 |
# Create the source, and delete the explicit configuration |
| 558 |
# of the submodules. |
| 559 |
gitsource = repo.source_config(ref=ref) |
| 560 |
del gitsource["submodules"] |
| 561 |
|
| 562 |
# Write out our test target |
| 563 |
element = {"kind": "import", "sources": [gitsource]} |
| 564 |
generate_element(project, "target.bst", element) |
| 565 |
|
| 566 |
# Fetch the repo, we will not see the warning because we |
| 567 |
# are still pointing to a ref which predates the submodules |
| 568 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 569 |
result.assert_success() |
| 570 |
assert "git:unlisted-submodule" not in result.stderr |
| 571 |
|
| 572 |
# We won't get a warning/error when tracking either, the source |
| 573 |
# has not become cached so the opportunity to check |
| 574 |
# for the warning has not yet arisen. |
| 575 |
result = cli.run(project=project, args=["source", "track", "target.bst"]) |
| 576 |
result.assert_success() |
| 577 |
assert "git:unlisted-submodule" not in result.stderr |
| 578 |
|
| 579 |
# Fetching the repo at the new ref will finally reveal the warning |
| 580 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 581 |
if fail == "error": |
| 582 |
result.assert_main_error(ErrorDomain.STREAM, None) |
| 583 |
result.assert_task_error(ErrorDomain.PLUGIN, "git:unlisted-submodule") |
| 584 |
else: |
| 585 |
result.assert_success() |
| 586 |
assert "git:unlisted-submodule" in result.stderr |
| 587 |
|
| 588 |
|
| 589 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 590 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 591 |
@pytest.mark.parametrize("fail", ["warn", "error"]) |
| 592 |
def test_invalid_submodule(cli, tmpdir, datafiles, fail): |
| 593 |
project = str(datafiles) |
| 594 |
|
| 595 |
# Make the warning an error if we're testing errors |
| 596 |
if fail == "error": |
| 597 |
generate_project_with_git(project, config={"fatal-warnings": ["git:invalid-submodule"]}) |
| 598 |
|
| 599 |
# Create the repo from 'repofiles' subdir |
| 600 |
repo = create_repo("git", str(tmpdir)) |
| 601 |
ref = repo.create(os.path.join(project, "repofiles")) |
| 602 |
|
| 603 |
# Create the source without any submodules, and add |
| 604 |
# an invalid submodule configuration to it. |
| 605 |
# |
| 606 |
# We expect this to cause an invalid submodule warning |
| 607 |
# after the source has been fetched and we know what |
| 608 |
# the real submodules actually are. |
| 609 |
# |
| 610 |
gitsource = repo.source_config(ref=ref) |
| 611 |
gitsource["submodules"] = {"subdir": {"url": "https://pony.org/repo.git"}} |
| 612 |
|
| 613 |
# Write out our test target |
| 614 |
element = {"kind": "import", "sources": [gitsource]} |
| 615 |
generate_element(project, "target.bst", element) |
| 616 |
|
| 617 |
# The warning or error is reported during fetch. There should be no |
| 618 |
# error with `bst show`. |
| 619 |
result = cli.run(project=project, args=["show", "target.bst"]) |
| 620 |
result.assert_success() |
| 621 |
assert "git:invalid-submodule" not in result.stderr |
| 622 |
|
| 623 |
# We will notice this directly in fetch, as it will try to fetch |
| 624 |
# the submodules it discovers as a result of fetching the primary repo. |
| 625 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 626 |
|
| 627 |
# Assert a warning or an error depending on what we're checking |
| 628 |
if fail == "error": |
| 629 |
result.assert_main_error(ErrorDomain.STREAM, None) |
| 630 |
result.assert_task_error(ErrorDomain.PLUGIN, "git:invalid-submodule") |
| 631 |
else: |
| 632 |
result.assert_success() |
| 633 |
assert "git:invalid-submodule" in result.stderr |
| 634 |
|
| 635 |
# Verify that `bst show` will still not error out after fetching. |
| 636 |
result = cli.run(project=project, args=["show", "target.bst"]) |
| 637 |
result.assert_success() |
| 638 |
assert "git:invalid-submodule" not in result.stderr |
| 639 |
|
| 640 |
|
| 641 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 642 |
@pytest.mark.skipif(HAVE_OLD_GIT, reason="old git rm does not update .gitmodules") |
| 643 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 644 |
@pytest.mark.parametrize("fail", ["warn", "error"]) |
| 645 |
def test_track_invalid_submodule(cli, tmpdir, datafiles, fail): |
| 646 |
project = str(datafiles) |
| 647 |
|
| 648 |
# Make the warning an error if we're testing errors |
| 649 |
if fail == "error": |
| 650 |
generate_project_with_git(project, config={"fatal-warnings": ["git:invalid-submodule"]}) |
| 651 |
|
| 652 |
# Create the submodule first from the 'subrepofiles' subdir |
| 653 |
subrepo = create_repo("git", str(tmpdir), "subrepo") |
| 654 |
subrepo.create(os.path.join(project, "subrepofiles")) |
| 655 |
|
| 656 |
# Create the repo from 'repofiles' subdir |
| 657 |
repo = create_repo("git", str(tmpdir)) |
| 658 |
repo.create(os.path.join(project, "repofiles")) |
| 659 |
|
| 660 |
# Add a submodule pointing to the one we created |
| 661 |
ref = repo.add_submodule("subdir", "file://" + subrepo.repo) |
| 662 |
|
| 663 |
# Add a commit beyond the ref which *removes* the submodule we've added |
| 664 |
repo.remove_path("subdir") |
| 665 |
|
| 666 |
# Create the source, this will keep the submodules so initially |
| 667 |
# the configuration is valid for the ref we're using |
| 668 |
gitsource = repo.source_config(ref=ref) |
| 669 |
|
| 670 |
# Write out our test target |
| 671 |
element = {"kind": "import", "sources": [gitsource]} |
| 672 |
generate_element(project, "target.bst", element) |
| 673 |
|
| 674 |
# Fetch the repo, we will not see the warning because we |
| 675 |
# are still pointing to a ref which predates the submodules |
| 676 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 677 |
result.assert_success() |
| 678 |
assert "git:invalid-submodule" not in result.stderr |
| 679 |
|
| 680 |
# After tracking we're pointing to a ref, which would trigger an invalid |
| 681 |
# submodule warning. However, cache validation is only performed as part |
| 682 |
# of fetch. |
| 683 |
result = cli.run(project=project, args=["source", "track", "target.bst"]) |
| 684 |
result.assert_success() |
| 685 |
|
| 686 |
# Fetch to trigger cache validation |
| 687 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 688 |
if fail == "error": |
| 689 |
result.assert_main_error(ErrorDomain.STREAM, None) |
| 690 |
result.assert_task_error(ErrorDomain.PLUGIN, "git:invalid-submodule") |
| 691 |
else: |
| 692 |
result.assert_success() |
| 693 |
assert "git:invalid-submodule" in result.stderr |
| 694 |
|
| 695 |
|
| 696 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 697 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 698 |
@pytest.mark.parametrize("ref_format", ["sha1", "git-describe"]) |
| 699 |
@pytest.mark.parametrize("tag,extra_commit", [(False, False), (True, False), (True, True)]) |
| 700 |
def test_track_fetch(cli, tmpdir, datafiles, ref_format, tag, extra_commit): |
| 701 |
project = str(datafiles) |
| 702 |
|
| 703 |
# Create the repo from 'repofiles' subdir |
| 704 |
repo = create_repo("git", str(tmpdir)) |
| 705 |
repo.create(os.path.join(project, "repofiles")) |
| 706 |
if tag: |
| 707 |
repo.add_tag("tag") |
| 708 |
if extra_commit: |
| 709 |
repo.add_commit() |
| 710 |
|
| 711 |
# Write out our test target |
| 712 |
element = {"kind": "import", "sources": [repo.source_config()]} |
| 713 |
element["sources"][0]["ref-format"] = ref_format |
| 714 |
generate_element(project, "target.bst", element) |
| 715 |
element_path = os.path.join(project, "target.bst") |
| 716 |
|
| 717 |
# Track it |
| 718 |
result = cli.run(project=project, args=["source", "track", "target.bst"]) |
| 719 |
result.assert_success() |
| 720 |
|
| 721 |
element = load_yaml(element_path) |
| 722 |
new_ref = element.get_sequence("sources").mapping_at(0).get_str("ref") |
| 723 |
|
| 724 |
if ref_format == "git-describe" and tag: |
| 725 |
# Check and strip prefix |
| 726 |
prefix = "tag-{}-g".format(0 if not extra_commit else 1) |
| 727 |
assert new_ref.startswith(prefix) |
| 728 |
new_ref = new_ref[len(prefix) :] |
| 729 |
|
| 730 |
# 40 chars for SHA-1 |
| 731 |
assert len(new_ref) == 40 |
| 732 |
|
| 733 |
# Fetch it |
| 734 |
result = cli.run(project=project, args=["source", "fetch", "target.bst"]) |
| 735 |
result.assert_success() |
| 736 |
|
| 737 |
|
| 738 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 739 |
@pytest.mark.skipif(HAVE_OLD_GIT, reason="old git describe lacks --first-parent") |
| 740 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 741 |
@pytest.mark.parametrize("ref_storage", [("inline"), ("project.refs")]) |
| 742 |
@pytest.mark.parametrize("tag_type", [("annotated"), ("lightweight")]) |
| 743 |
def test_git_describe(cli, tmpdir, datafiles, ref_storage, tag_type): |
| 744 |
project = str(datafiles) |
| 745 |
|
| 746 |
project_config = load_yaml(os.path.join(project, "project.conf")) |
| 747 |
project_config["ref-storage"] = ref_storage |
| 748 |
generate_project_with_git(project, config=project_config) |
| 749 |
|
| 750 |
repofiles = os.path.join(str(tmpdir), "repofiles") |
| 751 |
os.makedirs(repofiles, exist_ok=True) |
| 752 |
file0 = os.path.join(repofiles, "file0") |
| 753 |
with open(file0, "w", encoding="utf-8") as f: |
| 754 |
f.write("test\n") |
| 755 |
|
| 756 |
repo = create_repo("git", str(tmpdir)) |
| 757 |
|
| 758 |
def tag(name): |
| 759 |
if tag_type == "annotated": |
| 760 |
repo.add_annotated_tag(name, name) |
| 761 |
else: |
| 762 |
repo.add_tag(name) |
| 763 |
|
| 764 |
repo.create(repofiles) |
| 765 |
tag("uselesstag") |
| 766 |
|
| 767 |
file1 = os.path.join(str(tmpdir), "file1") |
| 768 |
with open(file1, "w", encoding="utf-8") as f: |
| 769 |
f.write("test\n") |
| 770 |
repo.add_file(file1) |
| 771 |
tag("tag1") |
| 772 |
|
| 773 |
file2 = os.path.join(str(tmpdir), "file2") |
| 774 |
with open(file2, "w", encoding="utf-8") as f: |
| 775 |
f.write("test\n") |
| 776 |
repo.branch("branch2") |
| 777 |
repo.add_file(file2) |
| 778 |
tag("tag2") |
| 779 |
|
| 780 |
repo.checkout("master") |
| 781 |
file3 = os.path.join(str(tmpdir), "file3") |
| 782 |
with open(file3, "w", encoding="utf-8") as f: |
| 783 |
f.write("test\n") |
| 784 |
repo.add_file(file3) |
| 785 |
|
| 786 |
repo.merge("branch2") |
| 787 |
|
| 788 |
config = repo.source_config() |
| 789 |
config["track"] = repo.latest_commit() |
| 790 |
config["track-tags"] = True |
| 791 |
|
| 792 |
# Write out our test target |
| 793 |
element = { |
| 794 |
"kind": "import", |
| 795 |
"sources": [config], |
| 796 |
} |
| 797 |
generate_element(project, "target.bst", element) |
| 798 |
element_path = os.path.join(project, "target.bst") |
| 799 |
|
| 800 |
if ref_storage == "inline": |
| 801 |
result = cli.run(project=project, args=["source", "track", "target.bst"]) |
| 802 |
result.assert_success() |
| 803 |
else: |
| 804 |
result = cli.run(project=project, args=["source", "track", "target.bst", "--deps", "all"]) |
| 805 |
result.assert_success() |
| 806 |
|
| 807 |
if ref_storage == "inline": |
| 808 |
element = load_yaml(element_path) |
| 809 |
tags = element.get_sequence("sources").mapping_at(0).get_sequence("tags") |
| 810 |
assert len(tags) == 2 |
| 811 |
for tag in tags: |
| 812 |
assert "tag" in tag |
| 813 |
assert "commit" in tag |
| 814 |
assert "annotated" in tag |
| 815 |
assert tag.get_bool("annotated") == (tag_type == "annotated") |
| 816 |
|
| 817 |
assert {(tag.get_str("tag"), tag.get_str("commit")) for tag in tags} == { |
| 818 |
("tag1", repo.rev_parse("tag1^{commit}")), |
| 819 |
("tag2", repo.rev_parse("tag2^{commit}")), |
| 820 |
} |
| 821 |
|
| 822 |
checkout = os.path.join(str(tmpdir), "checkout") |
| 823 |
|
| 824 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 825 |
result.assert_success() |
| 826 |
result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkout]) |
| 827 |
result.assert_success() |
| 828 |
|
| 829 |
if tag_type == "annotated": |
| 830 |
options = [] |
| 831 |
else: |
| 832 |
options = ["--tags"] |
| 833 |
describe = subprocess.check_output(["git", "describe", *options], cwd=checkout, universal_newlines=True) |
| 834 |
assert describe.startswith("tag2-2-") |
| 835 |
|
| 836 |
describe_fp = subprocess.check_output( |
| 837 |
["git", "describe", "--first-parent", *options], cwd=checkout, universal_newlines=True |
| 838 |
) |
| 839 |
assert describe_fp.startswith("tag1-2-") |
| 840 |
|
| 841 |
tags = subprocess.check_output(["git", "tag"], cwd=checkout, universal_newlines=True) |
| 842 |
tags = set(tags.splitlines()) |
| 843 |
assert tags == set(["tag1", "tag2"]) |
| 844 |
|
| 845 |
with pytest.raises(subprocess.CalledProcessError): |
| 846 |
subprocess.run(["git", "log", repo.rev_parse("uselesstag")], cwd=checkout, check=True) |
| 847 |
|
| 848 |
|
| 849 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 850 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 851 |
@pytest.mark.parametrize("ref_storage", [("inline"), ("project.refs")]) |
| 852 |
@pytest.mark.parametrize("tag_type", [("annotated"), ("lightweight")]) |
| 853 |
def test_git_describe_head_is_tagged(cli, tmpdir, datafiles, ref_storage, tag_type): |
| 854 |
project = str(datafiles) |
| 855 |
|
| 856 |
project_config = load_yaml(os.path.join(project, "project.conf")) |
| 857 |
project_config["ref-storage"] = ref_storage |
| 858 |
generate_project_with_git(project, config=project_config) |
| 859 |
|
| 860 |
repofiles = os.path.join(str(tmpdir), "repofiles") |
| 861 |
os.makedirs(repofiles, exist_ok=True) |
| 862 |
file0 = os.path.join(repofiles, "file0") |
| 863 |
with open(file0, "w", encoding="utf-8") as f: |
| 864 |
f.write("test\n") |
| 865 |
|
| 866 |
repo = create_repo("git", str(tmpdir)) |
| 867 |
|
| 868 |
def tag(name): |
| 869 |
if tag_type == "annotated": |
| 870 |
repo.add_annotated_tag(name, name) |
| 871 |
else: |
| 872 |
repo.add_tag(name) |
| 873 |
|
| 874 |
repo.create(repofiles) |
| 875 |
tag("uselesstag") |
| 876 |
|
| 877 |
file1 = os.path.join(str(tmpdir), "file1") |
| 878 |
with open(file1, "w", encoding="utf-8") as f: |
| 879 |
f.write("test\n") |
| 880 |
repo.add_file(file1) |
| 881 |
|
| 882 |
file2 = os.path.join(str(tmpdir), "file2") |
| 883 |
with open(file2, "w", encoding="utf-8") as f: |
| 884 |
f.write("test\n") |
| 885 |
repo.branch("branch2") |
| 886 |
repo.add_file(file2) |
| 887 |
|
| 888 |
repo.checkout("master") |
| 889 |
file3 = os.path.join(str(tmpdir), "file3") |
| 890 |
with open(file3, "w", encoding="utf-8") as f: |
| 891 |
f.write("test\n") |
| 892 |
repo.add_file(file3) |
| 893 |
|
| 894 |
tagged_ref = repo.merge("branch2") |
| 895 |
tag("tag") |
| 896 |
|
| 897 |
config = repo.source_config() |
| 898 |
config["track"] = repo.latest_commit() |
| 899 |
config["track-tags"] = True |
| 900 |
|
| 901 |
# Write out our test target |
| 902 |
element = { |
| 903 |
"kind": "import", |
| 904 |
"sources": [config], |
| 905 |
} |
| 906 |
generate_element(project, "target.bst", element) |
| 907 |
element_path = os.path.join(project, "target.bst") |
| 908 |
|
| 909 |
if ref_storage == "inline": |
| 910 |
result = cli.run(project=project, args=["source", "track", "target.bst"]) |
| 911 |
result.assert_success() |
| 912 |
else: |
| 913 |
result = cli.run(project=project, args=["source", "track", "target.bst", "--deps", "all"]) |
| 914 |
result.assert_success() |
| 915 |
|
| 916 |
if ref_storage == "inline": |
| 917 |
element = load_yaml(element_path) |
| 918 |
source = element.get_sequence("sources").mapping_at(0) |
| 919 |
tags = source.get_sequence("tags") |
| 920 |
assert len(tags) == 1 |
| 921 |
|
| 922 |
tag = source.get_sequence("tags").mapping_at(0) |
| 923 |
assert "tag" in tag |
| 924 |
assert "commit" in tag |
| 925 |
assert "annotated" in tag |
| 926 |
assert tag.get_bool("annotated") == (tag_type == "annotated") |
| 927 |
|
| 928 |
tag_name = tag.get_str("tag") |
| 929 |
commit = tag.get_str("commit") |
| 930 |
assert (tag_name, commit) == ("tag", repo.rev_parse("tag^{commit}")) |
| 931 |
|
| 932 |
checkout = os.path.join(str(tmpdir), "checkout") |
| 933 |
|
| 934 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 935 |
result.assert_success() |
| 936 |
result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkout]) |
| 937 |
result.assert_success() |
| 938 |
|
| 939 |
if tag_type == "annotated": |
| 940 |
options = [] |
| 941 |
else: |
| 942 |
options = ["--tags"] |
| 943 |
describe = subprocess.check_output(["git", "describe", *options], cwd=checkout, universal_newlines=True) |
| 944 |
assert describe.startswith("tag") |
| 945 |
|
| 946 |
tags = subprocess.check_output(["git", "tag"], cwd=checkout, universal_newlines=True) |
| 947 |
tags = set(tags.splitlines()) |
| 948 |
assert tags == set(["tag"]) |
| 949 |
|
| 950 |
rev_list = subprocess.check_output(["git", "rev-list", "--all"], cwd=checkout, universal_newlines=True) |
| 951 |
|
| 952 |
assert set(rev_list.splitlines()) == set([tagged_ref]) |
| 953 |
|
| 954 |
with pytest.raises(subprocess.CalledProcessError): |
| 955 |
subprocess.run(["git", "log", repo.rev_parse("uselesstag")], cwd=checkout, check=True) |
| 956 |
|
| 957 |
|
| 958 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 959 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 960 |
def test_git_describe_relevant_history(cli, tmpdir, datafiles): |
| 961 |
project = str(datafiles) |
| 962 |
|
| 963 |
project_config = load_yaml(os.path.join(project, "project.conf")) |
| 964 |
project_config["ref-storage"] = "project.refs" |
| 965 |
generate_project_with_git(project, config=project_config) |
| 966 |
|
| 967 |
repofiles = os.path.join(str(tmpdir), "repofiles") |
| 968 |
os.makedirs(repofiles, exist_ok=True) |
| 969 |
file0 = os.path.join(repofiles, "file0") |
| 970 |
with open(file0, "w", encoding="utf-8") as f: |
| 971 |
f.write("test\n") |
| 972 |
|
| 973 |
repo = create_repo("git", str(tmpdir)) |
| 974 |
repo.create(repofiles) |
| 975 |
|
| 976 |
file1 = os.path.join(str(tmpdir), "file1") |
| 977 |
with open(file1, "w", encoding="utf-8") as f: |
| 978 |
f.write("test\n") |
| 979 |
repo.add_file(file1) |
| 980 |
repo.branch("branch") |
| 981 |
repo.checkout("master") |
| 982 |
|
| 983 |
file2 = os.path.join(str(tmpdir), "file2") |
| 984 |
with open(file2, "w", encoding="utf-8") as f: |
| 985 |
f.write("test\n") |
| 986 |
repo.add_file(file2) |
| 987 |
|
| 988 |
file3 = os.path.join(str(tmpdir), "file3") |
| 989 |
with open(file3, "w", encoding="utf-8") as f: |
| 990 |
f.write("test\n") |
| 991 |
branch_boundary = repo.add_file(file3) |
| 992 |
|
| 993 |
repo.checkout("branch") |
| 994 |
file4 = os.path.join(str(tmpdir), "file4") |
| 995 |
with open(file4, "w", encoding="utf-8") as f: |
| 996 |
f.write("test\n") |
| 997 |
tagged_ref = repo.add_file(file4) |
| 998 |
repo.add_annotated_tag("tag1", "tag1") |
| 999 |
|
| 1000 |
head = repo.merge("master") |
| 1001 |
|
| 1002 |
config = repo.source_config() |
| 1003 |
config["track"] = head |
| 1004 |
config["track-tags"] = True |
| 1005 |
|
| 1006 |
# Write out our test target |
| 1007 |
element = { |
| 1008 |
"kind": "import", |
| 1009 |
"sources": [config], |
| 1010 |
} |
| 1011 |
generate_element(project, "target.bst", element) |
| 1012 |
|
| 1013 |
result = cli.run(project=project, args=["source", "track", "target.bst", "--deps", "all"]) |
| 1014 |
result.assert_success() |
| 1015 |
|
| 1016 |
checkout = os.path.join(str(tmpdir), "checkout") |
| 1017 |
|
| 1018 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 1019 |
result.assert_success() |
| 1020 |
result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkout]) |
| 1021 |
result.assert_success() |
| 1022 |
|
| 1023 |
describe = subprocess.check_output(["git", "describe"], cwd=checkout, universal_newlines=True) |
| 1024 |
assert describe.startswith("tag1-2-") |
| 1025 |
|
| 1026 |
rev_list = subprocess.check_output(["git", "rev-list", "--all"], cwd=checkout, universal_newlines=True) |
| 1027 |
|
| 1028 |
assert set(rev_list.splitlines()) == set([head, tagged_ref, branch_boundary]) |
| 1029 |
|
| 1030 |
|
| 1031 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 1032 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 1033 |
def test_default_do_not_track_tags(cli, tmpdir, datafiles): |
| 1034 |
project = str(datafiles) |
| 1035 |
|
| 1036 |
project_config = load_yaml(os.path.join(project, "project.conf")) |
| 1037 |
project_config["ref-storage"] = "inline" |
| 1038 |
generate_project_with_git(project, config=project_config) |
| 1039 |
|
| 1040 |
repofiles = os.path.join(str(tmpdir), "repofiles") |
| 1041 |
os.makedirs(repofiles, exist_ok=True) |
| 1042 |
file0 = os.path.join(repofiles, "file0") |
| 1043 |
with open(file0, "w", encoding="utf-8") as f: |
| 1044 |
f.write("test\n") |
| 1045 |
|
| 1046 |
repo = create_repo("git", str(tmpdir)) |
| 1047 |
|
| 1048 |
repo.create(repofiles) |
| 1049 |
repo.add_tag("tag") |
| 1050 |
|
| 1051 |
config = repo.source_config() |
| 1052 |
config["track"] = repo.latest_commit() |
| 1053 |
|
| 1054 |
# Write out our test target |
| 1055 |
element = { |
| 1056 |
"kind": "import", |
| 1057 |
"sources": [config], |
| 1058 |
} |
| 1059 |
generate_element(project, "target.bst", element) |
| 1060 |
element_path = os.path.join(project, "target.bst") |
| 1061 |
|
| 1062 |
result = cli.run(project=project, args=["source", "track", "target.bst"]) |
| 1063 |
result.assert_success() |
| 1064 |
|
| 1065 |
element = load_yaml(element_path) |
| 1066 |
source = element.get_sequence("sources").mapping_at(0) |
| 1067 |
assert "tags" not in source |
| 1068 |
|
| 1069 |
|
| 1070 |
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") |
| 1071 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) |
| 1072 |
def test_overwrite_rogue_tag_multiple_remotes(cli, tmpdir, datafiles): |
| 1073 |
"""When using multiple remotes in cache (i.e. when using aliases), we |
| 1074 |
need to make sure we override tags. This is not allowed to fetch |
| 1075 |
tags that were present from different origins |
| 1076 |
""" |
| 1077 |
|
| 1078 |
project = str(datafiles) |
| 1079 |
|
| 1080 |
repofiles = os.path.join(str(tmpdir), "repofiles") |
| 1081 |
os.makedirs(repofiles, exist_ok=True) |
| 1082 |
file0 = os.path.join(repofiles, "file0") |
| 1083 |
with open(file0, "w", encoding="utf-8") as f: |
| 1084 |
f.write("test\n") |
| 1085 |
|
| 1086 |
repo = create_repo("git", str(tmpdir)) |
| 1087 |
|
| 1088 |
top_commit = repo.create(repofiles) |
| 1089 |
|
| 1090 |
repodir, reponame = os.path.split(repo.repo) |
| 1091 |
project_config = load_yaml(os.path.join(project, "project.conf")) |
| 1092 |
project_config["aliases"] = Node.from_dict({"repo": "http://example.com/"}) |
| 1093 |
project_config["mirrors"] = [{"name": "middle-earth", "aliases": {"repo": ["file://{}/".format(repodir)]}}] |
| 1094 |
generate_project_with_git(project, config=project_config) |
| 1095 |
|
| 1096 |
repo.add_annotated_tag("tag", "tag") |
| 1097 |
|
| 1098 |
file1 = os.path.join(repofiles, "file1") |
| 1099 |
with open(file1, "w", encoding="utf-8") as f: |
| 1100 |
f.write("test\n") |
| 1101 |
|
| 1102 |
ref = repo.add_file(file1) |
| 1103 |
|
| 1104 |
config = repo.source_config(ref=ref) |
| 1105 |
del config["track"] |
| 1106 |
config["url"] = "repo:{}".format(reponame) |
| 1107 |
|
| 1108 |
# Write out our test target |
| 1109 |
element = { |
| 1110 |
"kind": "import", |
| 1111 |
"sources": [config], |
| 1112 |
} |
| 1113 |
generate_element(project, "target.bst", element) |
| 1114 |
|
| 1115 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 1116 |
result.assert_success() |
| 1117 |
|
| 1118 |
repo.checkout(top_commit) |
| 1119 |
|
| 1120 |
file2 = os.path.join(repofiles, "file2") |
| 1121 |
with open(file2, "w", encoding="utf-8") as f: |
| 1122 |
f.write("test\n") |
| 1123 |
|
| 1124 |
new_ref = repo.add_file(file2) |
| 1125 |
|
| 1126 |
repo.delete_tag("tag") |
| 1127 |
repo.add_annotated_tag("tag", "tag") |
| 1128 |
repo.checkout("master") |
| 1129 |
|
| 1130 |
otherpath = os.path.join(str(tmpdir), "other_path") |
| 1131 |
shutil.copytree(repo.repo, os.path.join(otherpath, "repo")) |
| 1132 |
create_repo("git", otherpath) |
| 1133 |
|
| 1134 |
repodir, reponame = os.path.split(repo.repo) |
| 1135 |
|
| 1136 |
generate_project_with_git(project, config=project_config) |
| 1137 |
|
| 1138 |
config = repo.source_config(ref=new_ref) |
| 1139 |
del config["track"] |
| 1140 |
config["url"] = "repo:{}".format(reponame) |
| 1141 |
|
| 1142 |
element = { |
| 1143 |
"kind": "import", |
| 1144 |
"sources": [config], |
| 1145 |
} |
| 1146 |
generate_element(project, "target.bst", element) |
| 1147 |
|
| 1148 |
result = cli.run(project=project, args=["build", "target.bst"]) |
| 1149 |
result.assert_success() |
| 1150 |
|
| 1151 |
|
| 1152 |
@pytest.mark.datafiles(os.path.join(DATA_DIR, "source-info")) |
| 1153 |
@pytest.mark.parametrize( |
| 1154 |
"target, expected_kind, expected_url, expected_medium, expected_version_type, expected_version, expected_guess_version, expected_offset", |
| 1155 |
[ |
| 1156 |
( |
| 1157 |
"no-describe.bst", |
| 1158 |
"git", |
| 1159 |
"https://flying-ponies.com/pony.git", |
| 1160 |
"git", |
| 1161 |
"commit", |
| 1162 |
"40af18f96af0c7f1b56f67339ef53e000e738754", |
| 1163 |
None, |
| 1164 |
None, |
| 1165 |
), |
| 1166 |
( |
| 1167 |
"describe.bst", |
| 1168 |
"git", |
| 1169 |
"https://flying-ponies.com/pony.git", |
| 1170 |
"git", |
| 1171 |
"commit", |
| 1172 |
"40af18f96af0c7f1b56f67339ef53e000e738754", |
| 1173 |
"3.4", |
| 1174 |
None, |
| 1175 |
), |
| 1176 |
( |
| 1177 |
"describe-offset.bst", |
| 1178 |
"git", |
| 1179 |
"https://flying-ponies.com/pony.git", |
| 1180 |
"git", |
| 1181 |
"commit", |
| 1182 |
"40af18f96af0c7f1b56f67339ef53e000e738754", |
| 1183 |
"3.4", |
| 1184 |
"7", |
| 1185 |
), |
| 1186 |
( |
| 1187 |
"describe-custom.bst", |
| 1188 |
"git", |
| 1189 |
"https://flying-ponies.com/pony.git", |
| 1190 |
"git", |
| 1191 |
"commit", |
| 1192 |
"40af18f96af0c7f1b56f67339ef53e000e738754", |
| 1193 |
"3.4.6", |
| 1194 |
None, |
| 1195 |
), |
| 1196 |
( |
| 1197 |
"override.bst", |
| 1198 |
"git", |
| 1199 |
"https://flying-ponies.com/pony.git", |
| 1200 |
"git", |
| 1201 |
"commit", |
| 1202 |
"40af18f96af0c7f1b56f67339ef53e000e738754", |
| 1203 |
"5.5", |
| 1204 |
None, |
| 1205 |
), |
| 1206 |
], |
| 1207 |
ids=["no-describe", "describe", "commit-offset", "custom-pattern", "override"], |
| 1208 |
) |
| 1209 |
def test_source_info( |
| 1210 |
cli, |
| 1211 |
datafiles, |
| 1212 |
target, |
| 1213 |
expected_url, |
| 1214 |
expected_kind, |
| 1215 |
expected_medium, |
| 1216 |
expected_version_type, |
| 1217 |
expected_version, |
| 1218 |
expected_guess_version, |
| 1219 |
expected_offset, |
| 1220 |
): |
| 1221 |
project = str(datafiles) |
| 1222 |
result = cli.run(project=project, silent=True, args=["show", "--format", "%{name}:\n%{source-info}", target]) |
| 1223 |
result.assert_success() |
| 1224 |
|
| 1225 |
loaded = _yaml.load_data(result.output) |
| 1226 |
sources = loaded.get_sequence(target) |
| 1227 |
source_info = sources.mapping_at(0) |
| 1228 |
|
| 1229 |
assert source_info.get_str("kind") == expected_kind |
| 1230 |
assert source_info.get_str("url") == expected_url |
| 1231 |
assert source_info.get_str("medium") == expected_medium |
| 1232 |
assert source_info.get_str("version-type") == expected_version_type |
| 1233 |
assert source_info.get_str("version") == expected_version |
| 1234 |
|
| 1235 |
guess_version = source_info.get_str("version-guess", None) |
| 1236 |
if guess_version or expected_guess_version: |
| 1237 |
assert guess_version == expected_guess_version |
| 1238 |
|
| 1239 |
extra_data = source_info.get_mapping("extra-data", None) |
| 1240 |
if extra_data: |
| 1241 |
commit_offset = extra_data.get_str("commit-offset", None) |
| 1242 |
else: |
| 1243 |
commit_offset = None |
| 1244 |
|
| 1245 |
assert commit_offset == expected_offset |