Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

resolver.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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('path iterator', function (t) {
  213. t.plan(2);
  214. var resolverDir = path.join(__dirname, 'resolver');
  215. var exactIterator = function (x, start, getPackageCandidates, opts) {
  216. return [path.join(resolverDir, x)];
  217. };
  218. resolve('baz', { packageIterator: exactIterator }, function (err, res, pkg) {
  219. if (err) t.fail(err);
  220. t.equal(res, path.join(resolverDir, 'baz/quux.js'));
  221. t.equal(pkg && pkg.name, 'baz');
  222. });
  223. });
  224. test('incorrect main', function (t) {
  225. t.plan(1);
  226. var resolverDir = path.join(__dirname, 'resolver');
  227. var dir = path.join(resolverDir, 'incorrect_main');
  228. resolve('./incorrect_main', { basedir: resolverDir }, function (err, res, pkg) {
  229. if (err) t.fail(err);
  230. t.equal(res, path.join(dir, 'index.js'));
  231. });
  232. });
  233. test('without basedir', function (t) {
  234. t.plan(1);
  235. var dir = path.join(__dirname, 'resolver/without_basedir');
  236. var tester = require(path.join(dir, 'main.js'));
  237. tester(t, function (err, res, pkg) {
  238. if (err) {
  239. t.fail(err);
  240. } else {
  241. t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
  242. }
  243. });
  244. });
  245. test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
  246. t.plan(2);
  247. var dir = path.join(__dirname, 'resolver');
  248. resolve('./foo', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
  249. if (err) t.fail(err);
  250. t.equal(res, path.join(dir, 'same_names/foo.js'));
  251. });
  252. resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
  253. if (err) t.fail(err);
  254. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  255. });
  256. });
  257. test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
  258. t.plan(2);
  259. var dir = path.join(__dirname, 'resolver');
  260. resolve('./', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
  261. if (err) t.fail(err);
  262. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  263. });
  264. resolve('.', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
  265. if (err) t.fail(err);
  266. t.equal(res, path.join(dir, 'same_names/foo/index.js'));
  267. });
  268. });
  269. test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
  270. var testFile = path.basename(__filename);
  271. t.test('sanity check', function (st) {
  272. st.plan(1);
  273. resolve('./' + testFile, function (err, res, pkg) {
  274. if (err) t.fail(err);
  275. st.equal(res, __filename, 'sanity check');
  276. });
  277. });
  278. t.test('with a fake directory', function (st) {
  279. st.plan(4);
  280. resolve('./' + testFile + '/blah', function (err, res, pkg) {
  281. st.ok(err, 'there is an error');
  282. st.notOk(res, 'no result');
  283. st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
  284. st.equal(
  285. err && err.message,
  286. 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
  287. 'can not find nonexistent module'
  288. );
  289. st.end();
  290. });
  291. });
  292. t.end();
  293. });
  294. test('async dot main', function (t) {
  295. var start = new Date();
  296. t.plan(3);
  297. resolve('./resolver/dot_main', function (err, ret) {
  298. t.notOk(err);
  299. t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
  300. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  301. t.end();
  302. });
  303. });
  304. test('async dot slash main', function (t) {
  305. var start = new Date();
  306. t.plan(3);
  307. resolve('./resolver/dot_slash_main', function (err, ret) {
  308. t.notOk(err);
  309. t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
  310. t.ok(new Date() - start < 50, 'resolve.sync timedout');
  311. t.end();
  312. });
  313. });
  314. test('not a directory', function (t) {
  315. t.plan(6);
  316. var path = './foo';
  317. resolve(path, { basedir: __filename }, function (err, res, pkg) {
  318. t.ok(err, 'a non-directory errors');
  319. t.equal(arguments.length, 1);
  320. t.equal(res, undefined);
  321. t.equal(pkg, undefined);
  322. t.equal(err && err.message, 'Cannot find module \'' + path + '\' from \'' + __filename + '\'');
  323. t.equal(err && err.code, 'MODULE_NOT_FOUND');
  324. });
  325. });
  326. test('non-string "main" field in package.json', function (t) {
  327. t.plan(5);
  328. var dir = path.join(__dirname, 'resolver');
  329. resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
  330. t.ok(err, 'errors on non-string main');
  331. t.equal(err.message, 'package “invalid main” `main` must be a string');
  332. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  333. t.equal(res, undefined, 'res is undefined');
  334. t.equal(pkg, undefined, 'pkg is undefined');
  335. });
  336. });
  337. test('non-string "main" field in package.json', function (t) {
  338. t.plan(5);
  339. var dir = path.join(__dirname, 'resolver');
  340. resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
  341. t.ok(err, 'errors on non-string main');
  342. t.equal(err.message, 'package “invalid main” `main` must be a string');
  343. t.equal(err.code, 'INVALID_PACKAGE_MAIN');
  344. t.equal(res, undefined, 'res is undefined');
  345. t.equal(pkg, undefined, 'pkg is undefined');
  346. });
  347. });
  348. test('browser field in package.json', function (t) {
  349. t.plan(3);
  350. var dir = path.join(__dirname, 'resolver');
  351. resolve(
  352. './browser_field',
  353. {
  354. basedir: dir,
  355. packageFilter: function packageFilter(pkg) {
  356. if (pkg.browser) {
  357. pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
  358. delete pkg.browser; // eslint-disable-line no-param-reassign
  359. }
  360. return pkg;
  361. }
  362. },
  363. function (err, res, pkg) {
  364. if (err) t.fail(err);
  365. t.equal(res, path.join(dir, 'browser_field', 'b.js'));
  366. t.equal(pkg && pkg.main, 'b');
  367. t.equal(pkg && pkg.browser, undefined);
  368. }
  369. );
  370. });