resolver_sync.js 7.1 KB

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