Skip to content

Commit 5819a17

Browse files
molantthymikee
andauthored
chore: make cleanup async (#1156)
* chore: make `cleanup` async Using `rimraf.sync` can cause `ENOTEMPTY` and `EBUSY` errors on Windows. This can make the Windows CI to fail randomly. Changing to the async method defaults to autoretry 3 times and thus avoiding this issues. * chore: jest config * chore: e2e tests use sync cleanup * chore: pr feedback * chore: revert timeout * Update __e2e__/default.test.ts * Update __e2e__/default.test.ts Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
1 parent f497f5a commit 5819a17

15 files changed

Lines changed: 78 additions & 42 deletions

File tree

__e2e__/config.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {wrap} from 'jest-snapshot-serializer-raw';
44
import {
55
runCLI,
66
getTempDirectory,
7-
cleanup,
7+
cleanupSync,
88
writeFiles,
99
spawnScript,
1010
replaceProjectRootInOutput,
@@ -44,7 +44,7 @@ beforeAll(() => {
4444
}
4545

4646
// Clean up folder and re-create a new project
47-
cleanup(DIR);
47+
cleanupSync(DIR);
4848
writeFiles(DIR, {});
4949

5050
// Initialise React Native project
@@ -63,7 +63,7 @@ beforeAll(() => {
6363
});
6464

6565
afterAll(() => {
66-
cleanup(DIR);
66+
cleanupSync(DIR);
6767
});
6868

6969
test('shows up current config without unnecessary output', () => {

__e2e__/default.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
1+
import {
2+
runCLI,
3+
getTempDirectory,
4+
cleanupSync,
5+
writeFiles,
6+
} from '../jest/helpers';
27

38
const DIR = getTempDirectory('test_default_behavior');
49

510
beforeEach(() => {
6-
cleanup(DIR);
11+
cleanupSync(DIR);
712
writeFiles(DIR, {});
813
});
914
afterEach(() => {
10-
cleanup(DIR);
15+
cleanupSync(DIR);
1116
});
1217

1318
test('shows up help information without passing in any args', () => {

__e2e__/init.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import fs from 'fs';
22
import path from 'path';
3-
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
3+
import {
4+
runCLI,
5+
getTempDirectory,
6+
cleanupSync,
7+
writeFiles,
8+
} from '../jest/helpers';
49

510
const DIR = getTempDirectory('command-init');
611

@@ -27,11 +32,11 @@ const customTemplateCopiedFiles = [
2732
];
2833

2934
beforeEach(() => {
30-
cleanup(DIR);
35+
cleanupSync(DIR);
3136
writeFiles(DIR, {});
3237
});
3338
afterEach(() => {
34-
cleanup(DIR);
39+
cleanupSync(DIR);
3540
});
3641

3742
test('init --template fails without package name', () => {

__e2e__/install.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import path from 'path';
2-
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
2+
import {
3+
runCLI,
4+
getTempDirectory,
5+
cleanupSync,
6+
writeFiles,
7+
} from '../jest/helpers';
38

49
const DIR = getTempDirectory('command-install-test');
510
const pkg = 'react-native-config';
611

712
beforeEach(() => {
8-
cleanup(DIR);
13+
cleanupSync(DIR);
914
writeFiles(DIR, {
1015
'node_modules/react-native/package.json': '{}',
1116
'package.json': '{}',
1217
});
1318
});
14-
afterEach(() => cleanup(DIR));
19+
afterEach(() => cleanupSync(DIR));
1520

1621
test.each(['yarn', 'npm'])('install module with %s', pm => {
1722
if (pm === 'yarn') {

__e2e__/legacyInit.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import fs from 'fs';
22
import path from 'path';
33
// @ts-ignore
44
import execa from 'execa';
5-
import {getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
5+
import {getTempDirectory, cleanupSync, writeFiles} from '../jest/helpers';
66

77
const DIR = getTempDirectory('command-legacy-init');
88

99
beforeEach(() => {
10-
cleanup(DIR);
10+
cleanupSync(DIR);
1111
writeFiles(DIR, {});
1212
});
1313
afterEach(() => {
14-
cleanup(DIR);
14+
cleanupSync(DIR);
1515
});
1616

1717
// We skip this test, because it's flaky and we don't really update

__e2e__/root.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
spawnScript,
55
runCLI,
66
getTempDirectory,
7-
cleanup,
7+
cleanupSync,
88
writeFiles,
99
} from '../jest/helpers';
1010

@@ -19,7 +19,7 @@ beforeAll(() => {
1919
}
2020

2121
// Clean up folder and re-create a new project
22-
cleanup(cwd);
22+
cleanupSync(cwd);
2323
writeFiles(cwd, {});
2424

2525
// Initialise React Native project
@@ -37,7 +37,7 @@ beforeAll(() => {
3737
});
3838

3939
afterAll(() => {
40-
cleanup(cwd);
40+
cleanupSync(cwd);
4141
});
4242

4343
test('works when Gradle is run outside of the project hierarchy', () => {

__e2e__/uninstall.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
1+
import {
2+
runCLI,
3+
getTempDirectory,
4+
cleanupSync,
5+
writeFiles,
6+
} from '../jest/helpers';
27

38
const DIR = getTempDirectory('command-uninstall-test');
49
const pkg = 'react-native-config';
510

611
beforeEach(() => {
7-
cleanup(DIR);
12+
cleanupSync(DIR);
813
writeFiles(DIR, {
914
'node_modules/react-native/package.json': '{}',
1015
'node_modules/react-native-config/package.json': '{}',
@@ -15,7 +20,7 @@ beforeEach(() => {
1520
}`,
1621
});
1722
});
18-
afterEach(() => cleanup(DIR));
23+
afterEach(() => cleanupSync(DIR));
1924

2025
test('uninstall fails when package is not defined', () => {
2126
writeFiles(DIR, {

__e2e__/unknown.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
1+
import {
2+
runCLI,
3+
getTempDirectory,
4+
cleanupSync,
5+
writeFiles,
6+
} from '../jest/helpers';
27

38
const DIR = getTempDirectory('test_unknown');
49

510
beforeEach(() => {
6-
cleanup(DIR);
11+
cleanupSync(DIR);
712
writeFiles(DIR, {});
813
});
914
afterEach(() => {
10-
cleanup(DIR);
15+
cleanupSync(DIR);
1116
});
1217

1318
test('warn for passing in unknown commands', () => {

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
...common,
1010
displayName: 'e2e',
1111
setupFiles: ['<rootDir>/jest/setupE2eTests.js'],
12-
testMatch: ['<rootDir>/**/__e2e__/*{.,-}test.[jt]s'],
12+
testMatch: ['<rootDir>/__e2e__/*{.,-}test.[jt]s'],
1313
},
1414
{
1515
...common,

jest/helpers.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs';
22
import os from 'os';
33
import path from 'path';
4+
import {promisify} from 'util';
45
import {createDirectory} from 'jest-util';
56
// @ts-ignore jsfile
67
import rimraf from 'rimraf';
@@ -10,6 +11,8 @@ import slash from 'slash';
1011
// @ts-ignore jsfile
1112
import {Writable} from 'readable-stream';
1213

14+
const rimrafAsync = promisify(rimraf);
15+
1316
const CLI_PATH = path.resolve(__dirname, '../packages/cli/build/bin.js');
1417

1518
type RunOptions = {
@@ -77,7 +80,13 @@ export const makeTemplate = (
7780
return values[number - 1];
7881
});
7982

80-
export const cleanup = (directory: string) => rimraf.sync(directory);
83+
export const cleanup = (directory: string) => {
84+
return rimrafAsync(directory);
85+
};
86+
87+
export const cleanupSync = (directory: string) => {
88+
rimraf.sync(directory);
89+
};
8190

8291
/**
8392
* Creates a nested directory with files and their contents
@@ -189,7 +198,7 @@ function handleTestFailure(
189198
) {
190199
if (!options.expectedFailure && result.code !== 0) {
191200
console.log(`Running ${cmd} command failed for unexpected reason. Here's more info:
192-
${chalk.bold('cmd:')} ${cmd}
201+
${chalk.bold('cmd:')} ${cmd}
193202
${chalk.bold('options:')} ${JSON.stringify(options)}
194203
${chalk.bold('args:')} ${(args || []).join(' ')}
195204
${chalk.bold('stderr:')} ${result.stderr}

0 commit comments

Comments
 (0)