@@ -98,7 +98,6 @@ def replace_uri_in_graph(graph, old_uri_str, new_uri_str):
9898
9999 return g
100100
101-
102101def _uses_https_schema (graph : rdflib .Graph ) -> bool :
103102 for s , p , o in graph .triples ((None , None , None )):
104103 for term in (s , p , o ):
@@ -115,53 +114,36 @@ def _prepare_temp_rocrate(
115114) -> Path :
116115 # `mkdtemp` returns a stable path the test owns; `TemporaryDirectory().name`
117116 # was deleted on GC before copytree ran. `dirs_exist_ok=True` lets us copy
118- # into the already-created directory.
117+ # into the ( already-created) mkdtemp directory.
119118 temp_rocrate_path = Path (tempfile .mkdtemp ())
120- shutil .copytree (
121- rocrate_path ,
122- temp_rocrate_path ,
123- dirs_exist_ok = True ,
124- )
125-
126- metadata_path = temp_rocrate_path / "ro-crate-metadata.json"
127-
128- with metadata_path .open (encoding = "utf-8" ) as f :
119+ shutil .copytree (rocrate_path , temp_rocrate_path , dirs_exist_ok = True )
120+ with (temp_rocrate_path / "ro-crate-metadata.json" ).open (encoding = "utf-8" ) as f :
129121 rocrate = json .load (f )
130-
131122 if rocrate_entity_patch is not None :
132123 for key , value in rocrate_entity_patch .items ():
133124 for entity in rocrate ["@graph" ]:
134125 if entity ["@id" ] == key :
135126 entity .update (value )
136127 break
137-
138- with metadata_path .open ("w" , encoding = "utf-8" ) as f :
128+ with (temp_rocrate_path / "ro-crate-metadata.json" ).open ("w" , encoding = "utf-8" ) as f :
139129 json .dump (rocrate , f )
140-
141130 if rocrate_entity_mod_sparql is not None :
142131 rocrate_graph = load_graph_and_preserve_relative_ids (rocrate )
143-
144132 if _uses_https_schema (rocrate_graph ):
145133 rocrate_entity_mod_sparql = rocrate_entity_mod_sparql .replace (
146134 "http://schema.org/" ,
147135 "https://schema.org/" ,
148136 )
149-
150137 rocrate_graph .update (rocrate_entity_mod_sparql )
151-
152- context = rocrate .get (
153- "@context" ,
154- "https://w3id.org/ro/crate/1.2/context" ,
155- )
156-
138+ # preserve the original context to avoid forcing a downgrade/upgrade
139+ context_uri = rocrate .get ("@context" , "https://w3id.org/ro/crate/1.2/context" )
157140 rocrate_graph .serialize (
158- metadata_path ,
141+ Path ( temp_rocrate_path , "ro-crate-metadata.json" ) ,
159142 format = "json-ld" ,
160- context = context ,
143+ context = context_uri ,
161144 indent = 2 ,
162145 use_native_types = True ,
163146 )
164-
165147 return temp_rocrate_path
166148
167149
@@ -201,17 +183,12 @@ def do_entity_test( # pylint: disable=too-many-locals
201183 rocrate_path = Path (rocrate_path )
202184
203185 temp_rocrate_path = None
204-
205186 if (
206187 any ([rocrate_entity_patch , rocrate_entity_mod_sparql ])
207188 and isinstance (rocrate_path , Path )
208189 and rocrate_path .is_dir ()
209190 ):
210- temp_rocrate_path = _prepare_temp_rocrate (
211- rocrate_path ,
212- rocrate_entity_patch ,
213- rocrate_entity_mod_sparql ,
214- )
191+ temp_rocrate_path = _prepare_temp_rocrate (rocrate_path , rocrate_entity_patch , rocrate_entity_mod_sparql )
215192 rocrate_path = temp_rocrate_path
216193
217194 if expected_triggered_requirements is None :
@@ -225,8 +202,8 @@ def do_entity_test( # pylint: disable=too-many-locals
225202 logger .debug ("Requirement severity: %s" , requirement_severity )
226203 logger .debug ("Checks to skip: %s" , skip_checks )
227204
205+ # validate RO-Crate
228206 relative_root_path = Path (rocrate_relative_root_path ) if rocrate_relative_root_path is not None else None
229-
230207 result : models .ValidationResult = services .validate (
231208 models .ValidationSettings (
232209 rocrate_uri = models .URI (rocrate_path ),
@@ -247,25 +224,23 @@ def do_entity_test( # pylint: disable=too-many-locals
247224 )
248225
249226 assert result .context is not None , "Validation context should not be None"
250-
251227 logger .debug (
252228 "Expected requirement severity to be %s, got %s" ,
253229 requirement_severity ,
254230 result .context .requirement_severity ,
255231 )
256-
257232 assert result .passed () == expected_validation_result , (
258233 f"RO-Crate should be { 'valid' if expected_validation_result else 'invalid' } "
259234 )
260235
261- failed_requirements = [requirement .name for requirement in result .failed_requirements ]
236+ # check requirement
237+ failed_requirements = [_ .name for _ in result .failed_requirements ]
262238
263239 for expected_triggered_requirement in expected_triggered_requirements :
264240 if expected_triggered_requirement not in failed_requirements :
265241 raise AssertionError (
266- "The expected requirement "
267- f'"{ expected_triggered_requirement } " was not found in the '
268- "failed requirements"
242+ f"The expected requirement "
243+ f'"{ expected_triggered_requirement } " was not found in the failed requirements'
269244 )
270245
271246 detected_issues = [
@@ -276,42 +251,18 @@ def do_entity_test( # pylint: disable=too-many-locals
276251 logger .debug ("Expected issues: %s" , expected_triggered_issues )
277252
278253 for expected_issue in expected_triggered_issues :
279- if not any (expected_issue in issue for issue in detected_issues ):
254+ if not any (expected_issue in issue for issue in detected_issues ): # support partial match
280255 raise AssertionError (f'The expected issue "{ expected_issue } " was not found in the detected issues' )
281-
282256 except Exception :
283257 if logger .isEnabledFor (logging .DEBUG ):
284- logger .exception (
285- "Failed to validate RO-Crate @ path: %s" ,
286- rocrate_path ,
287- )
288- logger .debug (
289- "Requirement severity: %s" ,
290- requirement_severity ,
291- )
292- logger .debug (
293- "Expected validation result: %s" ,
294- expected_validation_result ,
295- )
296- logger .debug (
297- "Expected triggered requirements: %s" ,
298- expected_triggered_requirements ,
299- )
300- logger .debug (
301- "Expected triggered issues: %s" ,
302- expected_triggered_issues ,
303- )
304- logger .debug (
305- "Failed requirements: %s" ,
306- failed_requirements ,
307- )
308- logger .debug (
309- "Detected issues: %s" ,
310- detected_issues ,
311- )
312-
258+ logger .exception ("Failed to validate RO-Crate @ path: %s" , rocrate_path )
259+ logger .debug ("Requirement severity: %s" , requirement_severity )
260+ logger .debug ("Expected validation result: %s" , expected_validation_result )
261+ logger .debug ("Expected triggered requirements: %s" , expected_triggered_requirements )
262+ logger .debug ("Expected triggered issues: %s" , expected_triggered_issues )
263+ logger .debug ("Failed requirements: %s" , failed_requirements )
264+ logger .debug ("Detected issues: %s" , detected_issues )
313265 raise
314-
315266 finally :
316267 if temp_rocrate_path is not None :
317268 logger .debug (
0 commit comments