resolver_sync.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. var path = require('path');
  2. var test = require('tape');
  3. var resolve = require('../');
  4. test('foo', function (t) {
  5. var dir = path.join(__dirname, 'resolver');
  6. t.equal(
  7. resolve.sync('./foo', { basedir: dir }),
  8. path.join(dir, 'foo.js')
  9. );
  10. t.equal(
  11. resolve.sync('./foo.js', { basedir: dir }),
  12. path.join(dir, 'foo.js')
  13. );
  14. t.equal(
  15. resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
  16. path.join(dir, 'foo.js')
  17. );
  18. t.throws(function () {
  19. resolve.sync('foo', { basedir: dir });
  20. });
  21. // Test that filename is reported as the "from" value when passed.
  22. t.throws(
  23. function () {
  24. resolve.sync('foo', { basedir: dir, filename: path.join(dir, 'bar.js') });
  25. },
  26. {
  27. name: 'Error',
  28. message: "Cannot find module 'foo' from '" + path.join(dir, 'bar.js') + "'"
  29. }
  30. );
  31. t.end();
  32. });
  33. test('bar', function (t) {
  34. var dir = path.join(__dirname, 'resolver');
  35. t.equal(
  36. resolve.sync('foo', { basedir: path.join(dir, 'bar') }),
  37. path.join(dir, 'bar/node_modules/foo/index.js')
  38. );
  39. t.end();
  40. });
  41. test('baz', function (t) {
  42. var dir = path.join(__dirname, 'resolver');
  43. t.equal(
  44. resolve.sync('./baz', { basedir: dir }),
  45. path.join(dir, 'baz/quux.js')
  46. );
  47. t.end();
  48. });
  49. test('biz', function (t) {
  50. var dir = path.join(__dirname, 'resolver/biz/node_modules');
  51. t.equal(
  52. resolve.sync('./grux', { basedir: dir }),
  53. path.join(dir, 'grux/index.js')
  54. );
  55. t.equal(
  56. resolve.sync('tiv', { basedir: path.join(dir, 'grux') }),
  57. path.join(dir, 'tiv/index.js')
  58. );
  59. t.equal(
  60. resolve.sync('grux', { basedir: path.join(dir, 'tiv') }),
  61. path.join(dir, 'grux/index.js')
  62. );
  63. t.end();
  64. });
  65. test('normalize', function (t) {
  66. var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
  67. t.equal(
  68. resolve.sync('../grux', { basedir: dir }),
  69. path.join(dir, 'index.js')
  70. );
  71. t.end();
  72. });
  73. test('cup', function (t) {
  74. var dir = path.join(__dirname, 'resolver');
  75. t.equal(
  76. resolve.sync('./cup', {
  77. basedir: dir,
  78. extensions: ['.js', '.coffee']
  79. }),
  80. path.join(dir, 'cup.coffee')
  81. );
  82. t.equal(
  83. resolve.sync('./cup.coffee', { basedir: dir }),
  84. path.join(dir, 'cup.coffee')
  85. );
  86. t.throws(function () {
  87. resolve.sync('./cup', {
  88. basedir: dir,
  89. extensions: ['.js']
  90. });
  91. });
  92. t.end();
  93. });
  94. test('mug', function (t) {
  95. var dir = path.join(__dirname, 'resolver');
  96. t.equal(
  97. resolve.sync('./mug', { basedir: dir }),
  98. path.join(dir, 'mug.js')
  99. );
  100. t.equal(
  101. resolve.sync('./mug', {
  102. basedir: dir,
  103. extensions: ['.coffee', '.js']
  104. }),
  105. path.join(dir, 'mug.coffee')
  106. );
  107. t.equal(
  108. resolve.sync('./mug', {
  109. basedir: dir,
  110. extensions: ['.js', '.coffee']
  111. }),
  112. path.join(dir, 'mug.js')
  113. );
  114. t.end();
  115. });
  116. test('other path', function (t) {
  117. var resolverDir = path.join(__dirname, 'resolver');
  118. var dir = path.join(resolverDir, 'bar');
  119. var otherDir = path.join(resolverDir, 'other_path');
  120. t.equal(
  121. resolve.sync('root', {
  122. basedir: dir,
  123. paths: [otherDir]
  124. }),
  125. path.join(resolverDir, 'other_path/root.js')
  126. );
  127. t.equal(
  128. resolve.sync('lib/other-lib', {
  129. basedir: dir,
  130. paths: [otherDir]
  131. }),
  132. path.join(resolverDir, 'other_path/lib/other-lib.js')
  133. );
  134. t.throws(function () {
  135. resolve.sync('root', { basedir: dir });
  136. });
  137. t.throws(function () {
  138. resolve.sync('zzz', {
  139. basedir: dir,
  140. paths: [otherDir]
  141. });
  142. });
  143. t.end();
  144. });
  145. test('incorrect main', function (t) {
  146. var resolverDir = path.join(__dirname, 'resolver');
  147. var dir = path.join(resolverDir, 'incorrect_main');
  148. t.equal(
  149. resolve.sync('./incorrect_main', { basedir: resolverDir }),
  150. path.join(dir, 'index.js')
  151. );
  152. t.end();
  153. });
  154. test('#25: node modules with the same name as node stdlib modules', function (t) {
  155. var resolverDir = path.join(__dirname, 'resolver/punycode');
  156. t.equal(
  157. resolve.sync('punycode', { basedir: resolverDir }),
  158. path.join(resolverDir, 'node_modules/punycode/index.js')
  159. );
  160. t.end();
  161. });
  162. var stubStatSync = function stubStatSync(fn) {
  163. var fs = require('fs');
  164. var statSync = fs.statSync;
  165. try {
  166. fs.statSync = function () {
  167. throw new EvalError('Unknown Error');
  168. };
  169. return fn();
  170. } finally {
  171. fs.statSync = statSync;
  172. }
  173. };
  174. test('#79 - re-throw non ENOENT errors from stat', function (t) {
  175. var dir = path.join(__dirname, 'resolver');
  176. stubStatSync(function () {
  177. t.throws(function () {
  178. resolve.sync('foo', { basedir: dir });
  179. }, /Unknown Error/);
  180. });
  181. t.end();
  182. });
  183. test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
  184. var dir = path.join(__dirname, 'resolver');
  185. t.equal(
  186. resolve.sync('./foo', { basedir: path.join(dir, 'same_names') }),
  187. path.join(dir, 'same_names/foo.js')
  188. );
  189. t.equal(
  190. resolve.sync('./foo/', { basedir: path.join(dir, 'same_names') }),
  191. path.join(dir, 'same_names/foo/index.js')
  192. );
  193. t.end();
  194. });
  195. test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
  196. var testFile = path.basename(__filename);
  197. t.test('sanity check', function (st) {
  198. st.equal(
  199. resolve.sync('./' + testFile),
  200. __filename,
  201. 'sanity check'
  202. );
  203. st.end();
  204. });
  205. t.test('with a fake directory', function (st) {
  206. function run() { return resolve.sync('./' + testFile + '/blah'); }
  207. st.throws(run, 'throws an error');
  208. try {
  209. run();
  210. } catch (e) {
  211. st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  212. st.equal(
  213. e.message,
  214. 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
  215. 'can not find nonexistent module'
  216. );
  217. }
  218. st.end();
  219. });
  220. t.end();
  221. });
  222. test('sync dot main', function (t) {
  223. var start = new Date();
  224. t.equal(resolve.sync('./resolver/dot_main'), path.join(__dirname, 'resolver/dot_main/index.js'));
  225. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  226. t.end();
  227. });
  228. test('sync dot slash main', function (t) {
  229. var start = new Date();
  230. t.equal(resolve.sync('./resolver/dot_slash_main'), path.join(__dirname, 'resolver/dot_slash_main/index.js'));
  231. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  232. t.end();
  233. });
  234. test('not a directory', function (t) {
  235. var path = './foo';
  236. try {
  237. resolve.sync(path, { basedir: __filename });
  238. t.fail();
  239. } catch (err) {
  240. t.ok(err, 'a non-directory errors');
  241. t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
  242. t.equal(err && err.code, 'MODULE_NOT_FOUND');
  243. }
  244. t.end();
  245. });
  246. test('non-string "main" field in package.json', function (t) {
  247. var dir = path.join(__dirname, 'resolver');
  248. try {
  249. var result = resolve.sync('./invalid_main', { basedir: dir });
  250. t.equal(result, undefined, 'result should not exist');
  251. t.fail('should not get here');
  252. } catch (err) {
  253. t.ok(err, 'errors on non-string main');
  254. t.equal(err.message, 'package “invalid main” `main` must be a string');
  255. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  256. }
  257. t.end();
  258. });
  259. test('non-string "main" field in package.json', function (t) {
  260. var dir = path.join(__dirname, 'resolver');
  261. try {
  262. var result = resolve.sync('./invalid_main', { basedir: dir });
  263. t.equal(result, undefined, 'result should not exist');
  264. t.fail('should not get here');
  265. } catch (err) {
  266. t.ok(err, 'errors on non-string main');
  267. t.equal(err.message, 'package “invalid main” `main` must be a string');
  268. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  269. }
  270. t.end();
  271. });
  272. test('browser field in package.json', function (t) {
  273. var dir = path.join(__dirname, 'resolver');
  274. var res = resolve.sync('./browser_field', {
  275. basedir: dir,
  276. packageFilter: function packageFilter(pkg) {
  277. if (pkg.browser) {
  278. pkg.main = pkg.browser;
  279. delete pkg.browser;
  280. }
  281. return pkg;
  282. }
  283. });
  284. t.equal(res, path.join(dir, 'browser_field', 'b.js'));
  285. t.end();
  286. });