Skip to content

Commit 885dd63

Browse files
committed
more test coverage for useChartVersion: true
1 parent f8a49e4 commit 885dd63

File tree

1 file changed

+99
-7
lines changed

1 file changed

+99
-7
lines changed

tests/test_repo_interactions.py

Lines changed: 99 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import os
2+
import sys
3+
from contextlib import nullcontext
4+
5+
import pytest
26

37
import chartpress
48
from chartpress import PRERELEASE_PREFIX, yaml
@@ -24,13 +28,27 @@ def test_git_repo_fixture(git_repo):
2428
assert os.path.isfile("index.yaml")
2529

2630

27-
def test_chartpress_run(git_repo, capfd):
31+
@pytest.mark.parametrize("use_chart_version", [False, True])
32+
def test_chartpress_run(git_repo, capfd, use_chart_version):
2833
"""Run chartpress and inspect the output."""
2934

35+
with open("chartpress.yaml") as f:
36+
chartpress_config = yaml.load(f)
37+
for chart in chartpress_config["charts"]:
38+
chart["useChartVersion"] = use_chart_version
39+
with open("chartpress.yaml", "w") as f:
40+
yaml.dump(chartpress_config, f)
41+
3042
# summarize information from git_repo
3143
sha = git_repo.commit("HEAD").hexsha[:7]
3244
tag = f"0.0.1-{PRERELEASE_PREFIX}.1.h{sha}"
3345
check_version(tag)
46+
if use_chart_version:
47+
with open("testchart/Chart.yaml") as f:
48+
chart = yaml.load(f)
49+
chart["version"] = f"0.0.1-{PRERELEASE_PREFIX}.0.habc123"
50+
with open("testchart/Chart.yaml", "w") as f:
51+
yaml.dump(chart, f)
3452

3553
# run chartpress
3654
out = _capture_output([], capfd)
@@ -53,20 +71,23 @@ def test_chartpress_run(git_repo, capfd):
5371
)
5472

5573
# verify usage of chartpress.yaml's resetVersion and resetTag
74+
reset_version = "0.0.1-0.dev" if use_chart_version else "0.0.1-test.reset.version"
75+
reset_tag = reset_version if use_chart_version else "test-reset-tag"
5676
out = _capture_output(["--reset"], capfd)
57-
assert "Updating testchart/Chart.yaml: version: 0.0.1-test.reset.version" in out
77+
assert f"Updating testchart/Chart.yaml: version: {reset_version}" in out
5878
assert (
59-
"Updating testchart/values.yaml: image: testchart/testimage:test-reset-tag"
60-
in out
79+
f"Updating testchart/values.yaml: image: testchart/testimage:{reset_tag}" in out
6180
)
6281

6382
# --tag overrides resetVersion config
6483
out = _capture_output(["--reset", "--tag=1.0.0-dev"], capfd)
6584
assert "Updating testchart/Chart.yaml: version: 1.0.0-dev" in out
85+
assert "Updating testchart/values.yaml: image: testchart/testimage:1.0.0-dev" in out
6686

67-
# verify that we don't need to rebuild the image
68-
out = _capture_output([], capfd)
69-
assert "Skipping build" in out
87+
if not use_chart_version:
88+
# verify that we don't need to rebuild the image
89+
out = _capture_output([], capfd)
90+
assert "Skipping build" in out
7091

7192
# verify usage of --force-build
7293
out = _capture_output(["--force-build"], capfd)
@@ -335,3 +356,74 @@ def test_backport_branch(git_repo_backport_branch, capfd):
335356
tag = f"1.0.1-{PRERELEASE_PREFIX}.1.h{sha}"
336357
assert chart["version"] == tag
337358
check_version(tag)
359+
360+
361+
@pytest.mark.parametrize(
362+
"use_chart_version, chart_version, tag, expected_version",
363+
[
364+
(False, "0.0.1", "1.0.0", "1.0.0"),
365+
(False, "1.0.0", None, "0.0.1-test.reset.version"),
366+
(True, "0.0.1", "1.0.0", "1.0.0"),
367+
(
368+
True,
369+
"0.0.1",
370+
None,
371+
ValueError("--reset after a release must also specify --tag"),
372+
),
373+
(
374+
True,
375+
"0.0.1-test.reset.version",
376+
None,
377+
ValueError("has placeholder version 0.0.1-test.reset.version"),
378+
),
379+
],
380+
)
381+
def test_reset(
382+
git_repo, capfd, use_chart_version, chart_version, tag, expected_version
383+
):
384+
chartpress_yaml = "chartpress.yaml"
385+
chart_yaml = "testchart/Chart.yaml"
386+
values_yaml = "testchart/values.yaml"
387+
388+
with open(chartpress_yaml) as f:
389+
cfg = yaml.load(f)
390+
chart_cfg = cfg["charts"][0]
391+
chart_cfg["useChartVersion"] = use_chart_version
392+
with open(chartpress_yaml, "w") as f:
393+
yaml.dump(cfg, f)
394+
395+
with open(chart_yaml) as f:
396+
chart = yaml.load(f)
397+
chart["version"] = chart_version
398+
with open(chart_yaml, "w") as f:
399+
yaml.dump(chart, f)
400+
401+
args = ["--reset"]
402+
if tag:
403+
args.append(f"--tag={tag}")
404+
if isinstance(expected_version, Exception):
405+
context = pytest.raises(expected_version.__class__, match=str(expected_version))
406+
expect_failure = True
407+
else:
408+
context = nullcontext()
409+
expect_failure = False
410+
411+
with context:
412+
out = _capture_output(args, capfd)
413+
sys.stdout.write(out)
414+
if expect_failure:
415+
return
416+
417+
with open(chart_yaml) as f:
418+
chart = yaml.load(f)
419+
assert chart["version"] == expected_version
420+
421+
with open(values_yaml) as f:
422+
values = yaml.load(f)
423+
424+
if tag or use_chart_version:
425+
expected_tag = expected_version
426+
else:
427+
expected_tag = chart_cfg["resetTag"]
428+
429+
assert values["image"] == f"testchart/testimage:{expected_tag}"

0 commit comments

Comments
 (0)