Skip to content

Commit

Permalink
more tests & simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-c committed Jan 27, 2022
1 parent 1bee751 commit 786d2a4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
16 changes: 3 additions & 13 deletions wdl2cwl/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,10 @@ def __exit__(
return
raise self.makeError(str(exc_value)) from exc_value

def file(self) -> Optional[str]:
"""Test if the item has file information attached."""
if hasattr(self.item, "pos"):
pos: SourcePosition = cast(SourcePosition, self.item.pos)
return pos.uri
return None

def makeLead(self) -> str:
"""Caculate the error message prefix."""
file = self.file()
if file:
pos = cast(SourcePosition, self.item.pos)
return f"{file}:{pos.line}:{pos.column}:"
return ""
pos: SourcePosition = cast(SourcePosition, self.item.pos)
return f"{pos.uri}:{pos.line}:{pos.column}:"

def makeError(self, msg: str) -> Any:
"""Add the source info to the msg and instantiate the raise_type with it."""
Expand All @@ -77,5 +67,5 @@ def makeError(self, msg: str) -> Any:
if bool(lineno_re.match(m)):
errs.append(m)
else:
errs.append(f"{lead}{m}")
errs.append(f"{lead} {m}")
return self.raise_type("\n".join(errs))
32 changes: 31 additions & 1 deletion wdl2cwl/tests/test_wdlsourceline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
import WDL

from ..errors import WDLSourceLine
from ..main import ConversionException, Converter
from .test_cwl import get_file

Expand All @@ -15,7 +16,36 @@ def test_wdlsourceline() -> None:
with pytest.raises(
ConversionException,
match=re.escape(
"minCores.wdl:1:1:Unimplemented type: <class 'WDL.Tree.Document'>"
"minCores.wdl:1:1: Unimplemented type: <class 'WDL.Tree.Document'>"
),
):
Converter().load_wdl_objects(doc_tree) # type: ignore


def test_wdlsourceline_non_wdl() -> None:
"""Using non-miniWDL objects with WDLSourceLine."""
message = WDLSourceLine({"non-WDL object"}).makeError("Something happened")

assert message == "Something happened"


def test_wdlsourceline_non_wdl_context_manager() -> None:
"""Test non-miniWDL objects with WDLSourceLine as a context manager."""
with pytest.raises(ConversionException, match="Something went wrong"):
with WDLSourceLine({"non-WDL object"}, ConversionException) as test:
assert test is not None
raise Exception("Something went wrong")
assert True


def test_nested_wdlsourceline() -> None:
"""Nested test of WDLSourceLine."""
doc_tree = WDL.load(get_file("wdl_files/minCores.wdl"))
with pytest.raises(
ConversionException,
match=re.escape(
"minCores.wdl:1:1: Unimplemented type: <class 'WDL.Tree.Document'>"
),
):
with WDLSourceLine(doc_tree.tasks[0], ConversionException):
Converter().load_wdl_objects(doc_tree) # type: ignore

0 comments on commit 786d2a4

Please sign in to comment.