Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6ee1dda

Browse files
brandonrobertsFrozenPandaz
authored andcommittedAug 13, 2020
feat(core): use custom output logging for configuration out of sync errors
1 parent d7f1a03 commit 6ee1dda

File tree

2 files changed

+99
-47
lines changed

2 files changed

+99
-47
lines changed
 

Diff for: ‎packages/workspace/src/core/assert-workspace-validity.spec.ts

+65-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { assertWorkspaceValidity } from './assert-workspace-validity';
2+
import { output } from '../utils/output';
23

34
describe('assertWorkspaceValidity', () => {
45
let mockNxJson: any;
@@ -44,53 +45,85 @@ describe('assertWorkspaceValidity', () => {
4445
});
4546

4647
it('should throw for a missing project in workspace.json', () => {
48+
spyOn(output, 'error');
4749
delete mockWorkspaceJson.projects.app1;
48-
try {
49-
assertWorkspaceValidity(mockWorkspaceJson, mockNxJson);
50-
fail('Did not throw');
51-
} catch (e) {
52-
expect(e.message).toContain('projects are missing in');
53-
}
50+
51+
const mockExit = jest
52+
.spyOn(process, 'exit')
53+
.mockImplementation(((code?: number) => {}) as any);
54+
assertWorkspaceValidity(mockWorkspaceJson, mockNxJson);
55+
56+
expect(output.error).toHaveBeenCalledWith({
57+
title: 'Configuration Error',
58+
bodyLines: [
59+
`workspace.json and nx.json are out of sync. The following projects are missing in workspace.json: app1`,
60+
],
61+
});
62+
expect(mockExit).toHaveBeenCalledWith(1);
63+
mockExit.mockRestore();
5464
});
5565

5666
it('should throw for a missing project in nx.json', () => {
67+
spyOn(output, 'error');
68+
5769
delete mockNxJson.projects.app1;
58-
try {
59-
assertWorkspaceValidity(mockWorkspaceJson, mockNxJson);
60-
fail('Did not throw');
61-
} catch (e) {
62-
expect(e.message).toContain('projects are missing in nx.json');
63-
}
70+
71+
const mockExit = jest
72+
.spyOn(process, 'exit')
73+
.mockImplementation(((code?: number) => {}) as any);
74+
assertWorkspaceValidity(mockWorkspaceJson, mockNxJson);
75+
76+
expect(mockExit).toHaveBeenCalledWith(1);
77+
expect(output.error).toHaveBeenCalledWith({
78+
title: 'Configuration Error',
79+
bodyLines: [
80+
`workspace.json and nx.json are out of sync. The following projects are missing in nx.json: app1`,
81+
],
82+
});
83+
mockExit.mockRestore();
6484
});
6585

6686
it('should throw for an invalid top-level implicit dependency', () => {
87+
spyOn(output, 'error');
6788
mockNxJson.implicitDependencies = {
6889
'README.md': ['invalidproj'],
6990
};
70-
try {
71-
assertWorkspaceValidity(mockWorkspaceJson, mockNxJson);
72-
fail('Did not throw');
73-
} catch (e) {
74-
expect(e.message).toContain(
75-
'implicitDependencies specified in nx.json are invalid'
76-
);
77-
expect(e.message).toContain(' README.md');
78-
expect(e.message).toContain(' invalidproj');
79-
}
91+
92+
const mockExit = jest
93+
.spyOn(process, 'exit')
94+
.mockImplementation(((code?: number) => {}) as any);
95+
assertWorkspaceValidity(mockWorkspaceJson, mockNxJson);
96+
97+
expect(mockExit).toHaveBeenCalledWith(1);
98+
expect(output.error).toHaveBeenCalledWith({
99+
title: 'Configuration Error',
100+
bodyLines: [
101+
`The following implicitDependencies specified in nx.json are invalid:
102+
README.md
103+
invalidproj`,
104+
],
105+
});
106+
mockExit.mockRestore();
80107
});
81108

82109
it('should throw for an invalid project-level implicit dependency', () => {
110+
spyOn(output, 'error');
83111
mockNxJson.projects.app2.implicitDependencies = ['invalidproj'];
84112

85-
try {
86-
assertWorkspaceValidity(mockWorkspaceJson, mockNxJson);
87-
fail('Did not throw');
88-
} catch (e) {
89-
expect(e.message).toContain(
90-
'implicitDependencies specified in nx.json are invalid'
91-
);
92-
expect(e.message).toContain(' app2');
93-
expect(e.message).toContain(' invalidproj');
94-
}
113+
const mockExit = jest
114+
.spyOn(process, 'exit')
115+
.mockImplementation(((code?: number) => {}) as any);
116+
assertWorkspaceValidity(mockWorkspaceJson, mockNxJson);
117+
118+
expect(mockExit).toHaveBeenCalledWith(1);
119+
expect(output.error).toHaveBeenCalledWith({
120+
title: 'Configuration Error',
121+
bodyLines: [
122+
`The following implicitDependencies specified in nx.json are invalid:
123+
app2
124+
invalidproj`,
125+
],
126+
});
127+
mockExit.mockRestore();
95128
});
96129
});

