Andy Mikhailenko avatar Andy Mikhailenko committed eec17c9

Fix tests to embrace the improved behaviour of argparse since Python 3.3.

Comments (0)

Files changed (2)

 # -*- coding: utf-8 -*-
 
 import sys
+import re
 from argh.six import (
     PY3, BytesIO, StringIO, u, string_types, text_type, binary_type,
     iteritems
         except SystemExit as error:
             self.fail('Argument parsing failed for {0!r}: {1!r}'.format(
                 command_string, error))
-        self.assertEqual(result, expected_result)
+
+        if isinstance(expected_result, re._pattern_type):
+            self.assertRegex(result, expected_result)
+        else:
+            self.assertEqual(result, expected_result)
+
+    def assert_cmd_regex(self, command_string, pattern, **kwargs):
+        return self.assert_cmd_returns(command_string, re.compile(pattern),
+                                       **kwargs)
 
     def assert_cmd_exits(self, command_string, message_regex=None):
         "When a command forces exit, it *may* fail, but may just print help."
         sys.argv = _argv
 
     def test_no_command(self):
-        self.assert_cmd_fails('', 'too few arguments')
+        if sys.version_info < (3,3):
+            # Python before 3.3 exits with an error
+            self.assert_cmd_fails('', 'too few arguments')
+        else:
+            # Python since 3.3 returns a help message and doesn't exit
+            self.assert_cmd_regex('', 'usage:')
 
     def test_invalid_choice(self):
         self.assert_cmd_fails('whatchamacallit', '^invalid choice')
 
     def test_bare_namespace(self):
         "A command can be resolved to a function, not a namespace."
-        self.assert_cmd_fails('greet', 'too few arguments')
-        self.assert_cmd_fails('greet --name=world', 'too few arguments')
+
+        # without arguments
+
+        if sys.version_info < (3,3):
+            # Python before 3.3 exits with an error
+            self.assert_cmd_fails('greet', 'too few arguments')
+        else:
+            # Python since 3.3 returns a help message and doesn't exit
+            self.assert_cmd_regex('greet', 'usage:')
+
+        # with an argument
+
+        if sys.version_info < (3,3):
+            # Python before 3.3 exits with a less informative error
+            self.assert_cmd_fails('greet --name=world', 'too few arguments')
+        else:
+            # Python since 3.3 exits with a more informative error
+            self.assert_cmd_fails('greet --name=world',
+                                  'unrecognized arguments: --name=world')
 
     def test_namespaced_function(self):
         "A subcommand is resolved to a function."
         self.assert_cmd_returns('greet hello', 'Hello world!\n')
         self.assert_cmd_returns('greet hello --name=John', 'Hello John!\n')
         self.assert_cmd_fails('greet hello John', 'unrecognized arguments')
-        self.assert_cmd_fails('greet howdy --name=John', 'too few arguments')
+
+        if sys.version_info < (3,3):
+            # Python before 3.3 exits with a less informative error
+            missing_arg_regex = 'too few arguments'
+        else:
+            # Python since 3.3 exits with a more informative error
+            missing_arg_regex = 'the following arguments are required: buddy'
+
+        self.assert_cmd_fails('greet howdy --name=John', missing_arg_regex)
         self.assert_cmd_returns('greet howdy John', 'Howdy John?\n')
 
     def test_alias(self):
 [tox]
-envlist=py26,py27,pypy,py32
+envlist=py26,py27,pypy,py32,py33
 indexserver=
 default = http://pypi.python.org/simple
 
     nose
     rednose
     unittest2py3k
+
+[testenv:py33]
+deps =
+    nose
+    rednose
+    unittest2py3k
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.