|
| 1 | +# SPDX-FileCopyrightText: 2022-present Angus Hollands <[email protected]> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +import pytest |
| 5 | + |
| 6 | +from hatch_nodejs_version.version_source import NodeJSVersionSource |
| 7 | + |
| 8 | + |
| 9 | +GOOD_NODE_PYTHON_VERSIONS = [ |
| 10 | + ("1.4.5", "1.4.5"), |
| 11 | + ("1.4.5-a0", "1.4.5a0"), |
| 12 | + ("1.4.5-a", "1.4.5a"), |
| 13 | + ("1.4.5-b0", "1.4.5b0"), |
| 14 | + ("1.4.5-c1", "1.4.5c1"), |
| 15 | + ("1.4.5-rc0", "1.4.5rc0"), |
| 16 | + ("1.4.5-alpha0", "1.4.5alpha0"), |
| 17 | + ("1.4.5-beta0", "1.4.5beta0"), |
| 18 | + ("1.4.5-pre9", "1.4.5pre9"), |
| 19 | + ("1.4.5-preview0", "1.4.5preview0"), |
| 20 | +] |
| 21 | + |
| 22 | +BAD_NODE_VERSIONS = [ |
| 23 | + "1.4", |
| 24 | + "1.4.5a0", |
| 25 | + "1.4.5-c0.post1", |
| 26 | + "1.4.5-rc0.post1.dev2", |
| 27 | +] |
| 28 | +BAD_PYTHON_VERSIONS = [ |
| 29 | + "1.4", |
| 30 | + "1.4.5ab", |
| 31 | + "1.4.5-c0.smoke2", |
| 32 | + "1.4.5rc.post1@dev2", |
| 33 | +] |
| 34 | + |
| 35 | + |
| 36 | +class TestDefault: |
| 37 | + @pytest.mark.parametrize( |
| 38 | + "new_project, python_version", GOOD_NODE_PYTHON_VERSIONS, indirect=["new_project"] |
| 39 | + ) |
| 40 | + @pytest.mark.parametrize("config", [{"path": "other-package.json"}, {}]) |
| 41 | + def test_read_correct(self, new_project, python_version, config): |
| 42 | + version_source = NodeJSVersionSource(new_project, config) |
| 43 | + data = version_source.get_version_data() |
| 44 | + assert data["version"] == python_version |
| 45 | + |
| 46 | + @pytest.mark.parametrize("python_version,node_version", GOOD_NODE_PYTHON_VERSIONS) |
| 47 | + @pytest.mark.parametrize("config", [{"path": "other-package.json"}, {}]) |
| 48 | + @pytest.mark.parametrize("new_project", ["1.0.0"], indirect=True) |
| 49 | + def test_write_correct(self, new_project, python_version, node_version, config): |
| 50 | + version_source = NodeJSVersionSource(new_project, config) |
| 51 | + data = version_source.get_version_data() |
| 52 | + version_source.set_version(python_version, data) |
| 53 | + data = version_source.get_version_data() |
| 54 | + assert data["version"] == node_version |
| 55 | + |
| 56 | + @pytest.mark.parametrize( |
| 57 | + "new_project", |
| 58 | + BAD_NODE_VERSIONS, |
| 59 | + indirect=["new_project"], |
| 60 | + ) |
| 61 | + @pytest.mark.parametrize("config", [{"path": "other-package.json"}, {}]) |
| 62 | + def test_read_incorrect(self, new_project, config): |
| 63 | + version_source = NodeJSVersionSource(new_project, config) |
| 64 | + |
| 65 | + with pytest.raises(ValueError, match=".* did not match regex"): |
| 66 | + version_source.get_version_data() |
| 67 | + |
| 68 | + @pytest.mark.parametrize("python_version,", BAD_PYTHON_VERSIONS) |
| 69 | + @pytest.mark.parametrize("new_project", ["1.0.0"], indirect=True) |
| 70 | + @pytest.mark.parametrize("config", [{"path": "other-package.json"}, {}]) |
| 71 | + def test_write_incorrect(self, new_project, python_version, config): |
| 72 | + version_source = NodeJSVersionSource(new_project, config) |
| 73 | + data = version_source.get_version_data() |
| 74 | + with pytest.raises(ValueError, match=".* did not match regex"): |
| 75 | + version_source.set_version(python_version, data) |
0 commit comments