예제 #1
0
def test_duplicate_output():
    """An error is raised if within a single bundle, two jobs override
    each other.
    """
    assert_raises(
        BundleError, bundle_to_joblist,
        Bundle(Bundle('s1', output='foo'), Bundle('s2', output='foo')))
예제 #2
0
 def test_input_before_output_nested_merged(self):
     """Same thing as above - a parent input filter is passed done -
     but this time, ensure that duplicate filters are not applied twice.
     """
     child_bundle = Bundle('1', '2', filters=AppendFilter(input='-child'))
     parent_bundle = Bundle(child_bundle, output='out',
                            filters=AppendFilter(input='-parent'))
     parent_bundle.build()
     assert self.get('out') == 'foo-child\nfoo-child'
예제 #3
0
def test_filter_assign():
    """Test the different ways we can assign filters to the bundle.
    """

    class TestFilter(Filter):
        pass

    def _assert(list, length):
        """Confirm that everything in the list is a filter instance, and
        that the list as the required length."""
        assert len(list) == length
        assert bool([f for f in list if isinstance(f, Filter)])

    # Comma-separated string.
    b = Bundle(filters='jsmin,cssutils')
    _assert(b.filters, 2)

    # List of strings.
    b = Bundle(filters=['jsmin', 'cssutils'])
    _assert(b.filters, 2)
    # Strings inside a list may not be further comma separated
    assert_raises(ValueError, Bundle, filters=['jsmin,cssutils'])

    # A single or multiple classes may be given
    b = Bundle(filters=TestFilter)
    _assert(b.filters, 1)
    b = Bundle(filters=[TestFilter, TestFilter, TestFilter])
    _assert(b.filters, 3)

    # A single or multiple instance may be given
    b = Bundle(filters=TestFilter())
    _assert(b.filters, 1)
    b = Bundle(filters=[TestFilter(), TestFilter(), TestFilter()])
    _assert(b.filters, 3)

    # You can mix instances and classes
    b = Bundle(filters=[TestFilter, TestFilter()])
    _assert(b.filters, 2)

    # If something is wrong, an error is raised right away.
    assert_raises(ValueError, Bundle, filters='notreallyafilter')
    assert_raises(ValueError, Bundle, filters=object())

    # [bug] Specifically test that we can assign ``None``.
    Bundle().filters = None

    # Changing filters after bundle creation is no problem, either.
    b = Bundle()
    assert b.filters is ()
    b.filters = TestFilter
    _assert(b.filters, 1)

    # Assigning the own filter list should change nothing.
    old_filters = b.filters
    b.filters = b.filters
    assert b.filters == old_filters
예제 #4
0
def url(src, *args, **kwargs):
    src = src.format(*args, **kwargs).replace('//', '/')
    try:
        ret = url._cache[src]
    except KeyError:
        bundle = Bundle(src, output=src, merge=False)
        urls = bundle.urls(env=get_env(), binary=True)
        ret = urls[0]
        url._cache[src] = ret
    return ret
예제 #5
0
 def test_input_before_output_nested(self):
     """Ensure that when nested bundles are used, a parent bundles
     input filters are applied before a child bundles output filter.
     """
     child_bundle_with_output_filter = Bundle('1', '2',
             filters=ReplaceFilter(output=('foo', 'output was here')))
     parent_bundle_with_input_filter = Bundle(child_bundle_with_output_filter,
             output='out',
             filters=ReplaceFilter(input=('foo', 'input was here')))
     parent_bundle_with_input_filter.build()
     assert self.get('out') == 'input was here\ninput was here'
예제 #6
0
def test_nested():
    """If the bundle structure is nested, it is flattened.
    """
    b = Bundle('s1', Bundle('s2', Bundle('s3')), output='foo')
    jl = bundle_to_joblist(b)
    assert len(jl) == 1
    assert jl.keys()[0] == 'foo'
    assert len(jl['foo']) == 3
    assert jl['foo'][0][1] == ['s1']
    assert jl['foo'][1][1] == ['s2']
    assert jl['foo'][2][1] == ['s3']
예제 #7
0
def test_no_output_but_filters():
    """If a container specifies filters, those filters are applied to
    the sub-bundles.
    """
    jl = bundle_to_joblist(
        Bundle(Bundle('s1', output='foo'),
               Bundle('s2', output='bar', filters=[js]),
               filters=[css]))
    assert jl['foo'][0][0] == [css]
    assert jl['foo'][0][1] == ['s1']
    assert jl['bar'][0][0] == [js, css]
    assert jl['bar'][0][1] == ['s2']
