Exemplo n.º 1
0
    def test_main_basic_run_source_file_sourcemap_reldir(self):
        root = self.mkdtemp()
        source = join(root, 'source.js')
        with open(source, 'w') as fd:
            fd.write('var foo = "bar";')

        self.chdir(root)
        dest = join(root, 'dest.js')
        # manually specified name
        dest_map = join(root, 'dest.map')
        with self.assertRaises(SystemExit) as e:
            runtime.main('crimp', 'source.js', '-O', 'dest.js', '-s',
                         'dest.map')

        self.assertEqual(e.exception.args[0], 0)

        with open(dest) as fd:
            self.assertEqual(
                'var foo="bar";\n//# sourceMappingURL=dest.map\n',
                fd.read(),
            )

        # implied sourcemap file should be created at the same level at
        # the same dirname.
        with open(dest_map) as fd:
            mapping = json.loads(fd.read())
            self.assertEqual(
                {
                    'version': 3,
                    'file': 'dest.js',
                    'mappings': 'AAAA,OAAQ,CAAE',
                    'names': [],
                    'sources': ['source.js'],
                }, mapping)
Exemplo n.º 2
0
    def test_version_check(self):
        self.stub_stdio()
        with self.assertRaises(SystemExit) as e:
            runtime.main('crimp', '--version')

        self.assertEqual(e.exception.args[0], 0)
        self.assertTrue(sys.stdout.getvalue().startswith('crimp'))
Exemplo n.º 3
0
    def test_main_basic_inline_sourcemap(self):
        root = self.mkdtemp()
        source = join(root, 'source.js')
        with open(source, 'w') as fd:
            fd.write('var foo = "bar";')

        self.chdir(root)
        dest = join(root, 'dest.js')
        with self.assertRaises(SystemExit) as e:
            runtime.main('crimp', 'source.js', '-O', 'dest.js', '-s',
                         'dest.js')

        self.assertEqual(e.exception.args[0], 0)

        with open(dest) as fd:
            raw = fd.read()

        self.assertIn(
            'var foo="bar";\n//# sourceMappingURL=data:application/json',
            raw,
        )

        self.assertEqual(
            {
                "version": 3,
                "sources": ["source.js"],
                "names": [],
                "mappings": "AAAA,OAAQ,CAAE",
                "file": "dest.js"
            },
            json.loads(
                base64.b64decode(raw.splitlines()[-1].split(',')[-1].encode(
                    'utf8')).decode('utf8')))
Exemplo n.º 4
0
    def test_pretty_print_stdio_text(self):
        self.stub_stdio_text()
        sys.stdin.write(u'var foo="bar"')
        sys.stdin.seek(0)

        with self.assertRaises(SystemExit) as e:
            runtime.main('crimp', '-ps')

        self.assertEqual(e.exception.args[0], 0)
        self.assertIn(u'var foo = "bar";\n\n//# sourceMappingURL=',
                      sys.stdout.getvalue())
Exemplo n.º 5
0
    def test_unicode_decode_error(self):
        self.stub_stdio()
        root = self.mkdtemp()
        source = join(root, 'source.js')
        with open(source, 'wb') as fd:
            fd.write(b'var \x82\xcd\x82\xa2 = 1;')

        with self.assertRaises(SystemExit) as e:
            runtime.main('crimp', source, '--encoding=utf-8')

        self.assertEqual(e.exception.args[0], 1)
        self.assertIn("codec can't decode byte 0x82", sys.stderr.getvalue())
Exemplo n.º 6
0
    def test_main_basic_run_source_file(self):
        root = self.mkdtemp()
        source = join(root, 'source.js')
        with open(source, 'w') as fd:
            fd.write('var foo = "bar";')

        self.stub_stdio()
        with self.assertRaises(SystemExit) as e:
            runtime.main('crimp', source)

        self.assertEqual(e.exception.args[0], 0)
        self.assertEqual('var foo="bar";', sys.stdout.getvalue())
Exemplo n.º 7
0
    def test_main_basic_run_source_file_sourcemap_stdout(self):
        root = self.mkdtemp()
        source = join(root, 'source.js')
        with open(source, 'w') as fd:
            fd.write('var foo = "bar";')

        self.stub_stdio()
        with self.assertRaises(SystemExit) as e:
            runtime.main('crimp', source, '-s')

        self.assertEqual(e.exception.args[0], 0)
        self.assertIn(
            'var foo="bar";\n//# sourceMappingURL=data:application/json',
            sys.stdout.getvalue())
Exemplo n.º 8
0
    def test_main_basic_run_source_file_dest_file(self):
        root = self.mkdtemp()
        source = join(root, 'source.js')
        dest = join(root, 'dest.js')
        with open(source, 'w') as fd:
            fd.write('var foo = "bar";')

        with self.assertRaises(SystemExit) as e:
            runtime.main('crimp', source, '-O', dest)

        self.assertEqual(e.exception.args[0], 0)

        with open(dest) as fd:
            self.assertEqual('var foo="bar";', fd.read())
