Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #301 from snyk/fix/test_updater
fix: add tests for update-notifier and adjust code
- Loading branch information
Showing
3 changed files
with
59 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as updateNotifier from 'update-notifier'; | ||
import * as fs from 'fs'; | ||
import * as p from 'path'; | ||
|
||
export function updateCheck() { | ||
const pkgPath = p.join(__dirname, '../..', 'package.json'); | ||
const isPkgFilePresent = fs.existsSync(pkgPath); | ||
|
||
if (!isPkgFilePresent) { | ||
return false; | ||
} | ||
|
||
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); | ||
|
||
// if there's no version (f.e. during tests) - do not proceed | ||
if (!pkg.version) { | ||
return false; | ||
} | ||
|
||
// Checks for available update and returns an instance | ||
// Default updateCheckInterval is once a day | ||
const notifier = updateNotifier({pkg}); | ||
notifier.notify(); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const tap = require('tap'); | ||
const test = tap.test; | ||
const updateCheck = require('../src/lib/updater').updateCheck; | ||
const path = require('path'); | ||
const sinon = require('sinon').createSandbox(); | ||
const updateNotifier = require('update-notifier'); | ||
|
||
// Fake location of the package.json file and verify the code behaves well | ||
test('missing package.json', (t) => { | ||
t.plan(1); | ||
let resolveStub = sinon.stub(path, 'resolve'); | ||
resolveStub.onFirstCall().returns('falseDir'); | ||
resolveStub.onSecondCall().returns('falseFile'); | ||
|
||
t.tearDown(() => { | ||
resolveStub.restore(); | ||
}); | ||
|
||
t.equal(updateCheck(), false, 'Notifier was not started on missing package.json'); | ||
t.end(); | ||
}); | ||
|
||
// Run updateNotifier API for the basic package. THe target is to verify API still stands | ||
test('verify updater', (t) => { | ||
var pkg = { | ||
name: 'snyk', | ||
version: '1.0.0' | ||
}; | ||
const notifier = updateNotifier({pkg}); | ||
|
||
t.equal(notifier.packageName, 'snyk', 'Successfull call to notifier'); | ||
t.end(); | ||
}); |