Skip to content

Commit 9e03e3e

Browse files
devversionjosephperrott
authored andcommitted
chore(schematics): cleanup temporary directory after test case run (angular#14447)
* Currently we run _multiple_ test cases for the schematics and create temporary directories in order to test them with TSLint. This pollutes the temporary directory because we don't remove the temporary directory afterwards.
1 parent 41b6021 commit 9e03e3e

File tree

8 files changed

+38
-12
lines changed

8 files changed

+38
-12
lines changed

src/cdk/schematics/ng-update/test-cases/misc/method-call-checks.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import {runTestCases} from '../../../testing';
44
describe('v6 method call checks', () => {
55

66
it('should properly report invalid method calls', async () => {
7-
const {logOutput} = await runTestCases('migration-v6', migrationCollection, {
7+
const {logOutput, removeTempDir} = await runTestCases('migration-v6', migrationCollection, {
88
'method-call-checks': require.resolve('./method-call-checks_input.ts')
99
});
1010

1111
expect(logOutput)
1212
.toMatch(/\[15,.*Found call to "FocusMonitor\.monitor".*renderer.*has been removed/);
1313
expect(logOutput)
1414
.toMatch(/\[16,.*Found call to "FocusMonitor\.monitor".*renderer.*has been removed/);
15+
16+
removeTempDir();
1517
});
1618
});

src/cdk/schematics/ng-update/test-cases/v6-test-cases.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,23 @@ describe('v6 upgrade test cases', () => {
1616
];
1717

1818
let testCasesOutputPath: string;
19+
let cleanupTestApp: () => void;
1920

2021
beforeAll(async () => {
2122
const testCaseInputs = testCases.reduce((inputs, testCaseName) => {
2223
inputs[testCaseName] = require.resolve(`./${testCaseName}_input.ts`);
2324
return inputs;
2425
}, {} as {[name: string]: string});
2526

26-
const {tempPath} = await runTestCases('migration-v6', migrationCollection, testCaseInputs);
27+
const {tempPath, removeTempDir} =
28+
await runTestCases('migration-v6', migrationCollection, testCaseInputs);
2729

2830
testCasesOutputPath = join(tempPath, 'projects/cdk-testing/src/test-cases/');
31+
cleanupTestApp = removeTempDir;
2932
});
3033

34+
afterAll(() => cleanupTestApp());
35+
3136
// Iterates through every test case directory and generates a jasmine test block that will
3237
// verify that the update schematics properly updated the test input to the expected output.
3338
testCases.forEach(testCaseName => {

src/cdk/schematics/ng-update/test-cases/v7-test-cases.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@ describe('v7 upgrade test cases', () => {
1313
];
1414

1515
let testCasesOutputPath: string;
16+
let cleanupTestApp: () => void;
1617

1718
beforeAll(async () => {
1819
const testCaseInputs = testCases.reduce((inputs, testCaseName) => {
1920
inputs[testCaseName] = require.resolve(`./${testCaseName}_input.ts`);
2021
return inputs;
2122
}, {} as {[name: string]: string});
2223

23-
const {tempPath} = await runTestCases('migration-v7', migrationCollection, testCaseInputs);
24+
const {tempPath, removeTempDir} =
25+
await runTestCases('migration-v7', migrationCollection, testCaseInputs);
2426

2527
testCasesOutputPath = join(tempPath, 'projects/cdk-testing/src/test-cases/');
28+
cleanupTestApp = removeTempDir;
2629
});
2730

31+
afterAll(() => cleanupTestApp());
32+
2833
// Iterates through every test case directory and generates a jasmine test block that will
2934
// verify that the update schematics properly updated the test input to the expected output.
3035
testCases.forEach(testCaseName => {

src/cdk/schematics/testing/test-case-setup.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {getSystemPath, normalize} from '@angular-devkit/core';
1010
import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing';
1111
import * as virtualFs from '@angular-devkit/core/src/virtual-fs/host';
1212
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
13-
import {readFileSync, writeFileSync, mkdirpSync} from 'fs-extra';
14-
import {join, dirname} from 'path';
13+
import {mkdirpSync, readFileSync, writeFileSync, removeSync} from 'fs-extra';
14+
import {dirname, join} from 'path';
1515
import {createTestApp, runPostScheduledTasks} from '../testing';
1616

1717
/** Reads the UTF8 content of the specified file. Normalizes the path and ensures that */
@@ -34,7 +34,7 @@ export function createFileSystemTestApp(runner: SchematicTestRunner) {
3434
tempFileSystemHost.sync.write(f, virtualFs.stringToFileBuffer(appTree.readContent(f)));
3535
});
3636

37-
return {appTree, tempPath};
37+
return {appTree, tempPath, removeTempDir: () => removeSync(tempPath)};
3838
}
3939

4040
export async function runTestCases(migrationName: string, collectionPath: string,
@@ -47,7 +47,7 @@ export async function runTestCases(migrationName: string, collectionPath: string
4747
let logOutput = '';
4848
runner.logger.subscribe(entry => logOutput += entry.message);
4949

50-
const {appTree, tempPath} = createFileSystemTestApp(runner);
50+
const {appTree, tempPath, removeTempDir} = createFileSystemTestApp(runner);
5151

5252
// Write each test-case input to the file-system. This is necessary because otherwise
5353
// TSLint won't be able to pick up the test cases.
@@ -71,5 +71,5 @@ export async function runTestCases(migrationName: string, collectionPath: string
7171
// Switch back to the initial working directory.
7272
process.chdir(initialWorkingDir);
7373

74-
return {tempPath, logOutput};
74+
return {tempPath, logOutput, removeTempDir};
7575
}

src/lib/schematics/ng-update/test-cases/misc/constructor-checks.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {runTestCases} from '@angular/cdk/schematics/testing';
44
describe('constructor checks', () => {
55

66
it('should properly report invalid constructor expression signatures', async () => {
7-
const {logOutput} = await runTestCases('migration-v6', migrationCollection, {
7+
const {logOutput, removeTempDir} = await runTestCases('migration-v6', migrationCollection, {
88
'constructor-checks': require.resolve('./constructor-checks_input.ts')
99
});
1010

@@ -38,6 +38,8 @@ describe('constructor checks', () => {
3838

3939
expect(logOutput).toMatch(/Found "ExtendedDateAdapter".*super.*: super\(string, Platform\)/);
4040
expect(logOutput).toMatch(/Found "ExtendedDateAdapter".*: new \w+\(string, Platform\)/);
41+
42+
removeTempDir();
4143
});
4244
});
4345

src/lib/schematics/ng-update/test-cases/misc/import-checks.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import {migrationCollection} from '../index.spec';
44
describe('v6 import misc checks', () => {
55

66
it('should report imports for deleted animation constants', async () => {
7-
const {logOutput} = await runTestCases('migration-v6', migrationCollection, {
7+
const {logOutput, removeTempDir} = await runTestCases('migration-v6', migrationCollection, {
88
'import-checks': require.resolve('./import-checks_input.ts')
99
});
1010

1111
expect(logOutput).toMatch(/Found deprecated symbol "SHOW_ANIMATION"/);
1212
expect(logOutput).toMatch(/Found deprecated symbol "HIDE_ANIMATION"/);
13+
14+
removeTempDir();
1315
});
1416
});
1517

src/lib/schematics/ng-update/test-cases/v6-test-cases.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@ describe('v6 upgrade test cases', () => {
1919
];
2020

2121
let testCasesOutputPath: string;
22+
let cleanupTestApp: () => void;
2223

2324
beforeAll(async () => {
2425
const testCaseInputs = testCases.reduce((inputs, testCaseName) => {
2526
inputs[testCaseName] = require.resolve(`./${testCaseName}_input.ts`);
2627
return inputs;
2728
}, {} as {[name: string]: string});
2829

29-
const {tempPath} = await runTestCases('migration-v6', migrationCollection, testCaseInputs);
30+
const {tempPath, removeTempDir} =
31+
await runTestCases('migration-v6', migrationCollection, testCaseInputs);
3032

3133
testCasesOutputPath = join(tempPath, 'projects/cdk-testing/src/test-cases/');
34+
cleanupTestApp = removeTempDir;
3235
});
3336

37+
afterAll(() => cleanupTestApp());
38+
3439
// Iterates through every test case directory and generates a jasmine test block that will
3540
// verify that the update schematics properly updated the test input to the expected output.
3641
testCases.forEach(testCaseName => {

src/lib/schematics/ng-update/test-cases/v7-test-cases.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@ describe('v7 upgrade test cases', () => {
1414
];
1515

1616
let testCasesOutputPath: string;
17+
let cleanupTestApp: () => void;
1718

1819
beforeAll(async () => {
1920
const testCaseInputs = testCases.reduce((inputs, testCaseName) => {
2021
inputs[testCaseName] = require.resolve(`./${testCaseName}_input.ts`);
2122
return inputs;
2223
}, {} as {[name: string]: string});
2324

24-
const {tempPath} = await runTestCases('migration-v7', migrationCollection, testCaseInputs);
25+
const {tempPath, removeTempDir} =
26+
await runTestCases('migration-v7', migrationCollection, testCaseInputs);
2527

2628
testCasesOutputPath = join(tempPath, 'projects/cdk-testing/src/test-cases/');
29+
cleanupTestApp = removeTempDir;
2730
});
2831

32+
afterAll(() => cleanupTestApp());
33+
2934
// Iterates through every test case directory and generates a jasmine test block that will
3035
// verify that the update schematics properly updated the test input to the expected output.
3136
testCases.forEach(testCaseName => {

0 commit comments

Comments
 (0)