-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
cache.test.ts
241 lines (213 loc) · 7.46 KB
/
cache.test.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import {
cleanupProject,
listFiles,
newProject,
rmDist,
runCLI,
uniq,
updateFile,
updateProjectConfig,
} from '@nrwl/e2e/utils';
describe('cache', () => {
beforeEach(() => newProject());
afterEach(() => cleanupProject());
it('should cache command execution', async () => {
const myapp1 = uniq('myapp1');
const myapp2 = uniq('myapp2');
runCLI(`generate @nrwl/web:app ${myapp1}`);
runCLI(`generate @nrwl/web:app ${myapp2}`);
const files = `--files="apps/${myapp1}/src/main.ts,apps/${myapp2}/src/main.ts"`;
// run build with caching
// --------------------------------------------
const outputThatPutsDataIntoCache = runCLI(`affected:build ${files}`);
const filesApp1 = listFiles(`dist/apps/${myapp1}`);
const filesApp2 = listFiles(`dist/apps/${myapp2}`);
// now the data is in cache
expect(outputThatPutsDataIntoCache).not.toContain(
'read the output from the cache'
);
rmDist();
const outputWithBothBuildTasksCached = runCLI(`affected:build ${files}`);
expect(outputWithBothBuildTasksCached).toContain(
'read the output from the cache'
);
expectCached(outputWithBothBuildTasksCached, [myapp1, myapp2]);
expect(listFiles(`dist/apps/${myapp1}`)).toEqual(filesApp1);
expect(listFiles(`dist/apps/${myapp2}`)).toEqual(filesApp2);
// run with skipping cache
const outputWithBothBuildTasksCachedButSkipped = runCLI(
`affected:build ${files} --skip-nx-cache`
);
expect(outputWithBothBuildTasksCachedButSkipped).not.toContain(
`read the output from the cache`
);
// touch myapp1
// --------------------------------------------
updateFile(`apps/${myapp1}/src/main.ts`, (c) => {
return `${c}\n//some comment`;
});
const outputWithBuildApp2Cached = runCLI(`affected:build ${files}`);
expect(outputWithBuildApp2Cached).toContain(
'read the output from the cache'
);
expectMatchedOutput(outputWithBuildApp2Cached, [myapp2]);
// touch package.json
// --------------------------------------------
updateFile(`package.json`, (c) => {
const r = JSON.parse(c);
r.description = 'different';
return JSON.stringify(r);
});
const outputWithNoBuildCached = runCLI(`affected:build ${files}`);
expect(outputWithNoBuildCached).not.toContain(
'read the output from the cache'
);
// build individual project with caching
const individualBuildWithCache = runCLI(`build ${myapp1}`);
expect(individualBuildWithCache).toContain(
'existing outputs match the cache'
);
// skip caching when building individual projects
const individualBuildWithSkippedCache = runCLI(
`build ${myapp1} --skip-nx-cache`
);
expect(individualBuildWithSkippedCache).not.toContain(
'existing outputs match the cache'
);
// run lint with caching
// --------------------------------------------
const outputWithNoLintCached = runCLI(`affected:lint ${files}`);
expect(outputWithNoLintCached).not.toContain(
'read the output from the cache'
);
const outputWithBothLintTasksCached = runCLI(`affected:lint ${files}`);
expect(outputWithBothLintTasksCached).toContain(
'read the output from the cache'
);
expectCached(outputWithBothLintTasksCached, [
myapp1,
myapp2,
`${myapp1}-e2e`,
`${myapp2}-e2e`,
]);
// cache task failures
// --------------------------------------------
// updateFile('workspace.json', (c) => {
// const workspaceJson = JSON.parse(c);
// workspaceJson.projects[myapp1].targets.lint = {
// executor: '@nrwl/workspace:run-commands',
// options: {
// command: 'echo hi && exit 1',
// },
// };
// return JSON.stringify(workspaceJson, null, 2);
// });
// const failingRun = runCLI(`lint ${myapp1}`, {
// silenceError: true,
// env: { ...process.env, NX_CACHE_FAILURES: 'true' },
// });
// expect(failingRun).not.toContain('[retrieved from cache]');
//
// const cachedFailingRun = runCLI(`lint ${myapp1}`, {
// silenceError: true,
// env: { ...process.env, NX_CACHE_FAILURES: 'true' },
// });
// expect(cachedFailingRun).toContain('[retrieved from cache]');
// run without caching
// --------------------------------------------
// disable caching
// --------------------------------------------
updateFile('nx.json', (c) => {
const nxJson = JSON.parse(c);
nxJson.tasksRunnerOptions = {
default: {
options: {
cacheableOperations: [],
},
},
};
return JSON.stringify(nxJson, null, 2);
});
const outputWithoutCachingEnabled1 = runCLI(`affected:build ${files}`);
expect(outputWithoutCachingEnabled1).not.toContain(
'read the output from the cache'
);
const outputWithoutCachingEnabled2 = runCLI(`affected:build ${files}`);
expect(outputWithoutCachingEnabled2).not.toContain(
'read the output from the cache'
);
}, 120000);
it('should only cache specific files if build outputs is configured with specific files', async () => {
const mylib1 = uniq('mylib1');
runCLI(`generate @nrwl/react:lib ${mylib1} --buildable`);
// Update outputs in workspace.json to just be a particular file
updateProjectConfig(mylib1, (config) => {
config.targets['build-base'] = {
...config.targets.build,
};
config.targets.build = {
executor: '@nrwl/workspace:run-commands',
outputs: [`dist/libs/${mylib1}/index.js`],
options: {
commands: [
{
command: `npx nx run ${mylib1}:build-base`,
},
],
parallel: false,
},
};
return config;
});
// run build with caching
// --------------------------------------------
const outputThatPutsDataIntoCache = runCLI(`run ${mylib1}:build`);
// now the data is in cache
expect(outputThatPutsDataIntoCache).not.toContain('cache');
rmDist();
const outputWithBuildTasksCached = runCLI(`run ${mylib1}:build`);
expect(outputWithBuildTasksCached).toContain('cache');
expectCached(outputWithBuildTasksCached, [mylib1]);
// Ensure that only the specific file in outputs was copied to cache
expect(listFiles(`dist/libs/${mylib1}`)).toEqual([`index.js`]);
}, 120000);
function expectCached(
actualOutput: string,
expectedCachedProjects: string[]
) {
expectProjectMatchTaskCacheStatus(actualOutput, expectedCachedProjects);
}
function expectMatchedOutput(
actualOutput: string,
expectedMatchedOutputProjects: string[]
) {
expectProjectMatchTaskCacheStatus(
actualOutput,
expectedMatchedOutputProjects,
'existing outputs match the cache'
);
}
function expectProjectMatchTaskCacheStatus(
actualOutput: string,
expectedProjects: string[],
cacheStatus: string = 'local cache'
) {
const matchingProjects = [];
const lines = actualOutput.split('\n');
lines.forEach((s) => {
if (s.trimStart().startsWith(`> nx run`)) {
const projectName = s
.trimStart()
.split(`> nx run `)[1]
.split(':')[0]
.trim();
if (s.indexOf(cacheStatus) > -1) {
matchingProjects.push(projectName);
}
}
});
matchingProjects.sort((a, b) => a.localeCompare(b));
expectedProjects.sort((a, b) => a.localeCompare(b));
expect(matchingProjects).toEqual(expectedProjects);
}
});