Esempio n. 1
0
    def test_build_exception_testcase(self):
        class DemoTestCase(unittest.TestCase):
            def runTest(self):
                """Dummy run method for PY2"""

        testcase = DemoTestCase()
        logger = getLogger('demo_test_case')
        original_level = logger.level
        original_handlers = len(logger.handlers)
        setup_logger(testcase, logger)
        self.assertNotEqual(original_level, logger.level)
        self.assertNotEqual(original_handlers, len(logger.handlers))
        testcase.doCleanups()
        self.assertEqual(original_level, logger.level)
        self.assertEqual(original_handlers, len(logger.handlers))
Esempio n. 2
0
    def test_write_sourcemap_no_normalize(self):
        logs = setup_logger(self, sourcemap.logger, logging.WARNING)
        root = mktemp()
        output_stream = StringIO()
        output_stream.name = join(root, 'srcfinal.js')
        sourcemap_stream = StringIO()
        sourcemap_stream.name = join(root, 'srcfinal.js.map')
        mappings = [[(0, 0, 0, 0, 0)]]
        sources = [join(root, 'src1.js'), join(root, 'src2.js')]
        names = ['foo']

        sourcemap.write_sourcemap(
            mappings, sources, names, output_stream, sourcemap_stream,
            normalize_paths=False)

        self.assertEqual({
            "version": 3,
            "sources": sources,
            "names": ["foo"],
            "mappings": "AAAAA",
            "file": output_stream.name,
        }, json.loads(sourcemap_stream.getvalue()))
        self.assertEqual(
            '\n//# sourceMappingURL=%s\n' % sourcemap_stream.name,
            output_stream.getvalue())

        # ensure no warnings have been logged
        self.assertEqual('', logs.getvalue())
Esempio n. 3
0
    def test_write_sourcemap_no_paths(self):
        logs = setup_logger(self, sourcemap.logger, logging.WARNING)
        output_stream = StringIO()
        sourcemap_stream = StringIO()
        mappings = [[(0, 0, 0, 0, 0)]]
        sources = [sourcemap.INVALID_SOURCE]
        names = ['foo']

        sourcemap.write_sourcemap(
            mappings, sources, names, output_stream, sourcemap_stream)

        self.assertEqual({
            "version": 3,
            "sources": ['about:invalid'],
            "names": ["foo"],
            "mappings": "AAAAA",
            "file": "about:invalid",
        }, json.loads(sourcemap_stream.getvalue()))
        self.assertEqual(
            '\n//# sourceMappingURL=about:invalid\n',
            output_stream.getvalue())

        # warnings about the about:invalid.
        self.assertIn(
            "sourcemap.file is either undefine or invalid - "
            "it is replaced with 'about:invalid'", logs.getvalue())
        self.assertIn(
            "sourcemap.sources[0] is either undefine or invalid - "
            "it is replaced with 'about:invalid'", logs.getvalue())
        self.assertIn(
            "sourceMappingURL is either undefine or invalid - "
            "it is replaced with 'about:invalid'", logs.getvalue())
Esempio n. 4
0
    def test_write_sourcemap_various_issues(self):
        logs = setup_logger(self, sourcemap.logger, logging.WARNING)
        root = mktemp()
        output_stream = StringIO()
        output_stream.name = join(root, 'build', 'srcfinal.js')
        sourcemap_stream = StringIO()
        sourcemap_stream.name = join(root, 'maps', 'srcfinal.js.map')
        mappings = [[(0, 0, 0, 0, 0)]]
        sources = [
            join(root, 'src', 'src1.js'), 'hmm/src2.js',
            sourcemap.INVALID_SOURCE]
        names = ['foo']

        sourcemap.write_sourcemap(
            mappings, sources, names, output_stream, sourcemap_stream)

        self.assertEqual({
            "version": 3,
            "sources": ["../src/src1.js", "hmm/src2.js", 'about:invalid'],
            "names": ["foo"],
            "mappings": "AAAAA",
            "file": "../build/srcfinal.js",
        }, json.loads(sourcemap_stream.getvalue()))
        self.assertEqual(
            '\n//# sourceMappingURL=../maps/srcfinal.js.map\n',
            output_stream.getvalue())

        # warning about the about:invalid token is applied.
        self.assertIn(
            "sourcemap.sources[2] is either undefine or invalid - "
            "it is replaced with 'about:invalid'", logs.getvalue())
