microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/test/debugger/sourceMap.test.ts
100lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import {SourceMapUtil} from "../../debugger/sourceMap"; |
| 5 | |
| 6 | import * as assert from "assert"; |
| 7 | import * as path from "path"; |
| 8 | import * as url from "url"; |
| 9 | |
| 10 | suite("sourceMap", function() { |
| 11 | suite("debuggerContext", function() { |
| 12 | test("should convert host filesystem paths to URL-style-paths", function() { |
| 13 | const sourceMap = new SourceMapUtil(); |
| 14 | const filePath = path.join("foo", "bar", "baz"); |
| 15 | const urlPath = "foo/bar/baz"; |
| 16 | const result = (<any>sourceMap).makeUnixStylePath(filePath); |
| 17 | assert(result === urlPath, `Expected "${urlPath}", found "${result}"`); |
| 18 | }); |
| 19 | |
| 20 | test("should resolve a valid sourcemap url", function () { |
| 21 | const scriptUrl: url.Url = url.parse("http://localhost:8081/index.ios.bundle?platform=ios&dev=true"); |
| 22 | const scriptBody = "//# sourceMappingURL=/index.ios.map?platform=ios&dev=true"; |
| 23 | const expectedUrlHref = "http://localhost:8081/index.ios.map?platform=ios&dev=true"; |
| 24 | |
| 25 | const sourceMap = new SourceMapUtil(); |
| 26 | const result = sourceMap.getSourceMapURL(scriptUrl, scriptBody); |
| 27 | assert.equal(expectedUrlHref, result.href); |
| 28 | }); |
| 29 | |
| 30 | test("should ignore inline sourcemap urls", function () { |
| 31 | const scriptUrl: url.Url = url.parse("http://localhost:8081/index.ios.bundle?platform=ios&dev=true"); |
| 32 | const scriptBody = "//# sourceMappingURL=data:application/json;base64,eyJmb28iOiJiYXIifQ==\n" + |
| 33 | "//# sourceMappingURL=/index.ios.map?platform=ios&dev=true"; |
| 34 | const expectedUrlHref = "http://localhost:8081/index.ios.map?platform=ios&dev=true"; |
| 35 | |
| 36 | const sourceMap = new SourceMapUtil(); |
| 37 | const result = sourceMap.getSourceMapURL(scriptUrl, scriptBody); |
| 38 | assert.equal(expectedUrlHref, result.href); |
| 39 | }); |
| 40 | |
| 41 | test("should return null for an invalid sourcemap url", function () { |
| 42 | const scriptUrl: url.Url = url.parse("http://localhost:8081/index.ios.bundle?platform=ios&dev=true"); |
| 43 | const scriptBody = ""; |
| 44 | |
| 45 | const sourceMap = new SourceMapUtil(); |
| 46 | const result = sourceMap.getSourceMapURL(scriptUrl, scriptBody); |
| 47 | assert(result === null); |
| 48 | }); |
| 49 | |
| 50 | test("should return null if there are only inline sourcemap urls", function () { |
| 51 | const scriptUrl: url.Url = url.parse("http://localhost:8081/index.ios.bundle?platform=ios&dev=true"); |
| 52 | const scriptBody = "//# sourceMappingURL=data:application/json;base64,eyJmb28iOiJiYXIifQ==\n" + |
| 53 | "//# sourceMappingURL=data:application/json;base64,eyJiYXoiOiJxdXV4In0="; |
| 54 | |
| 55 | const sourceMap = new SourceMapUtil(); |
| 56 | const result = sourceMap.getSourceMapURL(scriptUrl, scriptBody); |
| 57 | assert(result === null); |
| 58 | }); |
| 59 | |
| 60 | test("should update the contents of a source map file", function() { |
| 61 | const sourceMapBody: string = JSON.stringify({"version": 3, "sources": ["test/index.ts"], "names": [], "mappings": "", "file": "test/index.js", "sourceRoot": "../../src"}); |
| 62 | const scriptPath: string = "test/newIndex.ts"; |
| 63 | const sourcesRootPath: string = "new/src"; |
| 64 | const expectedSourceMapBody: string = JSON.stringify({"version": 3, "sources": ["../../test/index.ts"], "names": [], "mappings": "", "file": scriptPath, "sourceRoot": ""}); |
| 65 | const sourceMap = new SourceMapUtil(); |
| 66 | |
| 67 | const result: string = sourceMap.updateSourceMapFile(sourceMapBody, scriptPath, sourcesRootPath); |
| 68 | assert.equal(expectedSourceMapBody, result); |
| 69 | }); |
| 70 | |
| 71 | test("should update scripts with source mapping urls", function() { |
| 72 | const scriptBody: string = "//# sourceMappingURL=/index.ios.map?platform=ios&dev=true"; |
| 73 | const sourceMappingUrl: url.Url = url.parse("/index.android.map"); |
| 74 | const expectedScriptBody = "//# sourceMappingURL=index.android.map"; |
| 75 | const sourceMap = new SourceMapUtil(); |
| 76 | |
| 77 | const result = sourceMap.updateScriptPaths(scriptBody, sourceMappingUrl); |
| 78 | assert.equal(expectedScriptBody, result); |
| 79 | }); |
| 80 | |
| 81 | test("should not update scripts without source mapping urls", function() { |
| 82 | const scriptBody: string = "var path = require('path');"; |
| 83 | const sourceMappingUrl: url.Url = url.parse("/index.android.map"); |
| 84 | const sourceMap = new SourceMapUtil(); |
| 85 | |
| 86 | const result = sourceMap.updateScriptPaths(scriptBody, sourceMappingUrl); |
| 87 | assert.equal(scriptBody, result); |
| 88 | }); |
| 89 | |
| 90 | test("should update absolute source path to relative unix style path", function() { |
| 91 | const sourcePath: string = "foo/bar"; |
| 92 | const sourcesRootPath: string = "baz/fuzz"; |
| 93 | const expectedPath: string = "../../foo/bar"; |
| 94 | const sourceMap = new SourceMapUtil(); |
| 95 | |
| 96 | const result = (<any>sourceMap).updateSourceMapPath(sourcePath, sourcesRootPath); |
| 97 | assert.equal(expectedPath, result); |
| 98 | }); |
| 99 | }); |
| 100 | }); |
| 101 | |