Ejemplo n.º 1
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())
Ejemplo 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())
Ejemplo n.º 3
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())
Ejemplo n.º 4
0
    def test_write_sourcemap_source_mapping_encoded_same(self):
        root = mktemp()
        # emulating a codecs.open with encoding as shift_jis
        raw_stream = BytesIO()
        raw_stream.encoding = 'shift_jis'
        raw_stream.name = join(root, 'lang.js')
        output_stream = codecs.getwriter(raw_stream.encoding)(raw_stream)
        output_stream.write('yes\u306f\u3044//')
        mappings = [[(0, 0, 0, 0, 0)]]
        sources = [join(root, 'src', 'ja.js')]
        names = ['yes\u306f\u3044']

        sourcemap.write_sourcemap(
            mappings, sources, names, output_stream, output_stream,
            source_mapping_url='src.map')

        encoded = raw_stream.getvalue().splitlines()[-1]
        # the sourcemap is written as a sourceMappingURL, with the
        # source_mapping_url argument ignored.
        self.assertIn(b'application/json;base64;charset=shift_jis', encoded)
        # decode the base64 string; note the shift_jis encoding
        self.assertEqual({
            "version": 3,
            "sources": ['src/ja.js'],
            "names": ["yes\u306f\u3044"],
            "mappings": "AAAAA",
            "file": 'lang.js',
        }, json.loads(base64.b64decode(
            encoded.split(b',')[-1]).decode('shift_jis')))
Ejemplo n.º 5
0
    def test_write_sourcemap_source_mapping_same_output_stream(self):
        root = mktemp()
        output_stream = StringIO()
        output_stream.name = join(root, 'srcfinal.js')
        output_stream.write('//')
        mappings = [[(0, 0, 0, 0, 0)]]
        sources = [join(root, 'src', 'src1.js'), join(root, 'src', 'src2.js')]
        names = ['foo']

        sourcemap.write_sourcemap(
            mappings, sources, names, output_stream, output_stream,
            source_mapping_url='src.map')

        output = output_stream.getvalue()
        # the sourcemap is written as a sourceMappingURL, with the
        # source_mapping_url argument ignored.
        self.assertIn(
            '# sourceMappingURL=data:application/json;base64;charset=utf8',
            output,
        )
        # decode the base64 string
        self.assertEqual({
            "version": 3,
            "sources": ['src/src1.js', 'src/src2.js'],
            "names": ["foo"],
            "mappings": "AAAAA",
            "file": 'srcfinal.js',
        }, json.loads(base64.b64decode(
            output.splitlines()[-1].split(',')[-1].encode('utf8')
        ).decode('utf8')))
Ejemplo n.º 6
0
    def test_write_sourcemap_source_mapping_url_none(self):
        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, 'src', 'src1.js'), join(root, 'src', 'src2.js')]
        names = ['foo']

        sourcemap.write_sourcemap(mappings,
                                  sources,
                                  names,
                                  output_stream,
                                  sourcemap_stream,
                                  source_mapping_url=None)

        self.assertEqual(
            {
                "version": 3,
                "sources": ['src/src1.js', 'src/src2.js'],
                "names": ["foo"],
                "mappings": "AAAAA",
                "file": 'srcfinal.js',
            }, json.loads(sourcemap_stream.getvalue()))
        self.assertEqual('', output_stream.getvalue())
Ejemplo n.º 7
0
def write(unparser,
          nodes,
          output_stream,
          sourcemap_stream=None,
          sourcemap_normalize_mappings=True,
          sourcemap_normalize_paths=True,
          source_mapping_url=NotImplemented):
    """
    Write out the node using the unparser into an output stream, and
    optionally the sourcemap using the sourcemap stream.

    Ideally, file objects should be passed to the *_stream arguments, so
    that the name resolution built into the sourcemap builder function
    will be used.  Also, if these file objects are opened using absolute
    path arguments, enabling the sourcemap_normalize_paths flag will
    have all paths normalized to their relative form.

    If the provided streams are not anchored on the filesystem, or that
    the provide node was generated from a string or in-memory stream,
    the generation of the sourcemap should be done using the lower level
    `write` function provided by the sourcemap module, which this method
    wraps.  Alternatively, the top level node should have its sourcepath
    set to path that this node originated from.

    Arguments

    unparser
        An unparser instance.
    nodes
        The Node or list of Nodes to stream to the output stream with
        the unparser.
    output_stream
        Either a stream object or a callable that produces one.  The
        stream object to write to; its 'write' method will be invoked.

        If a callable was provided, the 'close' method on its return
        value will be called to close the stream.
    sourcemap_stream
        If one is provided, the sourcemap will be written out to it.
        Like output_stream, it could also be a callable and be handled
        in the same manner.

        If this argument is the same as output_stream (note: the return
        value of any callables are not compared), the stream object that
        is the same as the output_stream will be used for writing out
        the source map, and the source map will instead be encoded as a
        'data:application/json;base64,' URL.
    sourcemap_normalize_mappings
        Flag for the normalization of the sourcemap mappings; Defaults
        to True to enable a reduction in output size.
    sourcemap_normalize_paths
        If set to true, all absolute paths will be converted to the
        relative form when the sourcemap is generated, if all paths
        provided are in the absolute form.

        Defaults to True to enable a reduction in output size.
    source_mapping_url
        If unspecified, the default derived path will be written as a
        sourceMappingURL comment into the output stream.  If explicitly
        specified with a value, that will be written instead.  Set to
        None to disable this.
    """

    closer = []

    def get_stream(stream):
        if callable(stream):
            result = stream()
            closer.append(result.close)
        else:
            result = stream
        return result

    def cleanup():
        for close in reversed(closer):
            close()

    chunks = None
    if isinstance(nodes, Node):
        chunks = unparser(nodes)
    elif isinstance(nodes, Iterable):
        raw = [unparser(node) for node in nodes if isinstance(node, Node)]
        if raw:
            chunks = chain(*raw)

    if not chunks:
        raise TypeError('must either provide a Node or list containing Nodes')

    try:
        out_s = get_stream(output_stream)
        sourcemap_stream = (out_s if sourcemap_stream is output_stream else
                            sourcemap_stream)
        mappings, sources, names = sourcemap.write(
            chunks, out_s, normalize=sourcemap_normalize_mappings)
        if sourcemap_stream:
            sourcemap_stream = get_stream(sourcemap_stream)
            sourcemap.write_sourcemap(
                mappings,
                sources,
                names,
                out_s,
                sourcemap_stream,
                normalize_paths=sourcemap_normalize_paths,
                source_mapping_url=source_mapping_url,
            )
    finally:
        cleanup()