示例#1
0
 def test_precompiler_cache_issue750(self, mock_cache):
     # emulate memcached and return string
     mock_cache.side_effect = (lambda key: str("body { color:#990; }"))
     command = '%s %s -f {infile} -o {outfile}' % (sys.executable,
                                                   self.test_precompiler)
     compiler = CachedCompilerFilter(command=command,
                                     **self.cached_precompiler_args)
     self.assertEqual("body { color:#990; }", compiler.input())
     self.assertEqual(type(compiler.input()),
                      type(smart_str("body { color:#990; }")))
示例#2
0
    def precompile(
        self, content, kind=None, elem=None, filename=None, charset=None, **kwargs
    ):
        """
        Processes file using a pre compiler.

        This is the place where files like coffee script are processed.
        """
        if not kind:
            return False, content
        attrs = self.parser.elem_attribs(elem)
        mimetype = attrs.get("type", None)
        if mimetype is None:
            return False, content

        filter_or_command = self.precompiler_mimetypes.get(mimetype)
        if filter_or_command is None:
            if mimetype in ("text/css", "text/javascript"):
                return False, content
            raise CompressorError(
                "Couldn't find any precompiler in "
                "COMPRESS_PRECOMPILERS setting for "
                "mimetype '%s'." % mimetype
            )

        mod_name, cls_name = get_mod_func(filter_or_command)
        try:
            mod = import_module(mod_name)
        except (ImportError, TypeError):
            filter = CachedCompilerFilter(
                content=content,
                filter_type=self.type,
                filename=filename,
                charset=charset,
                command=filter_or_command,
                mimetype=mimetype,
            )
            return True, filter.input(**kwargs)
        try:
            precompiler_class = getattr(mod, cls_name)
        except AttributeError:
            raise FilterDoesNotExist('Could not find "%s".' % filter_or_command)
        filter = precompiler_class(
            content,
            attrs=attrs,
            filter_type=self.type,
            charset=charset,
            filename=filename,
        )
        return True, filter.input(**kwargs)
示例#3
0
    def test_precompiler_caches_empty_files(self):
        command = '%s %s -f {infile} -o {outfile}' % (sys.executable,
                                                      self.test_precompiler)
        compiler = CachedCompilerFilter(command=command,
                                        **self.cached_precompiler_args)
        self.assertEqual("body { color:#990; }", compiler.input())

        cache.set(compiler.get_cache_key(), "")
        compiler = CachedCompilerFilter(command=command,
                                        **self.cached_precompiler_args)
        self.assertEqual("", compiler.input())
    def precompile(self, content, kind=None, elem=None, filename=None,
                   charset=None, **kwargs):
        """
        Processes file using a pre compiler.

        This is the place where files like coffee script are processed.
        """
        if not kind:
            return False, content
        attrs = self.parser.elem_attribs(elem)
        mimetype = attrs.get("type", None)
        if mimetype is None:
            return False, content

        filter_or_command = self.precompiler_mimetypes.get(mimetype)
        if filter_or_command is None:
            if mimetype in ("text/css", "text/javascript"):
                return False, content
            raise CompressorError("Couldn't find any precompiler in "
                                  "COMPRESS_PRECOMPILERS setting for "
                                  "mimetype '%s'." % mimetype)

        mod_name, cls_name = get_mod_func(filter_or_command)
        try:
            mod = import_module(mod_name)
        except (ImportError, TypeError):
            filter = CachedCompilerFilter(
                content=content, filter_type=self.type, filename=filename,
                charset=charset, command=filter_or_command, mimetype=mimetype)
            return True, filter.input(**kwargs)
        try:
            precompiler_class = getattr(mod, cls_name)
        except AttributeError:
            raise FilterDoesNotExist('Could not find "%s".' % filter_or_command)
        filter = precompiler_class(
            content, attrs=attrs, filter_type=self.type, charset=charset,
            filename=filename)
        return True, filter.input(**kwargs)
示例#5
0
    def test_precompiler_not_cacheable(self):
        command = '%s %s -f {infile} -o {outfile}' % (sys.executable,
                                                      self.test_precompiler)
        self.cached_precompiler_args['mimetype'] = 'text/different'
        compiler = CachedCompilerFilter(command=command,
                                        **self.cached_precompiler_args)
        self.assertEqual("body { color:#990; }", compiler.input())
        self.assertIsNotNone(compiler.infile)  # Not cached

        compiler = CachedCompilerFilter(command=command,
                                        **self.cached_precompiler_args)
        self.assertEqual("body { color:#990; }", compiler.input())
        self.assertIsNotNone(compiler.infile)  # Not cached
示例#6
0
    def test_precompiler_cache(self):
        # The cache may already have data in it depending on the order the tests are
        # run, so start by clearing it:
        cache.clear()
        command = '%s %s -f {infile} -o {outfile}' % (sys.executable,
                                                      self.test_precompiler)
        compiler = CachedCompilerFilter(command=command,
                                        **self.cached_precompiler_args)
        self.assertEqual("body { color:#990; }", compiler.input())
        # We tell whether the precompiler actually ran by inspecting compiler.infile. If not None, the compiler had to
        # write the input out to the file for the external command. If None, it was in the cache and thus skipped.
        self.assertIsNotNone(compiler.infile)  # Not cached

        compiler = CachedCompilerFilter(command=command,
                                        **self.cached_precompiler_args)
        self.assertEqual("body { color:#990; }", compiler.input())
        self.assertIsNone(compiler.infile)  # Cached

        self.cached_precompiler_args[
            'content'] += ' '  # Invalidate cache by slightly changing content
        compiler = CachedCompilerFilter(command=command,
                                        **self.cached_precompiler_args)
        self.assertEqual("body { color:#990; }", compiler.input())
        self.assertIsNotNone(compiler.infile)  # Not cached