Esempio n. 5
0
 def test_source_map_inferred_standard_newline(self):
     # for cases where pretty printing happened
     # e.g. (function() { console.log("hello world"); })()
     err = setup_logger(self, sourcemap.logger)
     stream = StringIO()
     fragments = [
         ('(', 1, 1, None, None),
         ('function', 1, 2, None, None),
         ('(', 1, 10, None, None),
         (') ', 1, 11, None, None),
         ('{\n', 1, 13, None, None),
         # may be another special case here, to normalize the _first_
         # fragment
         ('  ', None, None, None, None),
         ('console', 1, 15, None, 'demo.js'),
         ('.', 1, 22, None, None),
         ('log', 1, 23, None, None),
         ('(', 0, 0, None, None),
         ('"hello world"', 1, 27, None, None),
         (')', 0, 0, None, None),
         (';\n', 0, 0, None, None),
         # note that the AST will need to record/provide the ending
         # value to this if the usage on a newline is to be supported
         # like so, otherwise all unmarked symbols will be clobbered
         # in an indeterminate manner, given that there could be
         # arbitrary amount of white spaces before the following
         # ending characters in the original source text.
         # In other words, if '}' is not tagged with (1,43), (or the
         # position that it was from, the generated source map will
         # guaranteed to be wrong as the starting column cannot be
         # correctly inferred without the original text.
         ('}', 1, 43, None, None),  # this one cannot be inferred.
         (')', 0, 0, None, None),   # this one can be.
         ('(', 1, 45, None, None),  # next starting symbol
         (')', 1, 46, None, None),
         (';', 0, 0, None, None),
     ]
     mapping, _, names = sourcemap.write(fragments, stream, normalize=False)
     self.assertEqual(stream.getvalue(), textwrap.dedent("""
     (function() {
       console.log("hello world");
     })();
     """).strip())
     self.assertEqual(names, [])
     self.assertEqual([[
         (0, 0, 0, 0), (1, 0, 0, 1), (8, 0, 0, 8), (1, 0, 0, 1),
         (2, 0, 0, 2)
     ], [
         (0,), (2, 0, 0, 2), (7, 0, 0, 7), (1, 0, 0, 1), (3, 0, 0, 3),
         (1, 0, 0, 1), (13, 0, 0, 13), (1, 0, 0, 1)
     ], [
         (0, 0, 0, 2), (1, 0, 0, 1), (1, 0, 0, 1), (1, 0, 0, 1),
         (1, 0, 0, 1)
     ]], mapping)
     self.assertNotIn("WARNING", err.getvalue())
     # the normalized version should also have the correct offsets
     mapping, sources, names = sourcemap.write(fragments, stream)
     self.assertEqual(
         [[(0, 0, 0, 0)], [(2, 0, 0, 14)], [(0, 0, 0, 28)]], mapping)
     self.assertEqual(sources, ['demo.js'])
Esempio n. 6
0
 def test_source_map_inferred_trailing_newline_unknown(self):
     err = setup_logger(self, sourcemap.logger)
     stream = StringIO()
     # Note the None values, as that signifies inferred elements.
     fragments = [
         ('console.log(\n  "hello world");', 1, 1, None, None),
         ('/* foo\nbar */', 0, 0, None, None),
         (' /* foo\nbar */', None, None, None, 'some_script.js'),
     ]
     mapping, _, names = sourcemap.write(fragments, stream)
     self.assertEqual(
         stream.getvalue(),
         'console.log(\n  "hello world");/* foo\nbar */ /* foo\nbar */',
     )
     self.assertEqual(names, [])
     self.assertEqual(
         [
             [(0, 0, 0, 0)],
             [(0, 0, 1, 0)],
             # yeah the comments really came out wrong it looks like
             [(0, 0, 0, 17), (6, )],
             [],
         ],
         mapping)
     self.assertIn(
         "WARNING text in the generated stream at line 3 may be mapped "
         "incorrectly due to stream fragment containing a trailing newline "
         "character provided without both lineno and colno defined; "
         "text fragment originated from: <unknown>", err.getvalue())
     self.assertIn(
         "WARNING text in the generated stream at line 4 may be mapped "
         "incorrectly due to stream fragment containing a trailing newline "
         "character provided without both lineno and colno defined; "
         "text fragment originated from: some_script.js", err.getvalue())
 def test_token_handler_setup_manual(self):
     stream = setup_logger(self, logger)
     definitions = {}
     unparser = BaseUnparser(definitions, token_handler_str_default)
     token_handler, layout_handlers, deferrable_handlers, prewalk_hooks = (
         unparser.setup())
     self.assertIs(token_handler, token_handler_str_default)
     self.assertIn(
         "DEBUG 'BaseUnparser' instance using manually specified "
         "token_handler 'token_handler_str_default'", stream.getvalue())
 def test_token_handler_default(self):
     stream = setup_logger(self, logger)
     definitions = {}
     unparser = BaseUnparser(definitions)
     token_handler, layout_handlers, deferrable_handlers, prewalk_hooks = (
         unparser.setup())
     self.assertIs(token_handler, token_handler_str_default)
     self.assertIn(
         "DEBUG 'BaseUnparser' instance has no token_handler specified; "
         "default handler 'token_handler_str_default' activate",
         stream.getvalue())
