-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub:workflow-configure-ci:test.js
140 lines (127 loc) · 3.54 KB
/
github:workflow-configure-ci:test.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
134
135
136
137
138
139
140
import update from '../lib/update.js';
import contains from '../lib/contains.js';
import parseYAML from '../lib/yaml/parse.js';
import find from '../lib/lines/find.js';
import replace from '../lib/lines/replace.js';
export const description = 'Configure workflow to automate tests of changes.';
const oldScript = 'travis';
const oldFilename = '.travis.yml';
const newScript = 'ci:test';
const newFilename = `.github/workflows/${newScript}.yml`;
const README = 'README.md';
const oldBadge = (repository, branch = 'main') =>
`[![Build](https://img.shields.io/travis/${repository}/${branch}.svg)](https://travis-ci.com/${repository}/branches)`;
const newBadge = (repository, branch = 'main') =>
`[![Tests](https://img.shields.io/github/workflow/status/${repository}/${newScript}?event=push&label=tests)](https://github.com/${repository}/actions/workflows/${newScript}.yml?query=branch:${branch})`;
export const commit = {
type: 'config',
scope: 'github',
subject: description,
};
const slug = ({url}) => url.match(/\/([^/]+\/[^/]+)$/)[1];
export async function postcondition({
readPkg,
exists,
lines,
readYAML,
assert,
}) {
const {scripts, repository} = await readPkg();
const repo = slug(repository);
assert(scripts[oldScript] === undefined);
assert(scripts[newScript] !== undefined);
assert(!(await exists(oldFilename)));
await contains({
assert,
read: () => readYAML(newFilename),
test: (contents) => assert.deepStrictEqual(contents, parseYAML(newConfig)),
});
const oldFound = await find([oldBadge(repo)], [README], {
lines,
method: find.exact,
});
assert(!oldFound);
const newFound = await find([newBadge(repo)], [README], {
lines,
method: find.exact,
});
assert(newFound);
}
export async function precondition({readPkg, exists, lines, assert}) {
const {scripts, repository} = await readPkg();
const repo = slug(repository);
assert(scripts[oldScript] !== undefined);
assert(scripts[newScript] === undefined);
assert(await exists(oldFilename));
assert(!(await exists(newFilename)));
const oldFound = await find([oldBadge(repo)], [README], {
lines,
method: find.exact,
});
assert(oldFound);
const newFound = await find([newBadge(repo)], [README], {
lines,
method: find.exact,
});
assert(!newFound);
}
export async function apply({
readPkg,
writePkg,
read,
lines,
write,
remove,
fixConfig,
}) {
const {repository} = await readPkg();
const repo = slug(repository);
await update({
read: readPkg,
write: writePkg,
edit: (pkgjson) => {
pkgjson.scripts[newScript] = pkgjson.scripts[oldScript];
delete pkgjson.scripts[oldScript];
return pkgjson;
},
});
await update({
create: true,
overwrite: false,
read: () => read(newFilename),
write: (data) => write(newFilename, data),
edit: () => newConfig.trim() + '\n',
});
await replace([[oldBadge(repo), newBadge(repo)]], [README], {
lines,
write,
method: replace.whole,
});
await remove([oldFilename]);
await fixConfig();
}
const newConfig = `
name: ${newScript}
on:
- push
- pull_request
jobs:
test:
name: Continuous integration (tests)
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v2
- name: Install 🔧
uses: bahmutov/npm-install@v1
with:
install-command: yarn --frozen-lockfile --ignore-scripts
useRollingCache: true
- name: Test 🔬
run: yarn ${newScript}
- name: Publish coverage report 📃
uses: codecov/codecov-action@v2
with:
fail_ci_if_error: true
`;
export const dependencies = ['ci:travis-setup'];