From a36b4cad7240badb3dbd1715fc9cd45f641c1eec Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Sun, 26 Mar 2023 19:38:31 +0100 Subject: [PATCH] use ruff config args when possible --- pytest_examples/config.py | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/pytest_examples/config.py b/pytest_examples/config.py index 2bfb91e..6e4d0d0 100644 --- a/pytest_examples/config.py +++ b/pytest_examples/config.py @@ -41,23 +41,10 @@ def hash(self) -> str: return hashlib.md5(str(self).encode()).hexdigest() def ruff_config(self) -> tuple[str, ...]: - ruff_toml = self._to_ruff_toml() - - if ruff_toml is None: - # if there's no custom config, prevent ruff using a local config - return ('--isolated',) - - config_file = Path(tempfile.gettempdir()) / 'pytest-examples-ruff-config' / self.hash() / 'ruff.toml' - if not config_file.exists(): - config_file.parent.mkdir(parents=True, exist_ok=True) - config_file.write_text(ruff_toml) - - return '--config', str(config_file) - - def _to_ruff_toml(self) -> str | None: config_lines = [] select = [] ignore = [] + args = [] # line length is enforced by black if self.ruff_line_length is None: @@ -65,7 +52,7 @@ def _to_ruff_toml(self) -> str | None: # by default, ruff sets the line length to 88 ignore.append('E501') else: - config_lines.append(f'line-length = {self.ruff_line_length}') + args.append(f'--line-length={self.ruff_line_length}') if self.quotes == 'single': # enforce single quotes using ruff, black will enforce double quotes @@ -73,7 +60,7 @@ def _to_ruff_toml(self) -> str | None: config_lines.append("flake8-quotes = {inline-quotes = 'single', multiline-quotes = 'double'}") if self.target_version: - config_lines.append(f'target-version = "{self.target_version}"') + args.append(f'--target-version={self.target_version}') if self.upgrade: select.append('UP') @@ -84,9 +71,18 @@ def _to_ruff_toml(self) -> str | None: ignore.extend(self.ruff_ignore) if select: - config_lines.append(f'select = {select}') + # use extend to not disable default select + args.append(f'--extend-select={",".join(select)}') if ignore: - config_lines.append(f'ignore = {ignore}') + args.append(f'--ignore={",".join(ignore)}') if config_lines: - return '\n'.join(config_lines) + config_toml = '\n'.join(config_lines) + config_file = Path(tempfile.gettempdir()) / 'pytest-examples-ruff-config' / self.hash() / 'ruff.toml' + if not config_file.exists() or config_file.read_text() != config_toml: + config_file.parent.mkdir(parents=True, exist_ok=True) + config_file.write_text(config_toml) + + args.append(f'--config={config_file}') + + return tuple(args)