Merge pull request #3925 from crazymerlyn/fix-exit-code
Fix exit code for command line errors
This commit is contained in:
		
						commit
						29bfa5efa4
					
				
							
								
								
									
										1
									
								
								AUTHORS
								
								
								
								
							
							
						
						
									
										1
									
								
								AUTHORS
								
								
								
								
							| 
						 | 
				
			
			@ -46,6 +46,7 @@ Christian Boelsen
 | 
			
		|||
Christian Theunert
 | 
			
		||||
Christian Tismer
 | 
			
		||||
Christopher Gilling
 | 
			
		||||
CrazyMerlyn
 | 
			
		||||
Cyrus Maden
 | 
			
		||||
Dhiren Serai
 | 
			
		||||
Daniel Grana
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
Pytest now returns with correct exit code (EXIT_USAGEERROR, 4) when called with unknown arguments.
 | 
			
		||||
| 
						 | 
				
			
			@ -2,8 +2,13 @@ import six
 | 
			
		|||
import warnings
 | 
			
		||||
import argparse
 | 
			
		||||
 | 
			
		||||
from gettext import gettext as _
 | 
			
		||||
import sys as _sys
 | 
			
		||||
 | 
			
		||||
import py
 | 
			
		||||
 | 
			
		||||
from ..main import EXIT_USAGEERROR
 | 
			
		||||
 | 
			
		||||
FILE_OR_DIR = "file_or_dir"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -329,6 +334,16 @@ class MyOptionParser(argparse.ArgumentParser):
 | 
			
		|||
        # an usage error to provide more contextual information to the user
 | 
			
		||||
        self.extra_info = extra_info
 | 
			
		||||
 | 
			
		||||
    def error(self, message):
 | 
			
		||||
        """error(message: string)
 | 
			
		||||
 | 
			
		||||
        Prints a usage message incorporating the message to stderr and
 | 
			
		||||
        exits.
 | 
			
		||||
        Overrides the method in parent class to change exit code"""
 | 
			
		||||
        self.print_usage(_sys.stderr)
 | 
			
		||||
        args = {"prog": self.prog, "message": message}
 | 
			
		||||
        self.exit(EXIT_USAGEERROR, _("%(prog)s: error: %(message)s\n") % args)
 | 
			
		||||
 | 
			
		||||
    def parse_args(self, args=None, namespace=None):
 | 
			
		||||
        """allow splitting of positional arguments"""
 | 
			
		||||
        args, argv = self.parse_known_args(args, namespace)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1061,3 +1061,8 @@ def test_fixture_mock_integration(testdir):
 | 
			
		|||
    p = testdir.copy_example("acceptance/fixture_mock_integration.py")
 | 
			
		||||
    result = testdir.runpytest(p)
 | 
			
		||||
    result.stdout.fnmatch_lines("*1 passed*")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_usage_error_code(testdir):
 | 
			
		||||
    result = testdir.runpytest("-unknown-option-")
 | 
			
		||||
    assert result.ret == EXIT_USAGEERROR
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue