예제 #1
0
 def test_unexpectedError(self):
     """
     C{unexpectedError} reports an error processing a source file.
     """
     err = StringIO()
     reporter = Reporter(None, err)
     reporter.unexpectedError('source.py', 'error message')
     self.assertEquals('source.py: error message\n', err.getvalue())
예제 #2
0
 def test_unexpectedError(self):
     """
     C{unexpectedError} reports an error processing a source file.
     """
     err = StringIO()
     reporter = Reporter(None, err)
     reporter.unexpectedError('source.py', 'error message')
     self.assertEqual('source.py: error message\n', err.getvalue())
예제 #3
0
 def test_syntaxErrorNoOffset(self):
     """
     C{syntaxError} doesn't include a caret pointing to the error if
     C{offset} is passed as C{None}.
     """
     err = StringIO()
     reporter = Reporter(None, err)
     reporter.syntaxError("foo.py", "a problem", 3, None, "bad line of source")
     self.assertEqual(("foo.py:3: a problem\n" "bad line of source\n"), err.getvalue())
예제 #4
0
 def test_flake(self):
     """
     C{flake} reports a code warning from Pyflakes.  It is exactly the
     str() of a L{pyflakes.messages.Message}.
     """
     out = StringIO()
     reporter = Reporter(out, None)
     message = UnusedImport('foo.py', 42, 'bar')
     reporter.flake(message)
     self.assertEquals(out.getvalue(), "%s\n" % (message, ))
예제 #5
0
 def test_flake(self):
     """
     C{flake} reports a code warning from Pyflakes.  It is exactly the
     str() of a L{pyflakes.messages.Message}.
     """
     out = StringIO()
     reporter = Reporter(out, None)
     message = UnusedImport('foo.py', Node(42), 'bar')
     reporter.flake(message)
     self.assertEqual(out.getvalue(), "%s\n" % (message,))
예제 #6
0
 def test_multiLineSyntaxError(self):
     """
     If there's a multi-line syntax error, then we only report the last
     line.  The offset is adjusted so that it is relative to the start of
     the last line.
     """
     err = StringIO()
     lines = ["bad line of source", "more bad lines of source"]
     reporter = Reporter(None, err)
     reporter.syntaxError("foo.py", "a problem", 3, len(lines[0]) + 7, "\n".join(lines))
     self.assertEqual(("foo.py:3:7: a problem\n" + lines[-1] + "\n" + "      ^\n"), err.getvalue())
예제 #7
0
 def test_syntaxError(self):
     """
     C{syntaxError} reports that there was a syntax error in the source
     file.  It reports to the error stream and includes the filename, line
     number, error message, actual line of source and a caret pointing to
     where the error is.
     """
     err = StringIO()
     reporter = Reporter(None, err)
     reporter.syntaxError("foo.py", "a problem", 3, 7, "bad line of source")
     self.assertEqual(("foo.py:3:8: a problem\n" "bad line of source\n" "       ^\n"), err.getvalue())
예제 #8
0
 def test_syntaxErrorNoOffset(self):
     """
     C{syntaxError} doesn't include a caret pointing to the error if
     C{offset} is passed as C{None}.
     """
     err = StringIO()
     reporter = Reporter(None, err)
     reporter.syntaxError('foo.py', 'a problem', 3, None,
                          'bad line of source')
     self.assertEquals(("foo.py:3: a problem\n"
                        "bad line of source\n"), err.getvalue())
예제 #9
0
 def test_syntaxError(self):
     """
     C{syntaxError} reports that there was a syntax error in the source
     file.  It reports to the error stream and includes the filename, line
     number, error message, actual line of source and a caret pointing to
     where the error is.
     """
     err = StringIO()
     reporter = Reporter(None, err)
     reporter.syntaxError('foo.py', 'a problem', 3, 4, 'bad line of source')
     self.assertEquals(("foo.py:3: a problem\n"
                        "bad line of source\n"
                        "     ^\n"), err.getvalue())
예제 #10
0
 def test_multiLineSyntaxError(self):
     """
     If there's a multi-line syntax error, then we only report the last
     line.  The offset is adjusted so that it is relative to the start of
     the last line.
     """
     err = StringIO()
     lines = ["bad line of source", "more bad lines of source"]
     reporter = Reporter(None, err)
     reporter.syntaxError("foo.py", "a problem", 3,
                          len(lines[0]) + 7, "\n".join(lines))
     self.assertEqual(
         ("foo.py:3:7: a problem\n" + lines[-1] + "\n" + "      ^\n"),
         err.getvalue(),
     )