Diff for: ‎packages/workspace/src/core/assert-workspace-validity.ts

+34-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
import { workspaceFileName } from './file-utils';
2-
import { ImplicitJsonSubsetDependency } from '@nrwl/workspace/src/core/shared-interfaces';
2+
import {
3+
ImplicitJsonSubsetDependency,
4+
NxJson,
5+
} from '@nrwl/workspace/src/core/shared-interfaces';
6+
import { output } from '../utils/output';
37

4-
export function assertWorkspaceValidity(workspaceJson, nxJson) {
8+
export function assertWorkspaceValidity(workspaceJson, nxJson: NxJson) {
59
const workspaceJsonProjects = Object.keys(workspaceJson.projects);
610
const nxJsonProjects = Object.keys(nxJson.projects);
711

812
if (minus(workspaceJsonProjects, nxJsonProjects).length > 0) {
9-
throw new Error(
10-
`${workspaceFileName()} and nx.json are out of sync. The following projects are missing in nx.json: ${minus(
11-
workspaceJsonProjects,
12-
nxJsonProjects
13-
).join(', ')}`
14-
);
13+
output.error({
14+
title: 'Configuration Error',
15+
bodyLines: [
16+
`${workspaceFileName()} and nx.json are out of sync. The following projects are missing in nx.json: ${minus(
17+
workspaceJsonProjects,
18+
nxJsonProjects
19+
).join(', ')}`,
20+
],
21+
});
22+
23+
process.exit(1);
1524
}
1625

1726
if (minus(nxJsonProjects, workspaceJsonProjects).length > 0) {
18-
throw new Error(
19-
`${workspaceFileName()} and nx.json are out of sync. The following projects are missing in ${workspaceFileName()}: ${minus(
20-
nxJsonProjects,
21-
workspaceJsonProjects
22-
).join(', ')}`
23-
);
27+
output.error({
28+
title: 'Configuration Error',
29+
bodyLines: [
30+
`${workspaceFileName()} and nx.json are out of sync. The following projects are missing in ${workspaceFileName()}: ${minus(
31+
nxJsonProjects,
32+
workspaceJsonProjects
33+
).join(', ')}`,
34+
],
35+
});
36+
37+
process.exit(1);
2438
}
2539

2640
const projects = {
@@ -81,7 +95,12 @@ export function assertWorkspaceValidity(workspaceJson, nxJson) {
8195
message += str;
8296
});
8397

84-
throw new Error(message);
98+
output.error({
99+
title: 'Configuration Error',
100+
bodyLines: [message],
101+
});
102+
103+
process.exit(1);
85104
}
86105

87106
function detectAndSetInvalidProjectValues(

0 commit comments

Comments
 (0)
Please sign in to comment.