forked from sindresorhus/np
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-tasks.js
133 lines (123 loc) · 3.68 KB
/
git-tasks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import test from 'ava';
import execaStub from 'execa_test_double';
import mockery from 'mockery';
import {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 release branch is not specified, current branch is not main/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 `main`/`master` branch. Use --any-branch to publish anyway, or set a different release branch using --branch.'});
t.true(SilentRenderer.tasks.some(task => task.title === 'Check current branch' && task.hasFailed()));
});
test.serial('should fail when current branch is not the specified release branch 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({branch: 'release'})),
{message: 'Not on `release` branch. Use --any-branch to publish anyway, or set a different release branch using --branch.'});
t.true(SilentRenderer.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(SilentRenderer.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(SilentRenderer.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(SilentRenderer.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 t.notThrowsAsync(run(testedModule({})));
});