Exemplo n.º 9
0
    def test_unicode_encode_error(self):
        self.stub_stdio_bytes()
        sys.stdin.encoding = 'shift_jis'
        sys.stdin.write(b'var \x82\xcd\x82\xa2 = "yes"')
        sys.stdin.seek(0)

        root = self.mkdtemp()
        dest = join(root, 'dest.js')

        with self.assertRaises(SystemExit) as e:
            runtime.main('crimp', '--encoding=ascii', '-O', dest)

        self.assertEqual(e.exception.args[0], 1)
        self.assertIn("'ascii' codec can't encode characters",
                      sys.stderr.getvalue())
Exemplo n.º 10
0
    def test_source_error(self):
        self.stub_stdio()
        root = self.mkdtemp()
        source = join(root, 'source.js')
        with open(source, 'w') as fd:
            fd.write("function(){};")

        with self.assertRaises(SystemExit) as e:
            # not actually using '-m' to avoid new flags from future
            # versions
            runtime.main('crimp', source)

        self.assertEqual(e.exception.args[0], 1)
        self.assertEqual(
            "Function statement requires a name at 1:9 in %r\n" % source,
            sys.stderr.getvalue())
Exemplo n.º 11
0
    def test_main_all_mangle(self):
        root = self.mkdtemp()
        source = join(root, 'source.js')
        with open(source, 'w') as fd:
            fd.write(
                dedent("""
            (function(root) {
              var bar = 'bar';

              var foo = function() {
                return bar;
              };

              root.name = 'demo';
              root.foo = foo;
              root.bar = bar;
            })(window)
            """).lstrip())

        self.chdir(root)
        dest = join(root, 'dest.js')
        with self.assertRaises(SystemExit) as e:
            # not actually using '-m' to avoid new flags from future
            # versions
            runtime.main('crimp', 'source.js', '-O', 'dest.js', '-s',
                         '--obfuscate', '--drop-semi')

        self.assertEqual(e.exception.args[0], 0)

        with open(dest) as fd:
            self.assertEqual(
                "(function(a){var b='bar';var c=function(){return b};"
                "a.name='demo';a.foo=c;a.bar=b})(window)\n"
                "//# sourceMappingURL=dest.js.map\n",
                fd.read(),
            )

        # implied sourcemap file should be created at the same level at
        # the same dirname.
        # resolve sourcemap name
        dest_map = join(root, 'dest.js.map')
        with open(dest_map) as fd:
            mapping = json.loads(fd.read())
            self.assertEqual(['root', 'bar', 'foo'], mapping['names'])
            self.assertEqual(['source.js'], mapping['sources'])
Exemplo n.º 12
0
    def test_write_error_fallback(self):
        def error():
            raise IOError()  # not a real valid one, but...

        self.stub_stdio()
        # cheap way to emulate this, normally this happen with a file...
        sys.stdout.close = error

        root = self.mkdtemp()
        source = join(root, 'source.js')
        with open(source, 'w') as fd:
            fd.write('var foo = 1;')

        with self.assertRaises(SystemExit) as e:
            # not actually using '-m' to avoid new flags from future
            # versions
            runtime.main('crimp', source)

        self.assertEqual(e.exception.args[0], 5)
Exemplo n.º 13
0
    def test_write_error(self):
        def error():
            raise OSError(28, 'No space left on device')

        self.stub_stdio()
        # cheap way to emulate this, normally this happen with a file...
        sys.stdout.close = error

        root = self.mkdtemp()
        source = join(root, 'source.js')
        with open(source, 'w') as fd:
            fd.write('var foo = 1;')

        with self.assertRaises(SystemExit) as e:
            # not actually using '-m' to avoid new flags from future
            # versions
            runtime.main('crimp', source)

        self.assertEqual(e.exception.args[0], 28)
        self.assertIn("No space left on device", sys.stderr.getvalue())
Exemplo n.º 14
0
    def test_main_keyboard_interrupt_source_file_dest_file(self):
        self.stub_stdio()

        def keyboard_break():
            raise KeyboardInterrupt

        sys.stdin.read = keyboard_break

        root = self.mkdtemp()
        dest = join(root, 'source.js')
        # This will at least stop the case where only one file being
        # listed as the outfile without any input from overwriting a
        # potential source file.
        with self.assertRaises(SystemExit) as e:
            runtime.main('crimp', '-O', dest)

        # the exit code for keyboard interrupted.
        self.assertEqual(e.exception.args[0], 130)
        # ensure destination file is NOT created (which could be an
        # accidentally specified source file that could have been
        # unintentionally be overwritten otherwise).
        self.assertFalse(exists(dest))
Exemplo n.º 15
0
# -*- coding: utf-8 -*-
import sys
from crimp.runtime import main

if __name__ == '__main__':  # pragma: no cover
    main(*sys.argv)