Skip to content

Commit

Permalink
Add test for prog name; make them independent from actual test binary…
Browse files Browse the repository at this point in the history
… name
  • Loading branch information
neithere committed Oct 12, 2015
1 parent 2fb48c4 commit b8ee9c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 6 additions & 0 deletions test/base.py
Expand Up @@ -3,6 +3,7 @@
Common stuff for tests
~~~~~~~~~~~~~~~~~~~~~~
"""
import os
import sys
from collections import namedtuple

Expand Down Expand Up @@ -80,3 +81,8 @@ def run(parser, command_string, kwargs=None, exit=False):
else:
if exit:
raise AssertionError('Did not exit')


def get_usage_string(definitions='{cmd} ...'):
prog = os.path.basename(sys.argv[0])
return 'usage: ' + prog + ' [-h] ' + definitions + '\n\n'
21 changes: 18 additions & 3 deletions test/test_integration.py
Expand Up @@ -13,7 +13,7 @@
import argh
from argh.exceptions import AssemblingError

from .base import DebugArghParser, run, CmdResult as R
from .base import DebugArghParser, get_usage_string, run, CmdResult as R


@pytest.mark.xfail(reason='TODO')
Expand Down Expand Up @@ -760,6 +760,20 @@ def func():
assert func.__doc__ in p.format_help()


def test_prog():
"Program name propagates from sys.argv[0]"

def cmd(foo=1):
return foo

p = DebugArghParser()
p.add_commands([cmd])

usage = get_usage_string()

assert run(p, '') == R(out=usage, err='')


def test_unknown_args():

def cmd(foo=1):
Expand All @@ -768,8 +782,9 @@ def cmd(foo=1):
p = DebugArghParser()
p.set_default_command(cmd)

usage = get_usage_string('[-f FOO]')

assert run(p, '--foo 1') == R(out='1\n', err='')
assert run(p, '--bar 1', exit=True) == 'unrecognized arguments: --bar 1'
assert run(p, '--bar 1', exit=False,
kwargs={'skip_unknown_args': True}) == \
R(out='usage: py.test [-h] [-f FOO]\n\n', err='')
kwargs={'skip_unknown_args': True}) == R(out=usage, err='')

0 comments on commit b8ee9c3

Please sign in to comment.