Example #1
0
 def lint_path(
     self,
     path: str,
     fix: bool = False,
     ignore_non_existent_files: bool = False,
     ignore_files: bool = True,
     processes: int = 1,
 ) -> LintedDir:
     """Lint a path."""
     linted_path = LintedDir(path)
     if self.formatter:
         self.formatter.dispatch_path(path)
     fnames = list(
         self.paths_from_path(
             path,
             ignore_non_existent_files=ignore_non_existent_files,
             ignore_files=ignore_files,
         )
     )
     runner = get_runner(
         self,
         self.config,
         processes=processes,
         allow_process_parallelism=self.allow_process_parallelism,
     )
     for linted_file in runner.run(fnames, fix):
         linted_path.add(linted_file)
         # If any fatal errors, then stop iteration.
         if any(v.fatal for v in linted_file.violations):  # pragma: no cover
             linter_logger.error("Fatal linting error. Halting further linting.")
             break
     return linted_path
Example #2
0
    def lint_path(
        self,
        path: str,
        fix: bool = False,
        ignore_non_existent_files: bool = False,
        ignore_files: bool = True,
        processes: int = 1,
    ) -> LintedDir:
        """Lint a path."""
        linted_path = LintedDir(path)
        if self.formatter:
            self.formatter.dispatch_path(path)
        fnames = list(
            self.paths_from_path(
                path,
                ignore_non_existent_files=ignore_non_existent_files,
                ignore_files=ignore_files,
            ))

        # to avoid circular import
        from sqlfluff.core.linter.runner import get_runner

        runner = get_runner(
            self,
            self.config,
            processes=processes,
            allow_process_parallelism=self.allow_process_parallelism,
        )

        # Show files progress bar only when there is more than one.
        files_count = len(fnames)
        progress_bar_files = tqdm(
            total=files_count,
            desc=f"file {os.path.basename(fnames[0] if fnames else '')}",
            leave=False,
            disable=files_count <= 1
            or progress_bar_configuration.disable_progress_bar,
        )

        for i, linted_file in enumerate(runner.run(fnames, fix), start=1):
            linted_path.add(linted_file)
            # If any fatal errors, then stop iteration.
            if any(v.fatal
                   for v in linted_file.violations):  # pragma: no cover
                linter_logger.error(
                    "Fatal linting error. Halting further linting.")
                break

            # Progress bar for files is rendered only when there is more than one file.
            # Additionally as it's updated after each loop, we need to get file name
            # from the next loop. This is why `enumerate` starts with `1` and there
            # is `i < len` to not exceed files list length.
            progress_bar_files.update(n=1)
            if i < len(fnames):
                progress_bar_files.set_description(
                    f"file {os.path.basename(fnames[i])}")

        return linted_path
Example #3
0
def test__linter__get_runner_processes(patched_cpu_count, mock_cpu,
                                       in_processes, exp_processes):
    """Test that get_runner handles processes correctly."""
    # Make the mocked cpu count a really high value which is
    # unlikely to collide with the real value.
    patched_cpu_count.return_value = mock_cpu
    _, return_processes = get_runner(
        linter=Linter(),
        config=FluffConfig(overrides={"dialect": "ansi"}),
        processes=in_processes,
    )
    assert return_processes == exp_processes