Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Submanager uses wrong app instance #161

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions flask_script/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ def __call__(self, app=None, **kwargs):

If your sub-Manager does not override this, any values for options will get lost.
"""
# If we have a parent, we are a sub-manager
if self.parent and self.app:
# call our own app and use the result if it's not None
res = self.app(app=app, **kwargs)
if res != None:
app = res

if app is None:
app = self.app
if app is None:
Expand Down
26 changes: 25 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,20 @@ def __exit__(self, a,b,c):
pass

class AppForTesting(object):
name = "APP"
def __init__(self, verbose=False):
self.verbose = verbose
def test_request_context(self):
return EmptyContext()
def __call__(self,**kw):
if self.verbose:
print("APP "+" ".join("%s=%s" % (k,v) for k,v in kw.items()))
print("{} {}".format(self.name,
" ".join("%s=%s" % (k,v) for k,v in kw.items())))
return self

class SubAppForTesting(AppForTesting):
name = "SUB"


class TestManager:

Expand Down Expand Up @@ -804,6 +809,25 @@ def test_submanager_separate_options(self, capsys):
assert 'APP name_sub=MySubName' in out
assert 'OK name=MyName' in out

def test_submanager_overrides_app(self, capsys):
def gen_app(app, **kwargs):
return SubAppForTesting(verbose=True)
sub_manager = Manager(gen_app, with_default_commands=False)
sub_manager.add_command('opt', CommandWithOptionalArg())
sub_manager.add_option('-n', '--name', dest='name_sub', required=False)

manager = Manager(AppForTesting(verbose=True), with_default_commands=False)
manager.add_command('sub_manager', sub_manager)
manager.add_option('-n', '--name', dest='name_main', required=False)

code = run('manage.py -n MyMainName sub_manager -n MySubName opt -n MyName', manager.run)
out, err = capsys.readouterr()
assert code == 0
assert 'APP name_main=MyMainName' in out
assert 'SUB name_sub=MySubName' in out
assert 'OK name=MyName' in out


def test_manager_usage_with_submanager(self, capsys):

sub_manager = Manager(usage='Example sub-manager')
Expand Down