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

Add pre-publish task tests #509

Merged
merged 4 commits into from Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions package.json
Expand Up @@ -67,8 +67,27 @@
},
"devDependencies": {
"ava": "^2.3.0",
"execa_test_double": "^4.0.0",
"mockery": "^2.1.0",
"proxyquire": "^2.1.0",
"sinon": "^8.0.1",
"xo": "^0.25.3"
},
"xo": {
bunysae marked this conversation as resolved.
Show resolved Hide resolved
"rules": {
"ava/no-import-test-files": [
"off",
{
"files": [
"test/*"
]
}
]
}
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
},
"ava": {
"files": [
"!test/fixtures"
]
}
}
20 changes: 20 additions & 0 deletions test/fixtures/listr-renderer.js
@@ -0,0 +1,20 @@

class SilentRenderer {
constructor(_tasks) {
module.exports.tasks = _tasks;
}

static get nonTTY() {
return true;
}

render() {
}

end() {

}
}

module.exports.SilentRenderer = SilentRenderer;

sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
121 changes: 121 additions & 0 deletions test/git-tasks.js
@@ -0,0 +1,121 @@
import test from 'ava';
import execaStub from 'execa_test_double';
import mockery from 'mockery';
import {tasks, SilentRenderer} from './fixtures/listr-renderer';

let testedModule;

const run = async listr => {
listr.setRenderer(SilentRenderer);
await listr.run();
};

test.before(() => {
mockery.registerMock('execa', execaStub.execa);
mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false
});
testedModule = require('../source/git-tasks');
});

test.beforeEach(() => {
execaStub.resetStub();
});

test.serial('should fail when current branch not master and publishing from any branch not permitted', async t => {
execaStub.createStub([
{
command: 'git symbolic-ref --short HEAD',
exitCode: 0,
stdout: 'feature'
}
]);
await t.throwsAsync(run(testedModule({})),
{message: 'Not on `master` branch. Use --any-branch to publish anyway.'});
t.true(tasks.some(task => task.title === 'Check current branch' && task.hasFailed()));
});

test.serial('should not fail when current branch not master and publishing from any branch permitted', async t => {
execaStub.createStub([
{
command: 'git symbolic-ref --short HEAD',
exitCode: 0,
stdout: 'feature'
},
{
command: 'git status --porcelain',
exitCode: 0,
stdout: ''
},
{
command: 'git rev-list --count --left-only @{u}...HEAD',
exitCode: 0,
stdout: ''
}
]);
await run(testedModule({anyBranch: true}));
t.false(tasks.some(task => task.title === 'Check current branch'));
});

test.serial('should fail when local working tree modified', async t => {
execaStub.createStub([
{
command: 'git symbolic-ref --short HEAD',
exitCode: 0,
stdout: 'master'
},
{
command: 'git status --porcelain',
exitCode: 0,
stdout: 'M source/git-tasks.js'
}
]);
await t.throwsAsync(run(testedModule({})), {message: 'Unclean working tree. Commit or stash changes first.'});
t.true(tasks.some(task => task.title === 'Check local working tree' && task.hasFailed()));
});

test.serial('should fail when remote history differs', async t => {
execaStub.createStub([
{
command: 'git symbolic-ref --short HEAD',
exitCode: 0,
stdout: 'master'
},
{
command: 'git status --porcelain',
exitCode: 0,
stdout: ''
},
{
command: 'git rev-list --count --left-only @{u}...HEAD',
exitCode: 0,
stdout: '1'
}
]);
await t.throwsAsync(run(testedModule({})), {message: 'Remote history differs. Please pull changes.'});
t.true(tasks.some(task => task.title === 'Check remote history' && task.hasFailed()));
});

test.serial('checks should pass when publishing from master, working tree is clean and remote history not different', async t => {
execaStub.createStub([
{
command: 'git symbolic-ref --short HEAD',
exitCode: 0,
stdout: 'master'
},
{
command: 'git status --porcelain',
exitCode: 0,
stdout: ''
},
{
command: 'git rev-list --count --left-only @{u}...HEAD',
exitCode: 0,
stdout: ''
}
]);
await run(testedModule({}));
t.pass();
});