Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. var path = require('path');
  2. var test = require('tape');
  3. var resolve = require('../');
  4. test('async foo', function (t) {
  5. t.plan(12);
  6. var dir = path.join(__dirname, 'resolver');
  7. resolve('./foo', { basedir: dir }, function (err, res, pkg) {
  8. if (err) t.fail(err);
  9. t.equal(res, path.join(dir, 'foo.js'));
  10. t.equal(pkg && pkg.name, 'resolve');
  11. });
  12. resolve('./foo.js', { basedir: dir }, function (err, res, pkg) {
  13. if (err) t.fail(err);
  14. t.equal(res, path.join(dir, 'foo.js'));
  15. t.equal(pkg && 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, path.join(dir, 'foo.js'));
  20. t.equal(pkg && 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, path.join(dir, 'foo.js'));
  25. t.equal(pkg.main, 'resolver');
  26. });
  27. resolve('./foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err, res) {
  28. if (err) t.fail(err);
  29. t.equal(res, path.join(dir, 'foo.js'));
  30. });
  31. resolve('foo', { basedir: dir }, function (err) {
  32. t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
  33. t.equal(err.code, 'MODULE_NOT_FOUND');
  34. });
  35. // Test that filename is reported as the "from" value when passed.
  36. resolve('foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err) {
  37. t.equal(err.message, "Cannot find module 'foo' from '" + path.join(dir, 'baz.js') + "'");
  38. });
  39. });
  40. test('bar', function (t) {
  41. t.plan(6);
  42. var dir = path.join(__dirname, 'resolver');
  43. resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
  44. if (err) t.fail(err);
  45. t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
  46. t.equal(pkg, undefined);
  47. });
  48. resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
  49. if (err) t.fail(err);
  50. t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
  51. t.equal(pkg, undefined);
  52. });
  53. resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) {
  54. if (err) t.fail(err);
  55. t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
  56. t.equal(pkg.main, 'bar');
  57. });
  58. });
  59. test('baz', function (t) {
  60. t.plan(4);
  61. var dir = path.join(__dirname, 'resolver');
  62. resolve('./baz', { basedir: dir }, function (err, res, pkg) {
  63. if (err) t.fail(err);
  64. t.equal(res, path.join(dir, 'baz/quux.js'));
  65. t.equal(pkg.main, 'quux.js');
  66. });
  67. resolve('./baz', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
  68. if (err) t.fail(err);
  69. t.equal(res, path.join(dir, 'baz/quux.js'));
  70. t.equal(pkg.main, 'quux.js');
  71. });
  72. });
  73. test('biz', function (t) {
  74. t.plan(24);
  75. var dir = path.join(__dirname, 'resolver/biz/node_modules');
  76. resolve('./grux', { basedir: dir }, function (err, res, pkg) {
  77. if (err) t.fail(err);
  78. t.equal(res, path.join(dir, 'grux/index.js'));
  79. t.equal(pkg, undefined);
  80. });
  81. resolve('./grux', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
  82. if (err) t.fail(err);
  83. t.equal(res, path.join(dir, 'grux/index.js'));
  84. t.equal(pkg.main, 'biz');
  85. });
  86. resolve('./garply', { basedir: dir }, function (err, res, pkg) {
  87. if (err) t.fail(err);
  88. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  89. t.equal(pkg.main, './lib');
  90. });
  91. resolve('./garply', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
  92. if (err) t.fail(err);
  93. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  94. t.equal(pkg.main, './lib');
  95. });
  96. resolve('tiv', { basedir: dir + '/grux' }, function (err, res, pkg) {
  97. if (err) t.fail(err);
  98. t.equal(res, path.join(dir, 'tiv/index.js'));
  99. t.equal(pkg, undefined);
  100. });
  101. resolve('tiv', { basedir: dir + '/grux', 'package': { main: 'grux' } }, function (err, res, pkg) {
  102. if (err) t.fail(err);
  103. t.equal(res, path.join(dir, 'tiv/index.js'));
  104. t.equal(pkg.main, 'grux');
  105. });
  106. resolve('tiv', { basedir: dir + '/garply' }, function (err, res, pkg) {
  107. if (err) t.fail(err);
  108. t.equal(res, path.join(dir, 'tiv/index.js'));
  109. t.equal(pkg, undefined);
  110. });
  111. resolve('tiv', { basedir: dir + '/garply', 'package': { main: './lib' } }, function (err, res, pkg) {
  112. if (err) t.fail(err);
  113. t.equal(res, path.join(dir, 'tiv/index.js'));
  114. t.equal(pkg.main, './lib');
  115. });
  116. resolve('grux', { basedir: dir + '/tiv' }, function (err, res, pkg) {
  117. if (err) t.fail(err);
  118. t.equal(res, path.join(dir, 'grux/index.js'));
  119. t.equal(pkg, undefined);
  120. });
  121. resolve('grux', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
  122. if (err) t.fail(err);
  123. t.equal(res, path.join(dir, 'grux/index.js'));
  124. t.equal(pkg.main, 'tiv');
  125. });
  126. resolve('garply', { basedir: dir + '/tiv' }, function (err, res, pkg) {
  127. if (err) t.fail(err);
  128. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  129. t.equal(pkg.main, './lib');
  130. });
  131. resolve('garply', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
  132. if (err) t.fail(err);
  133. t.equal(res, path.join(dir, 'garply/lib/index.js'));
  134. t.equal(pkg.main, './lib');
  135. });
  136. });
  137. test('quux', function (t) {
  138. t.plan(2);
  139. var dir = path.join(__dirname, 'resolver/quux');
  140. resolve('./foo', { basedir: dir, 'package': { main: 'quux' } }, function (err, res, pkg) {
  141. if (err) t.fail(err);
  142. t.equal(res, path.join(dir, 'foo/index.js'));
  143. t.equal(pkg.main, 'quux');
  144. });
  145. });
  146. test('normalize', function (t) {
  147. t.plan(2);
  148. var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
  149. resolve('../grux', { basedir: dir }, function (err, res, pkg) {
  150. if (err) t.fail(err);
  151. t.equal(res, path.join(dir, 'index.js'));
  152. t.equal(pkg, undefined);
  153. });
  154. });
  155. test('cup', function (t) {
  156. t.plan(5);
  157. var dir = path.join(__dirname, 'resolver');
  158. resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
  159. if (err) t.fail(err);
  160. t.equal(res, path.join(dir, 'cup.coffee'));
  161. });
  162. resolve('./cup.coffee', { basedir: dir }, function (err, res) {
  163. if (err) t.fail(err);
  164. t.equal(res, path.join(dir, 'cup.coffee'));
  165. });
  166. resolve('./cup', { basedir: dir, extensions: ['.js'] }, function (err, res) {
  167. t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
  168. t.equal(err.code, 'MODULE_NOT_FOUND');
  169. });
  170. // Test that filename is reported as the "from" value when passed.
  171. resolve('./cup', { basedir: dir, extensions: ['.js'], filename: path.join(dir, 'cupboard.js') }, function (err, res) {
  172. t.equal(err.message, "Cannot find module './cup' from '" + path.join(dir, 'cupboard.js') + "'");
  173. });
  174. });
  175. test('mug', function (t) {
  176. t.plan(3);
  177. var dir = path.join(__dirname, 'resolver');
  178. resolve('./mug', { basedir: dir }, function (err, res) {
  179. if (err) t.fail(err);
  180. t.equal(res, path.join(dir, 'mug.js'));
  181. });
  182. resolve('./mug', { basedir: dir, extensions: ['.coffee', '.js'] }, function (err, res) {
  183. if (err) t.fail(err);
  184. t.equal(res, path.join(dir, '/mug.coffee'));
  185. });
  186. resolve('./mug', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
  187. t.equal(res, path.join(dir, '/mug.js'));
  188. });
  189. });
  190. test('other path', function (t) {
  191. t.plan(6);
  192. var resolverDir = path.join(__dirname, 'resolver');
  193. var dir = path.join(resolverDir, 'bar');
  194. var otherDir = path.join(resolverDir, 'other_path');
  195. resolve('root', { basedir: dir, paths: [otherDir] }, function (err, res) {
  196. if (err) t.fail(err);
  197. t.equal(res, path.join(resolverDir, 'other_path/root.js'));
  198. });
  199. resolve('lib/other-lib', { basedir: dir, paths: [otherDir] }, function (err, res) {
  200. if (err) t.fail(err);
  201. t.equal(res, path.join(resolverDir, 'other_path/lib/other-lib.js'));
  202. });
  203. resolve('root', { basedir: dir }, function (err, res) {
  204. t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
  205. t.equal(err.code, 'MODULE_NOT_FOUND');
  206. });
  207. resolve('zzz', { basedir: dir, paths: [otherDir] }, function (err, res) {
  208. t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
  209. t.equal(err.code, 'MODULE_NOT_FOUND');
  210. });
  211. });
  212. test('incorrect main', function (t) {
  213. t.plan(1);
  214. var resolverDir = path.join(__dirname, 'resolver');
  215. var dir = path.join(resolverDir, 'incorrect_main');
  216. resolve('./incorrect_main', { basedir: resolverDir }, function (err, res, pkg) {
  217. if (err) t.fail(err);
  218. t.equal(res, path.join(dir, 'index.js'));
  219. });
  220. });
  221. test('without basedir', function (t) {
  222. t.plan(1);
  223. var dir = path.join(__dirname, 'resolver/without_basedir');
  224. var tester = require(path.join(dir, 'main.js'));
  225. tester(t, function (err, res, pkg) {
  226. if (err) {
  227. t.fail(err);
  228. } else {
  229. t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
  230. }
  231. });
  232. });
  233. test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
  234. t.plan(2);
  235. var dir = path.join(__dirname, 'resolver');
  236. resolve('./foo', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
  237. if (err) t.fail(err);
  238. t.equal(res, path.join(dir, 'same_names/foo.js'));
  239. });
  240. resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
  241. if (err) t.fail(err);
  242. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  243. });
  244. });
  245. test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
  246. var testFile = path.basename(__filename);
  247. t.test('sanity check', function (st) {
  248. st.plan(1);
  249. resolve('./' + testFile, function (err, res, pkg) {
  250. if (err) t.fail(err);
  251. st.equal(res, __filename, 'sanity check');
  252. });
  253. });
  254. t.test('with a fake directory', function (st) {
  255. st.plan(4);
  256. resolve('./' + testFile + '/blah', function (err, res, pkg) {
  257. st.ok(err, 'there is an error');
  258. st.notOk(res, 'no result');
  259. st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  260. st.equal(
  261. err && err.message,
  262. 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
  263. 'can not find nonexistent module'
  264. );
  265. st.end();
  266. });
  267. });
  268. t.end();
  269. });
  270. test('async dot main', function (t) {
  271. var start = new Date();
  272. t.plan(3);
  273. resolve('./resolver/dot_main', function (err, ret) {
  274. t.notOk(err);
  275. t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
  276. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  277. t.end();
  278. });
  279. });
  280. test('async dot slash main', function (t) {
  281. var start = new Date();
  282. t.plan(3);
  283. resolve('./resolver/dot_slash_main', function (err, ret) {
  284. t.notOk(err);
  285. t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
  286. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  287. t.end();
  288. });
  289. });
  290. test('not a directory', function (t) {
  291. t.plan(6);
  292. var path = './foo';
  293. resolve(path, { basedir: __filename }, function (err, res, pkg) {
  294. t.ok(err, 'a non-directory errors');
  295. t.equal(arguments.length, 1);
  296. t.equal(res, undefined);
  297. t.equal(pkg, undefined);
  298. t.equal(err && err.message, 'Cannot find module \'' + path + '\' from \'' + __filename + '\'');
  299. t.equal(err && err.code, 'MODULE_NOT_FOUND');
  300. });
  301. });
  302. test('non-string "main" field in package.json', function (t) {
  303. t.plan(5);
  304. var dir = path.join(__dirname, 'resolver');
  305. resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
  306. t.ok(err, 'errors on non-string main');
  307. t.equal(err.message, 'package “invalid main” `main` must be a string');
  308. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  309. t.equal(res, undefined, 'res is undefined');
  310. t.equal(pkg, undefined, 'pkg is undefined');
  311. });
  312. });
  313. test('non-string "main" field in package.json', function (t) {
  314. t.plan(5);
  315. var dir = path.join(__dirname, 'resolver');
  316. resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
  317. t.ok(err, 'errors on non-string main');
  318. t.equal(err.message, 'package “invalid main” `main` must be a string');
  319. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  320. t.equal(res, undefined, 'res is undefined');
  321. t.equal(pkg, undefined, 'pkg is undefined');
  322. });
  323. });
  324. test('browser field in package.json', function (t) {
  325. t.plan(3);
  326. var dir = path.join(__dirname, 'resolver');
  327. resolve(
  328. './browser_field',
  329. {
  330. basedir: dir,
  331. packageFilter: function packageFilter(pkg) {
  332. if (pkg.browser) {
  333. pkg.main = pkg.browser;
  334. delete pkg.browser;
  335. }
  336. return pkg;
  337. }
  338. },
  339. function (err, res, pkg) {
  340. if (err) t.fail(err);
  341. t.equal(res, path.join(dir, 'browser_field', 'b.js'));
  342. t.equal(pkg && pkg.main, 'b');
  343. t.equal(pkg && pkg.browser, undefined);
  344. }
  345. );
  346. });