windows.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2014 Simon Lydell
  2. // X11 (“MIT”) Licensed. (See LICENSE.)
  3. var path = require("path")
  4. var test = require("tape")
  5. var asyncify = require("simple-asyncify")
  6. var common = require("./common")
  7. var u = common.u
  8. var read = common.read
  9. var identity = common.identity
  10. var sourceMapResolve = require("../")
  11. path.sep = "\\"
  12. function testResolveSourceMap(method, sync) {
  13. return function(t) {
  14. var wrap = (sync ? identity : asyncify)
  15. var codeUrl = "c:\\a\\b\\c\\foo.js"
  16. t.plan(3 * 2)
  17. if (sync) {
  18. method = asyncify(method)
  19. }
  20. var map = {}
  21. var readMap = wrap(read(JSON.stringify(map)))
  22. method(u("foo.js.map"), codeUrl, readMap, function(error, result) {
  23. t.error(error)
  24. t.deepEqual(result, {
  25. sourceMappingURL: "foo.js.map",
  26. url: "/a/b/c/foo.js.map",
  27. sourcesRelativeTo: "/a/b/c/foo.js.map",
  28. map: map
  29. })
  30. })
  31. method(u("/foo.js.map"), codeUrl, readMap, function(error, result) {
  32. t.error(error)
  33. t.deepEqual(result, {
  34. sourceMappingURL: "/foo.js.map",
  35. url: "/foo.js.map",
  36. sourcesRelativeTo: "/foo.js.map",
  37. map: map
  38. })
  39. })
  40. method(u("../foo.js.map"), codeUrl, readMap, function(error, result) {
  41. t.error(error)
  42. t.deepEqual(result, {
  43. sourceMappingURL: "../foo.js.map",
  44. url: "/a/b/foo.js.map",
  45. sourcesRelativeTo: "/a/b/foo.js.map",
  46. map: map
  47. })
  48. })
  49. }
  50. }
  51. test(".resolveSourceMap", testResolveSourceMap(sourceMapResolve.resolveSourceMap, false))
  52. test(".resolveSourceMapSync", testResolveSourceMap(sourceMapResolve.resolveSourceMapSync, true))
  53. function testResolveSources(method, sync) {
  54. return function(t) {
  55. var wrap = (sync ? identity : asyncify)
  56. var mapUrl = "c:\\a\\b\\c\\foo.js.map"
  57. t.plan(1 * 3)
  58. if (sync) {
  59. method = asyncify(method)
  60. }
  61. var map = {
  62. sources: ["foo.js", "/foo.js", "../foo.js"]
  63. }
  64. method(map, mapUrl, wrap(identity), function(error, result) {
  65. t.error(error)
  66. t.deepEqual(result.sourcesResolved, ["/a/b/c/foo.js", "/foo.js", "/a/b/foo.js"])
  67. t.deepEqual(result.sourcesContent, ["/a/b/c/foo.js", "/foo.js", "/a/b/foo.js"])
  68. })
  69. }
  70. }
  71. test(".resolveSources", testResolveSources(sourceMapResolve.resolveSources, false))
  72. test(".resolveSourcesSync", testResolveSources(sourceMapResolve.resolveSourcesSync, true))
  73. function testResolve(method, sync) {
  74. return function(t) {
  75. var wrap = (sync ? identity : asyncify)
  76. var wrapMap = function(mapFn, fn) {
  77. return wrap(function(url) {
  78. if (/\.map$/.test(url)) {
  79. return mapFn(url)
  80. }
  81. return fn(url)
  82. })
  83. }
  84. var codeUrl = "c:\\a\\b\\c\\foo.js"
  85. t.plan(3 * 2)
  86. if (sync) {
  87. method = asyncify(method)
  88. }
  89. var map = {
  90. sources: ["foo.js", "/foo.js", "../foo.js"]
  91. }
  92. var readMap = wrapMap(read(JSON.stringify(map)), identity)
  93. method(u("foo.js.map"), codeUrl, readMap, function(error, result) {
  94. t.error(error)
  95. t.deepEqual(result, {
  96. sourceMappingURL: "foo.js.map",
  97. url: "/a/b/c/foo.js.map",
  98. sourcesRelativeTo: "/a/b/c/foo.js.map",
  99. map: map,
  100. sourcesResolved: ["/a/b/c/foo.js", "/foo.js", "/a/b/foo.js"],
  101. sourcesContent: ["/a/b/c/foo.js", "/foo.js", "/a/b/foo.js"]
  102. })
  103. })
  104. method(u("/foo.js.map"), codeUrl, readMap, function(error, result) {
  105. t.error(error)
  106. t.deepEqual(result, {
  107. sourceMappingURL: "/foo.js.map",
  108. url: "/foo.js.map",
  109. sourcesRelativeTo: "/foo.js.map",
  110. map: map,
  111. sourcesResolved: ["/foo.js", "/foo.js", "/foo.js"],
  112. sourcesContent: ["/foo.js", "/foo.js", "/foo.js"]
  113. })
  114. })
  115. method(u("../foo.js.map"), codeUrl, readMap, function(error, result) {
  116. t.error(error)
  117. t.deepEqual(result, {
  118. sourceMappingURL: "../foo.js.map",
  119. url: "/a/b/foo.js.map",
  120. sourcesRelativeTo: "/a/b/foo.js.map",
  121. map: map,
  122. sourcesResolved: ["/a/b/foo.js", "/foo.js", "/a/foo.js"],
  123. sourcesContent: ["/a/b/foo.js", "/foo.js", "/a/foo.js"]
  124. })
  125. })
  126. }
  127. }
  128. test(".resolve", testResolve(sourceMapResolve.resolve, false))
  129. test(".resolveSync", testResolve(sourceMapResolve.resolveSync, true))