From d2e6be5496f47d2a79a465829c166f7bdf07efa0 Mon Sep 17 00:00:00 2001 From: Burt Bielicki Date: Thu, 5 May 2016 11:38:44 -0700 Subject: [PATCH 1/8] put changes back to the way they were originally --- src/azure/cli/tests/test_help.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/azure/cli/tests/test_help.py b/src/azure/cli/tests/test_help.py index 1cbbd37c58d..679039b4587 100644 --- a/src/azure/cli/tests/test_help.py +++ b/src/azure/cli/tests/test_help.py @@ -469,20 +469,15 @@ def test_handler(args): config.get_command_table = lambda: cmd_table app = Application(config) - # there is an argparse bug on <2.7.10 where SystemExit is not thrown on missing required param - if sys.version_info < (2, 7, 10): + with self.assertRaises(SystemExit): app.execute('n1 -fb a --foobar value'.split()) + with self.assertRaises(SystemExit): app.execute('n1 -fb a --foobar2 value --foobar3 extra'.split()) - else: - with self.assertRaises(SystemExit): - app.execute('n1 -fb a --foobar value'.split()) - with self.assertRaises(SystemExit): - app.execute('n1 -fb a --foobar2 value --foobar3 extra'.split()) - - self.assertTrue('required' in io.getvalue() - and '--foobar/-fb' not in io.getvalue() - and '--foobar2/-fb2' in io.getvalue() - and 'unrecognized arguments: --foobar3 extra' in io.getvalue()) + + self.assertTrue('required' in io.getvalue() + and '--foobar/-fb' not in io.getvalue() + and '--foobar2/-fb2' in io.getvalue() + and 'unrecognized arguments: --foobar3 extra' in io.getvalue()) @redirect_io def test_help_group_help(self): From c8106c1068652553876c9f65c72a411b72a6f227 Mon Sep 17 00:00:00 2001 From: Burt Bielicki Date: Thu, 5 May 2016 12:01:36 -0700 Subject: [PATCH 2/8] override exit to match 3.5 --- src/azure/cli/parser.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/azure/cli/parser.py b/src/azure/cli/parser.py index 095107e2efd..f9bda195c76 100644 --- a/src/azure/cli/parser.py +++ b/src/azure/cli/parser.py @@ -1,4 +1,5 @@ import argparse +import sys import azure.cli._help as _help class IncorrectUsageError(Exception): @@ -75,3 +76,9 @@ def format_help(self): else self), is_group) self.exit() + + def exit(self, status = 0, message = None): + if message: + self._print_message(message, sys.stderr) + #raise SystemExit(status) + sys.exit(status) From fc22b0c4477e0820c14f3839859ec2744d81f17b Mon Sep 17 00:00:00 2001 From: Burt Bielicki Date: Thu, 5 May 2016 12:33:25 -0700 Subject: [PATCH 3/8] lint --- src/azure/cli/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/azure/cli/parser.py b/src/azure/cli/parser.py index f9bda195c76..7921975998a 100644 --- a/src/azure/cli/parser.py +++ b/src/azure/cli/parser.py @@ -77,7 +77,7 @@ def format_help(self): is_group) self.exit() - def exit(self, status = 0, message = None): + def exit(self, status=0, message=None): if message: self._print_message(message, sys.stderr) #raise SystemExit(status) From 785d4c80ebcc37613207fe81318d697368751aab Mon Sep 17 00:00:00 2001 From: Burt Bielicki Date: Thu, 5 May 2016 12:37:17 -0700 Subject: [PATCH 4/8] manually raise SystemExit --- src/azure/cli/parser.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/azure/cli/parser.py b/src/azure/cli/parser.py index 7921975998a..5ce5201d7bd 100644 --- a/src/azure/cli/parser.py +++ b/src/azure/cli/parser.py @@ -80,5 +80,4 @@ def format_help(self): def exit(self, status=0, message=None): if message: self._print_message(message, sys.stderr) - #raise SystemExit(status) - sys.exit(status) + raise SystemExit(status) From 35695f0c53cc959c33645244f0a06bec3eec4d37 Mon Sep 17 00:00:00 2001 From: Burt Bielicki Date: Thu, 5 May 2016 13:00:55 -0700 Subject: [PATCH 5/8] another approach --- src/azure/cli/parser.py | 6 ------ src/azure/cli/tests/test_help.py | 9 +++++++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/azure/cli/parser.py b/src/azure/cli/parser.py index 5ce5201d7bd..095107e2efd 100644 --- a/src/azure/cli/parser.py +++ b/src/azure/cli/parser.py @@ -1,5 +1,4 @@ import argparse -import sys import azure.cli._help as _help class IncorrectUsageError(Exception): @@ -76,8 +75,3 @@ def format_help(self): else self), is_group) self.exit() - - def exit(self, status=0, message=None): - if message: - self._print_message(message, sys.stderr) - raise SystemExit(status) diff --git a/src/azure/cli/tests/test_help.py b/src/azure/cli/tests/test_help.py index 679039b4587..8ac685a1c22 100644 --- a/src/azure/cli/tests/test_help.py +++ b/src/azure/cli/tests/test_help.py @@ -469,10 +469,15 @@ def test_handler(args): config.get_command_table = lambda: cmd_table app = Application(config) - with self.assertRaises(SystemExit): + try: app.execute('n1 -fb a --foobar value'.split()) - with self.assertRaises(SystemExit): + except SystemExit: + pass + + try: app.execute('n1 -fb a --foobar2 value --foobar3 extra'.split()) + except SystemExit: + pass self.assertTrue('required' in io.getvalue() and '--foobar/-fb' not in io.getvalue() From 7a2a1629c8166a086a902afbd97de05e50de3bf3 Mon Sep 17 00:00:00 2001 From: Burt Bielicki Date: Thu, 5 May 2016 13:09:37 -0700 Subject: [PATCH 6/8] yet another approach --- src/azure/cli/tests/test_help.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/azure/cli/tests/test_help.py b/src/azure/cli/tests/test_help.py index 8ac685a1c22..cf57c7c2280 100644 --- a/src/azure/cli/tests/test_help.py +++ b/src/azure/cli/tests/test_help.py @@ -469,15 +469,26 @@ def test_handler(args): config.get_command_table = lambda: cmd_table app = Application(config) - try: - app.execute('n1 -fb a --foobar value'.split()) - except SystemExit: - pass - - try: - app.execute('n1 -fb a --foobar2 value --foobar3 extra'.split()) - except SystemExit: - pass + if sys.version_info < (2, 7, 10): + try: + app.execute('n1 -fb a --foobar value'.split()) + except SystemExit: + pass + + try: + app.execute('n1 -fb a --foobar2 value --foobar3 extra'.split()) + except SystemExit: + pass + else: + with self.assertRaises(SystemExit): + app.execute('n1 -fb a --foobar value'.split()) + with self.assertRaises(SystemExit): + app.execute('n1 -fb a --foobar2 value --foobar3 extra'.split()) + + self.assertTrue('required' in io.getvalue() + and '--foobar/-fb' not in io.getvalue() + and '--foobar2/-fb2' in io.getvalue() + and 'unrecognized arguments: --foobar3 extra' in io.getvalue()) self.assertTrue('required' in io.getvalue() and '--foobar/-fb' not in io.getvalue() From 66a7204267c6711790211eeb1c83a23bc253f69b Mon Sep 17 00:00:00 2001 From: Burt Bielicki Date: Thu, 5 May 2016 13:19:33 -0700 Subject: [PATCH 7/8] remove dup code --- src/azure/cli/tests/test_help.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/azure/cli/tests/test_help.py b/src/azure/cli/tests/test_help.py index cf57c7c2280..a7e38e78d1a 100644 --- a/src/azure/cli/tests/test_help.py +++ b/src/azure/cli/tests/test_help.py @@ -490,11 +490,6 @@ def test_handler(args): and '--foobar2/-fb2' in io.getvalue() and 'unrecognized arguments: --foobar3 extra' in io.getvalue()) - self.assertTrue('required' in io.getvalue() - and '--foobar/-fb' not in io.getvalue() - and '--foobar2/-fb2' in io.getvalue() - and 'unrecognized arguments: --foobar3 extra' in io.getvalue()) - @redirect_io def test_help_group_help(self): app = Application(Configuration([])) From e1fc88226e95bf3ae6fceee23ded4e77062404e8 Mon Sep 17 00:00:00 2001 From: Burt Bielicki Date: Thu, 5 May 2016 13:24:20 -0700 Subject: [PATCH 8/8] add comment --- src/azure/cli/tests/test_help.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/azure/cli/tests/test_help.py b/src/azure/cli/tests/test_help.py index a7e38e78d1a..ffb52182a5a 100644 --- a/src/azure/cli/tests/test_help.py +++ b/src/azure/cli/tests/test_help.py @@ -469,6 +469,8 @@ def test_handler(args): config.get_command_table = lambda: cmd_table app = Application(config) + # work around an argparse behavior where output is not printed and SystemExit + # is not raised on Python 2.7.9 if sys.version_info < (2, 7, 10): try: app.execute('n1 -fb a --foobar value'.split())