Skip to content

Commit 73cb6e7

Browse files
authored
Make SRC or code mandatory and mutually exclusive (#2360) (#2804)
Closes #2360: I'd like to make passing SRC or `--code` mandatory and the arguments mutually exclusive. This will change our (partially already broken) promises of CLI behavior, but I'll comment below.
1 parent 3905173 commit 73cb6e7

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
`--preview` (#2789)
3939
- Enable Python 3.10+ by default, without any extra need to specify
4040
`--target-version=py310`. (#2758)
41+
- Make passing `SRC` or `--code` mandatory and mutually exclusive (#2804)
4142

4243
### Packaging
4344

docs/usage_and_configuration/the_basics.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Foundational knowledge on using and configuring Black.
44

55
_Black_ is a well-behaved Unix-style command-line tool:
66

7-
- it does nothing if no sources are passed to it;
7+
- it does nothing if it finds no sources to format;
88
- it will read from standard input and write to standard output if `-` is used as the
99
filename;
1010
- it only outputs messages to users on standard error;
11-
- exits with code 0 unless an internal error occurred (or `--check` was used).
11+
- exits with code 0 unless an internal error occurred or a CLI option prompted it.
1212

1313
## Usage
1414

src/black/__init__.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,17 @@ def main(
431431
) -> None:
432432
"""The uncompromising code formatter."""
433433
ctx.ensure_object(dict)
434+
435+
if src and code is not None:
436+
out(
437+
main.get_usage(ctx)
438+
+ "\n\n'SRC' and 'code' cannot be passed simultaneously."
439+
)
440+
ctx.exit(1)
441+
if not src and code is None:
442+
out(main.get_usage(ctx) + "\n\nOne of 'SRC' or 'code' is required.")
443+
ctx.exit(1)
444+
434445
root, method = find_project_root(src) if code is None else (None, None)
435446
ctx.obj["root"] = root
436447

@@ -569,7 +580,6 @@ def get_sources(
569580
) -> Set[Path]:
570581
"""Compute the set of files to be formatted."""
571582
sources: Set[Path] = set()
572-
path_empty(src, "No Path provided. Nothing to do 😴", quiet, verbose, ctx)
573583

574584
if exclude is None:
575585
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)

tests/test_black.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -972,10 +972,13 @@ def test_check_diff_use_together(self) -> None:
972972
# Multi file command.
973973
self.invokeBlack([str(src1), str(src2), "--diff", "--check"], exit_code=1)
974974

975-
def test_no_files(self) -> None:
975+
def test_no_src_fails(self) -> None:
976976
with cache_dir():
977-
# Without an argument, black exits with error code 0.
978-
self.invokeBlack([])
977+
self.invokeBlack([], exit_code=1)
978+
979+
def test_src_and_code_fails(self) -> None:
980+
with cache_dir():
981+
self.invokeBlack([".", "-c", "0"], exit_code=1)
979982

980983
def test_broken_symlink(self) -> None:
981984
with cache_dir() as workspace:
@@ -1229,13 +1232,18 @@ def test_invalid_cli_regex(self) -> None:
12291232

12301233
def test_required_version_matches_version(self) -> None:
12311234
self.invokeBlack(
1232-
["--required-version", black.__version__], exit_code=0, ignore_config=True
1235+
["--required-version", black.__version__, "-c", "0"],
1236+
exit_code=0,
1237+
ignore_config=True,
12331238
)
12341239

12351240
def test_required_version_does_not_match_version(self) -> None:
1236-
self.invokeBlack(
1237-
["--required-version", "20.99b"], exit_code=1, ignore_config=True
1241+
result = BlackRunner().invoke(
1242+
black.main,
1243+
["--required-version", "20.99b", "-c", "0"],
12381244
)
1245+
self.assertEqual(result.exit_code, 1)
1246+
self.assertIn("required version", result.stderr)
12391247

12401248
def test_preserves_line_endings(self) -> None:
12411249
with TemporaryDirectory() as workspace:

0 commit comments

Comments
 (0)