예제 #8
0
def test_filter_merge():
    """Test that filter lists in a nested bundle structure are
    properly merged.
    """
    b = Bundle('s1',
               Bundle('s2', Bundle('s3', filters=[css, sass]), filters=[js]),
               output='foo')
    jl = bundle_to_joblist(b)
    assert jl['foo'][0][0] == []
    assert jl['foo'][0][1] == ['s1']
    assert jl['foo'][1][0] == [js]
    assert jl['foo'][1][1] == ['s2']
    assert jl['foo'][2][0] == [css, sass, js]
    assert jl['foo'][2][1] == ['s3']
예제 #9
0
def test_single_bundle():
    """Test registering a single ``Bundle`` object.
    """

    b = Bundle()
    register('foo', b)
    assert _get() == b
예제 #10
0
def test_duplicate():
    """Test name clashes.
    """

    b1 = Bundle()
    b2 = Bundle()

    register('foo', b1)

    # Multiple calls with the same name are ignored if the given bundle
    # is the same as originally passed.
    register('foo', b1)
    assert len(_REGISTRY) == 1

    # Otherwise, an error is raised.
    assert_raises(RegistryError, register, 'foo', b2)
    assert_raises(RegistryError, register, 'foo', 's1', 's2', 's3')
예제 #11
0
def test_debug_inheritance():
    """Test the bundle ``debug`` setting in a nested scenario.
    """
    sub2 = Bundle('s4', filters=[js], output="bar")
    sub1 = Bundle('s3', sub2, debug='merge', output='foo', filters=[css])
    b = Bundle('s1', 's2', sub1, filters=[js])

    jl = bundle_to_joblist(b)
    assert len(jl) == 3
    assert 's1' in jl and 's2' in jl
    assert jl['foo'][0][0] == []
    assert jl['foo'][1][0] == []

    sub2.debug = True
    jl = bundle_to_joblist(b)
    assert len(jl) == 4
    assert 's1' in jl and 's2' in jl and 's4' in jl
    assert jl['foo'][0][0] == []
예제 #12
0
def test_unmergable():
    """A subbundle that is unmergable will be pulled into a separate job.
    """
    b = Bundle('s1',
               's2',
               Bundle('s3', debug=False, filters=css, output="bar"),
               output='foo',
               filters=js)
    jl = bundle_to_joblist(b)
    assert len(jl) == 3
    assert 's1' in jl and 's2' in jl
    assert jl['bar'][0][0] == [css]
    assert 's3' in jl['bar'][0][1]

    # However, the bundle that is pulled up needs to have it's own output
    # target, or we can't continue.
    assert_raises(BundleError, bundle_to_joblist,
                  Bundle('s1', Bundle('s2', debug=False), output='foo'))
예제 #13
0
def test_flat():
    """If the bundle structure is already flat, we don't have to do much.
    """
    b = Bundle('s1', 'a2', output='foo')
    jl = bundle_to_joblist(b)
    assert len(jl) == 1
    assert jl.keys()[0] == 'foo'
    assert len(jl['foo']) == 1
    assert len(jl['foo'][0][1]) == 2
예제 #14
0
def test_debug_inheritance():
    """Test the bundle ``debug`` setting in a nested scenario.
    """
    sub2 = Bundle('s4', filters=[js], output="bar")
    sub1 = Bundle('s3', sub2, debug='merge', output='foo', filters=[css])
    b = Bundle('s1', 's2', sub1, filters=[js])

    jl = bundle_to_joblist(b)
    assert len(jl) == 3
    assert 's1' in jl and 's2' in jl
    assert jl['foo'][0][0] == []
    assert jl['foo'][1][0] == []

    sub2.debug = True
    jl = bundle_to_joblist(b)
    assert len(jl) == 4
    assert 's1' in jl and 's2' in jl and 's4' in jl
    assert jl['foo'][0][0] == []
예제 #15
0
def test_debug_merge_only():
    """Test the 'merge only' debug option (no filters).
    """
    sub = Bundle('s3', filters=[css], output="bar")
    b = Bundle('s1', 's2', sub, output='foo', filters=[js])
    jl = bundle_to_joblist(b)
    assert len(jl) == 1
    assert jl['foo'][0][0] == []
    assert jl['foo'][1][0] == []

    sub.debug = False
    jl = bundle_to_joblist(b)
    assert len(jl) == 1
    assert jl['foo'][0][0] == []
    assert jl['foo'][1][0] == [css]

    sub.debug = True
    jl = bundle_to_joblist(b)
    assert len(jl) == 2
    assert jl['foo'][0][0] == []