Esempio n. 9
0
 def test_source_map_known_standard_newline(self):
     # for cases where pretty printing happened
     # e.g. (function() { console.log("hello world"); })()
     # with all values known.
     err = setup_logger(self, sourcemap.logger)
     stream = StringIO()
     fragments = [
         ('(', 1, 1, None, None),
         ('function', 1, 2, None, None),
         ('(', 1, 10, None, None),
         (') ', 1, 11, None, None),
         ('{\n', 1, 13, None, None),
         # may be another special case here, to normalize the _first_
         # fragment
         ('  ', None, None, None, None),
         ('console', 1, 15, None, None),
         ('.', 1, 22, None, None),
         ('log', 1, 23, None, None),
         ('(', 1, 26, None, None),
         ('"hello world"', 1, 27, None, None),
         (')', 1, 40, None, None),
         (';\n', 1, 41, None, None),
         ('}', 1, 43, None, None),
         (')', 1, 44, None, None),
         ('(', 1, 45, None, None),
         (')', 1, 46, None, None),
         (';', 1, 47, None, None),
     ]
     mapping, _, names = sourcemap.write(fragments, stream, normalize=False)
     self.assertEqual(stream.getvalue(), textwrap.dedent("""
     (function() {
       console.log("hello world");
     })();
     """).strip())
     self.assertEqual(names, [])
     self.assertEqual([[
         (0, 0, 0, 0), (1, 0, 0, 1), (8, 0, 0, 8), (1, 0, 0, 1),
         (2, 0, 0, 2)
     ], [
         (0,), (2, 0, 0, 2), (7, 0, 0, 7), (1, 0, 0, 1), (3, 0, 0, 3),
         (1, 0, 0, 1), (13, 0, 0, 13), (1, 0, 0, 1)
     ], [
         (0, 0, 0, 2), (1, 0, 0, 1), (1, 0, 0, 1), (1, 0, 0, 1),
         (1, 0, 0, 1)
     ]], mapping)
     self.assertNotIn("WARNING", err.getvalue())
     # the normalized version should also have the correct offsets
     mapping, _, names = sourcemap.write(fragments, stream)
     self.assertEqual(
         [[(0, 0, 0, 0)], [(2, 0, 0, 14)], [(0, 0, 0, 28)]], mapping)
Esempio n. 10
0
 def test_source_map_inferred_trailing_newline_calculated(self):
     err = setup_logger(self, sourcemap.logger)
     stream = StringIO()
     # Note the None values, as that signifies inferred elements.
     fragments = [
         ('console.log(\n  "hello world");', 1, 1, None, None),
     ]
     mapping, _, names = sourcemap.write(fragments, stream)
     self.assertEqual(stream.getvalue(), 'console.log(\n  "hello world");')
     self.assertEqual(names, [])
     self.assertEqual([
         [(0, 0, 0, 0)],
         [(0, 0, 1, 0)],
     ], mapping)
     self.assertNotIn("WARNING", err.getvalue())
    def test_token_handler_setup_with_rules(self):
        def rule1():
            def handler1():
                "handler1"

            return {'token_handler': handler1}

        def rule2():
            def handler2():
                "handler2"

            return {'token_handler': handler2}

        def custom_handler():
            "custom handler"

        stream = setup_logger(self, logger)
        definitions = {}
        unparser = BaseUnparser(definitions, rules=(rule1, rule2))
        token_handler, layout_handlers, deferrable_handlers, prewalk_hooks = (
            unparser.setup())
        self.assertEqual(token_handler.__name__, 'handler2')

        self.assertIn(
            "DEBUG rule 'rule1' specified a token_handler 'handler1'",
            stream.getvalue())
        self.assertIn(
            "WARNING rule 'rule2' specified a new token_handler 'handler2', "
            "overriding previously assigned token_handler 'handler1'",
            stream.getvalue())

        unparser = BaseUnparser(definitions,
                                token_handler=custom_handler,
                                rules=(rule1, rule2))
        token_handler, layout_handlers, deferrable_handlers, prewalk_hooks = (
            unparser.setup())
        self.assertIs(token_handler, custom_handler)
        self.assertIn(
            "INFO manually specified token_handler 'custom_handler' to the "
            "'BaseUnparser' instance will override rule derived token_handler "
            "'handler2'", stream.getvalue())