|
2 | 2 | #
|
3 | 3 | # SPDX-License-Identifier: MIT
|
4 | 4 | import pytest
|
| 5 | +import json |
5 | 6 |
|
6 | 7 | from hatch_nodejs_version.version_source import NodeJSVersionSource
|
7 | 8 |
|
8 |
| - |
9 | 9 | GOOD_NODE_PYTHON_VERSIONS = [
|
10 | 10 | ("1.4.5", "1.4.5"),
|
11 | 11 | ("1.4.5-a0", "1.4.5a0"),
|
|
33 | 33 | ]
|
34 | 34 |
|
35 | 35 |
|
36 |
| -class TestDefault: |
| 36 | +class TestVersion: |
37 | 37 | @pytest.mark.parametrize(
|
38 |
| - "new_project, python_version", |
| 38 | + "node_version, python_version", |
39 | 39 | GOOD_NODE_PYTHON_VERSIONS,
|
40 |
| - indirect=["new_project"], |
41 | 40 | )
|
42 |
| - @pytest.mark.parametrize("config", [{"path": "other-package.json"}, {}]) |
43 |
| - def test_read_correct(self, new_project, python_version, config): |
44 |
| - version_source = NodeJSVersionSource(new_project, config) |
45 |
| - data = version_source.get_version_data() |
46 |
| - assert data["version"] == python_version |
| 41 | + def test_parse_correct(self, node_version, python_version): |
| 42 | + node_version_parsed = NodeJSVersionSource.python_version_to_node(python_version) |
| 43 | + assert node_version_parsed == node_version |
47 | 44 |
|
48 |
| - @pytest.mark.parametrize("python_version,node_version", GOOD_NODE_PYTHON_VERSIONS) |
49 |
| - @pytest.mark.parametrize("config", [{"path": "other-package.json"}, {}]) |
50 |
| - @pytest.mark.parametrize("new_project", ["1.0.0"], indirect=True) |
51 |
| - def test_write_correct(self, new_project, python_version, node_version, config): |
52 |
| - version_source = NodeJSVersionSource(new_project, config) |
53 |
| - data = version_source.get_version_data() |
54 |
| - version_source.set_version(python_version, data) |
55 |
| - data = version_source.get_version_data() |
56 |
| - assert data["version"] == node_version |
| 45 | + @pytest.mark.parametrize( |
| 46 | + "python_version", |
| 47 | + BAD_PYTHON_VERSIONS, |
| 48 | + ) |
| 49 | + def test_parse_python_incorrect(self, python_version): |
| 50 | + with pytest.raises(ValueError, match=".* did not match regex"): |
| 51 | + NodeJSVersionSource.python_version_to_node(python_version) |
57 | 52 |
|
58 | 53 | @pytest.mark.parametrize(
|
59 |
| - "new_project", |
| 54 | + "node_version", |
60 | 55 | BAD_NODE_VERSIONS,
|
61 |
| - indirect=["new_project"], |
62 | 56 | )
|
63 |
| - @pytest.mark.parametrize("config", [{"path": "other-package.json"}, {}]) |
64 |
| - def test_read_incorrect(self, new_project, config): |
65 |
| - version_source = NodeJSVersionSource(new_project, config) |
66 |
| - |
| 57 | + def test_parse_python_incorrect(self, node_version): |
67 | 58 | with pytest.raises(ValueError, match=".* did not match regex"):
|
68 |
| - version_source.get_version_data() |
| 59 | + NodeJSVersionSource.node_version_to_python(node_version) |
69 | 60 |
|
70 |
| - @pytest.mark.parametrize("python_version,", BAD_PYTHON_VERSIONS) |
71 |
| - @pytest.mark.parametrize("new_project", ["1.0.0"], indirect=True) |
72 |
| - @pytest.mark.parametrize("config", [{"path": "other-package.json"}, {}]) |
73 |
| - def test_write_incorrect(self, new_project, python_version, config): |
74 |
| - version_source = NodeJSVersionSource(new_project, config) |
| 61 | + @pytest.mark.parametrize( |
| 62 | + "node_version, python_version", |
| 63 | + GOOD_NODE_PYTHON_VERSIONS, |
| 64 | + ) |
| 65 | + @pytest.mark.parametrize( |
| 66 | + "alt_package_json", |
| 67 | + [None, "package-other.json"], |
| 68 | + ) |
| 69 | + def test_version_from_package( |
| 70 | + self, project, node_version, python_version, alt_package_json |
| 71 | + ): |
| 72 | + # Create a simple project |
| 73 | + (project / "pyproject.toml").write_text( |
| 74 | + """ |
| 75 | +[build - system] |
| 76 | +requires = ["hatchling", "hatch-vcs"] |
| 77 | +build - backend = "hatchling.build" |
| 78 | +[project] |
| 79 | +name = "my-app" |
| 80 | +dynamic = ["version"] |
| 81 | +[tool.hatch.version] |
| 82 | +source = "nodejs" |
| 83 | + """ |
| 84 | + ) |
| 85 | + package_json = "package.json" if alt_package_json is None else alt_package_json |
| 86 | + (project / package_json).write_text( |
| 87 | + f""" |
| 88 | +{{ |
| 89 | + "name": "my-app", |
| 90 | + "version": "{node_version}" |
| 91 | +}} |
| 92 | +""" |
| 93 | + ) |
| 94 | + config = {} if alt_package_json is None else {"path": alt_package_json} |
| 95 | + version_source = NodeJSVersionSource(project, config=config) |
75 | 96 | data = version_source.get_version_data()
|
76 |
| - with pytest.raises(ValueError, match=".* did not match regex"): |
77 |
| - version_source.set_version(python_version, data) |
| 97 | + assert data['version'] == python_version |
| 98 | + |
| 99 | + @pytest.mark.parametrize( |
| 100 | + "node_version, python_version", |
| 101 | + GOOD_NODE_PYTHON_VERSIONS, |
| 102 | + ) |
| 103 | + @pytest.mark.parametrize( |
| 104 | + "alt_package_json", |
| 105 | + [None, "package-other.json"], |
| 106 | + ) |
| 107 | + def test_version_to_package( |
| 108 | + self, project, node_version, python_version, alt_package_json |
| 109 | + ): |
| 110 | + package_json = "package.json" if alt_package_json is None else alt_package_json |
| 111 | + (project / "pyproject.toml").write_text( |
| 112 | + """ |
| 113 | +[build - system] |
| 114 | +requires = ["hatchling", "hatch-vcs"] |
| 115 | +build - backend = "hatchling.build" |
| 116 | +[project] |
| 117 | +name = "my-app" |
| 118 | +dynamic = ["version"] |
| 119 | +[tool.hatch.version] |
| 120 | +source = "nodejs" |
| 121 | + """ |
| 122 | + ) |
| 123 | + (project / package_json).write_text( |
| 124 | + f""" |
| 125 | +{{ |
| 126 | + "name": "my-app", |
| 127 | + "version": "0.0.0" |
| 128 | +}} |
| 129 | +""" |
| 130 | + ) |
| 131 | + config = {} if alt_package_json is None else {"path": alt_package_json} |
| 132 | + version_source = NodeJSVersionSource(project, config=config) |
| 133 | + version_data = version_source.get_version_data() |
| 134 | + version_source.set_version(python_version, version_data) |
| 135 | + |
| 136 | + written_package = json.loads((project / package_json).read_text()) |
| 137 | + assert written_package['version'] == node_version |
| 138 | + |
0 commit comments