|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2023 Greptime Team |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import importlib.util |
| 17 | +import subprocess |
| 18 | +import sys |
| 19 | +import unittest |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | + |
| 23 | +def load_module(): |
| 24 | + path = Path(__file__).resolve().parent / "previous-release-tag.py" |
| 25 | + spec = importlib.util.spec_from_file_location("previous_release_tag", path) |
| 26 | + assert spec is not None and spec.loader is not None |
| 27 | + module = importlib.util.module_from_spec(spec) |
| 28 | + spec.loader.exec_module(module) |
| 29 | + return module |
| 30 | + |
| 31 | + |
| 32 | +TAGS = [ |
| 33 | + "v1.1.0", |
| 34 | + "v1.2.0", |
| 35 | + "v1.2.1", |
| 36 | + "v1.2.1^{}", |
| 37 | + "v1.3.0-alpha.1", |
| 38 | + "v1.3.0-beta.1", |
| 39 | + "v1.3.0-beta.2", |
| 40 | + "v1.2.0-nightly-20260706", |
| 41 | +] |
| 42 | + |
| 43 | + |
| 44 | +class PreviousReleaseTagTest(unittest.TestCase): |
| 45 | + @classmethod |
| 46 | + def setUpClass(cls) -> None: |
| 47 | + cls.mod = load_module() |
| 48 | + |
| 49 | + def test_beta_uses_latest_older_stable_not_an_ancestor(self) -> None: |
| 50 | + self.assertEqual(self.mod.previous_stable_tag("v1.3.0-beta.2", TAGS), "v1.2.1") |
| 51 | + |
| 52 | + def test_stable_release_skips_itself(self) -> None: |
| 53 | + self.assertEqual(self.mod.previous_stable_tag("v1.2.1", TAGS), "v1.2.0") |
| 54 | + |
| 55 | + def test_same_line_stable_is_not_baseline_for_its_prerelease(self) -> None: |
| 56 | + tags = TAGS + ["v1.3.0"] |
| 57 | + self.assertEqual(self.mod.previous_stable_tag("v1.3.0-beta.2", tags), "v1.2.1") |
| 58 | + self.assertEqual(self.mod.previous_stable_tag("v1.3.0", tags), "v1.2.1") |
| 59 | + |
| 60 | + def test_nightly_of_unreleased_version_uses_previous_stable(self) -> None: |
| 61 | + self.assertEqual( |
| 62 | + self.mod.previous_stable_tag("v1.3.0-nightly-20261013", TAGS), |
| 63 | + "v1.2.1", |
| 64 | + ) |
| 65 | + |
| 66 | + def test_no_older_stable_tag(self) -> None: |
| 67 | + self.assertIsNone(self.mod.previous_stable_tag("v0.1.0", ["v0.1.0", "v1.2.1"])) |
| 68 | + |
| 69 | + def test_invalid_version(self) -> None: |
| 70 | + with self.assertRaises(ValueError): |
| 71 | + self.mod.previous_stable_tag("nightly-20261013", TAGS) |
| 72 | + |
| 73 | + def test_unordered_tags_use_numeric_order(self) -> None: |
| 74 | + forward = ["v1.2.1", "v1.9.0", "v1.10.0", "v0.10.0", "v1.9.9"] |
| 75 | + backward = list(reversed(forward)) |
| 76 | + for tags in (forward, backward): |
| 77 | + self.assertEqual(self.mod.previous_stable_tag("v1.10.0", tags), "v1.9.9") |
| 78 | + self.assertEqual(self.mod.previous_stable_tag("v1.10.0-rc.1", tags), "v1.9.9") |
| 79 | + |
| 80 | + def test_multi_digit_components_compare_as_integers(self) -> None: |
| 81 | + tags = ["v1.9.0", "v1.10.0", "v0.9.10", "v0.10.0", "v2.9.15", "v2.10.0", "v10.2.0"] |
| 82 | + self.assertEqual(self.mod.previous_stable_tag("v1.10.0", tags), "v1.9.0") |
| 83 | + self.assertEqual(self.mod.previous_stable_tag("v0.10.0", tags), "v0.9.10") |
| 84 | + self.assertEqual(self.mod.previous_stable_tag("v2.10.0", tags), "v2.9.15") |
| 85 | + self.assertEqual(self.mod.previous_stable_tag("v10.2.0", ["v10.0.9", "v9.20.0"]), "v10.0.9") |
| 86 | + |
| 87 | + |
| 88 | +class PreviousReleaseTagCliTest(unittest.TestCase): |
| 89 | + SCRIPT = Path(__file__).resolve().parent / "previous-release-tag.py" |
| 90 | + |
| 91 | + def run_cli( |
| 92 | + self, version: str, tags: list[str] | None = None, stdin: str | None = None |
| 93 | + ) -> subprocess.CompletedProcess[str]: |
| 94 | + command = [sys.executable, str(self.SCRIPT), "--version", version] |
| 95 | + if tags is not None: |
| 96 | + command.extend(tags) |
| 97 | + return subprocess.run( |
| 98 | + command, |
| 99 | + input=stdin, |
| 100 | + text=True, |
| 101 | + capture_output=True, |
| 102 | + check=False, |
| 103 | + ) |
| 104 | + |
| 105 | + def test_stdin_selects_baseline_and_exits_zero(self) -> None: |
| 106 | + completed = self.run_cli( |
| 107 | + "v1.10.0", |
| 108 | + stdin="v1.2.1\nv1.9.0\n\nv1.10.0\nv1.9.0^{}\n", |
| 109 | + ) |
| 110 | + self.assertEqual(completed.returncode, 0) |
| 111 | + self.assertEqual(completed.stdout, "v1.9.0\n") |
| 112 | + self.assertEqual(completed.stderr, "") |
| 113 | + |
| 114 | + def test_positional_tags_ignore_stdin(self) -> None: |
| 115 | + completed = self.run_cli("v1.10.0", tags=["v1.9.0"], stdin="v1.9.1\n") |
| 116 | + self.assertEqual(completed.returncode, 0) |
| 117 | + self.assertEqual(completed.stdout, "v1.9.0\n") |
| 118 | + |
| 119 | + def test_no_older_tag_exits_zero_with_empty_stdout(self) -> None: |
| 120 | + completed = self.run_cli("v0.1.0", stdin="v0.1.0\nv1.2.1\n") |
| 121 | + self.assertEqual(completed.returncode, 0) |
| 122 | + self.assertEqual(completed.stdout, "") |
| 123 | + self.assertEqual(completed.stderr, "") |
| 124 | + |
| 125 | + def test_invalid_version_exits_one(self) -> None: |
| 126 | + completed = self.run_cli("nightly", stdin="v1.2.1\n") |
| 127 | + self.assertEqual(completed.returncode, 1) |
| 128 | + self.assertEqual(completed.stdout, "") |
| 129 | + self.assertIn("not a release version: nightly", completed.stderr) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + unittest.main() |
0 commit comments