| 1 |
import io |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
import progressbar.__main__ as main |
| 6 |
|
| 7 |
|
| 8 |
def test_size_to_bytes() -> None: |
| 9 |
assert main.size_to_bytes('1') == 1 |
| 10 |
assert main.size_to_bytes('1k') == 1024 |
| 11 |
assert main.size_to_bytes('1m') == 1048576 |
| 12 |
assert main.size_to_bytes('1g') == 1073741824 |
| 13 |
assert main.size_to_bytes('1p') == 1125899906842624 |
| 14 |
|
| 15 |
assert main.size_to_bytes('1024') == 1024 |
| 16 |
assert main.size_to_bytes('1024k') == 1048576 |
| 17 |
assert main.size_to_bytes('1024m') == 1073741824 |
| 18 |
assert main.size_to_bytes('1024g') == 1099511627776 |
| 19 |
assert main.size_to_bytes('1024p') == 1152921504606846976 |
| 20 |
|
| 21 |
|
| 22 |
def test_filename_to_bytes(tmp_path) -> None: |
| 23 |
file = tmp_path / 'test' |
| 24 |
file.write_text('test') |
| 25 |
assert main.size_to_bytes(f'@{file}') == 4 |
| 26 |
|
| 27 |
with pytest.raises(FileNotFoundError): |
| 28 |
main.size_to_bytes(f'@{tmp_path / "nonexistent"}') |
| 29 |
|
| 30 |
|
| 31 |
def test_create_argument_parser() -> None: |
| 32 |
parser = main.create_argument_parser() |
| 33 |
args = parser.parse_args( |
| 34 |
[ |
| 35 |
'-p', |
| 36 |
'-t', |
| 37 |
'-e', |
| 38 |
'-r', |
| 39 |
'-a', |
| 40 |
'-b', |
| 41 |
'-8', |
| 42 |
'-T', |
| 43 |
'-n', |
| 44 |
'-q', |
| 45 |
'input', |
| 46 |
'-o', |
| 47 |
'output', |
| 48 |
] |
| 49 |
) |
| 50 |
assert args.progress is True |
| 51 |
assert args.timer is True |
| 52 |
assert args.eta is True |
| 53 |
assert args.rate is True |
| 54 |
assert args.average_rate is True |
| 55 |
assert args.bytes is True |
| 56 |
assert args.bits is True |
| 57 |
assert args.buffer_percent is True |
| 58 |
assert args.last_written is None |
| 59 |
assert args.format is None |
| 60 |
assert args.numeric is True |
| 61 |
assert args.quiet is True |
| 62 |
assert args.input == ['input'] |
| 63 |
assert args.output == 'output' |
| 64 |
|
| 65 |
|
| 66 |
def test_main_binary(capsys) -> None: |
| 67 |
# Call the main function with different command line arguments |
| 68 |
main.main( |
| 69 |
[ |
| 70 |
'-p', |
| 71 |
'-t', |
| 72 |
'-e', |
| 73 |
'-r', |
| 74 |
'-a', |
| 75 |
'-b', |
| 76 |
'-8', |
| 77 |
'-T', |
| 78 |
'-n', |
| 79 |
'-q', |
| 80 |
__file__, |
| 81 |
] |
| 82 |
) |
| 83 |
|
| 84 |
captured = capsys.readouterr() |
| 85 |
assert 'test_main(capsys):' in captured.out |
| 86 |
|
| 87 |
|
| 88 |
def test_main_lines(capsys) -> None: |
| 89 |
# Call the main function with different command line arguments |
| 90 |
main.main( |
| 91 |
[ |
| 92 |
'-p', |
| 93 |
'-t', |
| 94 |
'-e', |
| 95 |
'-r', |
| 96 |
'-a', |
| 97 |
'-b', |
| 98 |
'-8', |
| 99 |
'-T', |
| 100 |
'-n', |
| 101 |
'-q', |
| 102 |
'-l', |
| 103 |
'-s', |
| 104 |
f'@{__file__}', |
| 105 |
__file__, |
| 106 |
] |
| 107 |
) |
| 108 |
|
| 109 |
captured = capsys.readouterr() |
| 110 |
assert 'test_main(capsys):' in captured.out |
| 111 |
|
| 112 |
|
| 113 |
class Input(io.StringIO): |
| 114 |
buffer: io.BytesIO |
| 115 |
|
| 116 |
@classmethod |
| 117 |
def create(cls, text: str) -> 'Input': |
| 118 |
instance = cls(text) |
| 119 |
instance.buffer = io.BytesIO(text.encode()) |
| 120 |
return instance |
| 121 |
|
| 122 |
|
| 123 |
def test_main_lines_output(monkeypatch, tmp_path) -> None: |
| 124 |
text = 'my input' |
| 125 |
monkeypatch.setattr('sys.stdin', Input.create(text)) |
| 126 |
output_filename = tmp_path / 'output' |
| 127 |
main.main(['-l', '-o', str(output_filename)]) |
| 128 |
|
| 129 |
assert output_filename.read_text() == text |
| 130 |
|
| 131 |
|
| 132 |
def test_main_bytes_output(monkeypatch, tmp_path) -> None: |
| 133 |
text = 'my input' |
| 134 |
|
| 135 |
monkeypatch.setattr('sys.stdin', Input.create(text)) |
| 136 |
output_filename = tmp_path / 'output' |
| 137 |
main.main(['-o', str(output_filename)]) |
| 138 |
|
| 139 |
assert output_filename.read_text() == f'{text}' |
| 140 |
|
| 141 |
|
| 142 |
def test_missing_input(tmp_path) -> None: |
| 143 |
with pytest.raises(SystemExit): |
| 144 |
main.main([str(tmp_path / 'output')]) |