| 1 |
import os |
| 2 |
import sys |
| 3 |
import time |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
if os.name == 'nt': |
| 8 |
import win32console |
| 9 |
else: |
| 10 |
pytest.skip('skipping windows-only tests', allow_module_level=True) |
| 11 |
|
| •12 |
import progressbar2 |
| 13 |
|
| 14 |
pytest_plugins = 'pytester' |
| 15 |
_WIDGETS = [ |
| •16 |
progressbar2.Percentage(), |
| 17 |
' ', |
| •18 |
progressbar2.Bar(), |
| 19 |
' ', |
| •20 |
progressbar2.FileTransferSpeed(), |
| 21 |
' ', |
| •22 |
progressbar2.ETA(), |
| 23 |
] |
| 24 |
_MB: int = 1024 * 1024 |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
def scrape_console(line_count): |
| 29 |
pcsb = win32console.GetStdHandle(win32console.STD_OUTPUT_HANDLE) |
| 30 |
csbi = pcsb.GetConsoleScreenBufferInfo() |
| 31 |
col_max = csbi['Size'].X |
| 32 |
row_max = csbi['CursorPosition'].Y |
| 33 |
|
| 34 |
line_count = min(line_count, row_max) |
| 35 |
lines = [] |
| 36 |
for row in range(line_count): |
| 37 |
pct = win32console.PyCOORDType(0, row + row_max - line_count) |
| 38 |
line = pcsb.ReadConsoleOutputCharacter(col_max, pct) |
| 39 |
lines.append(line.rstrip()) |
| 40 |
return lines |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
def runprogress() -> int: |
| 45 |
print('***BEGIN***') |
| •46 |
b = progressbar2.ProgressBar( |
| 47 |
widgets=['example.m4v: ', *_WIDGETS], |
| 48 |
max_value=10 * _MB, |
| 49 |
) |
| 50 |
for i in range(10): |
| 51 |
b.update((i + 1) * _MB) |
| 52 |
time.sleep(0.25) |
| 53 |
b.finish() |
| 54 |
print('***END***') |
| 55 |
return 0 |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
def find(lines, x): |
| 60 |
try: |
| 61 |
return lines.index(x) |
| 62 |
except ValueError: |
| 63 |
return -sys.maxsize |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
def test_windows(testdir: pytest.Testdir) -> None: |
| 68 |
testdir.run( |
| 69 |
sys.executable, '-c', 'import progressbar; print(progressbar.__file__)' |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
def main() -> int: |
| 74 |
runprogress() |
| 75 |
|
| 76 |
scraped_lines = scrape_console(100) |
| 77 |
|
| 78 |
scraped_lines.reverse() |
| 79 |
index_begin = find(scraped_lines, '***BEGIN***') |
| 80 |
index_end = find(scraped_lines, '***END***') |
| 81 |
|
| 82 |
if index_end + 2 != index_begin: |
| 83 |
print('ERROR: Unexpected multi-line output from progressbar') |
| 84 |
print(f'{index_begin=} {index_end=}') |
| 85 |
return 1 |
| 86 |
return 0 |
| 87 |
|
| 88 |
|
| 89 |
if __name__ == '__main__': |
| 90 |
main() |