10000 Add option to require a specific version to be running (#2300) Β· IBMZ-Linux-OSS-Python/black@a2b5ba2 Β· GitHub
[go: up one dir, main page]

Skip to content

Commit a2b5ba2

Browse files
authored
Add option to require a specific version to be running (psf#2300)
Closes psf#1246: This PR adds a new option (and automatically a toml entry, hooray for existing configuration management πŸŽ‰) to require a specific version of Black to be running. For example: `black --required-version 20.8b -c "format = 'this'"` Execution fails straight away if it doesn't match `__version__`.
1 parent df1c86c commit a2b5ba2

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

β€ŽCHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Correct max string length calculation when there are string operators (#2292)
88
- Fixed option usage when using the `--code` flag (#2259)
99
- Do not call `uvloop.install()` when _Black_ is used as a library (#2303)
10+
- Added `--required-version` option to require a specific version to be running (#2300)
1011

1112
## 21.5b2
1213

β€Ždocs/usage_and_configuration/the_basics.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ $ black src/ -q
167167
error: cannot format src/black_primer/cli.py: Cannot parse: 5:6: mport asyncio
168168
```
169169

170-
### Getting the version
170+
### Versions
171171

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

@@ -176,6 +176,19 @@ $ black --version
176176
black, version 21.5b0
177177
```
178178

179+
An option to require a specific version to be running is also provided.
180+
181+
```console
182+
$ black --required-version 21.5b2 -c "format = 'this'"
183+
format = "this"
184+
$ black --required-version 31.5b2 -c "still = 'beta?!'"
185+
Oh no! πŸ’₯ πŸ’” πŸ’₯ The required version does not match the running version!
186+
```
187+
188+
This is useful for example when running _Black_ in multiple environments that haven't
189+
necessarily installed the correct version. This option can be set in a configuration
190+
file for consistent results across environments.
191+
179192
## Configuration via a file
180193

181194
_Black_ is able to read project-specific default values for its command line options

β€Žsrc/black/__init__.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,14 @@ def validate_regex(
241241
is_flag=True,
242242
help="If --fast given, skip temporary sanity checks. [default: --safe]",
243243
)
244+
@click.option(
245+
"--required-version",
246+
type=str,
247+
help=(
248+
"Require a specific version of Black to be running (useful for unifying results"
249+
" across many environments e.g. with a pyproject.toml file)."
250+
),
251+
)
244252
@click.option(
245253
"--include",
246254
type=str,
@@ -351,6 +359,7 @@ def main(
351359
experimental_string_processing: bool,
352360
quiet: bool,
353361
verbose: bool,
362+
required_version: str,
354363
include: Pattern,
355364
exclude: Optional[Pattern],
356365
extend_exclude: Optional[Pattern],
@@ -360,6 +369,17 @@ def main(
360369
config: Optional[str],
361370
) -> None:
362371
"""The uncompromising code formatter."""
372+
if config and verbose:
373+
out(f"Using configuration from {config}.", bold=False, fg="blue")
374+
375+
error_msg = "Oh no! πŸ’₯ πŸ’” πŸ’₯"
376+
if required_version and required_version != __version__:
377+
err(
378+
f"{error_msg} The required version `{required_version}` does not match"
379+
f" the running version `{__version__}`!"
380+
)
381+
ctx.exit(1)
382+
363383
write_back = WriteBack.from_configuration(check=check, diff=diff, color=color)
364384
if target_version:
365385
versions = set(target_version)
@@ -374,8 +394,6 @@ def main(
374394
magic_trailing_comma=not skip_magic_trailing_comma,
375395
experimental_string_processing=experimental_string_processing,
376396
)
377-
if config and verbose:
378-
out(f"Using configuration from {config}.", bold=False, fg="blue")
379397

380398
if code is not None:
381399
# Run in quiet mode by default with -c; the extra output isn't useful.
@@ -428,9 +446,9 @@ def main(
428446
)
429447

430448
if verbose or not quiet:
431-
out("Oh no! πŸ’₯ πŸ’” πŸ’₯" if report.return_code else "All done! ✨ 🍰 ✨")
449+
out(error_msg if report.return_code else "All done! ✨ 🍰 ✨")
432450
if code is None:
433-
click.secho(str(report), err=True)
451+
click.echo(str(report), err=True)
434452
ctx.exit(report.return_code)
435453

436454

β€Žtests/test_black.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,16 @@ def test_invalid_cli_regex(self) -> None:
17961796
for option in ["--include", "--exclude", "--extend-exclude", "--force-exclude"]:
17971797
self.invokeBlack(["-", option, "**()(!!*)"], exit_code=2)
17981798

1799+
def test_required_version_matches_version(self) -> None:
1800+
self.invokeBlack(
1801+
["--required-version", black.__version__], exit_code=0, ignore_config=True
1802+
)
1803+
1804+
def test_required_version_does_not_match_version(self) -> None:
1805+
self.invokeBlack(
1806+
["--required-version", "20.99b"], exit_code=1, ignore_config=True
1807+
)
1808+
17991809
def test_preserves_line_endings(self) -> None:
18001810
with TemporaryDirectory() as workspace:
18011811
test_file = Path(workspace) / "test.py"

0 commit comments

Comments
Β (0)
0