예제 #11
0
def check_source(nb, filename):
    """
    Run pyflakes on a notebook, wil catch errors such as missing passed
    parameters that do not have default values
    """
    from pyflakes.api import check as pyflakes_check
    from pyflakes.reporter import Reporter

    # concatenate all cell's source code in a single string
    source = '\n'.join([c['source'] for c in nb.cells])

    # this objects are needed to capture pyflakes output
    warn = StringIO()
    err = StringIO()
    reporter = Reporter(warn, err)

    # run pyflakes.api.check on the source code
    pyflakes_check(source, filename=filename, reporter=reporter)

    warn.seek(0)
    err.seek(0)

    # return any error messages returned by pyflakes
    return {
        'warnings': '\n'.join(warn.readlines()),
        'errors': '\n'.join(err.readlines())
    }
예제 #12
0
def test_snippets(snippet):
    snippet.log_action(action="testing")
    pyflakes_stream = StringIO()
    if check(snippet.source, "", Reporter(pyflakes_stream, pyflakes_stream)):
        logger.error(f"Pyflakes error: {pyflakes_stream.getvalue()}")
        pytest.fail()
    steadymark.Runner(text=f"```python\n{snippet.source}\n```").run()
예제 #13
0
 def test_multiLineSyntaxError(self):
     """
     If there's a multi-line syntax error, then we only report the last
     line.  The offset is adjusted so that it is relative to the start of
     the last line.
     """
     err = StringIO()
     lines = [
         'bad line of source',
         'more bad lines of source',
     ]
     reporter = Reporter(None, err)
     reporter.syntaxError('foo.py', 'a problem', 3,
                          len(lines[0]) + 7, '\n'.join(lines))
     column = 25 if sys.version_info >= (3, 8) else 7
     self.assertEqual(
         ("foo.py:3:%d: a problem\n" % column + lines[-1] + "\n" + " " *
          (column - 1) + "^\n"), err.getvalue())
예제 #14
0
 def test_pyflakes(self):
     files = [
         GETTEXT_PY_FILE,
         THIS_FILE,
     ]
     out = io.StringIO()
     reporter = Reporter(out, out)
     errors = sum(map(lambda f: api.checkPath(f, reporter), files))
     self.assertEqual(errors, 0, '\n' + out.getvalue())
예제 #15
0
def check_code(script):
    """Run pyflakes checks over the script and capture warnings/errors."""
    errors = io.StringIO()
    warnings = io.StringIO()
    reporter = Reporter(warnings, errors)
    check(script, "notebook", reporter)

    warnings.seek(0)
    errors.seek(0)

    return warnings, errors
예제 #16
0
 def test_multiLineSyntaxError(self):
     """
     If there's a multi-line syntax error, then we only report the last
     line.  The offset is adjusted so that it is relative to the start of
     the last line.
     """
     err = StringIO()
     lines = [
         'bad line of source',
         'more bad lines of source',
     ]
     reporter = Reporter(None, err)
     reporter.syntaxError('foo.py', 'a problem', 3, len(lines[0]) + 7,
                          '\n'.join(lines))
     column = 25 if sys.version_info >= (3, 8) else 7
     self.assertEqual(
         ("foo.py:3:%d: a problem\n" % column +
          lines[-1] + "\n" +
          " " * (column - 1) + "^\n"),
         err.getvalue())
예제 #17
0
def test_pyflakes_conformance():
    """Test source code for PyFlakes conformance."""
    reporter = Reporter(sys.stdout, sys.stderr)
    base_path = os.path.join(os.path.dirname(__file__), '..')
    paths = [
        os.path.join(base_path, 'ros_buildfarm'),
        os.path.join(base_path, 'scripts'),
    ]
    warning_count = checkRecursive(paths, reporter)
    assert warning_count == 0, \
        'Found %d code style warnings' % warning_count
예제 #18
0
def _get_error_density(_file_path: str) -> Union[float, bool]:
    """ Get standard compliance for a single file. """

    _output_handler = StringIO()
    _error_handler = StringIO()
    _pyflake_reporter = Reporter(_output_handler, _error_handler)


    checkPath(_file_path, _pyflake_reporter)

    _error_handler.close()
    _error_handler = None

    _error_string = _output_handler.getvalue()
    # We will just have an empty string 
    # if pyflakes outputs nothing i.e.
    # if we have no errors.
    if _error_string:
        # We use strip to not get empty strings
        # at the start or end
        # when we split the lines on \n.
        _error_lines = _error_string.strip().split('\n')
        _errors = len(_error_lines)
    else:
        return float(0)

    # Clean up space
    _output_handler.close()
    _output_handler = None
    _error_lines = None
    _pyflake_reporter = None


    if _errors is None:
        return None

    _file = open(_file_path, 'r', encoding='utf-8', errors='ignore')

    _line_number = 0
    for _line in _file.readlines():
        if re.match(r'^\s*$', _line):
            continue
        _line_number = _line_number + 1

    _file.close()

    if _line_number == 0:
        return False

    _error_density = float(_errors / _line_number)
    # We have calculated the percentage of non-compliant lines,
    # we want the percentage of the compliant lines.
    return _error_density
