1
1
import os
2
+ import shutil
2
3
from pathlib import Path
4
+ from typing import Dict , List , Union
3
5
4
6
import pytest
5
7
6
8
from hatch_openzim .metadata import update
7
9
10
+ Metadata = Dict [str , Union [str , List [str ]]]
11
+
8
12
9
13
@pytest .fixture
10
- def dynamic_metadata ():
14
+ def dynamic_metadata () -> List [ str ] :
11
15
return [
12
16
"authors" ,
13
17
"classifiers" ,
@@ -18,16 +22,28 @@ def dynamic_metadata():
18
22
19
23
20
24
@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 :
22
38
return {
23
39
"requires-python" : ">=3.10,<3.12" ,
24
40
"dynamic" : dynamic_metadata ,
25
41
}
26
42
27
43
28
- def test_metadata_nominal (metadata ):
44
+ def test_metadata_nominal (metadata : Metadata , root_folder : str ):
29
45
update (
30
- root = str ( Path ( os . path . dirname ( os . path . abspath ( __file__ ))). parent ) ,
46
+ root = root_folder ,
31
47
config = {},
32
48
metadata = metadata ,
33
49
)
@@ -57,15 +73,19 @@ def test_metadata_nominal(metadata):
57
73
("urls" ),
58
74
],
59
75
)
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" ])
61
81
metadata ["dynamic" ].remove (metadata_key )
62
82
with pytest .raises (
63
83
Exception ,
64
84
match = f"'{ metadata_key } ' must be listed in 'project.dynamic' when using openzim"
65
85
" metadata hook" ,
66
86
):
67
87
update (
68
- root = str ( Path ( os . path . dirname ( os . path . abspath ( __file__ ))). parent ) ,
88
+ root = root_folder ,
69
89
config = {},
70
90
metadata = metadata ,
71
91
)
@@ -81,15 +101,17 @@ def test_metadata_missing_dynamic(metadata, metadata_key):
81
101
("urls" ),
82
102
],
83
103
)
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
+ ):
85
107
metadata [metadata_key ] = "some_value"
86
108
with pytest .raises (
87
109
Exception ,
88
110
match = f"'{ metadata_key } ' must not be listed in the 'project' table when using "
89
111
"openzim metadata hook" ,
90
112
):
91
113
update (
92
- root = str ( Path ( os . path . dirname ( os . path . abspath ( __file__ ))). parent ) ,
114
+ root = root_folder ,
93
115
config = {},
94
116
metadata = metadata ,
95
117
)
@@ -105,38 +127,40 @@ def test_metadata_metadata_already_there(metadata, metadata_key):
105
127
("urls" ),
106
128
],
107
129
)
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
+ ):
109
133
metadata [metadata_key ] = f"some_value_for_{ metadata_key } "
110
134
config = {}
111
135
config [f"preserve-{ metadata_key } " ] = True
112
136
update (
113
- root = str ( Path ( os . path . dirname ( os . path . abspath ( __file__ ))). parent ) ,
137
+ root = root_folder ,
114
138
config = config ,
115
139
metadata = metadata ,
116
140
)
117
141
assert metadata [metadata_key ] == f"some_value_for_{ metadata_key } "
118
142
119
143
120
- def test_metadata_additional_keywords (metadata ):
144
+ def test_metadata_additional_keywords (metadata : Metadata , root_folder : str ):
121
145
config = {}
122
146
config ["additional-keywords" ] = ["keyword1" , "keyword2" ]
123
147
update (
124
- root = str ( Path ( os . path . dirname ( os . path . abspath ( __file__ ))). parent ) ,
148
+ root = root_folder ,
125
149
config = config ,
126
150
metadata = metadata ,
127
151
)
128
152
# we compare sets because order is not relevant
129
153
assert set (metadata ["keywords" ]) == {"openzim" , "keyword1" , "keyword2" }
130
154
131
155
132
- def test_metadata_additional_classifiers (metadata ):
156
+ def test_metadata_additional_classifiers (metadata : Metadata , root_folder : str ):
133
157
config = {}
134
158
config ["additional-classifiers" ] = [
135
159
"Development Status :: 5 - Production/Stable" ,
136
160
"Intended Audience :: Developers" ,
137
161
]
138
162
update (
139
- root = str ( Path ( os . path . dirname ( os . path . abspath ( __file__ ))). parent ) ,
163
+ root = root_folder ,
140
164
config = config ,
141
165
metadata = metadata ,
142
166
)
@@ -151,11 +175,11 @@ def test_metadata_additional_classifiers(metadata):
151
175
}
152
176
153
177
154
- def test_metadata_additional_authors (metadata ):
178
+ def test_metadata_additional_authors (metadata : Metadata , root_folder : str ):
155
179
config = {}
156
180
config ["additional-authors" ] = [{"email" : "someone@acme.org" , "name" : "Some One" }]
157
181
update (
158
- root = str ( Path ( os . path . dirname ( os . path . abspath ( __file__ ))). parent ) ,
182
+ root = root_folder ,
159
183
config = config ,
160
184
metadata = metadata ,
161
185
)
@@ -178,12 +202,14 @@ def test_metadata_additional_authors(metadata):
178
202
(None , "openzim" ),
179
203
],
180
204
)
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
+ ):
182
208
config = {}
183
209
if organization :
184
210
config ["organization" ] = organization
185
211
update (
186
- root = str ( Path ( os . path . dirname ( os . path . abspath ( __file__ ))). parent ) ,
212
+ root = root_folder ,
187
213
config = config ,
188
214
metadata = metadata ,
189
215
)
@@ -197,11 +223,11 @@ def test_metadata_organization(organization, expected_result, metadata):
197
223
raise Exception (f"Unexpected expected result: { expected_result } " )
198
224
199
225
200
- def test_metadata_is_scraper (metadata ):
226
+ def test_metadata_is_scraper (metadata : Metadata , root_folder : str ):
201
227
config = {}
202
228
config ["kind" ] = "scraper"
203
229
update (
204
- root = str ( Path ( os . path . dirname ( os . path . abspath ( __file__ ))). parent ) ,
230
+ root = root_folder ,
205
231
config = config ,
206
232
metadata = metadata ,
207
233
)
0 commit comments