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

feat(#3195): add preview option support for blackd #3217

Merged
merged 4 commits into from Aug 13, 2022
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
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -29,6 +29,8 @@

<!-- Changes to blackd -->

- `blackd` now supports preview style via `X-PREVIEW` header (#3195)
Mogost marked this conversation as resolved.
Show resolved Hide resolved

### Configuration

<!-- Changes to how Black can be configured -->
Expand Down
3 changes: 3 additions & 0 deletions docs/usage_and_configuration/black_as_a_server.md
Expand Up @@ -56,6 +56,9 @@ The headers controlling how source code is formatted are:
- `X-Skip-Magic-Trailing-Comma`: corresponds to the `--skip-magic-trailing-comma`
command line flag. If present and its value is not the empty string, trailing commas
will not be used as a reason to split lines.
- `X-Preview`: corresponds to the `--preview`command line flag. If present and its value
is not the empty string, experimental and potentially disruptive style changes will be
used.
Mogost marked this conversation as resolved.
Show resolved Hide resolved
- `X-Fast-Or-Safe`: if set to `fast`, `blackd` will act as _Black_ does when passed the
`--fast` command line flag.
- `X-Python-Variant`: if set to `pyi`, `blackd` will act as _Black_ does when passed the
Expand Down
4 changes: 4 additions & 0 deletions src/blackd/__init__.py
Expand Up @@ -32,6 +32,7 @@
PYTHON_VARIANT_HEADER = "X-Python-Variant"
SKIP_STRING_NORMALIZATION_HEADER = "X-Skip-String-Normalization"
SKIP_MAGIC_TRAILING_COMMA = "X-Skip-Magic-Trailing-Comma"
PREVIEW = "X-PREVIEW"
Mogost marked this conversation as resolved.
Show resolved Hide resolved
FAST_OR_SAFE_HEADER = "X-Fast-Or-Safe"
DIFF_HEADER = "X-Diff"

Expand All @@ -41,6 +42,7 @@
PYTHON_VARIANT_HEADER,
SKIP_STRING_NORMALIZATION_HEADER,
SKIP_MAGIC_TRAILING_COMMA,
PREVIEW,
FAST_OR_SAFE_HEADER,
DIFF_HEADER,
]
Expand Down Expand Up @@ -109,6 +111,7 @@ async def handle(request: web.Request, executor: Executor) -> web.Response:
skip_magic_trailing_comma = bool(
request.headers.get(SKIP_MAGIC_TRAILING_COMMA, False)
)
preview = bool(request.headers.get(PREVIEW, False))
fast = False
if request.headers.get(FAST_OR_SAFE_HEADER, "safe") == "fast":
fast = True
Expand All @@ -118,6 +121,7 @@ async def handle(request: web.Request, executor: Executor) -> web.Response:
line_length=line_length,
string_normalization=not skip_string_normalization,
magic_trailing_comma=not skip_magic_trailing_comma,
preview=preview,
)
req_bytes = await request.content.read()
charset = request.charset if request.charset is not None else "utf8"
Expand Down
7 changes: 7 additions & 0 deletions tests/test_blackd.py
Expand Up @@ -167,6 +167,13 @@ async def test_blackd_invalid_line_length(self) -> None:
)
self.assertEqual(response.status, 400)

@unittest_run_loop
async def test_blackd_preview(self) -> None:
response = await self.client.post(
"/", data=b'print("hello")\n', headers={blackd.PREVIEW: "true"}
)
self.assertEqual(response.status, 204)

@unittest_run_loop
async def test_blackd_response_black_version_header(self) -> None:
response = await self.client.post("/")
Expand Down