Skip to content

Commit

Permalink
WIP: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Jun 10, 2022
1 parent d842b97 commit 520c0be
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# SPDX-FileCopyrightText: 2022-present Angus Hollands <goosey15@gmail.com>
#
# SPDX-License-Identifier: MIT
import pytest
95 changes: 95 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# SPDX-FileCopyrightText: 2022-present Angus Hollands <goosey15@gmail.com>
# SPDX-FileCopyrightText: 2022-present Ofek Lev <oss@ofek.dev>
#
# 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
75 changes: 75 additions & 0 deletions tests/test_version_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# SPDX-FileCopyrightText: 2022-present Angus Hollands <goosey15@gmail.com>
#
# 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)

0 comments on commit 520c0be

Please sign in to comment.