|
| 1 | +import sys |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import gardenlinux.github.__main__ as gh |
| 6 | +from gardenlinux.constants import GARDENLINUX_GITHUB_RELEASE_BUCKET_NAME |
| 7 | + |
| 8 | +from ..constants import TEST_GARDENLINUX_COMMIT, TEST_GARDENLINUX_RELEASE |
| 9 | + |
| 10 | + |
| 11 | +def test_script_parse_args_wrong_command(monkeypatch, capfd): |
| 12 | + monkeypatch.setattr(sys, "argv", ["gh", "rejoice"]) |
| 13 | + |
| 14 | + with pytest.raises(SystemExit): |
| 15 | + gh.main() |
| 16 | + captured = capfd.readouterr() |
| 17 | + |
| 18 | + assert "argument command: invalid choice: 'rejoice'" in captured.err, "Expected help message printed" |
| 19 | + |
| 20 | + |
| 21 | +def test_script_parse_args_create_command_required_args(monkeypatch, capfd): |
| 22 | + monkeypatch.setattr(sys, "argv", ["gh", "create", "--owner", "gardenlinux", "--repo", "gardenlinux"]) |
| 23 | + |
| 24 | + with pytest.raises(SystemExit): |
| 25 | + gh.main() |
| 26 | + captured = capfd.readouterr() |
| 27 | + |
| 28 | + assert "the following arguments are required: --tag, --commit" in captured.err, \ |
| 29 | + "Expected help message on missing arguments for 'create' command" |
| 30 | + |
| 31 | + |
| 32 | +def test_script_parse_args_upload_command_required_args(monkeypatch, capfd): |
| 33 | + monkeypatch.setattr(sys, "argv", ["gh", "upload", "--owner", "gardenlinux", "--repo", "gardenlinux"]) |
| 34 | + |
| 35 | + with pytest.raises(SystemExit): |
| 36 | + gh.main() |
| 37 | + captured = capfd.readouterr() |
| 38 | + |
| 39 | + assert "the following arguments are required: --release_id, --file_path" in captured.err, \ |
| 40 | + "Expected help message on missing arguments for 'upload' command" |
| 41 | + |
| 42 | + |
| 43 | +def test_script_create_dry_run(monkeypatch, capfd): |
| 44 | + |
| 45 | + monkeypatch.setattr(sys, "argv", ["gh", "create", "--owner", "gardenlinux", "--repo", |
| 46 | + "gardenlinux", "--tag", TEST_GARDENLINUX_RELEASE, "--commit", TEST_GARDENLINUX_COMMIT, "--dry-run"]) |
| 47 | + monkeypatch.setattr("gardenlinux.github.__main__.create_github_release_notes", |
| 48 | + lambda tag, commit, bucket: f"{tag} {commit} {bucket}") |
| 49 | + |
| 50 | + gh.main() |
| 51 | + captured = capfd.readouterr() |
| 52 | + |
| 53 | + assert captured.out == f"Dry Run ...\nThis release would be created:\n{TEST_GARDENLINUX_RELEASE} {TEST_GARDENLINUX_COMMIT} {GARDENLINUX_GITHUB_RELEASE_BUCKET_NAME}\n", \ |
| 54 | + "Expected dry-run create to return generated release notes text" |
| 55 | + |
| 56 | + |
| 57 | +def test_script_create(monkeypatch, caplog): |
| 58 | + monkeypatch.setattr(sys, "argv", ["gh", "create", "--owner", "gardenlinux", "--repo", |
| 59 | + "gardenlinux", "--tag", TEST_GARDENLINUX_RELEASE, "--commit", TEST_GARDENLINUX_COMMIT]) |
| 60 | + monkeypatch.setattr("gardenlinux.github.__main__.create_github_release_notes", |
| 61 | + lambda tag, commit, bucket: f"{tag} {commit} {bucket}") |
| 62 | + monkeypatch.setattr("gardenlinux.github.__main__.create_github_release", |
| 63 | + lambda a1, a2, a3, a4, a5, a6: TEST_GARDENLINUX_RELEASE) |
| 64 | + |
| 65 | + gh.main() |
| 66 | + |
| 67 | + assert any(f"Release created with ID: {TEST_GARDENLINUX_RELEASE}" in record.message for record in caplog.records), \ |
| 68 | + "Expected a release creation confirmation log entry" |
| 69 | + |
| 70 | + |
| 71 | +def test_script_upload_dry_run(monkeypatch, capfd): |
| 72 | + monkeypatch.setattr(sys, "argv", ["gh", "upload", "--owner", "gardenlinux", "--repo", |
| 73 | + "gardenlinux", "--release_id", TEST_GARDENLINUX_RELEASE, "--file_path", "foo", "--dry-run"]) |
| 74 | + monkeypatch.setattr("gardenlinux.github.__main__.upload_to_github_release_page", |
| 75 | + lambda a1, a2, a3, a4, dry_run: print(f"dry-run: {dry_run}")) |
| 76 | + |
| 77 | + gh.main() |
| 78 | + captured = capfd.readouterr() |
| 79 | + |
| 80 | + assert captured.out == "dry-run: True\n" |
0 commit comments