This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
get-prefix.js
111 lines (100 loc) · 3.3 KB
/
get-prefix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use strict'
const os = require('os')
const path = require('path')
const requireInject = require('require-inject')
const Tacks = require('tacks')
const test = require('tap').test
const testDir = require('./util/test-dir.js')(__filename)
const Dir = Tacks.Dir
const File = Tacks.File
const getPrefix = require('../get-prefix.js')
test('navigates out of `node_modules` without fs nav', t => {
return getPrefix(
path.join(testDir, 'node_modules', 'node_modules')
).then(val => {
t.equal(val, testDir, 'navigates out of node_modules')
})
})
test('detects if currently in an npm package using package.json', t => {
const fixture = new Tacks(Dir({
'package.json': File({name: 'foo', version: '1.2.3'})
}))
fixture.create(testDir)
return getPrefix(testDir).then(prefix => {
t.equal(prefix, testDir, 'current dir worked out')
})
})
test('detects if currently in an npm package using node_modules', t => {
const fixture = new Tacks(Dir({
'node_modules': Dir({})
}))
fixture.create(testDir)
return getPrefix(testDir).then(prefix => {
t.equal(prefix, testDir, 'current dir worked out')
})
})
test('returns false if no package was found in parent dirs', t => {
// Hopefully folks' tmpdir isn't inside an npm package ;)
const tmp = os.tmpdir()
return getPrefix(tmp).then(prefix => {
t.equal(prefix, false, 'returned the false')
})
})
test('navigates up the filesystem until it finds a package', t => {
const fixture = new Tacks(Dir({
'node_modules': Dir({}),
'a': Dir({'b': Dir({'c': Dir({'d': Dir({})})})})
}))
fixture.create(testDir)
return getPrefix(path.join(testDir, 'a', 'b', 'c')).then(prefix => {
t.equal(prefix, testDir, 'navigated all the way up')
})
})
test('doesn\'t go too far while navigating up', t => {
const fixture = new Tacks(Dir({
'node_modules': Dir({}),
'a': Dir({'node_modules': Dir({}), 'b': Dir({'c': Dir({'d': Dir({})})})})
}))
fixture.create(testDir)
return getPrefix(path.join(testDir, 'a', 'b', 'c')).then(prefix => {
t.equal(prefix, path.join(testDir, 'a'), 'stopped before root')
})
})
test('fileExists unit', t => {
const fileExists = requireInject('../get-prefix.js', {
fs: {
stat (todo, cb) {
if (todo === 'exists') {
cb(null, {name: 'yay'})
} else if (todo === 'enoent') {
const err = new Error('not found')
err.code = 'ENOENT'
cb(err)
} else {
cb(new Error('idk'))
}
}
}
})._fileExists
return Promise.all([
fileExists('exists'),
fileExists('enoent'),
fileExists('kaboom').then(() => {
throw new Error('nope')
}, err => err)
]).then(results => {
t.deepEqual(results[0], {name: 'yay'}, 'existing stat returned')
t.notOk(results[1], 'missing file succeeds with falsy')
t.match(results[2].message, /^idk$/, 'other errors thrown')
})
})
test('isRootPath unit', t => {
const isRoot = getPrefix._isRootPath
t.ok(isRoot('C:\\', 'win32'), 'detected root on windows')
t.notOk(isRoot('C:\\foo', 'win32'), 'detected non-root on windows')
t.ok(isRoot('/', 'darwin'), 'detected root on darwin')
t.notOk(isRoot('/foo', 'darwin'), 'detected non-root on darwin')
t.ok(isRoot('/', 'linux'), 'detected root on linux')
t.notOk(isRoot('/foo', 'linux'), 'detected non-root on linux')
t.done()
})