Skip to content

Commit

Permalink
Display TaintConfig Error locations when available
Browse files Browse the repository at this point in the history
Display TaintConfigurationError locations when available. The Ocaml
binary now returns positions after Github PR: #734
(commit 59d2cf0). Parse and print when
the location(s) are available.

Adds test for the same and updates two existing ones.

Signed-off-by: Abishek V Ashok <[email protected]>
  • Loading branch information
abishekvashok committed May 31, 2023
1 parent 8c639dc commit 6b1d342
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
47 changes: 42 additions & 5 deletions client/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,35 @@ class TaintConfigurationError:
path: Optional[Path]
description: str
code: int
start_line: Optional[int]
start_column: Optional[int]
stop_line: Optional[int]
stop_column: Optional[int]

@staticmethod
def from_json(error_json: Dict[str, Any]) -> "TaintConfigurationError":
try:
error_location = error_json["location"]
if error_location is not None:
start_line = error_location["start"]["line"]
start_column = error_location["start"]["column"]
stop_line = error_location["stop"]["line"]
stop_column = error_location["stop"]["column"]
else:
start_line = None
start_column = None
stop_line = None
stop_column = None
return TaintConfigurationError(
path=Path(error_json["path"])
if error_json["path"] is not None
else None,
description=error_json["description"],
code=error_json["code"],
start_line=start_line,
start_column=start_column,
stop_line=stop_line,
stop_column=stop_column,
)
except KeyError as key_error:
message = f"Missing field from error json: {key_error}"
Expand All @@ -190,11 +209,21 @@ def to_json(self) -> Dict[str, Any]:
"path": str(self.path) if self.path is not None else None,
"description": self.description,
"code": self.code,
"start_line": self.start_line,
"start_column": self.start_column,
"stop_line": self.stop_line,
"stop_column": self.stop_column,
}

def to_text(self) -> str:
path = click.style(str(self.path or "?"), fg="red")
return f"{path} {self.description}"
location = click.style(
f":{self.start_line}:{self.start_column}"
if (self.start_line is not None) and (self.start_column is not None)
else "",
fg="red",
)
return f"{path}{location} {self.description}"

def to_sarif(self) -> Dict[str, Any]:
return {
Expand All @@ -210,10 +239,18 @@ def to_sarif(self) -> Dict[str, Any]:
"uri": str(self.path) if self.path is not None else None,
},
"region": {
"startLine": 0,
"startColumn": 0,
"endLine": 0,
"endColumn": 1,
"startLine": self.start_line
if self.start_line is not None
else 0,
"startColumn": self.start_column
if self.start_column is not None
else 0,
"endLine": self.stop_line
if self.stop_line is not None
else 0,
"endColumn": self.stop_column
if self.stop_column is not None
else 1,
},
},
},
Expand Down
30 changes: 30 additions & 0 deletions client/tests/error_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,53 @@ def assert_not_parsed(json: Dict[str, Any]) -> None:
"path": "test.py",
"description": "Some description",
"code": 1001,
"location": None,
},
expected=TaintConfigurationError(
path=Path("test.py"),
description="Some description",
code=1001,
start_line=None,
start_column=None,
stop_line=None,
stop_column=None,
),
)
assert_parsed(
{
"path": None,
"description": "Some description",
"code": 1001,
"location": None,
},
expected=TaintConfigurationError(
path=None,
description="Some description",
code=1001,
start_line=None,
start_column=None,
stop_line=None,
stop_column=None,
),
)
assert_parsed(
{
"path": None,
"description": "Some description",
"code": 1001,
"location": {
"start": {"line": 1, "column": 2},
"stop": {"line": 3, "column": 4},
},
},
expected=TaintConfigurationError(
path=None,
description="Some description",
code=1001,
start_line=1,
start_column=2,
stop_line=3,
stop_column=4,
),
)

Expand Down

0 comments on commit 6b1d342

Please sign in to comment.