Skip to content

Commit 5a89dcd

Browse files
authored
internal: fix cleanup (#32061)
1 parent ed1711a commit 5a89dcd

File tree

2 files changed

+119
-5
lines changed

2 files changed

+119
-5
lines changed

generators/base/generator.spec.ts

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { before, describe, esmocha, expect, it } from 'esmocha';
2020
import { basename } from 'node:path';
2121

2222
import EnvironmentBuilder from '../../cli/environment-builder.ts';
23-
import { defaultHelpers as helpers } from '../../lib/testing/index.ts';
23+
import { defaultHelpers as helpers, result } from '../../lib/testing/index.ts';
2424
import { getCommandHelpOutput, shouldSupportFeatures } from '../../test/support/tests.ts';
2525

2626
import BaseGenerator from './index.ts';
@@ -89,4 +89,118 @@ describe(`generator - ${generator}`, () => {
8989
expect(postWriting).not.toHaveBeenCalled();
9090
});
9191
});
92+
93+
describe('control', () => {
94+
class CustomGenerator extends BaseGenerator {
95+
beforeQueue() {
96+
this.customLifecycle = true;
97+
}
98+
}
99+
100+
describe('.jhipsterOldVersion', () => {
101+
let jhipsterOldVersion: string | undefined;
102+
103+
before(async () => {
104+
await helpers
105+
.run(CustomGenerator)
106+
.withJHipsterConfig({ jhipsterVersion: '1.0.0' })
107+
.commitFiles()
108+
.withJHipsterGenerators({ useDefaultMocks: true })
109+
.withTask('postWriting', async function (this, { control }) {
110+
jhipsterOldVersion = control.jhipsterOldVersion;
111+
});
112+
});
113+
114+
it('have jhipsterOldVersion set correctly', async () => {
115+
expect(jhipsterOldVersion).toBe('1.0.0');
116+
});
117+
});
118+
119+
describe('.cleanupFiles', () => {
120+
let removeFiles: ReturnType<typeof esmocha.fn>;
121+
122+
before(async () => {
123+
removeFiles = esmocha.fn();
124+
125+
await helpers
126+
.run(CustomGenerator)
127+
.withJHipsterConfig({ jhipsterVersion: '1.0.0' })
128+
.commitFiles()
129+
.withJHipsterGenerators({ useDefaultMocks: true })
130+
.withTask('postWriting', async function (this, { control }) {
131+
control.removeFiles = removeFiles;
132+
await control.cleanupFiles({
133+
'1.0.1': [
134+
'1.0.1-file.txt',
135+
[true, '1.0.1-conditional-truthy-file1.txt', '1.0.1-conditional-truthy-file2.txt'],
136+
[false, '1.0.1-conditional-falsy-file1.txt'],
137+
],
138+
});
139+
});
140+
});
141+
142+
it('should call removeFiles', async () => {
143+
expect(removeFiles.mock.lastCall).toMatchInlineSnapshot(`
144+
[
145+
{
146+
"oldVersion": "1.0.0",
147+
"removedInVersion": "1.0.1",
148+
},
149+
"1.0.1-file.txt",
150+
"1.0.1-conditional-truthy-file1.txt",
151+
"1.0.1-conditional-truthy-file2.txt",
152+
]
153+
`);
154+
});
155+
});
156+
157+
describe('.removeFiles', () => {
158+
before(async () => {
159+
await helpers
160+
.run(CustomGenerator)
161+
.withJHipsterConfig({ jhipsterVersion: '1.0.0' })
162+
.withFiles({ 'diskFileToBeRemoved1.txt': 'foo', 'diskFileToBeRemoved2.txt': 'foo', 'diskFileNotToBeRemoved1.txt': 'foo' })
163+
.commitFiles()
164+
.withFiles({ 'memFsFileToBeRemoved1.txt': 'foo' })
165+
.withJHipsterGenerators({ useDefaultMocks: true })
166+
.withTask('postWriting', async function (this, { control }) {
167+
await control.removeFiles({ removedInVersion: '1.0.0' }, 'diskFileNotToBeRemoved1.txt');
168+
await control.removeFiles({ removedInVersion: '1.0.1' }, 'diskFileToBeRemoved1.txt', 'memFsFileToBeRemoved1.txt');
169+
await control.removeFiles({ oldVersion: '0.1.0', removedInVersion: '1.0.0' }, 'diskFileToBeRemoved2.txt');
170+
});
171+
});
172+
173+
it('should remove files', () => {
174+
result.assertNoFile('diskFileToBeRemoved1.txt');
175+
result.assertNoFile('diskFileToBeRemoved2.txt');
176+
result.assertNoFile('memFsFileToBeRemoved1.txt');
177+
});
178+
it('should not remove files', () => {
179+
result.assertFile('diskFileNotToBeRemoved1.txt');
180+
});
181+
it('should match state snapshot', async () => {
182+
expect(result.getStateSnapshot()).toMatchInlineSnapshot(`
183+
{
184+
".yo-rc.json": {
185+
"stateCleared": "modified",
186+
},
187+
"diskFileNotToBeRemoved1.txt": {
188+
"stateCleared": "modified",
189+
},
190+
"diskFileToBeRemoved1.txt": {
191+
"state": "deleted",
192+
"stateCleared": "modified",
193+
},
194+
"diskFileToBeRemoved2.txt": {
195+
"state": "deleted",
196+
"stateCleared": "modified",
197+
},
198+
"memFsFileToBeRemoved1.txt": {
199+
"state": "deleted",
200+
},
201+
}
202+
`);
203+
});
204+
});
205+
});
92206
});

generators/base/generator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import assert from 'node:assert';
2020
import fs, { existsSync, readFileSync, statSync } from 'node:fs';
2121
import { rm } from 'node:fs/promises';
22-
import path, { join, relative } from 'node:path';
22+
import path, { relative } from 'node:path';
2323

2424
import chalk from 'chalk';
2525
import { execaCommandSync } from 'execa';
@@ -228,14 +228,14 @@ export default class BaseGenerator<
228228
);
229229
},
230230
async cleanupFiles(oldVersionOrCleanup: string | CleanupArgumentType, cleanup?: CleanupArgumentType) {
231-
if (!jhipsterOldVersion) return;
231+
if (!this.jhipsterOldVersion) return;
232232
let oldVersion: string;
233233
if (typeof oldVersionOrCleanup === 'string') {
234234
oldVersion = oldVersionOrCleanup;
235235
assert(cleanup, 'cleanupFiles requires cleanup object');
236236
} else {
237237
cleanup = oldVersionOrCleanup;
238-
oldVersion = jhipsterOldVersion;
238+
oldVersion = this.jhipsterOldVersion!;
239239
}
240240
await Promise.all(
241241
Object.entries(cleanup).map(async ([version, files]) => {
@@ -244,7 +244,7 @@ export default class BaseGenerator<
244244
if (Array.isArray(file)) {
245245
const [condition, ...fileParts] = file;
246246
if (condition) {
247-
stringFiles.push(join(...fileParts));
247+
stringFiles.push(...fileParts);
248248
}
249249
} else {
250250
stringFiles.push(file);

0 commit comments

Comments
 (0)