-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
124 lines (106 loc) · 3.1 KB
/
test.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
112
113
114
115
116
117
118
119
120
121
122
123
124
/*!
* detect-installed <https://github.com/tunnckoCore/detect-installed>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (http://i.am.charlike.online)
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
const test = require('mukla')
const detectInstalled = require('./index')
const mkdirp = require('mkdirp')
const getDir = require('pkg-dir')
const rimraf = require('rimraf')
test('async: should get TypeError when invalid `name` is passed', () => {
return detectInstalled(1234).catch((err) => {
test.strictEqual(err.name, 'TypeError')
test.strictEqual(/expect `name` to be string/.test(err.message), true)
})
})
test('async: should return true if exists globally', () => {
return detectInstalled('npm').then((actual) => {
test.strictEqual(actual, true)
})
})
test('async: should return false if not exists glboally', () => {
return detectInstalled('foo-bar-bqwewrwevdfg-sa').then((actual) => {
test.strictEqual(actual, false)
})
})
test('async: should return true if exists locally', () => {
return detectInstalled('global-modules', {
local: true
}).then((actual) => {
test.strictEqual(actual, true)
})
})
test('async: should return false if not exists locally', () => {
return detectInstalled('sdfjkhskh3-sf9sd78fsdf', {
local: true
}).then((actual) => {
test.strictEqual(actual, false)
})
})
/**
* testing synchronous mode
*/
test('synchronous: should return false if TypeError when invalid `name` is passed', (done) => {
test.strictEqual(detectInstalled.sync(1234), false)
done()
})
test('synchronous: should return true if exists globally', () => {
return new Promise((resolve) => {
test.strictEqual(detectInstalled.sync('npm'), true)
resolve()
})
})
test('synchronous: should return false if not exists globally', () => {
return new Promise((resolve) => {
test.strictEqual(detectInstalled.sync('foo-bar-bqwewrwevdfg-sa'), false)
resolve()
})
})
test('synchronous: should return true if exists locally', () => {
return new Promise((resolve) => {
const res = detectInstalled.sync('global-modules', {
local: true
})
test.strictEqual(res, true)
resolve()
})
})
test('synchronous: should return false if not exists locally', () => {
return new Promise((resolve) => {
const actual = detectInstalled.sync('sdfjkhskh3-sf9sd78fsdf', {
local: true
})
test.strictEqual(actual, false)
resolve()
})
})
test('synchronous: should work for subdirs (issue #5), exists locally', (done) => {
const dirname = __dirname
mkdirp.sync('barrr')
process.chdir('barrr')
const result = detectInstalled.sync('global-modules', {
cwd: getDir.sync(),
local: true
})
test.strictEqual(result, true)
process.chdir(dirname)
rimraf.sync('barrr')
done()
})
test('async: should work for #5, not exists locally', () => {
const dirname = __dirname
mkdirp.sync('subdir')
process.chdir('subdir')
return detectInstalled('npm', {
cwd: getDir.sync(),
local: true
}).then((actual) => {
test.strictEqual(actual, false)
process.chdir(dirname)
rimraf.sync('subdir')
})
})