예제 #19
0
 def test_syntaxError(self):
     """
     C{syntaxError} reports that there was a syntax error in the source
     file.  It reports to the error stream and includes the filename, line
     number, error message, actual line of source and a caret pointing to
     where the error is.
     """
     err = StringIO()
     reporter = Reporter(None, err)
     reporter.syntaxError(
         "foo.py",
         "a problem",
         3,
         8 if sys.version_info >= (3, 8) else 7,
         "bad line of source",
     )
     self.assertEqual(
         ("foo.py:3:8: a problem\n"
          "bad line of source\n"
          "       ^\n"),
         err.getvalue(),
     )
예제 #20
0
def pyflakes(packages):
    """
    Unfortunately, pyflakes does not have a way to suppress certain errors or
    a way to configure the checker class, so we have to monkey patch it.

    Returns number of warnings
    """
    Checker.___init___ = Checker.__init__
    Checker.__init__ = _pyflakes_no_migrations
    Checker.report = _pyflakes_report_with_nopyflakes
    reporter = Reporter(sys.stdout, sys.stderr)
    paths = [os.path.dirname(package.__file__) for package in packages]
    return _check_recursive(paths, reporter)
예제 #21
0
def publish_diagnostic(source: str,
                       file_name=None) -> Tuple[WarningMsg, ErrorMsg]:
    file_name = file_name or "<stdin>"

    warning_buffer = StringIO()
    error_buffer = StringIO()
    reporter = Reporter(warning_buffer, error_buffer)

    check(source, file_name, reporter)
    LOGGER.debug("warning message: \n%s", warning_buffer.getvalue())
    LOGGER.debug("error message: \n%s", error_buffer.getvalue())

    return (warning_buffer.getvalue(), error_buffer.getvalue())
예제 #22
0
def pyflakes(ticket, **kwds):
    """
    run pyflakes on the modified .py files

    we do not check the files names "all.py" and "__init__.py" that
    usually just contain unused import lines, always triggering non-pertinent
    pyflakes warnings

    same thing for files named "*catalog*.py"
    """
    changed_files = list(
        subprocess.Popen([
            'git', 'diff', '--name-only',
            'patchbot/base..patchbot/ticket_merged'
        ],
                         stdout=subprocess.PIPE).stdout)
    changed_files = [f.decode('utf8').strip("\n") for f in changed_files]

    errors = 0
    msg_list = []
    msg_here = '{} pyflakes errors in file {}'
    for a_file in changed_files:
        if os.path.exists(a_file) and isPythonFile(a_file):
            filename = os.path.split(a_file)[1]
            if not (filename == "all.py" or filename == "__init__.py"
                    or "catalog" in filename):
                error_stream = io.StringIO()
                report = Reporter(error_stream, sys.stderr)
                errors_here = checkPath(a_file, report)  # run pyflakes
                if errors_here:
                    # here we try to care for lazy imported names
                    lazys = list(find_lazy_imports(a_file))  # ok
                    if lazys:
                        for err in error_stream.getvalue().splitlines():
                            if any(x in err.split(' ')[-1] for x in lazys):
                                errors_here -= 1
                            else:
                                print(err)
                    else:
                        print(error_stream.getvalue())
                error_stream.close()
                if errors_here:
                    errors += errors_here
                    msg_list.append(msg_here.format(errors_here, a_file))

    full_msg = "found {} pyflakes errors in the modified files"
    full_msg = full_msg.format(errors)
    print(full_msg)
    if errors:
        raise ValueError(full_msg)
예제 #23
0
def default_reporter():
    warnings = io.StringIO()
    errors = io.StringIO()
    return Reporter(warnings, errors)
예제 #24
0
 def test_pyflakes_syntax(self) -> None:
     rootPath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
     pyflakes.api.checkRecursive([rootPath + '/qctoolkit/', rootPath + '/tests/'], Reporter(sys.stdout, sys.stderr))
예제 #25
0
 def test_pyflakes(self):
     rep = Reporter(sys.stdout, sys.stderr)
     error_count = checkRecursive(CHECK_DIRECTORYS, rep)
     self.assertEqual(error_count, 0, 'PyFlakes errors: %d' % error_count)
예제 #26
0
"""