Skip to content

Commit

Permalink
Check to ensure major version matches current year. (#20601)
Browse files Browse the repository at this point in the history
Fixes #20466
  • Loading branch information
karthiknadig authored Jan 31, 2023
1 parent 328e511 commit 184617e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 31 deletions.
84 changes: 53 additions & 31 deletions build/test_update_ext_version.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import datetime
import json

import freezegun
import pytest
import update_ext_version

TEST_DATETIME = "2022-03-14 01:23:45"

CURRENT_YEAR = datetime.datetime.now().year
TEST_DATETIME = f"{CURRENT_YEAR}-03-14 01:23:45"

# The build ID is calculated via:
# "1" + datetime.datetime.strptime(TEST_DATETIME,"%Y-%m-%d %H:%M:%S").strftime('%j%H%M')
Expand All @@ -31,14 +34,21 @@ def run_test(tmp_path, version, args, expected):
@pytest.mark.parametrize(
"version, args",
[
("1.0.0-rc", []),
("1.1.0-rc", ["--release"]),
("1.0.0-rc", ["--release", "--build-id", "-1"]),
("1.0.0-rc", ["--release", "--for-publishing", "--build-id", "-1"]),
("1.0.0-rc", ["--release", "--for-publishing", "--build-id", "999999999999"]),
("1.1.0-rc", ["--build-id", "-1"]),
("1.1.0-rc", ["--for-publishing", "--build-id", "-1"]),
("1.1.0-rc", ["--for-publishing", "--build-id", "999999999999"]),
("2000.1.0", []), # Wrong year for CalVer
(f"{CURRENT_YEAR}.0.0-rc", []),
(f"{CURRENT_YEAR}.1.0-rc", ["--release"]),
(f"{CURRENT_YEAR}.0.0-rc", ["--release", "--build-id", "-1"]),
(
f"{CURRENT_YEAR}.0.0-rc",
["--release", "--for-publishing", "--build-id", "-1"],
),
(
f"{CURRENT_YEAR}.0.0-rc",
["--release", "--for-publishing", "--build-id", "999999999999"],
),
(f"{CURRENT_YEAR}.1.0-rc", ["--build-id", "-1"]),
(f"{CURRENT_YEAR}.1.0-rc", ["--for-publishing", "--build-id", "-1"]),
(f"{CURRENT_YEAR}.1.0-rc", ["--for-publishing", "--build-id", "999999999999"]),
],
)
def test_invalid_args(tmp_path, version, args):
Expand All @@ -49,56 +59,68 @@ def test_invalid_args(tmp_path, version, args):
@pytest.mark.parametrize(
"version, args, expected",
[
("1.1.0-rc", ["--build-id", "12345"], ("1", "1", "12345", "rc")),
("1.0.0-rc", ["--release", "--build-id", "12345"], ("1", "0", "12345", "")),
(
"1.1.0-rc",
f"{CURRENT_YEAR}.1.0-rc",
["--build-id", "12345"],
(f"{CURRENT_YEAR}", "1", "12345", "rc"),
),
(
f"{CURRENT_YEAR}.0.0-rc",
["--release", "--build-id", "12345"],
(f"{CURRENT_YEAR}", "0", "12345", ""),
),
(
f"{CURRENT_YEAR}.1.0-rc",
["--for-publishing", "--build-id", "12345"],
("1", "1", "12345", ""),
(f"{CURRENT_YEAR}", "1", "12345", ""),
),
(
"1.0.0-rc",
f"{CURRENT_YEAR}.0.0-rc",
["--release", "--for-publishing", "--build-id", "12345"],
("1", "0", "12345", ""),
(f"{CURRENT_YEAR}", "0", "12345", ""),
),
(
"1.0.0-rc",
f"{CURRENT_YEAR}.0.0-rc",
["--release", "--build-id", "999999999999"],
("1", "0", "999999999999", ""),
(f"{CURRENT_YEAR}", "0", "999999999999", ""),
),
(
"1.1.0-rc",
f"{CURRENT_YEAR}.1.0-rc",
["--build-id", "999999999999"],
("1", "1", "999999999999", "rc"),
(f"{CURRENT_YEAR}", "1", "999999999999", "rc"),
),
(
f"{CURRENT_YEAR}.1.0-rc",
[],
(f"{CURRENT_YEAR}", "1", EXPECTED_BUILD_ID, "rc"),
),
("1.1.0-rc", [], ("1", "1", EXPECTED_BUILD_ID, "rc")),
(
"1.0.0-rc",
f"{CURRENT_YEAR}.0.0-rc",
["--release"],
("1", "0", "0", ""),
(f"{CURRENT_YEAR}", "0", "0", ""),
),
(
"1.1.0-rc",
f"{CURRENT_YEAR}.1.0-rc",
["--for-publishing"],
("1", "1", EXPECTED_BUILD_ID, ""),
(f"{CURRENT_YEAR}", "1", EXPECTED_BUILD_ID, ""),
),
(
"1.0.0-rc",
f"{CURRENT_YEAR}.0.0-rc",
["--release", "--for-publishing"],
("1", "0", "0", ""),
(f"{CURRENT_YEAR}", "0", "0", ""),
),
(
"1.0.0-rc",
f"{CURRENT_YEAR}.0.0-rc",
["--release"],
("1", "0", "0", ""),
(f"{CURRENT_YEAR}", "0", "0", ""),
),
(
"1.1.0-rc",
f"{CURRENT_YEAR}.1.0-rc",
[],
("1", "1", EXPECTED_BUILD_ID, "rc"),
(f"{CURRENT_YEAR}", "1", EXPECTED_BUILD_ID, "rc"),
),
],
)
@freezegun.freeze_time("2022-03-14 01:23:45")
@freezegun.freeze_time(f"{CURRENT_YEAR}-03-14 01:23:45")
def test_update_ext_version(tmp_path, version, args, expected):
run_test(tmp_path, version, args, expected)
7 changes: 7 additions & 0 deletions build/update_ext_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def main(package_json: pathlib.Path, argv: Sequence[str]) -> None:

major, minor, micro, suffix = parse_version(package["version"])

current_year = datetime.datetime.now().year
if int(major) != current_year:
raise ValueError(
f"Major version [{major}] must be the current year [{current_year}].",
f"If changing major version after new year's, change to {current_year}.1.0",
)

if args.release and not is_even(minor):
raise ValueError(
f"Release version should have EVEN numbered minor version: {package['version']}"
Expand Down

0 comments on commit 184617e

Please sign in to comment.