Skip to content

Commit 93406dd

Browse files
author
Alex Coleman
committed
Updating line numbers tests to use generated cwl files.
1 parent 5b10422 commit 93406dd

6 files changed

+62
-35
lines changed

schema_salad/metaschema.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,13 +1136,10 @@ def _document_load(
11361136
doc.pop("$schemas")
11371137
if "$base" in doc:
11381138
doc.pop("$base")
1139-
<<<<<<< HEAD
11401139

11411140
if isinstance(doc, CommentedMap):
11421141
global doc_line_info
11431142
doc_line_info = doc
1144-
=======
1145-
>>>>>>> main
11461143

11471144
if "$graph" in doc:
11481145
loadingOptions.idx[baseuri] = (

schema_salad/python_codegen_support.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,13 +1133,10 @@ def _document_load(
11331133
doc.pop("$schemas")
11341134
if "$base" in doc:
11351135
doc.pop("$base")
1136-
<<<<<<< HEAD
11371136

11381137
if isinstance(doc, CommentedMap):
11391138
global doc_line_info
11401139
doc_line_info = doc
1141-
=======
1142-
>>>>>>> main
11431140

11441141
if "$graph" in doc:
11451142
loadingOptions.idx[baseuri] = (

schema_salad/tests/test_line_numbers.py

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
# from parser import load_document_by_uri, save
1+
import importlib
22
from pathlib import Path
3-
from typing import Any, MutableSequence, Optional, cast
4-
from urllib.parse import unquote_plus, urlparse
3+
from typing import Any, Dict, List, MutableSequence, Optional, Union, cast
4+
from urllib.parse import urlparse
55

66
from ruamel.yaml.comments import CommentedMap
77

8-
import schema_salad.tests.cwl_v1_2 as cwl_v1_2
98
from schema_salad.utils import yaml_no_ts
9+
from schema_salad import codegen
10+
from schema_salad.avro.schema import Names
11+
from schema_salad.exceptions import ValidationException
12+
from schema_salad.schema import load_schema
1013

11-
from .util import get_data
14+
from .util import get_data, cwl_file_uri
1215

13-
14-
def test_secondary_files_dsl() -> None:
16+
def test_secondary_files_dsl(tmp_path: Path) -> None:
1517
"""
1618
Checks object is properly saving when dsl is used
1719
"""
1820
t = "test_secondary_files_dsl.cwl"
1921
path = get_data("tests/" + t)
20-
obj = load_document_by_uri(str(path))
22+
obj = load_document_by_uri(tmp_path, str(path))
2123
saved_obj = obj.save()
2224
assert isinstance(saved_obj, CommentedMap)
2325
assert saved_obj.lc.data == {
@@ -49,13 +51,13 @@ def test_secondary_files_dsl() -> None:
4951
}
5052

5153

52-
def test_outputs_before_inputs() -> None:
54+
def test_outputs_before_inputs(tmp_path: Path) -> None:
5355
"""
5456
Tests when output comes in cwl file before inputs
5557
"""
5658
t = "test_outputs_before_inputs.cwl"
5759
path = get_data("tests/" + t)
58-
obj = load_document_by_uri(str(path))
60+
obj = load_document_by_uri(tmp_path, str(path))
5961
saved_obj = obj.save()
6062
assert isinstance(saved_obj, CommentedMap)
6163
assert {
@@ -80,15 +82,15 @@ def test_outputs_before_inputs() -> None:
8082
}
8183

8284

83-
def test_type_dsl() -> None:
85+
def test_type_dsl(tmp_path: Path) -> None:
8486
"""
8587
Checks object is properly saving when type DSL is used.
8688
In this example, type for the input is File? which should expand to
8789
null, File.
8890
"""
8991
t = "test_type_dsl.cwl"
9092
path = get_data("tests/" + t)
91-
obj = load_document_by_uri(str(path))
93+
obj = load_document_by_uri(tmp_path, str(path))
9294
saved_obj = obj.save()
9395
assert isinstance(saved_obj, CommentedMap)
9496
assert {
@@ -114,28 +116,37 @@ def test_type_dsl() -> None:
114116
assert saved_obj["outputs"][0]["outputBinding"].lc.data == {"glob": [15, 6, 15, 12]}
115117

116118

117-
def load_document_by_uri(path: str) -> Any:
118-
"""
119-
Takes in a path and loads it via the python codegen.
120-
"""
121-
uri = urlparse(path)
122-
if not uri.scheme or uri.scheme == "file":
123-
real_path = Path(unquote_plus(uri.path)).resolve().as_uri()
119+
def load_document_by_uri(tmp_path: Path, path: Union[str, Path]) -> Any:
120+
src_target = tmp_path / "cwl_v1_0.py"
121+
python_codegen(cwl_file_uri, src_target)
122+
spec = importlib.util.spec_from_file_location("cwl_v1_0", src_target)
123+
assert isinstance(spec, importlib.machinery.ModuleSpec)
124+
assert isinstance(spec.loader, importlib.abc.Loader)
125+
temp_cwl_v1_0 = importlib.util.module_from_spec(spec)
126+
spec.loader.exec_module(temp_cwl_v1_0)
127+
cwl_v1_0: Any = temp_cwl_v1_0
128+
129+
if isinstance(path, str):
130+
uri = urlparse(path)
131+
if not uri.scheme or uri.scheme == "file":
132+
real_path = Path(uri.path).resolve().as_uri()
133+
else:
134+
real_path = path
124135
else:
125-
real_path = path
136+
real_path = path.resolve().as_uri()
126137

127138
baseuri = str(real_path)
128139

129-
loadingOptions = cwl_v1_2.LoadingOptions(fileuri=baseuri)
130-
# doc = loadingOptions.fetcher.fetch_text(real_path)
131-
with open(path, 'r') as file:
132-
doc = file.read()
140+
loadingOptions = cwl_v1_0.LoadingOptions(fileuri=baseuri)
133141

142+
with open(path, "r") as file:
143+
doc = file.read()
144+
# doc = loadingOptions.fetcher.fetch_text(urllib.parse.unquote(str(real_path)))
134145
yaml = yaml_no_ts()
135146
doc = yaml.load(doc)
136147

137-
result = cwl_v1_2.load_document_by_yaml(
138-
doc, baseuri, cast(Optional[cwl_v1_2.LoadingOptions], loadingOptions)
148+
result = cwl_v1_0.load_document_by_yaml(
149+
doc, baseuri, cast(Optional[cwl_v1_0.LoadingOptions], loadingOptions)
139150
)
140151

141152
if isinstance(result, MutableSequence):
@@ -144,3 +155,25 @@ def load_document_by_uri(path: str) -> Any:
144155
lst.append(r)
145156
return lst
146157
return result
158+
159+
160+
161+
def python_codegen(
162+
file_uri: str,
163+
target: Path,
164+
parser_info: Optional[str] = None,
165+
package: Optional[str] = None,
166+
) -> None:
167+
document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema(file_uri)
168+
assert isinstance(avsc_names, Names)
169+
schema_raw_doc = metaschema_loader.fetch(file_uri)
170+
schema_doc, schema_metadata = metaschema_loader.resolve_all(schema_raw_doc, file_uri)
171+
codegen.codegen(
172+
"python",
173+
cast(List[Dict[str, Any]], schema_doc),
174+
schema_metadata,
175+
document_loader,
176+
target=str(target),
177+
parser_info=parser_info,
178+
package=package,
179+
)

schema_salad/tests/test_outputs_before_inputs.cwl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class: CommandLineTool
2-
cwlVersion: v1.2
2+
cwlVersion: v1.0
33
baseCommand: python3
44

55
outputs:

schema_salad/tests/test_secondary_files_dsl.cwl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class: CommandLineTool
2-
cwlVersion: v1.2
2+
cwlVersion: v1.0
33
baseCommand: python3
44

55
inputs:

schema_salad/tests/test_type_dsl.cwl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class: CommandLineTool
2-
cwlVersion: v1.2
2+
cwlVersion: v1.0
33
baseCommand: python3
44

55
inputs:

0 commit comments

Comments
 (0)