Skip to content

Commit

Permalink
increase util.js test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
0xch4z committed May 28, 2019
1 parent 0842b07 commit 0eec968
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 11 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
"update-notifier": "^2.1.0"
},
"devDependencies": {
"ava": "^1.1.0",
"ava": "^1.4.1",
"sinon": "^7.3.2",
"with-tmp": "^1.1.1",
"xo": "^0.24.0"
}
}
61 changes: 53 additions & 8 deletions test/hyperlinks.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,57 @@
import test from 'ava';
import {linkifyIssues, linkifyCommit} from '../source/util';
import test, {afterEach} from 'ava';
import sinon from 'sinon';
import terminalLink from 'terminal-link';

test('link issues', t => {
t.is(linkifyIssues('https://github.com/unicorn/rainbow', 'Commit message - fixes #4'), 'Commit message - fixes ]8;;https://github.com/unicorn/rainbow/issues/4#4]8;;');
t.is(linkifyIssues('https://github.com/unicorn/rainbow', 'Commit message - fixes #3 #4'), 'Commit message - fixes ]8;;https://github.com/unicorn/rainbow/issues/3#3]8;; ]8;;https://github.com/unicorn/rainbow/issues/4#4]8;;');
t.is(linkifyIssues('https://github.com/unicorn/rainbow', 'Commit message - fixes foo/bar#4'), 'Commit message - fixes ]8;;https://github.com/foo/bar/issues/4foo/bar#4]8;;');
import {linkifyIssues, linkifyCommit, linkifyCommitRange} from '../source/util';

const MOCK_REPO_URL = 'https://github.com/unicorn/rainbow';
const MOCK_COMMIT_HASH = '5063f8a';
const MOCK_COMMIT_RANGE = `${MOCK_COMMIT_HASH}...master`;

const sandbox = sinon.createSandbox();

afterEach(() => {
sandbox.restore();
});

const mockTerminalLinkUnsupported = () =>
sandbox.stub(terminalLink, 'isSupported').value(false);

test('linkifyIssues correctly links issues', t => {
t.is(linkifyIssues(MOCK_REPO_URL, 'Commit message - fixes #4'), 'Commit message - fixes ]8;;https://github.com/unicorn/rainbow/issues/4#4]8;;');
t.is(linkifyIssues(MOCK_REPO_URL, 'Commit message - fixes #3 #4'), 'Commit message - fixes ]8;;https://github.com/unicorn/rainbow/issues/3#3]8;; ]8;;https://github.com/unicorn/rainbow/issues/4#4]8;;');
t.is(linkifyIssues(MOCK_REPO_URL, 'Commit message - fixes foo/bar#4'), 'Commit message - fixes ]8;;https://github.com/foo/bar/issues/4foo/bar#4]8;;');
});

test('linkifyIssues returns raw message if url is not provided', t => {
const msg = 'Commit message - fixes #5';
t.is(linkifyIssues(undefined, msg), msg);
});

test.serial('linkifyIssues returns raw message if terminalLink is not supported', t => {
mockTerminalLinkUnsupported();
const msg = 'Commit message - fixes #6';
t.is(linkifyIssues(MOCK_REPO_URL, msg), msg);
});

test('linkifyCommit correctly links commits', t => {
t.is(linkifyCommit(MOCK_REPO_URL, MOCK_COMMIT_HASH), ']8;;https://github.com/unicorn/rainbow/commit/5063f8a5063f8a]8;;');
});

test('linkifyCommit returns raw commit hash if url is not provided', t => {
t.is(linkifyCommit(undefined, MOCK_COMMIT_HASH), MOCK_COMMIT_HASH);
});

test.serial('linkifyCommit returns raw commit hash if terminalLink is not supported', t => {
mockTerminalLinkUnsupported();
t.is(linkifyCommit(MOCK_REPO_URL, MOCK_COMMIT_HASH), MOCK_COMMIT_HASH);
});

test('linkifyCommitRange returns raw commitRange if url is not provided', t => {
t.is(linkifyCommitRange(undefined, MOCK_COMMIT_RANGE), MOCK_COMMIT_RANGE);
});

test('link commit', t => {
t.is(linkifyCommit('https://github.com/unicorn/rainbow', '5063f8a'), ']8;;https://github.com/unicorn/rainbow/commit/5063f8a5063f8a]8;;');
test.serial('linkifyCommitRange returns raw commitRange if terminalLink is not supported', t => {
mockTerminalLinkUnsupported();
t.is(linkifyCommitRange(MOCK_REPO_URL, MOCK_COMMIT_RANGE), MOCK_COMMIT_RANGE);
});
19 changes: 17 additions & 2 deletions test/prefix.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import test from 'ava';
import test, {afterEach} from 'ava';
import sinon from 'sinon';
import execa from 'execa';
import {getTagVersionPrefix} from '../source/util';

const sandbox = sinon.createSandbox();

afterEach(() => {
sandbox.restore();
});

test('get tag prefix', async t => {
t.is(await getTagVersionPrefix({yarn: false}), 'v');
t.is(await getTagVersionPrefix({yarn: true}), 'v');
});

test('no options passed', async t => {
test.serial('no options passed', async t => {
await t.throwsAsync(getTagVersionPrefix(), {message: 'Expected `options` to be of type `object` but received type `undefined`'});
await t.throwsAsync(getTagVersionPrefix({}), {message: 'Expected object `options` to have keys `["yarn"]`'});
});

test.serial('defaults to "v" when command fails', async t => {
sandbox.stub(execa, 'stdout')
.onFirstCall()
.rejects(new Error());
t.is(await getTagVersionPrefix({yarn: true}), 'v');
});
33 changes: 33 additions & 0 deletions test/read-pkg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as fs from 'fs';

import test from 'ava';
import {withinTmpdir} from 'with-tmp';

import {readPkg} from '../source/util';

test.serial('readPkg throws error when no package.json is found', t => {
t.plan(1);

return withinTmpdir('readPkg-no-pkg', () => {
t.throws(() => readPkg());
});
});

test.serial('readPkg gets package.json', t => {
t.plan(1);

const manifest = {
version: '0.0.1',
name: 'test-package',
license: 'MIT'
};
return withinTmpdir('readPkg-valid-pkg', () => {
fs.writeFileSync('package.json', JSON.stringify(manifest));
const pkg = readPkg();
t.deepEqual(pkg, {
...manifest,
_id: 'test-package@0.0.1',
readme: 'ERROR: No README data found!'
});
});
});

0 comments on commit 0eec968

Please sign in to comment.