コード例 #1
0
 def into_semgrep_error(self) -> SemgrepError:
     if self._check_id == "Timeout":
         return MatchTimeoutError(self._path, self._rule_id)
     elif self._check_id == "OutOfMemory":
         return OutOfMemoryError(self._path, self._rule_id)
     elif self._check_id == "LexicalError":
         return LexicalError(self._path, self._rule_id)
     else:
         try:
             with open(self._path, errors="replace") as f:
                 file_hash = SourceTracker.add_source(f.read())
         except IOError as e:
             return SemgrepError(f"Could not open '{self._path}': {e}")
         error_span = Span(
             start=self._start,
             end=self._end,
             source_hash=file_hash,
             file=str(self._path),
         )
         return SourceParseError(
             short_msg="parse error",
             long_msg=f"Could not parse {self._path.name} as {self._language}",
             spans=[error_span],
             help="If the code appears to be valid, this may be a semgrep bug.",
         )
コード例 #2
0
 def into_semgrep_error(self) -> SemgrepError:
     with open(self._path) as f:
         file_hash = SourceTracker.add_source(f.read())
     error_span = Span(
         start=self._start,
         end=self._end,
         source_hash=file_hash,
         file=self._path.name,
     )
     return SourceParseError(
         short_msg="parse error",
         long_msg=f"Could not parse {self._path.name} as {self._language}",
         spans=[error_span],
         help="If the code appears to be valid, this may be a semgrep bug.",
     )
コード例 #3
0
ファイル: test_error.py プロジェクト: wisdomSir/semgrep
def test_same_hash():
    # SemgrepErrors with all same fields have the same hash
    error_1 = SourceParseError(
        short_msg="1",
        long_msg="2",
        spans=[],
        help="4",
    )

    error_2 = SourceParseError(
        short_msg="1",
        long_msg="2",
        spans=[],
        help="4",
    )

    assert error_1.__hash__() == error_2.__hash__()

    errors = set()
    errors.add(error_1)
    assert error_2 in errors
コード例 #4
0
ファイル: test_error.py プロジェクト: wisdomSir/semgrep
def test_different_hash():
    # SemgrepErrors with differing fields have different hash
    error_1 = SourceParseError(
        short_msg="1",
        long_msg="2",
        spans=[],
        help="4",
    )

    error_2 = SourceParseError(
        short_msg="not 1",
        long_msg="2",
        spans=[],
        help="4",
    )

    assert error_1.__hash__() != error_2.__hash__()

    errors = set()
    errors.add(error_1)
    assert error_2 not in errors