1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
# SPDX-FileCopyrightText: 2022-present Angus Hollands <goosey15@gmail.com>
#
# SPDX-License-Identifier: MIT
import json
import pytest
from hatch_nodejs_version.version_source import NodeJSVersionSource
GOOD_NODE_PYTHON_VERSIONS = [
("1.4.5", "1.4.5"),
("1.4.5-a0", "1.4.5a0"),
("1.4.5-a", "1.4.5a"),
("1.4.5-b0", "1.4.5b0"),
("1.4.5-c1", "1.4.5c1"),
("1.4.5-rc0", "1.4.5rc0"),
("1.4.5-dev0", "1.4.5dev0"),
("1.4.5-a1.dev0", "1.4.5a1dev0"),
("1.4.5-alpha0", "1.4.5alpha0"),
("1.4.5-beta0", "1.4.5beta0"),
("1.4.5-pre9", "1.4.5pre9"),
("1.4.5-preview0", "1.4.5preview0"),
("1.4.5-preview0+build1.0.0", "1.4.5preview0+build1.0.0"),
("1.4.5-preview0+build-1.0.0", "1.4.5preview0+build-1.0.0"),
("1.4.5-preview0+good-1_0.0", "1.4.5preview0+good-1_0.0"),
]
class TestVersion:
@pytest.mark.parametrize(
"python_version",
[
"1.4",
"1.4.5ab",
"1.4.5-c0.smoke2",
"1.4.5rc.post1@dev2",
"1.4.5rc0.post1+-bad",
"1.4.5rc0.post1+bad_",
],
)
def test_parse_python_incorrect(self, python_version):
with pytest.raises(ValueError, match=".* did not match regex"):
NodeJSVersionSource.python_version_to_node(python_version)
@pytest.mark.parametrize(
"node_version",
[
"1.4",
"1.4.5a0",
"1.4.5-c0.post1",
"1.4.5-rc0.post1.dev2",
"1.4.5-rc0.post1+-bad",
"1.4.5-rc0.post1+bad_",
],
)
def test_parse_node_incorrect(self, node_version):
with pytest.raises(ValueError, match=".* did not match regex"):
NodeJSVersionSource.node_version_to_python(node_version)
@pytest.mark.parametrize(
"node_version, python_version",
GOOD_NODE_PYTHON_VERSIONS,
)
@pytest.mark.parametrize(
"alt_package_json",
[None, "package-other.json"],
)
def test_version_from_package(
self, project, node_version, python_version, alt_package_json
):
# Create a simple project
(project / "pyproject.toml").write_text(
"""
[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"
[project]
name = "my-app"
dynamic = ["version"]
[tool.hatch.version]
source = "nodejs"
"""
)
package_json = "package.json" if alt_package_json is None else alt_package_json
(project / package_json).write_text(
f"""
{{
"name": "my-app",
"version": "{node_version}"
}}
"""
)
config = {} if alt_package_json is None else {"path": alt_package_json}
version_source = NodeJSVersionSource(project, config=config)
data = version_source.get_version_data()
assert data["version"] == python_version
@pytest.mark.parametrize(
"node_version, python_version",
GOOD_NODE_PYTHON_VERSIONS,
)
@pytest.mark.parametrize(
"alt_package_json",
[None, "package-other.json"],
)
def test_version_to_package(
self, project, node_version, python_version, alt_package_json
):
package_json = "package.json" if alt_package_json is None else alt_package_json
(project / "pyproject.toml").write_text(
"""
[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"
[project]
name = "my-app"
dynamic = ["version"]
[tool.hatch.version]
source = "nodejs"
"""
)
(project / package_json).write_text(
"""
{
"name": "my-app",
"version": "0.0.0"
}
"""
)
config = {} if alt_package_json is None else {"path": alt_package_json}
version_source = NodeJSVersionSource(project, config=config)
version_data = version_source.get_version_data()
version_source.set_version(python_version, version_data)
written_package = json.loads((project / package_json).read_text())
assert written_package["version"] == node_version
|