Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase util.js test coverage #419

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
},
"devDependencies": {
"ava": "^2.1.0",
"proxyquire": "^2.1.0",
"sinon": "^7.3.2",
"xo": "^0.24.0"
}
}
58 changes: 51 additions & 7 deletions test/hyperlinks.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
import test from 'ava';
import {linkifyIssues, linkifyCommit} from '../source/util';
import sinon from 'sinon';
import terminalLink from 'terminal-link';
import {linkifyIssues, linkifyCommit, linkifyCommitRange} from '../source/util';

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;;');
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();

test.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);
});
6 changes: 6 additions & 0 deletions test/prefix.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import test from 'ava';
import proxyquire from 'proxyquire';
import {getTagVersionPrefix} from '../source/util';

test('get tag prefix', async t => {
Expand All @@ -10,3 +11,8 @@ test('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 => {
proxyquire('../source/util', {execa: Promise.reject});
t.is(await getTagVersionPrefix({yarn: true}), 'v');
});