Skip to content

Commit 8113b5d

Browse files
committed
Improve test for percent escaping in argument documentation
Replace expensive test that iterated through all services with focused unit tests that directly verify % character escaping behavior. The new tests use controlled test data to verify both CLIArgument and BooleanArgument properly escape % in symbols (% ^) and URL-encoded strings (%28%29) when passed to argparse.
1 parent 66be536 commit 8113b5d

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

tests/unit/test_argprocess.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
from awscli.arguments import ListArgument, BooleanArgument
3333
from awscli.arguments import create_argument_model_from_schema
3434

35-
from awscli.clidriver import ServiceOperation, CLIOperationCaller
36-
from awscli.argparser import ArgTableArgParser
37-
35+
import argparse
3836

3937
# These tests use real service types so that we can
4038
# verify the real shapes of services.
@@ -454,6 +452,26 @@ def test_csv_syntax_errors(self):
454452
with self.assertRaisesRegex(ParamError, error_msg):
455453
self.parse_shorthand(p, ['ParameterKey=key,ParameterValue="foo,bar\''])
456454

455+
def _test_argument_escapes_percent(self, arg_class, arg_type, doc_string, expected_substring):
456+
argument = self.create_argument({'Test': {'type': arg_type}})
457+
argument.argument_model.members['Test'].documentation = doc_string
458+
arg = arg_class('test-arg', argument.argument_model.members['Test'], None, False)
459+
parser = argparse.ArgumentParser()
460+
arg.add_to_parser(parser)
461+
action = parser._actions[-1]
462+
self.assertIn(expected_substring, action.help)
463+
464+
def test_cli_argument_escapes_percent_in_symbols(self):
465+
self._test_argument_escapes_percent(CLIArgument, 'string', 'Symbols: ! @ # $ % ^ & * ( )', '% ^')
466+
467+
def test_cli_argument_escapes_percent_in_url_encoding(self):
468+
self._test_argument_escapes_percent(CLIArgument, 'string', 'test_file%283%29.png', '%28')
469+
470+
def test_boolean_argument_escapes_percent_in_symbols(self):
471+
self._test_argument_escapes_percent(BooleanArgument, 'boolean', 'Symbols: ! @ # $ % ^ & * ( )', '% ^')
472+
473+
def test_boolean_argument_escapes_percent_in_url_encoding(self):
474+
self._test_argument_escapes_percent(BooleanArgument, 'boolean', 'test_file%283%29.png', '%28')
457475

458476
class TestParamShorthandCustomArguments(BaseArgProcessTest):
459477

@@ -897,28 +915,6 @@ def test_json_value_decode_error(self):
897915
with self.assertRaises(ParamError):
898916
unpack_cli_arg(self.p, value)
899917

900-
class TestPercentInDocumentation(BaseArgProcessTest):
901-
def test_percent_characters_escaped_in_argument_help(self):
902-
operation_caller = CLIOperationCaller(self.session)
903-
for service_name in self.session.get_available_services():
904-
service_model = self.session.get_service_model(service_name)
905-
for operation_name in service_model.operation_names:
906-
operation_model = service_model.operation_model(operation_name)
907-
if not operation_model.input_shape:
908-
continue
909-
has_percent = any(
910-
'%' in (member.documentation or '')
911-
for member in operation_model.input_shape.members.values()
912-
)
913-
if has_percent:
914-
service_op = ServiceOperation(
915-
name=xform_name(operation_name, '-'),
916-
parent_name=service_name,
917-
operation_caller=operation_caller,
918-
operation_model=operation_model,
919-
session=self.session,
920-
)
921-
ArgTableArgParser(service_op.arg_table)
922918

923919
if __name__ == '__main__':
924920
unittest.main()

0 commit comments

Comments
 (0)