apify-test-tools 0.10.2-beta.3 → 0.10.2-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/cli/apify-test-tools.mjs +117 -118
- package/.github/scripts/run-with-apify-tokens.mjs +1 -1
- package/.github/workflows/public_pr-build-test.yaml +25 -25
- package/.github/workflows/public_push-build-latest.yaml +20 -4
- package/CHANGELOG.md +6 -0
- package/CONTRIBUTING.md +4 -1
- package/README.md +104 -56
- package/bin/affected-actors.ts +135 -103
- package/bin/build.ts +53 -38
- package/bin/diff-changes.ts +7 -6
- package/bin/diff-json-schema.ts +5 -6
- package/bin/git.ts +24 -69
- package/bin/main.ts +46 -80
- package/bin/slack.ts +15 -4
- package/bin/stdin-input.ts +65 -0
- package/bin/types.ts +6 -0
- package/dist/bin/affected-actors.d.ts +23 -27
- package/dist/bin/affected-actors.d.ts.map +1 -1
- package/dist/bin/affected-actors.js +111 -65
- package/dist/bin/affected-actors.js.map +1 -1
- package/dist/bin/build.d.ts +11 -5
- package/dist/bin/build.d.ts.map +1 -1
- package/dist/bin/build.js +43 -23
- package/dist/bin/build.js.map +1 -1
- package/dist/bin/diff-changes.d.ts +4 -3
- package/dist/bin/diff-changes.d.ts.map +1 -1
- package/dist/bin/diff-changes.js +4 -4
- package/dist/bin/diff-changes.js.map +1 -1
- package/dist/bin/diff-json-schema.d.ts +2 -2
- package/dist/bin/diff-json-schema.d.ts.map +1 -1
- package/dist/bin/diff-json-schema.js +1 -4
- package/dist/bin/diff-json-schema.js.map +1 -1
- package/dist/bin/git.d.ts +7 -15
- package/dist/bin/git.d.ts.map +1 -1
- package/dist/bin/git.js +23 -61
- package/dist/bin/git.js.map +1 -1
- package/dist/bin/main.js +39 -71
- package/dist/bin/main.js.map +1 -1
- package/dist/bin/slack.d.ts +6 -3
- package/dist/bin/slack.d.ts.map +1 -1
- package/dist/bin/slack.js +13 -1
- package/dist/bin/slack.js.map +1 -1
- package/dist/bin/stdin-input.d.ts +17 -0
- package/dist/bin/stdin-input.d.ts.map +1 -0
- package/dist/bin/stdin-input.js +54 -0
- package/dist/bin/stdin-input.js.map +1 -0
- package/dist/bin/types.d.ts +5 -0
- package/dist/bin/types.d.ts.map +1 -1
- package/dist/test/bundle/cli-bundle.test.js +32 -11
- package/dist/test/bundle/cli-bundle.test.js.map +1 -1
- package/dist/test/e2e/cli-bundle.e2e.test.js +23 -57
- package/dist/test/e2e/cli-bundle.e2e.test.js.map +1 -1
- package/dist/test/e2e/git-build-release.e2e.test.d.ts +2 -0
- package/dist/test/e2e/git-build-release.e2e.test.d.ts.map +1 -0
- package/dist/test/e2e/git-build-release.e2e.test.js +87 -0
- package/dist/test/e2e/git-build-release.e2e.test.js.map +1 -0
- package/dist/test/e2e/helpers.d.ts +37 -0
- package/dist/test/e2e/helpers.d.ts.map +1 -0
- package/dist/test/e2e/helpers.js +57 -0
- package/dist/test/e2e/helpers.js.map +1 -0
- package/dist/test/unit/bin/affected-actors-cli.test.d.ts +2 -0
- package/dist/test/unit/bin/affected-actors-cli.test.d.ts.map +1 -0
- package/dist/test/unit/bin/affected-actors-cli.test.js +161 -0
- package/dist/test/unit/bin/affected-actors-cli.test.js.map +1 -0
- package/dist/test/unit/bin/affected-actors.test.js +68 -87
- package/dist/test/unit/bin/affected-actors.test.js.map +1 -1
- package/dist/test/unit/bin/build-cli.test.d.ts +2 -0
- package/dist/test/unit/bin/build-cli.test.d.ts.map +1 -0
- package/dist/test/unit/bin/build-cli.test.js +44 -0
- package/dist/test/unit/bin/build-cli.test.js.map +1 -0
- package/dist/test/unit/bin/build.test.js +61 -4
- package/dist/test/unit/bin/build.test.js.map +1 -1
- package/dist/test/unit/bin/compiled-cli.d.ts +6 -0
- package/dist/test/unit/bin/compiled-cli.d.ts.map +1 -0
- package/dist/test/unit/bin/compiled-cli.js +16 -0
- package/dist/test/unit/bin/compiled-cli.js.map +1 -0
- package/dist/test/unit/bin/diff-changes.test.js +55 -55
- package/dist/test/unit/bin/diff-changes.test.js.map +1 -1
- package/dist/test/unit/bin/diff.test.js +25 -28
- package/dist/test/unit/bin/diff.test.js.map +1 -1
- package/dist/test/unit/bin/git.test.js +28 -108
- package/dist/test/unit/bin/git.test.js.map +1 -1
- package/dist/test/unit/bin/slack.test.js +19 -2
- package/dist/test/unit/bin/slack.test.js.map +1 -1
- package/dist/test/unit/bin/stdin-input.test.d.ts +2 -0
- package/dist/test/unit/bin/stdin-input.test.d.ts.map +1 -0
- package/dist/test/unit/bin/stdin-input.test.js +50 -0
- package/dist/test/unit/bin/stdin-input.test.js.map +1 -0
- package/dist/test/unit/should-built-and-test.test.js +18 -21
- package/dist/test/unit/should-built-and-test.test.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/test/bundle/cli-bundle.test.ts +47 -11
- package/test/e2e/cli-bundle.e2e.test.ts +47 -77
- package/test/e2e/git-build-release.e2e.test.ts +113 -0
- package/test/e2e/helpers.ts +82 -0
- package/test/unit/bin/affected-actors-cli.test.ts +193 -0
- package/test/unit/bin/affected-actors.test.ts +86 -94
- package/test/unit/bin/build-cli.test.ts +55 -0
- package/test/unit/bin/build.test.ts +71 -3
- package/test/unit/bin/compiled-cli.ts +17 -0
- package/test/unit/bin/diff-changes.test.ts +55 -55
- package/test/unit/bin/diff.test.ts +25 -28
- package/test/unit/bin/git.test.ts +35 -132
- package/test/unit/bin/slack.test.ts +21 -2
- package/test/unit/bin/stdin-input.test.ts +61 -0
- package/test/unit/should-built-and-test.test.ts +19 -22
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { execFileSync, spawnSync } from 'node:child_process';
|
|
2
|
+
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import process from 'node:process';
|
|
6
|
+
|
|
7
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
|
8
|
+
|
|
9
|
+
import { compileCli } from './compiled-cli.js';
|
|
10
|
+
|
|
11
|
+
// `get-affected-actors --base` on real git histories. Actors a and c share `src/`, b is standalone.
|
|
12
|
+
// `main` is the base branch and `feature` the PR branch, checked out like in the PR workflow.
|
|
13
|
+
const CONFIG = {
|
|
14
|
+
actors: [
|
|
15
|
+
{
|
|
16
|
+
folder: 'actors/a',
|
|
17
|
+
actorFullName: 'owner/actor-a',
|
|
18
|
+
tokenEnvVar: 'T',
|
|
19
|
+
overrideActorContext: ['actors/a', 'src'],
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
folder: 'actors/c',
|
|
23
|
+
actorFullName: 'owner/actor-c',
|
|
24
|
+
tokenEnvVar: 'T',
|
|
25
|
+
overrideActorContext: ['actors/c', 'src'],
|
|
26
|
+
},
|
|
27
|
+
{ folder: 'actors/b', actorFullName: 'owner/actor-b', tokenEnvVar: 'T' },
|
|
28
|
+
],
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Only what node and git need, so nothing from the developer's git config leaks in
|
|
32
|
+
const env = { PATH: process.env.PATH ?? '', HOME: os.tmpdir() };
|
|
33
|
+
const gitEnv = {
|
|
34
|
+
...env,
|
|
35
|
+
GIT_AUTHOR_NAME: 'Tester',
|
|
36
|
+
GIT_AUTHOR_EMAIL: 'tester@example.com',
|
|
37
|
+
GIT_COMMITTER_NAME: 'Tester',
|
|
38
|
+
GIT_COMMITTER_EMAIL: 'tester@example.com',
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
let cliPath: string;
|
|
42
|
+
let cleanup: (() => Promise<void>) | undefined;
|
|
43
|
+
let repoDir: string;
|
|
44
|
+
|
|
45
|
+
const git = (...args: string[]) => execFileSync('git', args, { cwd: repoDir, env: gitEnv, encoding: 'utf8' }).trim();
|
|
46
|
+
|
|
47
|
+
const writeFiles = async (content: string, files: string[]) => {
|
|
48
|
+
for (const file of files) {
|
|
49
|
+
await mkdir(path.dirname(path.join(repoDir, file)), { recursive: true });
|
|
50
|
+
await writeFile(path.join(repoDir, file), `${content}\n`);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** Commits a change to each file and returns the commit. */
|
|
55
|
+
const commit = async (message: string, ...files: string[]) => {
|
|
56
|
+
await writeFiles(message, files);
|
|
57
|
+
git('add', '-A');
|
|
58
|
+
git('commit', '-q', '-m', message);
|
|
59
|
+
return git('rev-parse', 'HEAD');
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** Commits on `main`, then checks `feature` out again. */
|
|
63
|
+
const commitOnMain = async (message: string, ...files: string[]) => {
|
|
64
|
+
git('checkout', '-q', 'main');
|
|
65
|
+
await commit(message, ...files);
|
|
66
|
+
git('checkout', '-q', 'feature');
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const runGetAffectedActors = (...args: string[]) => {
|
|
70
|
+
const result = spawnSync(cliPath, ['get-affected-actors', ...args], { cwd: repoDir, env, encoding: 'utf8' });
|
|
71
|
+
expect(result.status, result.stderr).toBe(0);
|
|
72
|
+
return JSON.parse(result.stdout);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const getAffectedActors = (...args: string[]) => {
|
|
76
|
+
const document = runGetAffectedActors(...args);
|
|
77
|
+
const names = (actors: { actorFullName: string }[]) =>
|
|
78
|
+
actors.map(({ actorFullName }) => actorFullName.replace('owner/actor-', '')).sort();
|
|
79
|
+
return {
|
|
80
|
+
functional: names(document.functionallyChangedActors),
|
|
81
|
+
cosmetic: names(document.cosmeticallyChangedActors),
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
describe('get-affected-actors CLI on real git histories', () => {
|
|
86
|
+
beforeAll(async () => {
|
|
87
|
+
({ cliPath, cleanup } = await compileCli());
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
afterAll(async () => {
|
|
91
|
+
await cleanup?.();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
beforeEach(async () => {
|
|
95
|
+
repoDir = await mkdtemp(path.join(os.tmpdir(), 'apify-affected-actors-'));
|
|
96
|
+
git('init', '-q', '-b', 'main');
|
|
97
|
+
await writeFile(path.join(repoDir, 'apify-test-tools.config.json'), JSON.stringify(CONFIG));
|
|
98
|
+
for (const { folder } of CONFIG.actors) {
|
|
99
|
+
await mkdir(path.join(repoDir, folder, '.actor'), { recursive: true });
|
|
100
|
+
await writeFile(
|
|
101
|
+
path.join(repoDir, folder, '.actor/actor.json'),
|
|
102
|
+
JSON.stringify({ actorSpecification: 1, name: `actor-${path.basename(folder)}`, version: '0.1' }),
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
await commit('chore: init', 'src/shared.ts', 'actors/a/main.ts', 'actors/b/main.ts', 'actors/c/main.ts');
|
|
106
|
+
git('checkout', '-q', '-b', 'feature');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
afterEach(async () => {
|
|
110
|
+
if (repoDir) await rm(repoDir, { recursive: true, force: true });
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("tests only what changed since the last validated commit, on the branch's own commits", async () => {
|
|
114
|
+
const validated = await commit('feat(a): validated change', 'actors/a/main.ts');
|
|
115
|
+
await commit('feat(c): new change, docs for a', 'actors/c/main.ts', 'actors/a/README.md');
|
|
116
|
+
|
|
117
|
+
// A is still the validated code, only its README changed since
|
|
118
|
+
expect(getAffectedActors('--base', 'main', '--since', validated)).toStrictEqual({
|
|
119
|
+
functional: ['c'],
|
|
120
|
+
cosmetic: ['a'],
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('tests nothing after merging cosmetic changes from base', async () => {
|
|
125
|
+
const validated = await commit('feat(a): validated change', 'actors/a/main.ts');
|
|
126
|
+
await commitOnMain('docs: readme and changelog', 'actors/a/README.md', 'CHANGELOG.md');
|
|
127
|
+
git('merge', '-q', '--no-edit', 'main');
|
|
128
|
+
|
|
129
|
+
expect(getAffectedActors('--base', 'main', '--since', validated)).toStrictEqual({
|
|
130
|
+
functional: [],
|
|
131
|
+
cosmetic: ['a'],
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("tests the Actors where base's functional change meets the branch's after a merge", async () => {
|
|
136
|
+
const validated = await commit('feat(a): validated change', 'actors/a/main.ts');
|
|
137
|
+
await commitOnMain('feat: shared change', 'src/shared.ts');
|
|
138
|
+
git('merge', '-q', '--no-edit', 'main');
|
|
139
|
+
|
|
140
|
+
// Base changed A and C. A can interact with the branch's change, C only has base's, which base validated
|
|
141
|
+
expect(getAffectedActors('--base', 'main', '--since', validated)).toStrictEqual({
|
|
142
|
+
functional: ['a'],
|
|
143
|
+
cosmetic: [],
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('leaves out Actors that only base changed', async () => {
|
|
148
|
+
await commit('feat: shared change', 'src/shared.ts');
|
|
149
|
+
await commitOnMain('feat(b): base change', 'actors/b/main.ts');
|
|
150
|
+
git('merge', '-q', '--no-edit', 'main');
|
|
151
|
+
|
|
152
|
+
expect(getAffectedActors('--base', 'main')).toStrictEqual({ functional: ['a', 'c'], cosmetic: [] });
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('counts code changed in the merge commit itself', async () => {
|
|
156
|
+
await commit('docs(a): readme', 'actors/a/README.md');
|
|
157
|
+
await commitOnMain('feat(b): base change', 'actors/b/main.ts');
|
|
158
|
+
git('merge', '-q', '--no-commit', '--no-ff', 'main');
|
|
159
|
+
await writeFiles('changed while merging', ['actors/c/main.ts']);
|
|
160
|
+
git('add', '-A');
|
|
161
|
+
git('commit', '-q', '--no-edit');
|
|
162
|
+
|
|
163
|
+
expect(getAffectedActors('--base', 'main')).toStrictEqual({ functional: ['c'], cosmetic: ['a'] });
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('tests nothing after a rebase onto base changes to other Actors', async () => {
|
|
167
|
+
const validated = await commit('feat(a): validated change', 'actors/a/main.ts');
|
|
168
|
+
await commitOnMain('feat(b): base change', 'actors/b/main.ts');
|
|
169
|
+
git('rebase', '-q', 'main');
|
|
170
|
+
|
|
171
|
+
expect(getAffectedActors('--base', 'main', '--since', validated)).toStrictEqual({
|
|
172
|
+
functional: [],
|
|
173
|
+
cosmetic: [],
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('checks the whole branch on a rerun or when --since is not in the history', async () => {
|
|
178
|
+
await commit('feat(a): validated change', 'actors/a/main.ts');
|
|
179
|
+
const head = await commit('docs(a): readme', 'actors/a/README.md');
|
|
180
|
+
|
|
181
|
+
const wholeBranch = { functional: ['a'], cosmetic: [] };
|
|
182
|
+
expect(getAffectedActors('--base', 'main', '--since', head)).toStrictEqual(wholeBranch);
|
|
183
|
+
expect(getAffectedActors('--base', 'main', '--since', 'e'.repeat(40))).toStrictEqual(wholeBranch);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('reports nothing for an empty range', async () => {
|
|
187
|
+
const nothing = { commits: [], changedFiles: [], functionallyChangedActors: [], cosmeticallyChangedActors: [] };
|
|
188
|
+
// The branch has no commits of its own yet
|
|
189
|
+
expect(runGetAffectedActors('--base', 'main')).toMatchObject(nothing);
|
|
190
|
+
// A release with nothing new since the last one
|
|
191
|
+
expect(runGetAffectedActors('--since', git('rev-parse', 'HEAD'))).toMatchObject(nothing);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { MockInstance } from 'vitest';
|
|
2
2
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
3
|
|
|
4
|
-
import { getAffectedActors
|
|
4
|
+
import { getAffectedActors } from '../../../bin/affected-actors.js';
|
|
5
5
|
import * as DiffChanges from '../../../bin/diff-changes.js';
|
|
6
6
|
import * as Git from '../../../bin/git.js';
|
|
7
7
|
import type { ActorConfig, Commit } from '../../../bin/types.js';
|
|
@@ -28,99 +28,125 @@ describe('getAffectedActors', () => {
|
|
|
28
28
|
.spyOn(DiffChanges, 'getChangedActors')
|
|
29
29
|
.mockResolvedValue({ functional: [actorA], cosmetic: [actorB] });
|
|
30
30
|
vi.spyOn(Git, 'resolveCommitSha').mockResolvedValue(headSha);
|
|
31
|
-
vi.spyOn(Git, 'resolveBranchName').mockResolvedValue('feature');
|
|
32
31
|
vi.spyOn(Git, 'getChangedFilesBetween').mockResolvedValue(changedFiles);
|
|
33
32
|
vi.spyOn(Git, 'getChangelogAdditions').mockResolvedValue('- Added foo');
|
|
34
33
|
});
|
|
35
34
|
|
|
36
35
|
it('requires --base or --since', async () => {
|
|
37
|
-
const options = {
|
|
36
|
+
const options = { ...selection };
|
|
38
37
|
await expect(getAffectedActors(options)).rejects.toThrow('Pass --base, --since, or both');
|
|
39
38
|
// An empty --since (e.g. from an empty cache file) counts as not passed
|
|
40
39
|
await expect(getAffectedActors({ ...options, since: '' })).rejects.toThrow('Pass --base, --since, or both');
|
|
41
40
|
});
|
|
42
41
|
|
|
43
42
|
describe('with --base', () => {
|
|
44
|
-
|
|
43
|
+
const mergeBaseSha = 'b'.repeat(40);
|
|
44
|
+
let commitsBetweenSpy: MockInstance;
|
|
45
45
|
|
|
46
46
|
beforeEach(() => {
|
|
47
|
-
vi.spyOn(Git, '
|
|
48
|
-
|
|
47
|
+
vi.spyOn(Git, 'getMergeBase').mockResolvedValue(mergeBaseSha);
|
|
48
|
+
commitsBetweenSpy = vi.spyOn(Git, 'getCommitsBetween').mockResolvedValue(commits);
|
|
49
49
|
});
|
|
50
50
|
|
|
51
|
-
it(
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
since: sinceSha,
|
|
56
|
-
...selection,
|
|
57
|
-
});
|
|
51
|
+
it("inspects the branch's own changes, from where it forked from or last merged --base", async () => {
|
|
52
|
+
const findCommitSpy = vi.spyOn(Git, 'findCommitSha');
|
|
53
|
+
|
|
54
|
+
const result = await getAffectedActors({ base: 'origin/master', ...selection });
|
|
58
55
|
|
|
59
56
|
expect(result).toStrictEqual({
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
version: 1,
|
|
58
|
+
head: headSha,
|
|
62
59
|
commits,
|
|
63
60
|
changedFiles,
|
|
64
61
|
changelog: '- Added foo',
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
expect(getCommitsSpy).toHaveBeenCalledWith({
|
|
69
|
-
sourceBranch: 'origin/feature',
|
|
70
|
-
targetBranch: 'origin/master',
|
|
71
|
-
baseCommit: sinceSha,
|
|
62
|
+
// Only the names, the config file stays the source of truth for the rest
|
|
63
|
+
functionallyChangedActors: [{ actorFullName: 'owner/a' }],
|
|
64
|
+
cosmeticallyChangedActors: [{ actorFullName: 'owner/b' }],
|
|
72
65
|
});
|
|
73
|
-
|
|
74
|
-
expect(Git.getChangedFilesBetween).toHaveBeenCalledWith(
|
|
75
|
-
expect(Git.getChangelogAdditions).toHaveBeenCalledWith(
|
|
66
|
+
expect(Git.getMergeBase).toHaveBeenCalledWith('origin/master', 'HEAD');
|
|
67
|
+
expect(Git.getChangedFilesBetween).toHaveBeenCalledWith(mergeBaseSha, headSha);
|
|
68
|
+
expect(Git.getChangelogAdditions).toHaveBeenCalledWith(mergeBaseSha, headSha, changedFiles);
|
|
69
|
+
expect(commitsBetweenSpy).toHaveBeenCalledWith(mergeBaseSha, headSha);
|
|
76
70
|
expect(changedActorsSpy).toHaveBeenCalledWith({
|
|
77
71
|
filepathsChanged: changedFiles,
|
|
78
72
|
actorConfigs: [actorA, actorB],
|
|
79
|
-
|
|
73
|
+
range: { fromRef: mergeBaseSha, toRef: headSha },
|
|
80
74
|
});
|
|
75
|
+
expect(findCommitSpy).not.toHaveBeenCalled();
|
|
81
76
|
});
|
|
82
77
|
|
|
83
|
-
describe('
|
|
78
|
+
describe('and --since', () => {
|
|
79
|
+
const sinceFiles = ['README.md'];
|
|
80
|
+
|
|
84
81
|
beforeEach(() => {
|
|
85
|
-
vi.spyOn(Git, '
|
|
86
|
-
vi.spyOn(Git, '
|
|
82
|
+
vi.spyOn(Git, 'findCommitSha').mockResolvedValue(sinceSha);
|
|
83
|
+
vi.spyOn(Git, 'getChangedFilesBetween').mockImplementation(async (fromRef) =>
|
|
84
|
+
fromRef === sinceSha ? sinceFiles : changedFiles,
|
|
85
|
+
);
|
|
87
86
|
});
|
|
88
87
|
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
88
|
+
const getNarrowed = async (
|
|
89
|
+
branch: DiffChanges.ChangedActors,
|
|
90
|
+
sinceValidated: DiffChanges.ChangedActors,
|
|
91
|
+
) => {
|
|
92
|
+
changedActorsSpy.mockImplementation(async ({ range }) =>
|
|
93
|
+
range.fromRef === sinceSha ? sinceValidated : branch,
|
|
92
94
|
);
|
|
93
|
-
|
|
95
|
+
const result = await getAffectedActors({ base: 'origin/master', since: sinceSha, ...selection });
|
|
96
|
+
return {
|
|
97
|
+
functional: result.functionallyChangedActors.map(({ actorFullName }) => actorFullName),
|
|
98
|
+
cosmetic: result.cosmeticallyChangedActors.map(({ actorFullName }) => actorFullName),
|
|
99
|
+
};
|
|
94
100
|
};
|
|
95
101
|
|
|
96
|
-
it(
|
|
97
|
-
await
|
|
102
|
+
it('classifies the changes since --since separately', async () => {
|
|
103
|
+
await getNarrowed({ functional: [actorA], cosmetic: [] }, { functional: [actorA], cosmetic: [] });
|
|
98
104
|
expect(changedActorsSpy).toHaveBeenCalledWith({
|
|
99
|
-
filepathsChanged:
|
|
105
|
+
filepathsChanged: sinceFiles,
|
|
100
106
|
actorConfigs: [actorA, actorB],
|
|
101
|
-
|
|
107
|
+
range: { fromRef: sinceSha, toRef: headSha },
|
|
102
108
|
});
|
|
103
|
-
expect(getCommitsSpy).toHaveBeenCalledWith({ sourceBranch: 'feature', targetBranch: 'master' });
|
|
104
109
|
});
|
|
105
110
|
|
|
106
|
-
it('
|
|
107
|
-
const result = await
|
|
108
|
-
expect(result.
|
|
109
|
-
expect(result.
|
|
111
|
+
it('still describes the whole branch', async () => {
|
|
112
|
+
const result = await getAffectedActors({ base: 'origin/master', since: sinceSha, ...selection });
|
|
113
|
+
expect(result.commits).toStrictEqual(commits);
|
|
114
|
+
expect(result.changedFiles).toStrictEqual(changedFiles);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('retests an Actor whose validated change meets a new functional change', async () => {
|
|
118
|
+
await expect(
|
|
119
|
+
getNarrowed({ functional: [actorA], cosmetic: [] }, { functional: [actorA], cosmetic: [] }),
|
|
120
|
+
).resolves.toStrictEqual({ functional: ['owner/a'], cosmetic: [] });
|
|
110
121
|
});
|
|
111
122
|
|
|
112
|
-
it('
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
123
|
+
it('leaves out Actors that only the branch changed before --since or only --base changed', async () => {
|
|
124
|
+
await expect(
|
|
125
|
+
getNarrowed({ functional: [actorA], cosmetic: [] }, { functional: [actorB], cosmetic: [] }),
|
|
126
|
+
).resolves.toStrictEqual({ functional: [], cosmetic: [] });
|
|
116
127
|
});
|
|
117
128
|
|
|
118
|
-
it('
|
|
119
|
-
|
|
120
|
-
expect(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
129
|
+
it('takes the less impactful change when the two disagree', async () => {
|
|
130
|
+
// A validated functional change, then only cosmetic changes (e.g. a README merged in from base)
|
|
131
|
+
await expect(
|
|
132
|
+
getNarrowed({ functional: [actorA], cosmetic: [] }, { functional: [], cosmetic: [actorA] }),
|
|
133
|
+
).resolves.toStrictEqual({ functional: [], cosmetic: ['owner/a'] });
|
|
134
|
+
// A functional change merged in from base, while the branch only changes the README
|
|
135
|
+
await expect(
|
|
136
|
+
getNarrowed({ functional: [], cosmetic: [actorA] }, { functional: [actorA], cosmetic: [] }),
|
|
137
|
+
).resolves.toStrictEqual({ functional: [], cosmetic: ['owner/a'] });
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it.each([
|
|
141
|
+
['is HEAD (a rerun)', headSha],
|
|
142
|
+
['is not in the local history', undefined],
|
|
143
|
+
])('checks the whole branch when --since %s', async (_, found) => {
|
|
144
|
+
vi.spyOn(Git, 'findCommitSha').mockResolvedValue(found);
|
|
145
|
+
const result = await getAffectedActors({ base: 'origin/master', since: sinceSha, ...selection });
|
|
146
|
+
expect(result.functionallyChangedActors).toStrictEqual([{ actorFullName: 'owner/a' }]);
|
|
147
|
+
// With no usable validated commit, the "since" diff falls back to the same range as the
|
|
148
|
+
// base diff, so getChangedActors runs twice for identical ranges instead of once.
|
|
149
|
+
expect(changedActorsSpy).toHaveBeenCalledTimes(2);
|
|
124
150
|
});
|
|
125
151
|
});
|
|
126
152
|
});
|
|
@@ -134,57 +160,23 @@ describe('getAffectedActors', () => {
|
|
|
134
160
|
commitsBetweenSpy = vi.spyOn(Git, 'getCommitsBetween').mockResolvedValue(commits);
|
|
135
161
|
});
|
|
136
162
|
|
|
137
|
-
it('inspects everything after --since up to
|
|
138
|
-
const result = await getAffectedActors({
|
|
163
|
+
it('inspects everything after --since up to HEAD', async () => {
|
|
164
|
+
const result = await getAffectedActors({ since: 'HEAD~2', ...selection });
|
|
139
165
|
|
|
140
166
|
expect(result).toStrictEqual({
|
|
141
|
-
|
|
142
|
-
|
|
167
|
+
version: 1,
|
|
168
|
+
head: headSha,
|
|
143
169
|
commits,
|
|
144
170
|
changedFiles,
|
|
145
171
|
changelog: '- Added foo',
|
|
146
|
-
|
|
147
|
-
|
|
172
|
+
// Only the names, the config file stays the source of truth for the rest
|
|
173
|
+
functionallyChangedActors: [{ actorFullName: 'owner/a' }],
|
|
174
|
+
cosmeticallyChangedActors: [{ actorFullName: 'owner/b' }],
|
|
148
175
|
});
|
|
149
|
-
expect(resolveBaseSpy).toHaveBeenCalledWith('HEAD~2'
|
|
176
|
+
expect(resolveBaseSpy).toHaveBeenCalledWith('HEAD~2');
|
|
150
177
|
expect(commitsBetweenSpy).toHaveBeenCalledWith(sinceSha, headSha);
|
|
151
178
|
expect(Git.getChangedFilesBetween).toHaveBeenCalledWith(sinceSha, headSha);
|
|
152
179
|
expect(Git.getChangelogAdditions).toHaveBeenCalledWith(sinceSha, headSha, changedFiles);
|
|
153
180
|
});
|
|
154
|
-
|
|
155
|
-
it('has no commits when head is the --since commit', async () => {
|
|
156
|
-
resolveBaseSpy.mockResolvedValue(headSha);
|
|
157
|
-
|
|
158
|
-
const result = await getAffectedActors({ head: 'HEAD', since: headSha, ...selection });
|
|
159
|
-
|
|
160
|
-
expect(result.commits).toStrictEqual([]);
|
|
161
|
-
// Empty `git log x..x` output doesn't parse as a commit, so it's never run
|
|
162
|
-
expect(commitsBetweenSpy).not.toHaveBeenCalled();
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
describe('toAffectedActorsDocument', () => {
|
|
168
|
-
it('keeps only the Actor names, the config file stays the source of truth for the rest', () => {
|
|
169
|
-
expect(
|
|
170
|
-
toAffectedActorsDocument({
|
|
171
|
-
headSha,
|
|
172
|
-
ref: null,
|
|
173
|
-
commits,
|
|
174
|
-
changedFiles: ['src/main.ts'],
|
|
175
|
-
changelog: null,
|
|
176
|
-
functionallyChangedActors: [actorA],
|
|
177
|
-
cosmeticallyChangedActors: [actorB],
|
|
178
|
-
}),
|
|
179
|
-
).toStrictEqual({
|
|
180
|
-
version: 1,
|
|
181
|
-
ref: null,
|
|
182
|
-
head: headSha,
|
|
183
|
-
commits,
|
|
184
|
-
changedFiles: ['src/main.ts'],
|
|
185
|
-
changelog: null,
|
|
186
|
-
functionallyChangedActors: [{ actorFullName: 'owner/a' }],
|
|
187
|
-
cosmeticallyChangedActors: [{ actorFullName: 'owner/b' }],
|
|
188
|
-
});
|
|
189
181
|
});
|
|
190
182
|
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import process from 'node:process';
|
|
3
|
+
|
|
4
|
+
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
5
|
+
|
|
6
|
+
import { compileCli } from './compiled-cli.js';
|
|
7
|
+
|
|
8
|
+
let cliPath: string;
|
|
9
|
+
let cleanup: (() => Promise<void>) | undefined;
|
|
10
|
+
|
|
11
|
+
const run = (command: string[], input: string, env: NodeJS.ProcessEnv = process.env) =>
|
|
12
|
+
spawnSync(cliPath, command, { encoding: 'utf8', input, stdio: ['pipe', 'pipe', 'pipe'], env });
|
|
13
|
+
const runBuild = (input: string, args: string[] = ['--version-number', '0.99']) => run(['build', ...args], input);
|
|
14
|
+
|
|
15
|
+
describe('build and notify-release CLI', () => {
|
|
16
|
+
beforeAll(async () => {
|
|
17
|
+
({ cliPath, cleanup } = await compileCli());
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterAll(async () => {
|
|
21
|
+
await cleanup?.();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('builds nothing for an empty list and prints an empty build list', () => {
|
|
25
|
+
const result = runBuild('{"actors":[]}');
|
|
26
|
+
expect(result.status, result.stderr).toBe(0);
|
|
27
|
+
expect(JSON.parse(result.stdout)).toStrictEqual([]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('fails on empty stdin instead of building nothing', () => {
|
|
31
|
+
const result = runBuild('');
|
|
32
|
+
expect(result.status).toBe(1);
|
|
33
|
+
expect(result.stderr).toContain('Expected the Actors to build as JSON on stdin, got nothing');
|
|
34
|
+
expect(result.stdout).toBe('');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('rejects a malformed --version-number', () => {
|
|
38
|
+
const result = runBuild('{"actors":[]}', ['--version-number', 'latest']);
|
|
39
|
+
expect(result.status).toBe(1);
|
|
40
|
+
expect(result.stderr).toContain('--version-number must look like 0.1 or be "default"');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('requires --version-number', () => {
|
|
44
|
+
const result = runBuild('{"actors":[]}', []);
|
|
45
|
+
expect(result.status).toBe(1);
|
|
46
|
+
expect(result.stderr).toContain('Missing required argument: version-number');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('notify-release announces nothing when there are no new commits', () => {
|
|
50
|
+
const document = { commits: [], changedFiles: [], changelog: null, functionallyChangedActors: [] };
|
|
51
|
+
const result = run(['notify-release'], JSON.stringify(document));
|
|
52
|
+
expect(result.status, result.stderr).toBe(0);
|
|
53
|
+
expect(result.stderr).toContain('No new commits, so there is no release to announce');
|
|
54
|
+
});
|
|
55
|
+
});
|
|
@@ -7,11 +7,15 @@ import { ActorSourceType, ApifyClient } from 'apify-client';
|
|
|
7
7
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
8
8
|
|
|
9
9
|
import {
|
|
10
|
+
ApifyBuilder,
|
|
10
11
|
assertRepoUrlMatchesDefaultVersion,
|
|
12
|
+
buildActors,
|
|
11
13
|
deleteOldBuilds,
|
|
12
14
|
resolveDefaultVersion,
|
|
13
15
|
runBuilds,
|
|
14
16
|
} from '../../../bin/build.js';
|
|
17
|
+
import * as Git from '../../../bin/git.js';
|
|
18
|
+
import { logger } from '../../../bin/logger.js';
|
|
15
19
|
import type { ActorConfig } from '../../../bin/types.js';
|
|
16
20
|
import { ExistingDir, ExistingFile, RelativeDir } from '../../../bin/utils/path/repo-relative.js';
|
|
17
21
|
|
|
@@ -84,6 +88,71 @@ describe('assertRepoUrlMatchesDefaultVersion', () => {
|
|
|
84
88
|
});
|
|
85
89
|
});
|
|
86
90
|
|
|
91
|
+
describe('runBuilds version number', () => {
|
|
92
|
+
// Only the name and folder of the Actor matter here, the builder is stubbed
|
|
93
|
+
const actorConfig = { actorFullName: ACTOR, folder: { isRoot: () => true } } as unknown as ActorConfig;
|
|
94
|
+
// Restored after each test, so the stub never reaches the tests that run the real builder
|
|
95
|
+
afterEach(() => {
|
|
96
|
+
vi.mocked(ApifyBuilder.fromActorConfig).mockRestore();
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const dryRunBuild = async (versionNumber: string, actorInfo = makeActorInfo()) => {
|
|
100
|
+
vi.spyOn(ApifyBuilder, 'fromActorConfig').mockReturnValue({
|
|
101
|
+
getActorInfo: async () => actorInfo,
|
|
102
|
+
} as unknown as ApifyBuilder);
|
|
103
|
+
const result = await runBuilds({
|
|
104
|
+
actorConfigs: [actorConfig],
|
|
105
|
+
versionNumber,
|
|
106
|
+
repoUrl: REPO_URL,
|
|
107
|
+
shouldVerifyRepoUrl: false,
|
|
108
|
+
branch: 'master',
|
|
109
|
+
dryRun: true,
|
|
110
|
+
});
|
|
111
|
+
if (Array.isArray(result)) throw new Error('A dry run must return a plan, not builds');
|
|
112
|
+
return result.builds[0].versionNumber;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
it('builds the version it is given', async () => {
|
|
116
|
+
await expect(dryRunBuild('0.99')).resolves.toBe('0.99');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("resolves default to the version the Actor's default build tag points to", async () => {
|
|
120
|
+
await expect(dryRunBuild('default')).resolves.toBe('0.1');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('warns when the default version does not give its builds the default build tag', async () => {
|
|
124
|
+
const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => undefined);
|
|
125
|
+
await dryRunBuild('default');
|
|
126
|
+
// The fixture's version 0.1 has no build tag of its own
|
|
127
|
+
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('users keep running the previous build'));
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('does not warn when the default version gives its builds the default build tag', async () => {
|
|
131
|
+
const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => undefined);
|
|
132
|
+
const versions = [{ versionNumber: '0.1', buildTag: 'latest', sourceType: ActorSourceType.GitRepo }];
|
|
133
|
+
await dryRunBuild('default', makeActorInfo({ versions } as Partial<ApifyClientTypes.Actor>));
|
|
134
|
+
expect(warnSpy).not.toHaveBeenCalled();
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe('buildActors', () => {
|
|
139
|
+
it('builds nothing and needs no branch or repository when there are no Actors', async () => {
|
|
140
|
+
const branchSpy = vi.spyOn(Git, 'getCurrentBranch');
|
|
141
|
+
const repoSpy = vi.spyOn(Git, 'resolveRepoUrl');
|
|
142
|
+
|
|
143
|
+
await expect(buildActors({ actorConfigs: [], versionNumber: '0.99', dryRun: false })).resolves.toStrictEqual(
|
|
144
|
+
[],
|
|
145
|
+
);
|
|
146
|
+
// A dry run keeps the dry-run shape even with nothing to build
|
|
147
|
+
await expect(buildActors({ actorConfigs: [], versionNumber: '0.99', dryRun: true })).resolves.toStrictEqual({
|
|
148
|
+
dryRun: true,
|
|
149
|
+
builds: [],
|
|
150
|
+
});
|
|
151
|
+
expect(branchSpy).not.toHaveBeenCalled();
|
|
152
|
+
expect(repoSpy).not.toHaveBeenCalled();
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
87
156
|
describe('dry run', () => {
|
|
88
157
|
const TOKEN_ENV_VAR = 'APIFY_TOKEN_OWNER';
|
|
89
158
|
const originalCwd = process.cwd();
|
|
@@ -150,7 +219,7 @@ describe('dry run', () => {
|
|
|
150
219
|
shouldVerifyRepoUrl: true,
|
|
151
220
|
branch: 'feature',
|
|
152
221
|
dryRun: true,
|
|
153
|
-
|
|
222
|
+
versionNumber: '0.99',
|
|
154
223
|
});
|
|
155
224
|
|
|
156
225
|
it('should return a build plan without creating versions or builds', async () => {
|
|
@@ -161,7 +230,6 @@ describe('dry run', () => {
|
|
|
161
230
|
actorFullName: ACTOR,
|
|
162
231
|
versionNumber: '0.99',
|
|
163
232
|
gitRepoUrl: `${REPO_URL}#feature:actors/a`,
|
|
164
|
-
useDockerCache: false,
|
|
165
233
|
},
|
|
166
234
|
],
|
|
167
235
|
});
|
|
@@ -182,7 +250,7 @@ describe('dry run', () => {
|
|
|
182
250
|
shouldVerifyRepoUrl: true,
|
|
183
251
|
branch: 'feature',
|
|
184
252
|
dryRun: true,
|
|
185
|
-
|
|
253
|
+
versionNumber: '0.99',
|
|
186
254
|
}),
|
|
187
255
|
).rejects.toThrow('Repository mismatch');
|
|
188
256
|
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { chmod, mkdtemp, rm } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
|
|
7
|
+
const projectRoot = fileURLToPath(new URL('../../../', import.meta.url));
|
|
8
|
+
const compilerPath = fileURLToPath(new URL('../../../node_modules/typescript/bin/tsc', import.meta.url));
|
|
9
|
+
|
|
10
|
+
/** Compiles the CLI from the sources into a temporary directory, so tests run it the way users do. */
|
|
11
|
+
export const compileCli = async () => {
|
|
12
|
+
const buildDir = await mkdtemp(path.join(projectRoot, '.cli-test-'));
|
|
13
|
+
execFileSync(process.execPath, [compilerPath, '--outDir', buildDir], { cwd: projectRoot });
|
|
14
|
+
const cliPath = path.join(buildDir, 'bin/main.js');
|
|
15
|
+
await chmod(cliPath, 0o755);
|
|
16
|
+
return { cliPath, cleanup: async () => rm(buildDir, { recursive: true, force: true }) };
|
|
17
|
+
};
|