1#!/usr/bin/env python3
2#
3# Copyright (c) 2021 Nordic Semiconductor ASA
4#
5# SPDX-License-Identifier: Apache-2.0
6
7from sys import version
8from unittest import TestCase, main
9from pathlib import Path
10from subprocess import Popen, PIPE
11from datetime import date
12
13p_script_dir = Path(__file__).absolute().parents[0]
14p_root = p_script_dir.parents[1]
15p_VERSION = p_root / "zcbor" / "VERSION"
16p_release_notes = p_root / "RELEASE_NOTES.md"
17p_HEAD_REF = p_script_dir / "HEAD_REF"
18
19
20class VersionTest(TestCase):
21    def test_version_num(self):
22        """For release branches - Test that all version numbers have been updated."""
23        current_branch = Popen(['git', 'branch', '--show-current'],
24                               stdout=PIPE).communicate()[0].decode("utf-8").strip()
25        if not current_branch:
26            current_branch = p_HEAD_REF.read_text(encoding="utf-8")
27        self.assertRegex(
28            current_branch, r"release/\d+\.\d+\.\d+",
29            "This test is meant to be run on a release branch on the form 'release/x.y.z'.")
30
31        version_number = current_branch.replace("release/", "")
32        self.assertRegex(
33            version_number, r'\d+\.\d+\.(?!99)\d+',
34            "Releases cannot have the x.y.99 development bugfix release number.")
35        self.assertEqual(
36            version_number, p_VERSION.read_text(encoding="utf-8"),
37            f"{p_VERSION} has not been updated to the correct version number.")
38        self.assertEqual(
39            p_release_notes.read_text(encoding="utf-8").splitlines()[0],
40            r"# zcbor v. " + version_number + f" ({date.today():%Y-%m-%d})",
41            f"{p_release_notes} has not been updated with the correct version number.")
42
43        tags_stdout, _ = Popen(['git', 'tag'], stdout=PIPE).communicate()
44        tags = tags_stdout.decode("utf-8").strip().splitlines()
45        self.assertNotIn(
46            version_number, tags,
47            "Version number already exists as a tag. Has the version number been updated?")
48
49
50if __name__ == "__main__":
51    main()
52