Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to require a specific version to be running #2300

Merged
merged 3 commits into from Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -6,6 +6,7 @@

- Correct max string length calculation when there are string operators (#2292)
- Fixed option usage when using the `--code` flag (#2259)
- Add `--revision` option to require a specific version to be running (#2300)

## 21.5b2

Expand Down
15 changes: 14 additions & 1 deletion docs/usage_and_configuration/the_basics.md
Expand Up @@ -167,7 +167,7 @@ $ black src/ -q
error: cannot format src/black_primer/cli.py: Cannot parse: 5:6: mport asyncio
```

### Getting the version
### Versions

You can check the version of _Black_ you have installed using the `--version` flag.

Expand All @@ -176,6 +176,19 @@ $ black --version
black, version 21.5b0
```

An option to requiring a specific version to be running is also provided.
felix-hilden marked this conversation as resolved.
Show resolved Hide resolved

```console
$ black --revision 21.5b2 -c "format = 'this'"
format = "this"
$ black --revision 31.5b2 -c "still = 'beta?!'"
Oh no! 💥 💔 💥 The required revision does not match the running version!
```

This is useful for example when running _Black_ in multiple environments that haven't
necessarily installed the correct version. Setting this option can be done in a
felix-hilden marked this conversation as resolved.
Show resolved Hide resolved
configuration file for consistent results across environments.

## Configuration via a file

_Black_ is able to read project-specific default values for its command line options
Expand Down
24 changes: 22 additions & 2 deletions src/black/__init__.py
Expand Up @@ -249,6 +249,14 @@ def validate_regex(
is_flag=True,
help="If --fast given, skip temporary sanity checks. [default: --safe]",
)
@click.option(
"--revision",
type=str,
help=(
"Require a specific version of Black to be running (useful for unifying results"
" across many environments e.g. with a pyproject.toml file)."
),
)
@click.option(
"--include",
type=str,
Expand Down Expand Up @@ -359,6 +367,7 @@ def main(
experimental_string_processing: bool,
quiet: bool,
verbose: bool,
revision: str,
include: Pattern,
exclude: Optional[Pattern],
extend_exclude: Optional[Pattern],
Expand All @@ -368,6 +377,17 @@ def main(
config: Optional[str],
) -> None:
"""The uncompromising code formatter."""
error_msg = "Oh no! 💥 💔 💥"
if revision and revision != __version__:
msg = (
f"{error_msg} The required revision `{revision}` does not match"
f" the running version `{__version__}`!"
felix-hilden marked this conversation as resolved.
Show resolved Hide resolved
)
if ctx.default_map and revision == ctx.default_map.get("revision", None):
msg += f"\n(configuration read from `{config}`)"
err(msg)
ctx.exit(1)

write_back = WriteBack.from_configuration(check=check, diff=diff, color=color)
if target_version:
versions = set(target_version)
Expand Down Expand Up @@ -436,9 +456,9 @@ def main(
)

if verbose or not quiet:
out("Oh no! 💥 💔 💥" if report.return_code else "All done! ✨ 🍰 ✨")
out(error_msg if report.return_code else "All done! ✨ 🍰 ✨")
if code is None:
click.secho(str(report), err=True)
click.echo(str(report), err=True)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit unrelated but noticed that styling isn't used here, so I changed it.

ctx.exit(report.return_code)


Expand Down
8 changes: 8 additions & 0 deletions tests/test_black.py
Expand Up @@ -1796,6 +1796,14 @@ def test_invalid_cli_regex(self) -> None:
for option in ["--include", "--exclude", "--extend-exclude", "--force-exclude"]:
self.invokeBlack(["-", option, "**()(!!*)"], exit_code=2)

def test_revision_matches_version(self) -> None:
self.invokeBlack(
["--revision", black.__version__], exit_code=0, ignore_config=True
)

def test_revision_does_not_match_version(self) -> None:
self.invokeBlack(["--revision", "20.99b"], exit_code=1, ignore_config=True)

def test_preserves_line_endings(self) -> None:
with TemporaryDirectory() as workspace:
test_file = Path(workspace) / "test.py"
Expand Down