예제 #16
0
def test_debug_merge_only():
    """Test the 'merge only' debug option (no filters).
    """
    sub = Bundle('s3', filters=[css], output="bar")
    b = Bundle('s1', 's2', sub, output='foo', filters=[js])
    jl = bundle_to_joblist(b)
    assert len(jl) == 1
    assert jl['foo'][0][0] == []
    assert jl['foo'][1][0] == []

    sub.debug = False
    jl = bundle_to_joblist(b)
    assert len(jl) == 1
    assert jl['foo'][0][0] == []
    assert jl['foo'][1][0] == [css]

    sub.debug = True
    jl = bundle_to_joblist(b)
    assert len(jl) == 2
    assert jl['foo'][0][0] == []
예제 #17
0
def test_no_output():
    """Each file in a bundle needs an output target if it is supposed
    to be merged. An error is raised if no target is available.
    """

    # The root bundle is lacking an output option.
    assert_raises(BundleError, bundle_to_joblist, Bundle('s1', 's2'))

    # Also, if the output is missing merely in a subtree, that's ok.
    bundle_to_joblist(Bundle('s1', Bundle('s2'), output='foo'))

    # If the root bundle is merely a container, that's ok, as long as
    # all the sub-bundles have their own output target.
    bundle_to_joblist(
        Bundle(Bundle('s1', output='foo'), Bundle('s2', output='bar')))
    assert_raises(BundleError, bundle_to_joblist,
                  Bundle(Bundle('s1', output='foo'), Bundle('s2')))
예제 #18
0
    def get_merged_bundles(self):
        files_by_ext = self.get_files_by_ext()

        for js_or_css in files_by_ext.values():
            for ext_files in js_or_css.values():
                ext_files["files"] = self._clean_duplicates(ext_files.get("files", []))
                ext_files["filters"] = self._clean_duplicates(ext_files.get("filters", []))

        css_bundles = []
        js_bundles = []

        for js_or_css, js_or_css_files in files_by_ext.items():
            bundles = css_bundles if js_or_css == "css" else js_bundles
            for ext, js_css in js_or_css_files.items():
                if js_css["filters"] is None:
                    raise ImproperlyConfigured('You need to specify ASSETS_DEFAULT_CSS_FILTERS in your Django settings file')
                elif js_css["filters"] == '':
                    bundles.append(Bundle(*js_css["files"], output="%s/js/%s_%s.%s" % (self.path, self.name, ext, js_or_css), debug=self.debug))
                else:
                    bundles.append(Bundle(*js_css["files"], filters=js_css["filters"], output="%s/js/%s_%s.%s" % (self.path, self.name, ext, js_or_css), debug=self.debug))

        return css_bundles, js_bundles
예제 #19
0
def test_new_bundle():
    """Test registering a new bundle on the fly.
    """

    b = Bundle()
    register('foo', b, 's2', 's3')
    assert b in _get('foo').contents

    # Special case of using only a single, non-bundle source argument.
    register('footon', 's1')
    assert 's1' in _get('footon').contents

    # Special case of specifying only a single bundle as a source, but
    # additional options - this also creates a wrapping bundle.
    register('foofighters', b, output="bar")
    assert b in _get('foofighters').contents
예제 #20
0
from django_assets import Bundle, register
from webassets.filter import get_filter

thirdparty_path = os.path.join(settings.BASE_DIR, "node_modules")
sass = get_filter("scss",
                  style="compressed",
                  load_paths=("./css", thirdparty_path))

uglify_args = ["--comments", "/^!/", "-m", "-c"]
uglify = get_filter("uglifyjs",
                    binary=os.path.join(thirdparty_path, ".bin", "uglifyjs"),
                    extra_args=uglify_args)

css = Bundle(
    "css/inboxen.scss",
    filters=(sass, ),
    output="compiled/css/website.%(version)s.css",
)
register("inboxen_css", css)

js = Bundle(
    "thirdparty/jquery/dist/jquery.js",
    "js/utils.js",
    "js/copy.js",
    "js/alert.js",
    "js/home.js",
    "js/search.js",
    "js/inbox.js",
    filters=(uglify, ),
    output="compiled/js/website.%(version)s.js",
)
예제 #21
0
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

