Skip to content

Commit

Permalink
Merge pull request #301 from snyk/fix/test_updater
Browse files Browse the repository at this point in the history
fix: add tests for update-notifier and adjust code
  • Loading branch information
yuliabaron committed Dec 10, 2018
2 parents 61889f8 + d2cfcb0 commit 56726e1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 26 deletions.
27 changes: 1 addition & 26 deletions src/cli/index.ts
Expand Up @@ -13,32 +13,7 @@ import spinner = require('../lib/spinner');
import errors = require('../lib/error');
import ansiEscapes = require('ansi-escapes');
import {isPathToPackageFile} from '../lib/detect';
import * as updateNotifier from 'update-notifier';
import * as fs from 'fs';
import * as p from 'path';

function updateCheck() {
const root = p.resolve(__dirname, p.normalize('../..'));
const pkgPath = p.resolve(root, 'package.json');
const isPkgFilePresent = fs.existsSync(pkgPath);

if (!isPkgFilePresent) {
return;
}

const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));

// if there's no version (f.e. during tests) - do not proceed
if (!pkg.version) {
return;
}

// Checks for available update and returns an instance
// Default updateCheckInterval is once a day
const notifier = updateNotifier({pkg});
notifier.notify();
return;
}
import {updateCheck} from '../lib/updater';

async function runCommand(args) {
const result = await args.method(...args.options._);
Expand Down
25 changes: 25 additions & 0 deletions src/lib/updater.ts
@@ -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;
}
33 changes: 33 additions & 0 deletions test/updater.test.js
@@ -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();
});

0 comments on commit 56726e1

Please sign in to comment.