Skip to content

Commit

Permalink
Add cpython repository into primer runs
Browse files Browse the repository at this point in the history
- cpython tests is probably the best repo for black to test on as the stdlib's unittests should use all syntax
  - Limit to running in recent versions of the python runtime - e.g. today >= 3.9
    - This allows us to parse more syntax
- Exclude all failing files for now
  - Definately have bugs to explore there - Refer to #2407 for more details there
  - Some test files on purpose have syntax errors, so we will never be able to parse them
- Add new black command arguments logging in debug mode; very handy for seeing how CLI arguments are formatted

cython now succeeds ignoring 16 files:
```
Oh no! 💥 💔 💥
1859 files would be reformatted, 148 files would be left unchanged.
```

Testing
- Ran locally with and without string processing - Very little runtime difference BUT 3 more failed files
```
time /tmp/tb/bin/black --experimental-string-processing --check . 2>&1 | tee /tmp/black_cpython_esp
...
Oh no! 💥 💔 💥
1859 files would be reformatted, 148 files would be left unchanged, 16 files would fail to reformat.

real	4m8.563s
user	16m21.735s
sys	0m6.000s
```
- Add unittest for new covienence config file flattening that allows long arguments to be broke up into an array/list of strings

Addresses #2407
  • Loading branch information
cooperlees committed Aug 15, 2021
1 parent e465acf commit e854fab
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/black_primer/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@
from subprocess import CalledProcessError
from sys import version_info
from tempfile import TemporaryDirectory
from typing import Any, Callable, Dict, NamedTuple, Optional, Sequence, Tuple
from typing import (
Any,
Callable,
Dict,
List,
NamedTuple,
Optional,
Sequence,
Tuple,
Union,
)
from urllib.parse import urlparse

import click
Expand Down Expand Up @@ -113,6 +123,23 @@ def analyze_results(project_count: int, results: Results) -> int:
return results.stats["failed"]


def _flatten_cli_args(cli_args: List[Union[Sequence[str], str]]) -> List[str]:
"""Allow a user to put long arguments into a list of strs
to make the JSON human readable"""
flat_args = []
for arg in cli_args:
if isinstance(arg, str):
flat_args.append(arg)
continue

new_args_str = ""
for arg_str in arg:
new_args_str += arg_str
flat_args.append(new_args_str)

return flat_args


async def black_run(
project_name: str,
repo_path: Optional[Path],
Expand All @@ -131,7 +158,7 @@ async def black_run(
stdin_test = project_name.upper() == "STDIN"
cmd = [str(which(BLACK_BINARY))]
if "cli_arguments" in project_config and project_config["cli_arguments"]:
cmd.extend(project_config["cli_arguments"])
cmd.extend(_flatten_cli_args(project_config["cli_arguments"]))
cmd.append("--check")
if not no_diff:
cmd.append("--diff")
Expand All @@ -154,6 +181,7 @@ async def black_run(

cwd_path = repo_path.parent if stdin_test else repo_path
try:
LOG.debug(f"Running black for {project_name}: {' '.join(cmd)}")
_stdout, _stderr = await _gen_check_output(
cmd, cwd=cwd_path, env=env, stdin=stdin
)
Expand Down
28 changes: 28 additions & 0 deletions src/black_primer/primer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@
"long_checkout": false,
"py_versions": ["all"]
},
"cpython": {
"cli_arguments": [
"--experimental-string-processing",
"--extend-exclude",
[
"Lib/lib2to3/tests/data/different_encoding.py",
"|Lib/lib2to3/tests/data/false_encoding.py",
"|Lib/lib2to3/tests/data/py2_test_grammar.py",
"|Lib/test/bad_coding.py",
"|Lib/test/bad_coding2.py",
"|Lib/test/badsyntax_3131.py",
"|Lib/test/badsyntax_pep3120.py",
"|Lib/test/test_base64.py",
"|Lib/test/test_exceptions.py",
"|Lib/test/test_grammar.py",
"|Lib/test/test_named_expressions.py",
"|Lib/test/test_patma.py",
"|Lib/test/test_tokenize.py",
"|Lib/test/test_xml_etree.py",
"|Lib/traceback.py",
"|Tools/c-analyzer/c_parser/parser/_delim.py"
]
],
"expect_formatting_changes": true,
"git_clone_url": "https://github.com/python/cpython.git",
"long_checkout": false,
"py_versions": ["3.9", "3.10"]
},
"django": {
"cli_arguments": [
"--experimental-string-processing",
Expand Down
7 changes: 7 additions & 0 deletions tests/test_primer.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ def test_black_run(self) -> None:
)
self.assertEqual(2, results.stats["failed"])

def test_flatten_cli_args(self) -> None:
fake_long_args = ["--arg", ["really/", "|long", "|regex", "|splitup"], "--done"]
expected = ["--arg", "really/|long|regex|splitup", "--done"]
self.assertEqual(expected, lib._flatten_cli_args(fake_long_args))

@event_loop()
def test_gen_check_output(self) -> None:
loop = asyncio.get_event_loop()
Expand Down Expand Up @@ -184,6 +189,8 @@ def test_git_checkout_or_rebase(self) -> None:
@patch("sys.stdout", new_callable=StringIO)
@event_loop()
def test_process_queue(self, mock_stdout: Mock) -> None:
"""Test the process queue on primer itself
- If you have non black conforming formatting in primer itself this can fail"""
loop = asyncio.get_event_loop()
config_path = Path(lib.__file__).parent / "primer.json"
with patch("black_primer.lib.git_checkout_or_rebase", return_false):
Expand Down

0 comments on commit e854fab

Please sign in to comment.