resolver.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. var path = require('path');
  2. var test = require('tape');
  3. var resolve = require('../');
  4. test('async foo', function (t) {
  5. t.plan(9);
  6. var dir = __dirname + '/resolver';
  7. resolve('./foo', { basedir : dir }, function (err, res, pkg) {
  8. if (err) t.fail(err);
  9. t.equal(res, dir + '/foo.js');
  10. t.equal(pkg.name, 'resolve');
  11. });
  12. resolve('./foo.js', { basedir : dir }, function (err, res, pkg) {
  13. if (err) t.fail(err);
  14. t.equal(res, dir + '/foo.js');
  15. t.equal(pkg.name, 'resolve');
  16. });
  17. resolve('./foo', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
  18. if (err) t.fail(err);
  19. t.equal(res, dir + '/foo.js');
  20. t.equal(pkg.main, 'resolver');
  21. });
  22. resolve('./foo.js', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
  23. if (err) t.fail(err);
  24. t.equal(res, dir + '/foo.js');
  25. t.equal(pkg.main, 'resolver');
  26. });
  27. resolve('foo', { basedir : dir }, function (err) {
  28. t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
  29. });
  30. });
  31. test('bar', function (t) {
  32. t.plan(6);
  33. var dir = __dirname + '/resolver';
  34. resolve('foo', { basedir : dir + '/bar' }, function (err, res, pkg) {
  35. if (err) t.fail(err);
  36. t.equal(res, dir + '/bar/node_modules/foo/index.js');
  37. t.equal(pkg, undefined);
  38. });
  39. resolve('foo', { basedir : dir + '/bar' }, function (err, res, pkg) {
  40. if (err) t.fail(err);
  41. t.equal(res, dir + '/bar/node_modules/foo/index.js');
  42. t.equal(pkg, undefined);
  43. });
  44. resolve('foo', { basedir : dir + '/bar', package: { main: 'bar' } }, function (err, res, pkg) {
  45. if (err) t.fail(err);
  46. t.equal(res, dir + '/bar/node_modules/foo/index.js');
  47. t.equal(pkg, undefined);
  48. });
  49. });
  50. test('baz', function (t) {
  51. t.plan(4);
  52. var dir = __dirname + '/resolver';
  53. resolve('./baz', { basedir : dir }, function (err, res, pkg) {
  54. if (err) t.fail(err);
  55. t.equal(res, dir + '/baz/quux.js');
  56. t.equal(pkg.main, 'quux.js');
  57. });
  58. resolve('./baz', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
  59. if (err) t.fail(err);
  60. t.equal(res, dir + '/baz/quux.js');
  61. t.equal(pkg.main, 'quux.js');
  62. });
  63. });
  64. test('biz', function (t) {
  65. t.plan(24);
  66. var dir = __dirname + '/resolver/biz/node_modules';
  67. resolve('./grux', { basedir : dir }, function (err, res, pkg) {
  68. if (err) t.fail(err);
  69. t.equal(res, dir + '/grux/index.js');
  70. t.equal(pkg, undefined);
  71. });
  72. resolve('./grux', { basedir : dir, package: { main: 'biz' } }, function (err, res, pkg) {
  73. if (err) t.fail(err);
  74. t.equal(res, dir + '/grux/index.js');
  75. t.equal(pkg.main, 'biz');
  76. });
  77. resolve('./garply', { basedir : dir }, function (err, res, pkg) {
  78. if (err) t.fail(err);
  79. t.equal(res, dir + '/garply/lib/index.js');
  80. t.equal(pkg.main, './lib');
  81. });
  82. resolve('./garply', { basedir : dir, package: { main: 'biz' } }, function (err, res, pkg) {
  83. if (err) t.fail(err);
  84. t.equal(res, dir + '/garply/lib/index.js');
  85. t.equal(pkg.main, './lib');
  86. });
  87. resolve('tiv', { basedir : dir + '/grux' }, function (err, res, pkg) {
  88. if (err) t.fail(err);
  89. t.equal(res, dir + '/tiv/index.js');
  90. t.equal(pkg, undefined);
  91. });
  92. resolve('tiv', { basedir : dir + '/grux', package: { main: 'grux' } }, function (err, res, pkg) {
  93. if (err) t.fail(err);
  94. t.equal(res, dir + '/tiv/index.js');
  95. t.equal(pkg, undefined);
  96. });
  97. resolve('tiv', { basedir : dir + '/garply' }, function (err, res, pkg) {
  98. if (err) t.fail(err);
  99. t.equal(res, dir + '/tiv/index.js');
  100. t.equal(pkg, undefined);
  101. });
  102. resolve('tiv', { basedir : dir + '/garply', package: { main: './lib' } }, function (err, res, pkg) {
  103. if (err) t.fail(err);
  104. t.equal(res, dir + '/tiv/index.js');
  105. t.equal(pkg, undefined);
  106. });
  107. resolve('grux', { basedir : dir + '/tiv' }, function (err, res, pkg) {
  108. if (err) t.fail(err);
  109. t.equal(res, dir + '/grux/index.js');
  110. t.equal(pkg, undefined);
  111. });
  112. resolve('grux', { basedir : dir + '/tiv', package: { main: 'tiv' } }, function (err, res, pkg) {
  113. if (err) t.fail(err);
  114. t.equal(res, dir + '/grux/index.js');
  115. t.equal(pkg, undefined);
  116. });
  117. resolve('garply', { basedir : dir + '/tiv' }, function (err, res, pkg) {
  118. if (err) t.fail(err);
  119. t.equal(res, dir + '/garply/lib/index.js');
  120. t.equal(pkg.main, './lib');
  121. });
  122. resolve('garply', { basedir : dir + '/tiv', package: { main: 'tiv' } }, function (err, res, pkg) {
  123. if (err) t.fail(err);
  124. t.equal(res, dir + '/garply/lib/index.js');
  125. t.equal(pkg.main, './lib');
  126. });
  127. });
  128. test('quux', function (t) {
  129. t.plan(2);
  130. var dir = __dirname + '/resolver/quux';
  131. resolve('./foo', { basedir : dir, package: { main: 'quux' } }, function (err, res, pkg) {
  132. if (err) t.fail(err);
  133. t.equal(res, dir + '/foo/index.js');
  134. t.equal(pkg.main, 'quux');
  135. });
  136. });
  137. test('normalize', function (t) {
  138. t.plan(2);
  139. var dir = __dirname + '/resolver/biz/node_modules/grux';
  140. resolve('../grux', { basedir : dir }, function (err, res, pkg) {
  141. if (err) t.fail(err);
  142. t.equal(res, dir + '/index.js');
  143. t.equal(pkg, undefined);
  144. });
  145. });
  146. test('cup', function (t) {
  147. t.plan(3);
  148. var dir = __dirname + '/resolver';
  149. resolve('./cup', { basedir : dir, extensions : [ '.js', '.coffee' ] },
  150. function (err, res) {
  151. if (err) t.fail(err);
  152. t.equal(res, dir + '/cup.coffee');
  153. });
  154. resolve('./cup.coffee', { basedir : dir }, function (err, res) {
  155. if (err) t.fail(err);
  156. t.equal(res, dir + '/cup.coffee');
  157. });
  158. resolve('./cup', { basedir : dir, extensions : [ '.js' ] },
  159. function (err, res) {
  160. t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
  161. });
  162. });
  163. test('mug', function (t) {
  164. t.plan(3);
  165. var dir = __dirname + '/resolver';
  166. resolve('./mug', { basedir : dir }, function (err, res) {
  167. if (err) t.fail(err);
  168. t.equal(res, dir + '/mug.js');
  169. });
  170. resolve('./mug', { basedir : dir, extensions : [ '.coffee', '.js' ] },
  171. function (err, res) {
  172. if (err) t.fail(err);
  173. t.equal(res, dir + '/mug.coffee');
  174. });
  175. resolve('./mug', { basedir : dir, extensions : [ '.js', '.coffee' ] },
  176. function (err, res) {
  177. t.equal(res, dir + '/mug.js');
  178. });
  179. });
  180. test('other path', function (t) {
  181. t.plan(4);
  182. var resolverDir = __dirname + '/resolver';
  183. var dir = resolverDir + '/bar';
  184. var otherDir = resolverDir + '/other_path';
  185. resolve('root', { basedir : dir, paths: [otherDir] }, function (err, res) {
  186. if (err) t.fail(err);
  187. t.equal(res, resolverDir + '/other_path/root.js');
  188. });
  189. resolve('lib/other-lib', { basedir : dir, paths: [otherDir] },
  190. function (err, res) {
  191. if (err) t.fail(err);
  192. t.equal(res, resolverDir + '/other_path/lib/other-lib.js');
  193. });
  194. resolve('root', { basedir : dir, }, function (err, res) {
  195. t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
  196. });
  197. resolve('zzz', { basedir : dir, paths: [otherDir] }, function (err, res) {
  198. t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
  199. });
  200. });
  201. test('incorrect main', function (t) {
  202. t.plan(1)
  203. var resolverDir = __dirname + '/resolver';
  204. var dir = resolverDir + '/incorrect_main';
  205. resolve('./incorrect_main', { basedir : resolverDir }, function (err, res, pkg) {
  206. if (err) t.fail(err);
  207. t.equal(res, dir + '/index.js');
  208. });
  209. });
  210. test('without basedir', function (t) {
  211. t.plan(1);
  212. var dir = __dirname + '/resolver/without_basedir';
  213. var tester = require(dir + '/main.js');
  214. tester(t, function (err, res, pkg){
  215. if (err) {
  216. t.fail(err);
  217. } else {
  218. t.equal(res, dir + '/node_modules/mymodule.js');
  219. }
  220. });
  221. });
  222. test('#25: node modules with the same name as node stdlib modules', function (t) {
  223. t.plan(1);
  224. var resolverDir = __dirname + '/resolver/punycode';
  225. resolve('punycode', { basedir : resolverDir }, function (err, res, pkg) {
  226. if (err) t.fail(err);
  227. t.equal(res, resolverDir + '/node_modules/punycode/index.js');
  228. });
  229. });