8000 Allow customizing the backend of ManageAssets. · woodcoder/flask-assets@41d047a · GitHub
[go: up one dir, main page]

Skip to content

Commit 41d047a

Browse files
committed
Allow customizing the backend of ManageAssets.
Also, this commit fixes a broken test.
1 parent 2fba71b commit 41d047a

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

src/flask_assets.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def init_app(self, app):
213213
pass
214214
else:
215215
import argparse
216+
from webassets.script import GenericArgparseImplementation
216217

217218
class CatchAllParser(object):
218219
def parse_known_args(self, app_args):
@@ -222,8 +223,9 @@ class ManageAssets(script.Command):
222223
"""Manage assets."""
223224
capture_all_args = True
224225

225-
def __init__(self, assets_env=None):
226+
def __init__(self, assets_env=None, impl=GenericArgparseImplementation):
226227
self.env = assets_env
228+
self.implementation = impl
227229

228230
def create_parser(self, prog):
229231
return CatchAllParser()
@@ -246,8 +248,6 @@ def run(self, args):
246248
import sys, os.path
247249
prog = '%s %s' % (os.path.basename(sys.argv[0]), sys.argv[1])
248250

249-
from webassets import script
250-
return script.GenericArgparseImplementation(
251-
self.env, prog=prog).main(args)
251+
return self.implementation(self.env, prog=prog).main(args)
252252

253253
__all__ = __all__ + ('ManageAssets',)

tests/test_script.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from nose import SkipTest
55
from flask import Flask
66
from flask.ext.assets import Environment, ManageAssets
7+
from webassets.script import GenericArgparseImplementation
78

89
try:
910
from flaskext.script import Manager
@@ -21,28 +22,25 @@ def test_call(self):
2122
# Setup the webassets.script with a mock main() function,
2223
# so we can check whether our call via Flask-Script actually
2324
# goes through.
24-
def dummy_main(argv, *a, **kw):
25-
self.last_script_call = argv
26-
return 0
27-
from webassets import script
28-
old_main = script.main
29-
script.main = dummy_main
25+
test_inst = self
26+
class DummyArgparseImplementation(GenericArgparseImplementation):
27+
def run_with_argv(self, argv):
8000 28+
test_inst.last_script_call = argv
29+
return 0
30+
31+
mgmt = Manager(self.app)
32+
mgmt.add_command('assets',
33+
ManageAssets(self.env, impl=DummyArgparseImplementation))
3034

3135
try:
32-
mgmt = Manager(self.app)
33-
mgmt.add_command('assets', ManageAssets(self.env))
34-
35-
try:
36-
# -h is a great test as that is something Flask-Script might
37-
# want to claim for itself.
38-
sys.argv = ['./manage.py', 'assets', '-h']
39-
mgmt.run()
40-
except SystemExit:
41-
# Always raised, regardless of success or failure of command
42-
pass
43-
assert self.last_script_call == ['-h']
44-
finally:
45-
script.main = old_main
36+
# -h is a great test as that is something Flask-Script might
37+
# want to claim for itself.
38+
sys.argv = ['./manage.py', 'assets', '-h']
39+
mgmt.run()
40+
except SystemExit:
41+
# Always raised, regardless of success or failure of command
42+
pass
43+
assert self.last_script_call == ['-h']
4644

4745
def test_call_auto_env(self):
4846
"""Regression test: Passing the environment to the ManageAssets command

0 commit comments

Comments
 (0)
0