GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — black/tests/test_schedule_formatting.py at main · psf/black

Expand file tree

/

test_schedule_formatting.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
90 lines (73 loc) · 2.64 KB
/

test_schedule_formatting.py

Copy path

File metadata and controls

90 lines (73 loc) · 2.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
Tests for `schedule_formatting`, the multi-file scheduling loop.
"""
from __future__ import annotations
import asyncio
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import Any, Optional
import black.concurrency as concurrency
from black import Mode, WriteBack
from black.report import Report
def test_formats_all_files_and_reports_counts(tmp_path: Path) -> None:
unformatted = tmp_path / "a.py"
unformatted.write_text("x=1\n", encoding="utf8")
formatted = tmp_path / "b.py"
formatted.write_text("x = 1\n", encoding="utf8")
report = Report()
async def run() -> None:
loop = asyncio.get_running_loop()
with ThreadPoolExecutor(max_workers=2) as executor:
await concurrency.schedule_formatting(
sources={unformatted, formatted},
fast=False,
write_back=WriteBack.YES,
mode=Mode(),
report=report,
loop=loop,
executor=executor,
no_cache=True,
)
asyncio.run(run())
assert unformatted.read_text(encoding="utf8") == "x = 1\n"
assert report.change_count == 1
assert report.same_count == 1
assert report.failure_count == 0
def test_reports_failed_files_without_aborting_the_run(
tmp_path: Path, monkeypatch: Any
) -> None:
sources = {tmp_path / f"test_{i}.py" for i in range(25)}
for src in sources:
src.write_text("x = 1\n", encoding="utf8")
failing = tmp_path / "failing.py"
def fake_format_file_in_place(
src: Path,
fast: bool,
mode: Mode,
write_back: WriteBack,
lock: Optional[object],
) -> bool:
if src == failing:
raise OSError("no space left on device")
return False
monkeypatch.setattr(concurrency, "format_file_in_place", fake_format_file_in_place)
report = Report()
async def run() -> None:
loop = asyncio.get_running_loop()
with ThreadPoolExecutor(max_workers=2) as executor:
await concurrency.schedule_formatting(
sources=sources | {failing},
fast=False,
write_back=WriteBack.CHECK,
mode=Mode(),
report=report,
loop=loop,
executor=executor,
no_cache=True,
)
asyncio.run(run())
# The failing file is reported, and every other file still completes: the
# scheduling loop must drain all tasks, not stop at the first failure.
assert report.failure_count == 1
assert report.same_count == len(sources)
assert report.change_count == 0
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.