Added test and basic precompiler script

Also fixed the compiler opening the file without needing it
This commit is contained in:
Harro van der Klauw
2011-05-12 17:25:56 +02:00
parent eff6c8f7d3
commit 7afa6cb58c
3 changed files with 36 additions and 3 deletions

View File

@@ -55,9 +55,9 @@ class CompilerFilter(FilterBase):
infile = tempfile.NamedTemporaryFile(mode='w')
infile.write(self.content)
infile.flush()
self.options["infile"] = infile.name
else:
infile = open(self.filename)
self.options["infile"] = infile.name
self.options["infile"] = self.filename
if "{outfile}" in self.command:
ext = ".%s" % self.type and self.type or ""
outfile = tempfile.NamedTemporaryFile(mode='w', suffix=ext)
@@ -65,7 +65,7 @@ class CompilerFilter(FilterBase):
cmd = stringformat.FormattableString(self.command).format(**self.options)
proc = subprocess.Popen(cmd_split(cmd),
stdout=self.stdout, stdin=self.stdin, stderr=self.stderr)
if infile is not None:
if infile is not None or self.filename is not None:
filtered, err = proc.communicate()
else:
filtered, err = proc.communicate(self.content)

View File

@@ -0,0 +1,19 @@
#!/usr/bin/env python
import optparse
def main():
p = optparse.OptionParser()
options, arguments = p.parse_args()
f = open(arguments[0])
content = f.read()
f.close()
f = open(arguments[1], 'w')
f.write(content.replace('background:', 'color:'))
f.close()
if __name__ == '__main__':
main()

View File

@@ -481,3 +481,17 @@ CssTidyTestCase = skipIf(
find_command(settings.COMPRESS_CSSTIDY_BINARY) is None,
'CSStidy binary %r not found' % settings.COMPRESS_CSSTIDY_BINARY
)(CssTidyTestCase)
class PrecompilerTestCase(TestCase):
def setUp(self):
self.command = 'python %s/precompiler.py {infile} {outfile}' % os.path.dirname(__file__)
self.filename = os.path.join(os.path.dirname(__file__), 'media/css/one.css')
f = open(self.filename, 'r')
self.content = f.read()
f.close()
def test_precompiler(self):
from compressor.filters.base import CompilerFilter
output = CompilerFilter(content=self.content, filename=self.filename, command=self.command).output()
self.assertEqual(u"body { color:#990; }", output)