def parse_configuration_and_cli(self, argv=None): # type: (Optional[List[str]]) -> None """Parse configuration files and the CLI options. :param list argv: Command-line arguments passed in directly. """ if self.options is None and self.args is None: self.options, self.args = aggregator.aggregate_options( self.option_manager, self.config_finder, argv ) self.running_against_diff = self.options.diff if self.running_against_diff: self.parsed_diff = utils.parse_unified_diff() if not self.parsed_diff: self.exit() self.options._running_from_vcs = False self.check_plugins.provide_options( self.option_manager, self.options, self.args ) self.formatting_plugins.provide_options( self.option_manager, self.options, self.args )
def parse_configuration_and_cli( self, config_finder: config.ConfigFileFinder, argv: List[str], ) -> None: """Parse configuration files and the CLI options. :param config.ConfigFileFinder config_finder: The finder for finding and reading configuration files. :param list argv: Command-line arguments passed in directly. """ self.options, self.args = aggregator.aggregate_options( self.option_manager, config_finder, argv, ) self.running_against_diff = self.options.diff if self.running_against_diff: self.parsed_diff = utils.parse_unified_diff() if not self.parsed_diff: self.exit() assert self.check_plugins is not None self.check_plugins.provide_options(self.option_manager, self.options, self.args) assert self.formatting_plugins is not None self.formatting_plugins.provide_options(self.option_manager, self.options, self.args)
def parse_configuration_and_cli_nightly( self, config_finder: config.ConfigFileFinder, argv: List[str]) -> None: """ Compat version of self.parse_configuration_and_cli to work with nightly build of flake8 master https://gitlab.com/pycqa/flake8/blob/master/src/flake8/main/application.py#L194 Parse configuration files and the CLI options. :param config.ConfigFileFinder config_finder: The finder for finding and reading configuration files. :param list argv: Command-line arguments passed in directly. """ self.options, self.args = aggregator.aggregate_options( self.option_manager, config_finder, argv, ) self.args = self.hack_args(self.args, self.options.exclude) self.running_against_diff = self.options.diff if self.running_against_diff: # pragma: no cover self.parsed_diff = utils.parse_unified_diff() if not self.parsed_diff: self.exit() self.options._running_from_vcs = False self.check_plugins.provide_options(self.option_manager, self.options, self.args) self.formatting_plugins.provide_options(self.option_manager, self.options, self.args)
def parse_configuration_and_cli(self, argv=None): # type: (Union[NoneType, List[str]]) -> NoneType """Parse configuration files and the CLI options. :param list argv: Command-line arguments passed in directly. """ if self.options is None and self.args is None: self.options, self.args = aggregator.aggregate_options( self.option_manager, argv ) self.running_against_diff = self.options.diff if self.running_against_diff: self.parsed_diff = utils.parse_unified_diff() if not self.parsed_diff: self.exit() self.options._running_from_vcs = False self.check_plugins.provide_options(self.option_manager, self.options, self.args) self.listening_plugins.provide_options(self.option_manager, self.options, self.args) self.formatting_plugins.provide_options(self.option_manager, self.options, self.args)
def parse_configuration_and_cli(self, argv: Optional[List[str]] = None) -> None: """ Parse configuration files and the CLI options. Parameters ---------- argv: List[str] Command-line arguments passed in directly. """ if self.options is None and self.args is None: # pragma: no branch self.options, self.args = aggregator.aggregate_options( self.option_manager, self.config_finder, argv) self.args = self.hack_args(self.args, self.options.exclude) self.running_against_diff = self.options.diff if self.running_against_diff: # pragma: no cover self.parsed_diff = utils.parse_unified_diff() if not self.parsed_diff: self.exit() self.options._running_from_vcs = False self.check_plugins.provide_options(self.option_manager, self.options, self.args) self.formatting_plugins.provide_options(self.option_manager, self.options, self.args)
def __init__(self, tree, total_lines, filename): self.total_lines = total_lines self.filename = Path(filename) self.last_changed_lines = { Path(f): max(changed_lines) for f, changed_lines in parse_unified_diff().items() if changed_lines }
def test_diff(capsys, tmp_path: Path): text = """ [tool.flakeheaven.plugins] pyflakes = ["+*"] [tool.flakeheaven.exceptions."tests/"] pyflakes = ["-F401"] """ (tmp_path / 'pyproject.toml').write_text(dedent(text)) (tmp_path / 'example.py').write_text('import sys\na') (tmp_path / 'tests').mkdir() (tmp_path / 'tests' / 'test_example.py').write_text('import sys\na') diff = """ --- a/tests/test_example.py +++ b/tests/test_example.py @@ -1,1 +1,2 @@ class FlakeHeavenCheckersManager(Manager): - . + import sys + a --- a/example.py +++ b/example.py @@ -1,1 +1,2 @@ class FlakeHeavenCheckersManager(Manager): - . + import sys + a """ diff = dedent(diff) exp = {'tests/test_example.py': {1, 2}, 'example.py': {1, 2}} assert dict(parse_unified_diff(diff)) == exp stdin_get_value.cache_clear() stream = BytesIO(diff.encode()) with patch('sys.stdin.buffer', stream): with chdir(tmp_path): result = main( ['lint', '--format', 'default', '--diff', 'example.py']) assert result == (1, '') captured = capsys.readouterr() assert captured.err == '' exp = """ example.py:1:1: F401 'sys' imported but unused example.py:2:1: F821 undefined name 'a' """ assert captured.out.strip() == dedent(exp).strip()
def test_parse_unified_diff(diff, parsed_diff): """Verify that what we parse from a diff matches expectations.""" assert utils.parse_unified_diff(diff) == parsed_diff
def parse_configuration_and_cli(self, config_finder, argv: List[str]) -> None: parser = self.option_manager.parser for action in parser._actions.copy(): if not action.option_strings: continue name = action.option_strings[-1] if name not in UNSUPPORTED: continue parser._handle_conflict_resolve(None, [(name, action)]) # if passed `--config` with path to TOML-config, we should extract it # before passing into flake8 mechanisms config_path, argv = self.extract_toml_config_path(argv=argv) # make default config config, _ = self.option_manager.parse_args([]) config.__dict__.update(DEFAULTS) # patch config wtih TOML # If config is explicilty passed, it will be used # If config isn't specified, flakehell will lookup for it config.__dict__.update(self.get_toml_config(config_path)) # Parse CLI options and legacy flake8 configs. # Based on `aggregate_options`. config_parser = MergedConfigParser( option_manager=self.option_manager, config_finder=config_finder, ) parsed_config = config_parser.parse() config.extended_default_select = self.option_manager.extended_default_select.copy( ) for config_name, value in parsed_config.items(): dest_name = config_name # If the config name is somehow different from the destination name, # fetch the destination name from our Option if not hasattr(config, config_name): dest_name = config_parser.config_options[config_name].dest setattr(config, dest_name, value) self.options, self.args = self.option_manager.parse_args( args=argv, values=config, ) if not hasattr(self.options, 'extended_default_ignore'): setattr(self.options, 'extended_default_ignore', set()) # All this goes from the original `parse_configuration_and_cli`. # We can't call `super` anymore because all `Application` methods # redefine everything. self.running_against_diff = self.options.diff if self.running_against_diff: self.parsed_diff = parse_unified_diff() if not self.parsed_diff: self.exit() self.options._running_from_vcs = False self.check_plugins.provide_options( optmanager=self.option_manager, options=self.options, extra_args=self.args, ) self.formatting_plugins.provide_options( optmanager=self.option_manager, options=self.options, extra_args=self.args, )