diff --git a/compressor/tests/precompiler.py b/compressor/tests/precompiler.py index 6cc185a..9c2ac13 100644 --- a/compressor/tests/precompiler.py +++ b/compressor/tests/precompiler.py @@ -1,17 +1,33 @@ #!/usr/bin/env python import optparse +import sys def main(): p = optparse.OptionParser() + p.add_option('-f', '--file', action="store", + type="string", dest="filename", + help="File to read from, defaults to stdin", default=None) + p.add_option('-o', '--output', action="store", + type="string", dest="outfile", + help="File to write to, defaults to stdout", default=None) + options, arguments = p.parse_args() - f = open(arguments[0]) - content = f.read() - f.close() + if options.filename: + f = open(options.filename) + content = f.read() + f.close() + else: + content = sys.stdin.read() - f = open(arguments[1], 'w') - f.write(content.replace('background:', 'color:')) - f.close() + content = content.replace('background:', 'color:') + + if options.outfile: + f = open(options.outfile, 'w') + f.write(content) + f.close() + else: + print content if __name__ == '__main__': diff --git a/compressor/tests/tests.py b/compressor/tests/tests.py index afa0eaa..324fb55 100644 --- a/compressor/tests/tests.py +++ b/compressor/tests/tests.py @@ -485,13 +485,35 @@ CssTidyTestCase = skipIf( 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): + def test_precompiler_infile_outfile(self): from compressor.filters.base import CompilerFilter - output = CompilerFilter(content=self.content, filename=self.filename, command=self.command).output() + command = 'python %s/precompiler.py -f {infile} -o {outfile}' % os.path.dirname(__file__) + output = CompilerFilter(content=self.content, filename=self.filename, command=command).output() self.assertEqual(u"body { color:#990; }", output) + + def test_precompiler_stdin_outfile(self): + from compressor.filters.base import CompilerFilter + command = 'python %s/precompiler.py -o {outfile}' % os.path.dirname(__file__) + output = CompilerFilter(content=self.content, filename=None, command=command).output() + self.assertEqual(u"body { color:#990; }", output) + + def test_precompiler_stdin_stdout(self): + from compressor.filters.base import CompilerFilter + command = 'python %s/precompiler.py' % os.path.dirname(__file__) + output = CompilerFilter(content=self.content, filename=None, command=command).output() + self.assertEqual(u"body { color:#990; }\n", output) + + def test_precompiler_infile_stdout(self): + from compressor.filters.base import CompilerFilter + command = 'python %s/precompiler.py -f {infile}' % os.path.dirname(__file__) + output = CompilerFilter(content=self.content, filename=None, command=command).output() + self.assertEqual(u"body { color:#990; }\n", output) + + + +