From 520c0be4d2eaca8f6ea43355a58ecbfb50b8844b Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Fri, 10 Jun 2022 21:58:21 +0100 Subject: [PATCH] WIP: add tests --- tests/__init__.py | 1 + tests/conftest.py | 95 ++++++++++++++++++++++++++++++++++++ tests/test_version_config.py | 75 ++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+) diff --git a/tests/__init__.py b/tests/__init__.py index 8cae8f6..9587a1d 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,3 +1,4 @@ # SPDX-FileCopyrightText: 2022-present Angus Hollands # # SPDX-License-Identifier: MIT +import pytest diff --git a/tests/conftest.py b/tests/conftest.py index e69de29..b22e258 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -0,0 +1,95 @@ +# SPDX-FileCopyrightText: 2022-present Angus Hollands +# SPDX-FileCopyrightText: 2022-present Ofek Lev +# +# SPDX-License-Identifier: MIT +import errno +import shutil +import stat +import tempfile +from contextlib import contextmanager +import os +import pytest + + +def touch_file(path): + with open(path, "a"): + os.utime(path, None) + + +def create_file(path, contents): + with open(path, "w") as f: + f.write(contents) + + +def handle_remove_readonly(func, path, exc): # no cov + # PermissionError: [WinError 5] Access is denied: '...\\.git\\...' + if func in (os.rmdir, os.remove, os.unlink) and exc[1].errno == errno.EACCES: + os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) + func(path) + else: + raise + + +@pytest.fixture +def temp_dir(): + directory = tempfile.mkdtemp() + try: + directory = os.path.realpath(directory) + yield directory + finally: + shutil.rmtree(directory, ignore_errors=False, onerror=handle_remove_readonly) + + +@contextmanager +def create_project(directory, metadata, version): + project_dir = os.path.join(directory, "my-app") + os.mkdir(project_dir) + + project_file = os.path.join(project_dir, "pyproject.toml") + create_file(project_file, metadata) + + package_dir = os.path.join(project_dir, "my_app") + os.mkdir(package_dir) + + package_file = os.path.join(project_dir, "package.json") + package = f""" +{{ + "name": "my-awesome-package", + "version": "{version}" +}} + """ + create_file(package_file, package) + + other_package_file = os.path.join(project_dir, "other-package.json") + create_file(other_package_file, package) + + touch_file(os.path.join(package_dir, "__init__.py")) + touch_file(os.path.join(package_dir, "foo.py")) + touch_file(os.path.join(package_dir, "bar.py")) + touch_file(os.path.join(package_dir, "baz.py")) + + origin = os.getcwd() + os.chdir(project_dir) + try: + yield project_dir + finally: + os.chdir(origin) + + +@pytest.fixture +def new_project(temp_dir, request): + with create_project( + temp_dir, + """\ +[build-system] +requires = ["hatchling", "hatch-vcs"] +build-backend = "hatchling.build" +[project] +name = "my-app" +dynamic = ["version"] +[tool.hatch.version] +source = "nodejs" +""", + request.param, + ) as project: + yield project diff --git a/tests/test_version_config.py b/tests/test_version_config.py index e69de29..bc78ac7 100644 --- a/tests/test_version_config.py +++ b/tests/test_version_config.py @@ -0,0 +1,75 @@ +# SPDX-FileCopyrightText: 2022-present Angus Hollands +# +# SPDX-License-Identifier: MIT +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-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"), +] + +BAD_NODE_VERSIONS = [ + "1.4", + "1.4.5a0", + "1.4.5-c0.post1", + "1.4.5-rc0.post1.dev2", +] +BAD_PYTHON_VERSIONS = [ + "1.4", + "1.4.5ab", + "1.4.5-c0.smoke2", + "1.4.5rc.post1@dev2", +] + + +class TestDefault: + @pytest.mark.parametrize( + "new_project, python_version", GOOD_NODE_PYTHON_VERSIONS, indirect=["new_project"] + ) + @pytest.mark.parametrize("config", [{"path": "other-package.json"}, {}]) + def test_read_correct(self, new_project, python_version, config): + version_source = NodeJSVersionSource(new_project, config) + data = version_source.get_version_data() + assert data["version"] == python_version + + @pytest.mark.parametrize("python_version,node_version", GOOD_NODE_PYTHON_VERSIONS) + @pytest.mark.parametrize("config", [{"path": "other-package.json"}, {}]) + @pytest.mark.parametrize("new_project", ["1.0.0"], indirect=True) + def test_write_correct(self, new_project, python_version, node_version, config): + version_source = NodeJSVersionSource(new_project, config) + data = version_source.get_version_data() + version_source.set_version(python_version, data) + data = version_source.get_version_data() + assert data["version"] == node_version + + @pytest.mark.parametrize( + "new_project", + BAD_NODE_VERSIONS, + indirect=["new_project"], + ) + @pytest.mark.parametrize("config", [{"path": "other-package.json"}, {}]) + def test_read_incorrect(self, new_project, config): + version_source = NodeJSVersionSource(new_project, config) + + with pytest.raises(ValueError, match=".* did not match regex"): + version_source.get_version_data() + + @pytest.mark.parametrize("python_version,", BAD_PYTHON_VERSIONS) + @pytest.mark.parametrize("new_project", ["1.0.0"], indirect=True) + @pytest.mark.parametrize("config", [{"path": "other-package.json"}, {}]) + def test_write_incorrect(self, new_project, python_version, config): + version_source = NodeJSVersionSource(new_project, config) + data = version_source.get_version_data() + with pytest.raises(ValueError, match=".* did not match regex"): + version_source.set_version(python_version, data)