1 | 'use strict';
|
---|
2 |
|
---|
3 | var fs = require('fs');
|
---|
4 | var homedir = require('../lib/homedir');
|
---|
5 | var path = require('path');
|
---|
6 |
|
---|
7 | var test = require('tape');
|
---|
8 | var mkdirp = require('mkdirp');
|
---|
9 | var rimraf = require('rimraf');
|
---|
10 | var mv = require('mv');
|
---|
11 | var copyDir = require('copy-dir');
|
---|
12 | var tmp = require('tmp');
|
---|
13 |
|
---|
14 | var HOME = homedir();
|
---|
15 |
|
---|
16 | var hnm = path.join(HOME, '.node_modules');
|
---|
17 | var hnl = path.join(HOME, '.node_libraries');
|
---|
18 |
|
---|
19 | var resolve = require('../async');
|
---|
20 |
|
---|
21 | function makeDir(t, dir, cb) {
|
---|
22 | mkdirp(dir, function (err) {
|
---|
23 | if (err) {
|
---|
24 | cb(err);
|
---|
25 | } else {
|
---|
26 | t.teardown(function cleanup() {
|
---|
27 | rimraf.sync(dir);
|
---|
28 | });
|
---|
29 | cb();
|
---|
30 | }
|
---|
31 | });
|
---|
32 | }
|
---|
33 |
|
---|
34 | function makeTempDir(t, dir, cb) {
|
---|
35 | if (fs.existsSync(dir)) {
|
---|
36 | var tmpResult = tmp.dirSync();
|
---|
37 | t.teardown(tmpResult.removeCallback);
|
---|
38 | var backup = path.join(tmpResult.name, path.basename(dir));
|
---|
39 | mv(dir, backup, function (err) {
|
---|
40 | if (err) {
|
---|
41 | cb(err);
|
---|
42 | } else {
|
---|
43 | t.teardown(function () {
|
---|
44 | mv(backup, dir, cb);
|
---|
45 | });
|
---|
46 | makeDir(t, dir, cb);
|
---|
47 | }
|
---|
48 | });
|
---|
49 | } else {
|
---|
50 | makeDir(t, dir, cb);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | test('homedir module paths', function (t) {
|
---|
55 | t.plan(7);
|
---|
56 |
|
---|
57 | makeTempDir(t, hnm, function (err) {
|
---|
58 | t.error(err, 'no error with HNM temp dir');
|
---|
59 | if (err) {
|
---|
60 | return t.end();
|
---|
61 | }
|
---|
62 |
|
---|
63 | var bazHNMDir = path.join(hnm, 'baz');
|
---|
64 | var dotMainDir = path.join(hnm, 'dot_main');
|
---|
65 | copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNMDir);
|
---|
66 | copyDir.sync(path.join(__dirname, 'resolver/dot_main'), dotMainDir);
|
---|
67 |
|
---|
68 | var bazPkg = { name: 'baz', main: 'quux.js' };
|
---|
69 | var dotMainPkg = { main: 'index' };
|
---|
70 |
|
---|
71 | var bazHNMmain = path.join(bazHNMDir, 'quux.js');
|
---|
72 | t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
|
---|
73 | var dotMainMain = path.join(dotMainDir, 'index.js');
|
---|
74 | t.equal(require.resolve('dot_main'), dotMainMain, 'sanity check: require.resolve finds `dot_main`');
|
---|
75 |
|
---|
76 | makeTempDir(t, hnl, function (err) {
|
---|
77 | t.error(err, 'no error with HNL temp dir');
|
---|
78 | if (err) {
|
---|
79 | return t.end();
|
---|
80 | }
|
---|
81 | var bazHNLDir = path.join(hnl, 'baz');
|
---|
82 | copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNLDir);
|
---|
83 |
|
---|
84 | var dotSlashMainDir = path.join(hnl, 'dot_slash_main');
|
---|
85 | var dotSlashMainMain = path.join(dotSlashMainDir, 'index.js');
|
---|
86 | var dotSlashMainPkg = { main: 'index' };
|
---|
87 | copyDir.sync(path.join(__dirname, 'resolver/dot_slash_main'), dotSlashMainDir);
|
---|
88 |
|
---|
89 | t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
|
---|
90 | t.equal(require.resolve('dot_slash_main'), dotSlashMainMain, 'sanity check: require.resolve finds HNL `dot_slash_main`');
|
---|
91 |
|
---|
92 | t.test('with temp dirs', function (st) {
|
---|
93 | st.plan(3);
|
---|
94 |
|
---|
95 | st.test('just in `$HOME/.node_modules`', function (s2t) {
|
---|
96 | s2t.plan(3);
|
---|
97 |
|
---|
98 | resolve('dot_main', function (err, res, pkg) {
|
---|
99 | s2t.error(err, 'no error resolving `dot_main`');
|
---|
100 | s2t.equal(res, dotMainMain, '`dot_main` resolves in `$HOME/.node_modules`');
|
---|
101 | s2t.deepEqual(pkg, dotMainPkg);
|
---|
102 | });
|
---|
103 | });
|
---|
104 |
|
---|
105 | st.test('just in `$HOME/.node_libraries`', function (s2t) {
|
---|
106 | s2t.plan(3);
|
---|
107 |
|
---|
108 | resolve('dot_slash_main', function (err, res, pkg) {
|
---|
109 | s2t.error(err, 'no error resolving `dot_slash_main`');
|
---|
110 | s2t.equal(res, dotSlashMainMain, '`dot_slash_main` resolves in `$HOME/.node_libraries`');
|
---|
111 | s2t.deepEqual(pkg, dotSlashMainPkg);
|
---|
112 | });
|
---|
113 | });
|
---|
114 |
|
---|
115 | st.test('in `$HOME/.node_libraries` and `$HOME/.node_modules`', function (s2t) {
|
---|
116 | s2t.plan(3);
|
---|
117 |
|
---|
118 | resolve('baz', function (err, res, pkg) {
|
---|
119 | s2t.error(err, 'no error resolving `baz`');
|
---|
120 | s2t.equal(res, bazHNMmain, '`baz` resolves in `$HOME/.node_modules` when in both');
|
---|
121 | s2t.deepEqual(pkg, bazPkg);
|
---|
122 | });
|
---|
123 | });
|
---|
124 | });
|
---|
125 | });
|
---|
126 | });
|
---|
127 | });
|
---|