resolver_sync.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. var test = require('tape');
  2. var resolve = require('../');
  3. test('foo', function (t) {
  4. var dir = __dirname + '/resolver';
  5. t.equal(
  6. resolve.sync('./foo', { basedir : dir }),
  7. dir + '/foo.js'
  8. );
  9. t.equal(
  10. resolve.sync('./foo.js', { basedir : dir }),
  11. dir + '/foo.js'
  12. );
  13. t.throws(function () {
  14. resolve.sync('foo', { basedir : dir });
  15. });
  16. t.end();
  17. });
  18. test('bar', function (t) {
  19. var dir = __dirname + '/resolver';
  20. t.equal(
  21. resolve.sync('foo', { basedir : dir + '/bar' }),
  22. dir + '/bar/node_modules/foo/index.js'
  23. );
  24. t.end();
  25. });
  26. test('baz', function (t) {
  27. var dir = __dirname + '/resolver';
  28. t.equal(
  29. resolve.sync('./baz', { basedir : dir }),
  30. dir + '/baz/quux.js'
  31. );
  32. t.end();
  33. });
  34. test('biz', function (t) {
  35. var dir = __dirname + '/resolver/biz/node_modules';
  36. t.equal(
  37. resolve.sync('./grux', { basedir : dir }),
  38. dir + '/grux/index.js'
  39. );
  40. t.equal(
  41. resolve.sync('tiv', { basedir : dir + '/grux' }),
  42. dir + '/tiv/index.js'
  43. );
  44. t.equal(
  45. resolve.sync('grux', { basedir : dir + '/tiv' }),
  46. dir + '/grux/index.js'
  47. );
  48. t.end();
  49. });
  50. test('normalize', function (t) {
  51. var dir = __dirname + '/resolver/biz/node_modules/grux';
  52. t.equal(
  53. resolve.sync('../grux', { basedir : dir }),
  54. dir + '/index.js'
  55. );
  56. t.end();
  57. });
  58. test('cup', function (t) {
  59. var dir = __dirname + '/resolver';
  60. t.equal(
  61. resolve.sync('./cup', {
  62. basedir : dir,
  63. extensions : [ '.js', '.coffee' ]
  64. }),
  65. dir + '/cup.coffee'
  66. );
  67. t.equal(
  68. resolve.sync('./cup.coffee', {
  69. basedir : dir
  70. }),
  71. dir + '/cup.coffee'
  72. );
  73. t.throws(function () {
  74. resolve.sync('./cup', {
  75. basedir : dir,
  76. extensions : [ '.js' ]
  77. })
  78. });
  79. t.end();
  80. });
  81. test('mug', function (t) {
  82. var dir = __dirname + '/resolver';
  83. t.equal(
  84. resolve.sync('./mug', { basedir : dir }),
  85. dir + '/mug.js'
  86. );
  87. t.equal(
  88. resolve.sync('./mug', {
  89. basedir : dir,
  90. extensions : [ '.coffee', '.js' ]
  91. }),
  92. dir + '/mug.coffee'
  93. );
  94. t.equal(
  95. resolve.sync('./mug', {
  96. basedir : dir,
  97. extensions : [ '.js', '.coffee' ]
  98. }),
  99. dir + '/mug.js'
  100. );
  101. t.end();
  102. });
  103. test('other path', function (t) {
  104. var resolverDir = __dirname + '/resolver';
  105. var dir = resolverDir + '/bar';
  106. var otherDir = resolverDir + '/other_path';
  107. var path = require('path');
  108. t.equal(
  109. resolve.sync('root', {
  110. basedir : dir,
  111. paths: [otherDir] }),
  112. resolverDir + '/other_path/root.js'
  113. );
  114. t.equal(
  115. resolve.sync('lib/other-lib', {
  116. basedir : dir,
  117. paths: [otherDir] }),
  118. 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. t.end();
  129. });
  130. test('incorrect main', function (t) {
  131. var resolverDir = __dirname + '/resolver';
  132. var dir = resolverDir + '/incorrect_main';
  133. t.equal(
  134. resolve.sync('./incorrect_main', { basedir : resolverDir }),
  135. dir + '/index.js'
  136. )
  137. t.end()
  138. });
  139. test('#25: node modules with the same name as node stdlib modules', function (t) {
  140. var resolverDir = __dirname + '/resolver/punycode';
  141. t.equal(
  142. resolve.sync('punycode', { basedir : resolverDir }),
  143. resolverDir + '/node_modules/punycode/index.js'
  144. )
  145. t.end()
  146. });