Skip to content

Commit 1d1818b

Browse files
committed
refactor(ci): remove unused isConfigured method
1 parent 9361089 commit 1d1818b

File tree

11 files changed

+67
-256
lines changed

11 files changed

+67
-256
lines changed

packages/ci/src/lib/monorepo/handlers/npm.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
MONOREPO_TOOL_DETECTORS,
32
hasCodePushUpDependency,
43
hasScript,
54
listWorkspaces,
@@ -9,10 +8,6 @@ import type { MonorepoToolHandler } from '../tools.js';
98
export const npmHandler: MonorepoToolHandler = {
109
tool: 'npm',
1110

12-
async isConfigured(options) {
13-
return MONOREPO_TOOL_DETECTORS.npm(options.cwd);
14-
},
15-
1611
async listProjects(options) {
1712
const { workspaces, rootPackageJson } = await listWorkspaces(options.cwd);
1813
return workspaces

packages/ci/src/lib/monorepo/handlers/npm.unit.test.ts

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,6 @@ describe('npmHandler', () => {
1818
const pkgJsonContent = (content: PackageJson): string =>
1919
JSON.stringify(content);
2020

21-
describe('isConfigured', () => {
22-
it('should detect NPM workspaces when package-lock.json exists and "workspaces" set in package.json', async () => {
23-
vol.fromJSON(
24-
{
25-
'package.json': pkgJsonContent({
26-
private: true,
27-
workspaces: ['packages/*'],
28-
}),
29-
'package-lock.json': '',
30-
},
31-
MEMFS_VOLUME,
32-
);
33-
await expect(npmHandler.isConfigured(options)).resolves.toBeTrue();
34-
});
35-
36-
it('should NOT detect NPM workspaces when "workspaces" not set in package.json', async () => {
37-
vol.fromJSON(
38-
{
39-
'package.json': pkgJsonContent({}),
40-
'package-lock.json': '',
41-
},
42-
MEMFS_VOLUME,
43-
);
44-
await expect(npmHandler.isConfigured(options)).resolves.toBeFalse();
45-
});
46-
47-
it("should NOT detect NPM workspaces when package-lock.json doesn't exist", async () => {
48-
vol.fromJSON(
49-
{
50-
'package.json': pkgJsonContent({
51-
private: true,
52-
workspaces: ['packages/*'],
53-
}),
54-
'yarn.lock': '',
55-
},
56-
MEMFS_VOLUME,
57-
);
58-
await expect(npmHandler.isConfigured(options)).resolves.toBeFalse();
59-
});
60-
});
61-
6221
describe('listProjects', () => {
6322
it('should list all NPM workspaces with code-pushup script', async () => {
6423
vol.fromJSON(

packages/ci/src/lib/monorepo/handlers/nx.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
MONOREPO_TOOL_DETECTORS,
32
executeProcess,
43
interpolate,
54
stringifyError,
@@ -10,21 +9,19 @@ import type { MonorepoToolHandler } from '../tools.js';
109
export const nxHandler: MonorepoToolHandler = {
1110
tool: 'nx',
1211

13-
async isConfigured(options) {
14-
return (
15-
(await MONOREPO_TOOL_DETECTORS.nx(options.cwd)) &&
16-
(
17-
await executeProcess({
18-
command: 'npx',
19-
args: ['nx', 'report'],
20-
cwd: options.cwd,
21-
ignoreExitCode: true,
22-
})
23-
).code === 0
24-
);
25-
},
26-
2712
async listProjects({ cwd, task, nxProjectsFilter }) {
13+
const { code, stderr } = await executeProcess({
14+
command: 'npx',
15+
args: ['nx', 'report'],
16+
cwd,
17+
ignoreExitCode: true,
18+
});
19+
if (code !== 0) {
20+
throw new Error(
21+
`'nx report' failed with exit code ${code}${stderr ? ` - ${stderr}` : ''}`,
22+
);
23+
}
24+
2825
const { stdout } = await executeProcess({
2926
command: 'npx',
3027
args: [

packages/ci/src/lib/monorepo/handlers/nx.unit.test.ts

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,15 @@ describe('nxHandler', () => {
1616
nxProjectsFilter: '--with-target={task}',
1717
};
1818

19-
describe('isConfigured', () => {
20-
it('should detect Nx when nx.json exists and `nx report` succeeds', async () => {
21-
vol.fromJSON({ 'nx.json': '{}' }, MEMFS_VOLUME);
22-
vi.spyOn(utils, 'executeProcess').mockResolvedValue({
23-
code: 0,
24-
stdout: 'NX Report complete - copy this into the issue template',
25-
} as utils.ProcessResult);
26-
27-
await expect(nxHandler.isConfigured(options)).resolves.toBeTrue();
28-
});
29-
30-
it("should NOT detect Nx when nx.json doesn't exist", async () => {
31-
vol.fromJSON({ 'turbo.json': '{}' }, MEMFS_VOLUME);
32-
vi.spyOn(utils, 'executeProcess').mockResolvedValue({
33-
code: 0,
34-
} as utils.ProcessResult);
35-
36-
await expect(nxHandler.isConfigured(options)).resolves.toBeFalse();
37-
});
38-
39-
it('should NOT detect Nx when `nx report` fails with non-zero exit code', async () => {
40-
vol.fromJSON({ 'nx.json': '' }, MEMFS_VOLUME);
41-
vi.spyOn(utils, 'executeProcess').mockResolvedValue({
42-
code: 1,
43-
stderr: 'Error: ValueExpected in nx.json',
44-
} as utils.ProcessResult);
45-
46-
await expect(nxHandler.isConfigured(options)).resolves.toBeFalse();
47-
});
48-
});
49-
5019
describe('listProjects', () => {
20+
const nxReportSuccess = { code: 0 } as utils.ProcessResult;
21+
5122
beforeEach(() => {
52-
vi.spyOn(utils, 'executeProcess').mockResolvedValue({
53-
stdout: '["backend","frontend"]',
54-
} as utils.ProcessResult);
23+
vi.spyOn(utils, 'executeProcess')
24+
.mockResolvedValueOnce(nxReportSuccess)
25+
.mockResolvedValueOnce({
26+
stdout: '["backend","frontend"]',
27+
} as utils.ProcessResult);
5528
});
5629

5730
it('should list projects from `nx show projects`', async () => {
@@ -95,20 +68,39 @@ describe('nxHandler', () => {
9568
} satisfies utils.ProcessConfig);
9669
});
9770

71+
it('should throw if `nx report` fails', async () => {
72+
vi.spyOn(utils, 'executeProcess')
73+
.mockReset()
74+
.mockResolvedValueOnce({
75+
code: 1,
76+
stderr: 'Error: ValueExpected in nx.json',
77+
} as utils.ProcessResult);
78+
79+
await expect(nxHandler.listProjects(options)).rejects.toThrow(
80+
"'nx report' failed with exit code 1 - Error: ValueExpected in nx.json",
81+
);
82+
});
83+
9884
it('should throw if `nx show projects` outputs invalid JSON', async () => {
99-
vi.spyOn(utils, 'executeProcess').mockResolvedValue({
100-
stdout: 'backend\nfrontend\n',
101-
} as utils.ProcessResult);
85+
vi.spyOn(utils, 'executeProcess')
86+
.mockReset()
87+
.mockResolvedValueOnce(nxReportSuccess)
88+
.mockResolvedValueOnce({
89+
stdout: 'backend\nfrontend\n',
90+
} as utils.ProcessResult);
10291

10392
await expect(nxHandler.listProjects(options)).rejects.toThrow(
10493
"Invalid non-JSON output from 'nx show projects' - SyntaxError: Unexpected token",
10594
);
10695
});
10796

10897
it("should throw if `nx show projects` JSON output isn't array of strings", async () => {
109-
vi.spyOn(utils, 'executeProcess').mockResolvedValue({
110-
stdout: '"backend"',
111-
} as utils.ProcessResult);
98+
vi.spyOn(utils, 'executeProcess')
99+
.mockReset()
100+
.mockResolvedValueOnce(nxReportSuccess)
101+
.mockResolvedValueOnce({
102+
stdout: '"backend"',
103+
} as utils.ProcessResult);
112104

113105
await expect(nxHandler.listProjects(options)).rejects.toThrow(
114106
'Invalid JSON output from \'nx show projects\', expected array of strings, received "backend"',

packages/ci/src/lib/monorepo/handlers/pnpm.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
MONOREPO_TOOL_DETECTORS,
32
hasCodePushUpDependency,
43
hasScript,
54
listPackages,
@@ -11,10 +10,6 @@ import type { MonorepoToolHandler } from '../tools.js';
1110
export const pnpmHandler: MonorepoToolHandler = {
1211
tool: 'pnpm',
1312

14-
async isConfigured(options) {
15-
return MONOREPO_TOOL_DETECTORS.pnpm(options.cwd);
16-
},
17-
1813
async listProjects(options) {
1914
const patterns = await readPnpmWorkspacePatterns(options.cwd);
2015
const packages = await listPackages(options.cwd, patterns);

packages/ci/src/lib/monorepo/handlers/pnpm.unit.test.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,6 @@ describe('pnpmHandler', () => {
1919
const pkgJsonContent = (content: PackageJson): string =>
2020
JSON.stringify(content);
2121

22-
describe('isConfigured', () => {
23-
it('should detect PNPM workspace when pnpm-workspace.yaml and package.json files exist', async () => {
24-
vol.fromJSON(
25-
{
26-
'package.json': pkgJsonContent({}),
27-
'pnpm-workspace.yaml': 'packages:\n- apps/*\n- libs/*\n\n',
28-
},
29-
MEMFS_VOLUME,
30-
);
31-
await expect(pnpmHandler.isConfigured(options)).resolves.toBeTrue();
32-
});
33-
34-
it("should NOT detect PNPM workspace when pnpm-workspace.yaml doesn't exist", async () => {
35-
vol.fromJSON(
36-
{
37-
'package.json': pkgJsonContent({}),
38-
'pnpm-lock.yaml': '',
39-
},
40-
MEMFS_VOLUME,
41-
);
42-
await expect(pnpmHandler.isConfigured(options)).resolves.toBeFalse();
43-
});
44-
45-
it("should NOT detect PNPM workspace when root package.json doesn't exist", async () => {
46-
vol.fromJSON(
47-
{
48-
'packages/cli/package.json': pkgJsonContent({}),
49-
'packages/cli/pnpm-lock.yaml': '',
50-
'packages/core/package.json': pkgJsonContent({}),
51-
'packages/core/pnpm-lock.yaml': '',
52-
},
53-
MEMFS_VOLUME,
54-
);
55-
await expect(pnpmHandler.isConfigured(options)).resolves.toBeFalse();
56-
});
57-
});
58-
5922
describe('listProjects', () => {
6023
it('should list all PNPM workspace packages with code-pushup script', async () => {
6124
vol.fromJSON(

packages/ci/src/lib/monorepo/handlers/turbo.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ type TurboConfig = {
1414
export const turboHandler: MonorepoToolHandler = {
1515
tool: 'turbo',
1616

17-
async isConfigured(options) {
17+
async listProjects(options) {
1818
const configPath = path.join(options.cwd, 'turbo.json');
19-
return (
20-
(await MONOREPO_TOOL_DETECTORS.turbo(options.cwd)) &&
21-
options.task in (await readJsonFile<TurboConfig>(configPath)).tasks
22-
);
23-
},
19+
if (
20+
!(options.task in (await readJsonFile<TurboConfig>(configPath)).tasks)
21+
) {
22+
throw new Error(`Task "${options.task}" not found in turbo.json`);
23+
}
2424

25-
async listProjects(options) {
2625
// eslint-disable-next-line functional/no-loop-statements
2726
for (const handler of WORKSPACE_HANDLERS) {
28-
if (await handler.isConfigured(options)) {
27+
if (await MONOREPO_TOOL_DETECTORS[handler.tool](options.cwd)) {
2928
const projects = await handler.listProjects(options);
3029
return projects
3130
.filter(({ bin }) => bin.includes(`run ${options.task}`)) // must have package.json script

packages/ci/src/lib/monorepo/handlers/turbo.unit.test.ts

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,61 +21,6 @@ describe('turboHandler', () => {
2121
const turboJsonContent = (content: { tasks: Record<string, object> }) =>
2222
JSON.stringify(content);
2323

24-
describe('isConfigured', () => {
25-
it('should detect Turborepo when turbo.json exists and has code-pushup task', async () => {
26-
vol.fromJSON(
27-
{
28-
'package.json': pkgJsonContent({}),
29-
'turbo.json': turboJsonContent({
30-
tasks: {
31-
'code-pushup': {
32-
env: ['CP_API_KEY'],
33-
outputs: ['.code-pushup'],
34-
},
35-
},
36-
}),
37-
},
38-
MEMFS_VOLUME,
39-
);
40-
await expect(turboHandler.isConfigured(options)).resolves.toBeTrue();
41-
});
42-
43-
it("should NOT detect Turborepo when turbo.json doesn't exist", async () => {
44-
vol.fromJSON(
45-
{
46-
'package.json': pkgJsonContent({}),
47-
'pnpm-lock.yaml': '',
48-
},
49-
MEMFS_VOLUME,
50-
);
51-
await expect(turboHandler.isConfigured(options)).resolves.toBeFalse();
52-
});
53-
54-
it("should NOT detect Turborepo when turbo.json doesn't include code-pushup task", async () => {
55-
vol.fromJSON(
56-
{
57-
'package.json': pkgJsonContent({}),
58-
'turbo.json': turboJsonContent({
59-
tasks: {
60-
build: {
61-
dependsOn: ['^build'],
62-
outputs: ['dist/**'],
63-
},
64-
lint: {},
65-
test: {},
66-
dev: {
67-
cache: false,
68-
persistent: true,
69-
},
70-
},
71-
}),
72-
},
73-
MEMFS_VOLUME,
74-
);
75-
await expect(turboHandler.isConfigured(options)).resolves.toBeFalse();
76-
});
77-
});
78-
7924
describe('listProjects', () => {
8025
it.each([
8126
[
@@ -148,6 +93,22 @@ describe('turboHandler', () => {
14893
},
14994
);
15095

96+
it('should throw if task not found in turbo.json', async () => {
97+
vol.fromJSON(
98+
{
99+
'package.json': pkgJsonContent({}),
100+
'turbo.json': turboJsonContent({
101+
tasks: { build: {}, lint: {} },
102+
}),
103+
},
104+
MEMFS_VOLUME,
105+
);
106+
107+
await expect(turboHandler.listProjects(options)).rejects.toThrow(
108+
'Task "code-pushup" not found in turbo.json',
109+
);
110+
});
111+
151112
it('should throw if no supported package manager configured', async () => {
152113
vol.fromJSON(
153114
{
@@ -171,19 +132,16 @@ describe('turboHandler', () => {
171132
name: 'api',
172133
directory: path.join(MEMFS_VOLUME, 'api'),
173134
bin: 'npx turbo run code-pushup --',
174-
binUncached: 'npx turbo run code-pushup --',
175135
},
176136
{
177137
name: 'cms',
178138
directory: path.join(MEMFS_VOLUME, 'cms'),
179139
bin: 'npx turbo run code-pushup --',
180-
binUncached: 'npx turbo run code-pushup --',
181140
},
182141
{
183142
name: 'web',
184143
directory: path.join(MEMFS_VOLUME, 'web'),
185144
bin: 'npx turbo run code-pushup --',
186-
binUncached: 'npx turbo run code-pushup --',
187145
},
188146
],
189147
};

0 commit comments

Comments
 (0)