Skip to content

Commit

Permalink
test(update): improve tests, add new ones for #5
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Dec 11, 2016
1 parent a9586e7 commit 1c26584
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function detectInstalled (name, opts) {
}

fs.stat(defaults(name, opts), (err, stats) => {
if (err) return reject(err)
if (err) return resolve(false)
resolve(stats.isDirectory())
})
})
Expand Down Expand Up @@ -47,8 +47,8 @@ const defaults = (name, opts) => {

const tryStatSync = (fp) => {
try {
return fs.statSync(fp).isDirectory()
return fs.statSync(fp).isDirectory()
} catch (err) {
return false
return false
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
"commitizen": "^2.8.6",
"coveralls": "^2.11.15",
"cz-conventional-changelog": "^1.2.0",
"mkdirp": "^0.5.1",
"mukla": "^0.4.8",
"nyc": "^10.0.0",
"pkg-dir": "^1.0.0",
"pre-commit": "^1.2.0",
"rimraf": "^2.5.4",
"standard": "^8.6.0",
"standard-version": "^4.0.0"
},
Expand Down
115 changes: 113 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,119 @@

const test = require('mukla')
const detectInstalled = require('./index')
const mkdirp = require('mkdirp')
const getDir = require('pkg-dir')
const rimraf = require('rimraf')

test('detect-installed', function (done) {
detectInstalled()
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 throw TypeError when invalid `name` is passed', (done) => {
function fixture () {
detectInstalled.sync(1234)
}

test.throws(fixture, TypeError)
test.throws(fixture, /expect `name` to be string/)
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 glboally', () => {
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')
})
})

0 comments on commit 1c26584

Please sign in to comment.