Skip to content

Commit e1997e9

Browse files
committed
Fix metadata tests to not rely on the fact they are ran from a Git clone of openzim/hatch-openzim
1 parent d783177 commit e1997e9

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

tests/configs/gitconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
6+
[remote "origin"]
7+
url = git@github.com:openzim/hatch-openzim.git
8+
fetch = +refs/heads/*:refs/remotes/origin/*
9+
[branch "main"]
10+
remote = origin
11+
merge = refs/heads/main

tests/test_metadata.py

+46-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import os
2+
import shutil
23
from pathlib import Path
4+
from typing import Dict, List, Union
35

46
import pytest
57

68
from hatch_openzim.metadata import update
79

10+
Metadata = Dict[str, Union[str, List[str]]]
11+
812

913
@pytest.fixture
10-
def dynamic_metadata():
14+
def dynamic_metadata() -> List[str]:
1115
return [
1216
"authors",
1317
"classifiers",
@@ -18,16 +22,28 @@ def dynamic_metadata():
1822

1923

2024
@pytest.fixture
21-
def metadata(dynamic_metadata):
25+
def root_folder(tmp_path: Path) -> str:
26+
git_folder = tmp_path / ".git"
27+
git_folder.mkdir()
28+
shutil.copy(
29+
Path(os.path.dirname(os.path.abspath(__file__))).parent
30+
/ "tests/configs/gitconfig",
31+
git_folder / "config",
32+
)
33+
return str(tmp_path)
34+
35+
36+
@pytest.fixture
37+
def metadata(dynamic_metadata: List[str]) -> Metadata:
2238
return {
2339
"requires-python": ">=3.10,<3.12",
2440
"dynamic": dynamic_metadata,
2541
}
2642

2743

28-
def test_metadata_nominal(metadata):
44+
def test_metadata_nominal(metadata: Metadata, root_folder: str):
2945
update(
30-
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent),
46+
root=root_folder,
3147
config={},
3248
metadata=metadata,
3349
)
@@ -57,15 +73,19 @@ def test_metadata_nominal(metadata):
5773
("urls"),
5874
],
5975
)
60-
def test_metadata_missing_dynamic(metadata, metadata_key):
76+
def test_metadata_missing_dynamic(
77+
metadata: Metadata, metadata_key: str, root_folder: str
78+
):
79+
assert isinstance(metadata["dynamic"], List)
80+
assert all(isinstance(item, str) for item in metadata["dynamic"])
6181
metadata["dynamic"].remove(metadata_key)
6282
with pytest.raises(
6383
Exception,
6484
match=f"'{metadata_key}' must be listed in 'project.dynamic' when using openzim"
6585
" metadata hook",
6686
):
6787
update(
68-
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent),
88+
root=root_folder,
6989
config={},
7090
metadata=metadata,
7191
)
@@ -81,15 +101,17 @@ def test_metadata_missing_dynamic(metadata, metadata_key):
81101
("urls"),
82102
],
83103
)
84-
def test_metadata_metadata_already_there(metadata, metadata_key):
104+
def test_metadata_metadata_already_there(
105+
metadata: Metadata, metadata_key: str, root_folder: str
106+
):
85107
metadata[metadata_key] = "some_value"
86108
with pytest.raises(
87109
Exception,
88110
match=f"'{metadata_key}' must not be listed in the 'project' table when using "
89111
"openzim metadata hook",
90112
):
91113
update(
92-
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent),
114+
root=root_folder,
93115
config={},
94116
metadata=metadata,
95117
)
@@ -105,38 +127,40 @@ def test_metadata_metadata_already_there(metadata, metadata_key):
105127
("urls"),
106128
],
107129
)
108-
def test_metadata_preserve_value(metadata, metadata_key):
130+
def test_metadata_preserve_value(
131+
metadata: Metadata, metadata_key: str, root_folder: str
132+
):
109133
metadata[metadata_key] = f"some_value_for_{metadata_key}"
110134
config = {}
111135
config[f"preserve-{metadata_key}"] = True
112136
update(
113-
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent),
137+
root=root_folder,
114138
config=config,
115139
metadata=metadata,
116140
)
117141
assert metadata[metadata_key] == f"some_value_for_{metadata_key}"
118142

119143

120-
def test_metadata_additional_keywords(metadata):
144+
def test_metadata_additional_keywords(metadata: Metadata, root_folder: str):
121145
config = {}
122146
config["additional-keywords"] = ["keyword1", "keyword2"]
123147
update(
124-
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent),
148+
root=root_folder,
125149
config=config,
126150
metadata=metadata,
127151
)
128152
# we compare sets because order is not relevant
129153
assert set(metadata["keywords"]) == {"openzim", "keyword1", "keyword2"}
130154

131155

132-
def test_metadata_additional_classifiers(metadata):
156+
def test_metadata_additional_classifiers(metadata: Metadata, root_folder: str):
133157
config = {}
134158
config["additional-classifiers"] = [
135159
"Development Status :: 5 - Production/Stable",
136160
"Intended Audience :: Developers",
137161
]
138162
update(
139-
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent),
163+
root=root_folder,
140164
config=config,
141165
metadata=metadata,
142166
)
@@ -151,11 +175,11 @@ def test_metadata_additional_classifiers(metadata):
151175
}
152176

153177

154-
def test_metadata_additional_authors(metadata):
178+
def test_metadata_additional_authors(metadata: Metadata, root_folder: str):
155179
config = {}
156180
config["additional-authors"] = [{"email": "someone@acme.org", "name": "Some One"}]
157181
update(
158-
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent),
182+
root=root_folder,
159183
config=config,
160184
metadata=metadata,
161185
)
@@ -178,12 +202,14 @@ def test_metadata_additional_authors(metadata):
178202
(None, "openzim"),
179203
],
180204
)
181-
def test_metadata_organization(organization, expected_result, metadata):
205+
def test_metadata_organization(
206+
organization: str, expected_result: str, metadata, root_folder: str
207+
):
182208
config = {}
183209
if organization:
184210
config["organization"] = organization
185211
update(
186-
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent),
212+
root=root_folder,
187213
config=config,
188214
metadata=metadata,
189215
)
@@ -197,11 +223,11 @@ def test_metadata_organization(organization, expected_result, metadata):
197223
raise Exception(f"Unexpected expected result: {expected_result}")
198224

199225

200-
def test_metadata_is_scraper(metadata):
226+
def test_metadata_is_scraper(metadata: Metadata, root_folder: str):
201227
config = {}
202228
config["kind"] = "scraper"
203229
update(
204-
root=str(Path(os.path.dirname(os.path.abspath(__file__))).parent),
230+
root=root_folder,
205231
config=config,
206232
metadata=metadata,
207233
)

0 commit comments

Comments
 (0)