Skip to content

Commit 520c0be

Browse files
committed
WIP: add tests
1 parent d842b97 commit 520c0be

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# SPDX-FileCopyrightText: 2022-present Angus Hollands <[email protected]>
22
#
33
# SPDX-License-Identifier: MIT
4+
import pytest

tests/conftest.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# SPDX-FileCopyrightText: 2022-present Angus Hollands <[email protected]>
2+
# SPDX-FileCopyrightText: 2022-present Ofek Lev <[email protected]>
3+
#
4+
# SPDX-License-Identifier: MIT
5+
import errno
6+
import shutil
7+
import stat
8+
import tempfile
9+
from contextlib import contextmanager
10+
import os
11+
import pytest
12+
13+
14+
def touch_file(path):
15+
with open(path, "a"):
16+
os.utime(path, None)
17+
18+
19+
def create_file(path, contents):
20+
with open(path, "w") as f:
21+
f.write(contents)
22+
23+
24+
def handle_remove_readonly(func, path, exc): # no cov
25+
# PermissionError: [WinError 5] Access is denied: '...\\.git\\...'
26+
if func in (os.rmdir, os.remove, os.unlink) and exc[1].errno == errno.EACCES:
27+
os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
28+
func(path)
29+
else:
30+
raise
31+
32+
33+
@pytest.fixture
34+
def temp_dir():
35+
directory = tempfile.mkdtemp()
36+
try:
37+
directory = os.path.realpath(directory)
38+
yield directory
39+
finally:
40+
shutil.rmtree(directory, ignore_errors=False, onerror=handle_remove_readonly)
41+
42+
43+
@contextmanager
44+
def create_project(directory, metadata, version):
45+
project_dir = os.path.join(directory, "my-app")
46+
os.mkdir(project_dir)
47+
48+
project_file = os.path.join(project_dir, "pyproject.toml")
49+
create_file(project_file, metadata)
50+
51+
package_dir = os.path.join(project_dir, "my_app")
52+
os.mkdir(package_dir)
53+
54+
package_file = os.path.join(project_dir, "package.json")
55+
package = f"""
56+
{{
57+
"name": "my-awesome-package",
58+
"version": "{version}"
59+
}}
60+
"""
61+
create_file(package_file, package)
62+
63+
other_package_file = os.path.join(project_dir, "other-package.json")
64+
create_file(other_package_file, package)
65+
66+
touch_file(os.path.join(package_dir, "__init__.py"))
67+
touch_file(os.path.join(package_dir, "foo.py"))
68+
touch_file(os.path.join(package_dir, "bar.py"))
69+
touch_file(os.path.join(package_dir, "baz.py"))
70+
71+
origin = os.getcwd()
72+
os.chdir(project_dir)
73+
try:
74+
yield project_dir
75+
finally:
76+
os.chdir(origin)
77+
78+
79+
@pytest.fixture
80+
def new_project(temp_dir, request):
81+
with create_project(
82+
temp_dir,
83+
"""\
84+
[build-system]
85+
requires = ["hatchling", "hatch-vcs"]
86+
build-backend = "hatchling.build"
87+
[project]
88+
name = "my-app"
89+
dynamic = ["version"]
90+
[tool.hatch.version]
91+
source = "nodejs"
92+
""",
93+
request.param,
94+
) as project:
95+
yield project

tests/test_version_config.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)