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('../sync');
|
---|
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 bazHNMmain = path.join(bazHNMDir, 'quux.js');
|
---|
69 | t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
|
---|
70 | var dotMainMain = path.join(dotMainDir, 'index.js');
|
---|
71 | t.equal(require.resolve('dot_main'), dotMainMain, 'sanity check: require.resolve finds `dot_main`');
|
---|
72 |
|
---|
73 | makeTempDir(t, hnl, function (err) {
|
---|
74 | t.error(err, 'no error with HNL temp dir');
|
---|
75 | if (err) {
|
---|
76 | return t.end();
|
---|
77 | }
|
---|
78 | var bazHNLDir = path.join(hnl, 'baz');
|
---|
79 | copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNLDir);
|
---|
80 |
|
---|
81 | var dotSlashMainDir = path.join(hnl, 'dot_slash_main');
|
---|
82 | var dotSlashMainMain = path.join(dotSlashMainDir, 'index.js');
|
---|
83 | copyDir.sync(path.join(__dirname, 'resolver/dot_slash_main'), dotSlashMainDir);
|
---|
84 |
|
---|
85 | t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
|
---|
86 | t.equal(require.resolve('dot_slash_main'), dotSlashMainMain, 'sanity check: require.resolve finds HNL `dot_slash_main`');
|
---|
87 |
|
---|
88 | t.test('with temp dirs', function (st) {
|
---|
89 | st.plan(3);
|
---|
90 |
|
---|
91 | st.test('just in `$HOME/.node_modules`', function (s2t) {
|
---|
92 | s2t.plan(1);
|
---|
93 |
|
---|
94 | var res = resolve('dot_main');
|
---|
95 | s2t.equal(res, dotMainMain, '`dot_main` resolves in `$HOME/.node_modules`');
|
---|
96 | });
|
---|
97 |
|
---|
98 | st.test('just in `$HOME/.node_libraries`', function (s2t) {
|
---|
99 | s2t.plan(1);
|
---|
100 |
|
---|
101 | var res = resolve('dot_slash_main');
|
---|
102 | s2t.equal(res, dotSlashMainMain, '`dot_slash_main` resolves in `$HOME/.node_libraries`');
|
---|
103 | });
|
---|
104 |
|
---|
105 | st.test('in `$HOME/.node_libraries` and `$HOME/.node_modules`', function (s2t) {
|
---|
106 | s2t.plan(1);
|
---|
107 |
|
---|
108 | var res = resolve('baz');
|
---|
109 | s2t.equal(res, bazHNMmain, '`baz` resolves in `$HOME/.node_modules` when in both');
|
---|
110 | });
|
---|
111 | });
|
---|
112 | });
|
---|
113 | });
|
---|
114 | });
|
---|