from django_assets import Bundle, register

js_common = Bundle('js/jquery/jquery.js',
                   'js/jquery/jquery.tipsy.js',
                   'js/jquery/jquery.cookie.js',
                   'js/jquery/jquery.bidi.js',
                   'js/jquery/jquery.fancybox.js',
                   'js/common.js',
                   'js/sorttable.js',
                   'js/shortcut.js',
                   filters='rjsmin',
                   output='js/common.min.js')
register('js_common', js_common)

js_admin = Bundle('js/jquery/jquery.form.js',
                  'js/admin.js',
                  filters='rjsmin',
                  output='js/admin.min.js')
register('js_admin', js_admin)

js_editor = Bundle('js/jquery/jquery.easing.js',
                   'js/jquery/jquery.history.js',
                   'js/jquery/jquery.tmpl.js',
예제 #22
0
from django_assets import Bundle, register

JS_ASSETS = (
    'js/jquery-1.7.1.min.js',
    'js/jquery-ui-1.10.3.custom.min.js',
    'js/jquery.uniform.js',
    'js/chosen.jquery.js',
    'bootstrap/js/bootstrap.js',
)

CSS_ASSETS = (
    'bootstrap/css/bootstrap.css',
    'bootstrap/css/bootstrap-responsive.css',
    'css/uniform.default.css',
    'css/chosen.css',
    'css/style.css',
)

js = Bundle(*JS_ASSETS, filters='jsmin', output='gen/packed.js')
css = Bundle(*CSS_ASSETS, filters='cssmin', output='gen/packed.css')

register('js', js)
register('css', css)
예제 #23
0
# Copyright (c) 2018, DjaoDjin inc.
# see LICENSE

import os

from django_assets import Bundle, register
from django.conf import settings

#pylint: disable=invalid-name

# All the CSS we need for the entire site. This tradeoff between
# bandwidth and latency is good as long as we have a high and consistent
# utilization of all the CSS tags for all pages on the site.
css_base = Bundle(os.path.join(settings.BASE_DIR,
                               'assets/less/base/base.less'),
                  filters=['less', 'cssmin'],
                  output='cache/base.css',
                  debug=False)
register('css_base', css_base)

css_email = Bundle(os.path.join(settings.BASE_DIR,
                                'assets/less/email/email.less'),
                   filters=['less', 'cssmin'],
                   output='cache/email.css',
                   debug=False)
register('css_email', css_email)

# Minimal: jquery and bootstrap always active on the site
js_base = Bundle('vendor/jquery.js',
                 'vendor/jquery.cookie.js',
                 'vendor/bootstrap.js',
예제 #24
0
from django_assets import Bundle, register

js_common = Bundle(
    'js/vendor/jquery/jquery.js',
    'js/vendor/jquery/jquery.tipsy.js',
    'js/vendor/jquery/jquery.cookie.js',
    'js/vendor/jquery/jquery.bidi.js',
    'js/vendor/jquery/jquery.magnific-popup.js',
    'js/vendor/jquery/jquery.utils.js',
    'js/vendor/jquery/jquery.easing.js',
    'js/vendor/jquery/jquery.serializeObject.js',
    'js/vendor/jquery/jquery.select2.js',
    'js/vendor/bootstrap/bootstrap-alert.js',
    'js/vendor/bootstrap/bootstrap-transition.js',
    'js/captcha.js',
    'js/common.js',
    'js/languages.js',
    'js/contact.js',
    'js/search.js',
    'js/stats.js',
    'js/utils.js',
    'js/zoom.js',
    'js/vendor/sorttable.js',
    'js/vendor/spin.js',
    'js/vendor/shortcut.js',  # Leave shortcut.js as the last one.
    filters='rjsmin',
    output='js/common.min.%(version)s.js')
register('js_common', js_common)

js_admin = Bundle('js/admin.js',
예제 #25
0
 def __init__(self, *a, **kw):
     Bundle.__init__(self, *a, **kw)
     self.env = get_env()
     # Kind of hacky, but gives us access to the last Bundle
     # instance used by our Django template tag.
     test_instance.the_bundle = self
예제 #26
0
    def test_cache_enabled(self):
	bundle = Bundle('in1', 'in2', output='out', filters="jsmin")
	self.cache.enabled = True
	bundle.build()
	assert_equals(self.cache.getops, 3)
	assert_equals(self.cache.setops, 0)