microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
test/debugger/sourceMap.test.ts
160lines · 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, IStrictUrl} from "../../src/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.strictEqual(expectedUrlHref, result && 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.strictEqual(expectedUrlHref, result && result.href); |
| 39 | }); |
| 40 | |
| 41 | test("should return correct sourcemap url for RN macOS", function () { |
| 42 | const scriptUrl: url.Url = url.parse("http://localhost:8081/index.bundle?platform=macos&dev=true"); |
| 43 | const scriptBody = "//# sourceMappingURL=//index.map?platform=macos&dev=true"; |
| 44 | const expectedUrlHref = "http://localhost:8081/index.map?platform=macos&dev=true"; |
| 45 | |
| 46 | const sourceMap = new SourceMapUtil(); |
| 47 | const result = sourceMap.getSourceMapURL(scriptUrl, scriptBody); |
| 48 | assert.equal(expectedUrlHref, result && result.href); |
| 49 | }); |
| 50 | |
| 51 | test("should return default IStrictUrl for an invalid sourcemap url", function () { |
| 52 | const scriptUrl: url.Url = url.parse("http://localhost:8081/index.ios.bundle?platform=ios&dev=true"); |
| 53 | const scriptBody = ""; |
| 54 | |
| 55 | const sourceMap = new SourceMapUtil(); |
| 56 | const result = sourceMap.getSourceMapURL(scriptUrl, scriptBody); |
| 57 | assert.deepStrictEqual(null, result); |
| 58 | }); |
| 59 | |
| 60 | test("should return default IStrictUrl if there are only inline sourcemap urls", function () { |
| 61 | const scriptUrl: url.Url = url.parse("http://localhost:8081/index.ios.bundle?platform=ios&dev=true"); |
| 62 | const scriptBody = "//# sourceMappingURL=data:application/json;base64,eyJmb28iOiJiYXIifQ==\n" + |
| 63 | "//# sourceMappingURL=data:application/json;base64,eyJiYXoiOiJxdXV4In0="; |
| 64 | |
| 65 | const sourceMap = new SourceMapUtil(); |
| 66 | const result = sourceMap.getSourceMapURL(scriptUrl, scriptBody); |
| 67 | assert.deepStrictEqual(null, result); |
| 68 | }); |
| 69 | |
| 70 | test("should update the contents of a source map file", function() { |
| 71 | const sourceMapBody: string = JSON.stringify({"version": 3, "sources": ["test/index.ts"], "names": [], "mappings": "", "file": "test/index.js", "sourceRoot": "../../src"}); |
| 72 | const scriptPath: string = "test/newIndex.ts"; |
| 73 | const sourcesRootPath: string = "new/src"; |
| 74 | const expectedSourceMapBody: string = JSON.stringify({"version": 3, "sources": ["../../test/index.ts"], "names": [], "mappings": "", "file": scriptPath, "sourceRoot": ""}); |
| 75 | const sourceMap = new SourceMapUtil(); |
| 76 | |
| 77 | const result: string = sourceMap.updateSourceMapFile(sourceMapBody, scriptPath, sourcesRootPath); |
| 78 | assert.strictEqual(expectedSourceMapBody, result); |
| 79 | }); |
| 80 | |
| 81 | test("should update source map file path for remote packager", function () { |
| 82 | const localRoot = "/home/local"; |
| 83 | const remoteRoot = "/home/remote"; |
| 84 | const sourceMapBody: string = JSON.stringify({ "version": 3, "sources": [`${remoteRoot}/test/index.ts`], "names": [], "mappings": "", "file": "test/index.js", "sourceRoot": "../../src" }); |
| 85 | const scriptPath: string = "test/newIndex.ts"; |
| 86 | const sourcesRootPath: string = `${localRoot}/new/src`; |
| 87 | const expectedSourceMapBody: string = JSON.stringify({ "version": 3, "sources": [`../../test/index.ts`], "names": [], "mappings": "", "file": scriptPath, "sourceRoot": "" }); |
| 88 | const sourceMap = new SourceMapUtil(); |
| 89 | |
| 90 | const result: string = sourceMap.updateSourceMapFile(sourceMapBody, scriptPath, sourcesRootPath, remoteRoot, localRoot); |
| 91 | assert.strictEqual(expectedSourceMapBody, result); |
| 92 | }); |
| 93 | |
| 94 | test("should update scripts with source mapping urls", function() { |
| 95 | const scriptBody: string = "//# sourceMappingURL=/index.ios.map?platform=ios&dev=true"; |
| 96 | const sourceMappingUrl: IStrictUrl = <IStrictUrl>url.parse("/index.android.map"); |
| 97 | const expectedScriptBody = "//# sourceMappingURL=index.android.map"; |
| 98 | const sourceMap = new SourceMapUtil(); |
| 99 | |
| 100 | const result = sourceMap.updateScriptPaths(scriptBody, sourceMappingUrl); |
| 101 | assert.strictEqual(expectedScriptBody, result); |
| 102 | }); |
| 103 | |
| 104 | test("should not update scripts without source mapping urls", function() { |
| 105 | const scriptBody: string = "var path = require('path');"; |
| 106 | const sourceMappingUrl: IStrictUrl = <IStrictUrl>url.parse("/index.android.map"); |
| 107 | const sourceMap = new SourceMapUtil(); |
| 108 | |
| 109 | const result = sourceMap.updateScriptPaths(scriptBody, sourceMappingUrl); |
| 110 | assert.strictEqual(scriptBody, result); |
| 111 | }); |
| 112 | |
| 113 | test("should update absolute source path to relative unix style path", function() { |
| 114 | const sourcePath: string = "foo/bar"; |
| 115 | const sourcesRootPath: string = "baz/fuzz"; |
| 116 | const expectedPath: string = "../../foo/bar"; |
| 117 | const sourceMap = new SourceMapUtil(); |
| 118 | |
| 119 | const result = (<any>sourceMap).updateSourceMapPath(sourcePath, sourcesRootPath); |
| 120 | assert.strictEqual(expectedPath, result); |
| 121 | }); |
| 122 | |
| 123 | test("should get only the latest sourceMappingURL", function() { |
| 124 | const scriptBody: string = `//# sourceMappingURL=abort.controller.js.map |
| 125 | //# sourceMappingURL=index.map |
| 126 | //# sourceURL=http://localhost:8081/index.bundle?platform=android&dev=true&minify=false`; |
| 127 | const expectedScriptBody = `index.map`; |
| 128 | const sourceMap = new SourceMapUtil(); |
| 129 | |
| 130 | const result = sourceMap.getSourceMapRelativeUrl(scriptBody); |
| 131 | assert.strictEqual(expectedScriptBody, result); |
| 132 | }); |
| 133 | |
| 134 | test("should remove sourceURL from the bundle script body correctly", function() { |
| 135 | const scriptBody: string = `var sourceURL = '//# sourceURL=' + (hasOwnProperty.call(options, 'sourceURL') ? (options.sourceURL + '').replace(/[\\r\\n]/g, ' ') : 'lodash.templateSources[' + ++templateCounter + ']') + '\\n'; |
| 136 | //# sourceMappingURL=index.map |
| 137 | //# sourceURL=http://localhost:8081/index.bundle?platform=android&dev=true&minify=false`; |
| 138 | const expectedScriptBody = `var sourceURL = '//# sourceURL=' + (hasOwnProperty.call(options, 'sourceURL') ? (options.sourceURL + '').replace(/[\\r\\n]/g, ' ') : 'lodash.templateSources[' + ++templateCounter + ']') + '\\n'; |
| 139 | //# sourceMappingURL=index.map\n`; |
| 140 | const sourceMap = new SourceMapUtil(); |
| 141 | |
| 142 | const result = sourceMap.removeSourceURL(scriptBody); |
| 143 | assert.strictEqual(expectedScriptBody, result); |
| 144 | }); |
| 145 | |
| 146 | test("should remove sourceURL if it is before sourceMappingURL", function() { |
| 147 | const scriptBody: string = `var sourceURL = '//# sourceURL=' + (hasOwnProperty.call(options, 'sourceURL') ? (options.sourceURL + '').replace(/[\\r\\n]/g, ' ') : 'lodash.templateSources[' + ++templateCounter + ']') + '\\n'; |
| 148 | //# sourceURL=http://localhost:8081/index.bundle?platform=android&dev=true&minify=false |
| 149 | //# sourceMappingURL=index.map`; |
| 150 | const expectedScriptBody = `var sourceURL = '//# sourceURL=' + (hasOwnProperty.call(options, 'sourceURL') ? (options.sourceURL + '').replace(/[\\r\\n]/g, ' ') : 'lodash.templateSources[' + ++templateCounter + ']') + '\\n'; |
| 151 | |
| 152 | //# sourceMappingURL=index.map`; |
| 153 | const sourceMap = new SourceMapUtil(); |
| 154 | |
| 155 | const result = sourceMap.removeSourceURL(scriptBody); |
| 156 | assert.strictEqual(expectedScriptBody, result); |
| 157 | }); |
| 158 | |
| 159 | }); |
| 160 | }); |
| 161 | |