Ejemplo n.º 1
0
 def defaults_to_80x24_when_stdout_not_a_tty(self, ioctl, stdout):
     # Make sure stdout acts like a real stream (means failure is
     # more obvious)
     stdout.fileno.return_value = 1
     # Ensure it fails the isatty() test too
     stdout.isatty.return_value = False
     # Test
     assert pty_size() == (80, 24)
Ejemplo n.º 2
0
 def defaults_to_80x24_when_stdout_not_a_tty(self, ioctl, stdout):
     # Make sure stdout acts like a real stream (means failure is
     # more obvious)
     stdout.fileno.return_value = 1
     # Ensure it fails the isatty() test too
     stdout.isatty.return_value = False
     # Test
     assert pty_size() == (80, 24)
Ejemplo n.º 3
0
def lint_example_notebooks(c, linter="flake8"):
    """Lint notebooks in the `./examples` directory.

    Supports flake8 and black linters.

    :param linter: The linter to validate the notebooks with.
                   Can be one of: ["flake8", "black"]
    :type linter: str
    """
    to_script_command = (
        "jupyter nbconvert {} --stdout --to python "
        "--RegexRemovePreprocessor.patterns=\"[r'\\s*\\Z']\" "  # remove empty code cells
        "--Exporter.preprocessors=\"['strip_magics.StripMagicsProcessor']\" "  # remove ipython magics
        "--template=code_cells_only.tpl "  # only lint code cells
        "| head -c -1"  # remove extra new-line at end
    )
    linter_commands = {
        "black": "black --check --diff -",
        # last 3 are to ignore trailing whitespace, rest are from tox.ini
        # should simplify this once flake8 fixes its --extend-ignore option
        "flake8": "flake8 - --show-source --ignore=E203,E501,W503,W391,W291,E402",
    }

    try:
        linter_command = linter_commands[linter]
    except KeyError:
        raise ValueError(f"Linter, {linter}, not supported!")

    nb_paths = pathlib.Path("./examples").glob("**/*[!checkpoint].ipynb")
    num_fails = 0
    failed_files = []

    for path in nb_paths:
        with c.cd("./.build_tools/invoke/"):
            run = c.run(
                to_script_command.format("../../" + str(path)) + " | " + linter_command,
                warn=True,  # don't exit task on first fail
                echo=True,  # print generated bash command to stdout
            )
            if run.failed:
                num_fails += 1
                failed_files.append(str(path))

    if num_fails > 0:
        failed_files = sorted(failed_files)
        print(
            "-" * pty_size()[0]
            + "\nSome notebook code is improperly formatted.\n"
            + f"Number of unformatted files reported: {num_fails}\n"
            + "Files with errors:\n{}".format(pformat(failed_files))
        )
        sys.exit(num_fails)
Ejemplo n.º 4
0
 def uses_default_when_stdout_triggers_ioctl_error(self, ioctl, stdout):
     ioctl.side_effect = TypeError
     assert pty_size() == (80, 24)
Ejemplo n.º 5
0
 def uses_default_when_stdout_lacks_fileno(self, ioctl, stdout):
     # i.e. when accessing it throws AttributeError
     stdout.fileno.side_effect = AttributeError
     assert pty_size() == (80, 24)
Ejemplo n.º 6
0
 def calls_fcntl_with_TIOCGWINSZ(self, ioctl):
     # Test the default (Unix) implementation because that's all we
     # can realistically do here.
     pty_size()
     assert ioctl.call_args_list[0][0][1] == termios.TIOCGWINSZ
Ejemplo n.º 7
0
 def uses_default_when_stdout_triggers_ioctl_error(
     self, ioctl, stdout
 ):
     ioctl.side_effect = TypeError
     assert pty_size() == (80, 24)
Ejemplo n.º 8
0
 def uses_default_when_stdout_lacks_fileno(self, ioctl, stdout):
     # i.e. when accessing it throws AttributeError
     stdout.fileno.side_effect = AttributeError
     assert pty_size() == (80, 24)
Ejemplo n.º 9
0
 def calls_fcntl_with_TIOCGWINSZ(self, ioctl):
     # Test the default (Unix) implementation because that's all we
     # can realistically do here.
     pty_size()
     assert ioctl.call_args_list[0][0][1] == termios.TIOCGWINSZ