| 1 |
###################################################################### |
| 2 |
# |
| 3 |
# File: test/unit/test_arg_parser.py |
| 4 |
# |
| 5 |
# Copyright 2020 Backblaze Inc. All Rights Reserved. |
| 6 |
# |
| 7 |
# License https://www.backblaze.com/using_b2_code.html |
| 8 |
# |
| 9 |
###################################################################### |
| 10 |
|
| 11 |
import argparse |
| 12 |
import sys |
| 13 |
|
| 14 |
from b2._internal._cli.arg_parser_types import ( |
| 15 |
parse_comma_separated_list, |
| 16 |
parse_millis_from_float_timestamp, |
| 17 |
parse_range, |
| 18 |
) |
| 19 |
from b2._internal.arg_parser import B2ArgumentParser |
| 20 |
from b2._internal.console_tool import B2 |
| 21 |
|
| 22 |
from .test_base import TestBase |
| 23 |
|
| 24 |
|
| 25 |
class TestCustomArgTypes(TestBase): |
| 26 |
def test_parse_comma_separated_list(self): |
| 27 |
self.assertEqual([''], parse_comma_separated_list('')) |
| 28 |
self.assertEqual(['1', '2', '3'], parse_comma_separated_list('1,2,3')) |
| 29 |
|
| 30 |
def test_parse_millis_from_float_timestamp(self): |
| 31 |
self.assertEqual(1367900664000, parse_millis_from_float_timestamp('1367900664')) |
| 32 |
self.assertEqual(1367900664152, parse_millis_from_float_timestamp('1367900664.152')) |
| 33 |
with self.assertRaises(ValueError): |
| 34 |
parse_millis_from_float_timestamp('!$@$%@!@$') |
| 35 |
|
| 36 |
def test_parse_range(self): |
| 37 |
self.assertEqual((1, 2), parse_range('1,2')) |
| 38 |
with self.assertRaises(argparse.ArgumentTypeError): |
| 39 |
parse_range('1') |
| 40 |
with self.assertRaises(argparse.ArgumentTypeError): |
| 41 |
parse_range('1,2,3') |
| 42 |
with self.assertRaises(ValueError): |
| 43 |
parse_range('!@#,%^&') |
| 44 |
|
| 45 |
|
| 46 |
class TestNonUTF8TerminalSupport(TestBase): |
| 47 |
class ASCIIEncodedStream: |
| 48 |
def __init__(self, original_stream): |
| 49 |
self.original_stream = original_stream |
| 50 |
self.encoding = 'ascii' |
| 51 |
|
| 52 |
def write(self, data): |
| 53 |
if isinstance(data, str): |
| 54 |
data = data.encode(self.encoding, 'strict') |
| 55 |
self.original_stream.buffer.write(data) |
| 56 |
|
| 57 |
def flush(self): |
| 58 |
self.original_stream.flush() |
| 59 |
|
| 60 |
def check_help_string(self, command_class, command_name): |
| 61 |
help_string = command_class.__doc__ |
| 62 |
|
| 63 |
# create a parser with a help message that is based on the command_class.__doc__ string |
| 64 |
parser = B2ArgumentParser(description=help_string) |
| 65 |
|
| 66 |
try: |
| 67 |
old_stdout = sys.stdout |
| 68 |
old_stderr = sys.stderr |
| 69 |
sys.stdout = TestNonUTF8TerminalSupport.ASCIIEncodedStream(sys.stdout) |
| 70 |
sys.stderr = TestNonUTF8TerminalSupport.ASCIIEncodedStream(sys.stderr) |
| 71 |
|
| 72 |
parser.print_help() |
| 73 |
|
| 74 |
except UnicodeEncodeError as e: |
| 75 |
self.fail( |
| 76 |
f'Failed to encode help message for command "{command_name}" on a non-UTF-8 terminal: {e}' |
| 77 |
) |
| 78 |
|
| 79 |
finally: |
| 80 |
# Restore original stdout and stderr |
| 81 |
sys.stdout = old_stdout |
| 82 |
sys.stderr = old_stderr |
| 83 |
|
| 84 |
def test_help_in_non_utf8_terminal(self): |
| 85 |
command_classes = dict(B2.subcommands_registry.items()) |
| 86 |
command_classes['b2'] = B2 |
| 87 |
|
| 88 |
for command_name, command_class in command_classes.items(): |
| 89 |
with self.subTest(command_class=command_class, command_name=command_name): |
| 90 |
self.check_help_string(command_class, command_name) |