Skip to content

Commit

Permalink
chore: make cleanup async (#1156)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
molant and thymikee authored May 12, 2020
1 parent f497f5a commit 5819a17
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 42 deletions.
6 changes: 3 additions & 3 deletions __e2e__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {wrap} from 'jest-snapshot-serializer-raw';
import {
runCLI,
getTempDirectory,
cleanup,
cleanupSync,
writeFiles,
spawnScript,
replaceProjectRootInOutput,
Expand Down Expand Up @@ -44,7 +44,7 @@ beforeAll(() => {
}

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

// Initialise React Native project
Expand All @@ -63,7 +63,7 @@ beforeAll(() => {
});

afterAll(() => {
cleanup(DIR);
cleanupSync(DIR);
});

test('shows up current config without unnecessary output', () => {
Expand Down
11 changes: 8 additions & 3 deletions __e2e__/default.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
import {
runCLI,
getTempDirectory,
cleanupSync,
writeFiles,
} from '../jest/helpers';

const DIR = getTempDirectory('test_default_behavior');

beforeEach(() => {
cleanup(DIR);
cleanupSync(DIR);
writeFiles(DIR, {});
});
afterEach(() => {
cleanup(DIR);
cleanupSync(DIR);
});

test('shows up help information without passing in any args', () => {
Expand Down
11 changes: 8 additions & 3 deletions __e2e__/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import fs from 'fs';
import path from 'path';
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
import {
runCLI,
getTempDirectory,
cleanupSync,
writeFiles,
} from '../jest/helpers';

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

Expand All @@ -27,11 +32,11 @@ const customTemplateCopiedFiles = [
];

beforeEach(() => {
cleanup(DIR);
cleanupSync(DIR);
writeFiles(DIR, {});
});
afterEach(() => {
cleanup(DIR);
cleanupSync(DIR);
});

test('init --template fails without package name', () => {
Expand Down
11 changes: 8 additions & 3 deletions __e2e__/install.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import path from 'path';
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
import {
runCLI,
getTempDirectory,
cleanupSync,
writeFiles,
} from '../jest/helpers';

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

beforeEach(() => {
cleanup(DIR);
cleanupSync(DIR);
writeFiles(DIR, {
'node_modules/react-native/package.json': '{}',
'package.json': '{}',
});
});
afterEach(() => cleanup(DIR));
afterEach(() => cleanupSync(DIR));

test.each(['yarn', 'npm'])('install module with %s', pm => {
if (pm === 'yarn') {
Expand Down
6 changes: 3 additions & 3 deletions __e2e__/legacyInit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import fs from 'fs';
import path from 'path';
// @ts-ignore
import execa from 'execa';
import {getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
import {getTempDirectory, cleanupSync, writeFiles} from '../jest/helpers';

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

beforeEach(() => {
cleanup(DIR);
cleanupSync(DIR);
writeFiles(DIR, {});
});
afterEach(() => {
cleanup(DIR);
cleanupSync(DIR);
});

// We skip this test, because it's flaky and we don't really update
Expand Down
6 changes: 3 additions & 3 deletions __e2e__/root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
spawnScript,
runCLI,
getTempDirectory,
cleanup,
cleanupSync,
writeFiles,
} from '../jest/helpers';

Expand All @@ -19,7 +19,7 @@ beforeAll(() => {
}

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

// Initialise React Native project
Expand All @@ -37,7 +37,7 @@ beforeAll(() => {
});

afterAll(() => {
cleanup(cwd);
cleanupSync(cwd);
});

test('works when Gradle is run outside of the project hierarchy', () => {
Expand Down
11 changes: 8 additions & 3 deletions __e2e__/uninstall.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
import {
runCLI,
getTempDirectory,
cleanupSync,
writeFiles,
} from '../jest/helpers';

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

beforeEach(() => {
cleanup(DIR);
cleanupSync(DIR);
writeFiles(DIR, {
'node_modules/react-native/package.json': '{}',
'node_modules/react-native-config/package.json': '{}',
Expand All @@ -15,7 +20,7 @@ beforeEach(() => {
}`,
});
});
afterEach(() => cleanup(DIR));
afterEach(() => cleanupSync(DIR));

test('uninstall fails when package is not defined', () => {
writeFiles(DIR, {
Expand Down
11 changes: 8 additions & 3 deletions __e2e__/unknown.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import {runCLI, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
import {
runCLI,
getTempDirectory,
cleanupSync,
writeFiles,
} from '../jest/helpers';

const DIR = getTempDirectory('test_unknown');

beforeEach(() => {
cleanup(DIR);
cleanupSync(DIR);
writeFiles(DIR, {});
});
afterEach(() => {
cleanup(DIR);
cleanupSync(DIR);
});

test('warn for passing in unknown commands', () => {
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
...common,
displayName: 'e2e',
setupFiles: ['<rootDir>/jest/setupE2eTests.js'],
testMatch: ['<rootDir>/**/__e2e__/*{.,-}test.[jt]s'],
testMatch: ['<rootDir>/__e2e__/*{.,-}test.[jt]s'],
},
{
...common,
Expand Down
13 changes: 11 additions & 2 deletions jest/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import {promisify} from 'util';
import {createDirectory} from 'jest-util';
// @ts-ignore jsfile
import rimraf from 'rimraf';
Expand All @@ -10,6 +11,8 @@ import slash from 'slash';
// @ts-ignore jsfile
import {Writable} from 'readable-stream';

const rimrafAsync = promisify(rimraf);

const CLI_PATH = path.resolve(__dirname, '../packages/cli/build/bin.js');

type RunOptions = {
Expand Down Expand Up @@ -77,7 +80,13 @@ export const makeTemplate = (
return values[number - 1];
});

export const cleanup = (directory: string) => rimraf.sync(directory);
export const cleanup = (directory: string) => {
return rimrafAsync(directory);
};

export const cleanupSync = (directory: string) => {
rimraf.sync(directory);
};

/**
* Creates a nested directory with files and their contents
Expand Down Expand Up @@ -189,7 +198,7 @@ function handleTestFailure(
) {
if (!options.expectedFailure && result.code !== 0) {
console.log(`Running ${cmd} command failed for unexpected reason. Here's more info:
${chalk.bold('cmd:')} ${cmd}
${chalk.bold('cmd:')} ${cmd}
${chalk.bold('options:')} ${JSON.stringify(options)}
${chalk.bold('args:')} ${(args || []).join(' ')}
${chalk.bold('stderr:')} ${result.stderr}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ describe('androidSDK', () => {
});
});

afterAll(() => cleanup(join(mockWorkingDir, 'android/build.gradle')));
afterAll(
async () => await cleanup(join(mockWorkingDir, 'android/build.gradle')),
);

let environmentInfo: EnvironmentInfo;

Expand Down Expand Up @@ -99,7 +101,7 @@ describe('androidSDK', () => {
stdout: 'build-tools;28.0.3',
});

cleanup(join(mockWorkingDir, 'android/build.gradle'));
await cleanup(join(mockWorkingDir, 'android/build.gradle'));

const diagnostics = await androidSDK.getDiagnostics(environmentInfo);
expect(diagnostics.needsToBeFixed).toBe(true);
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/tools/__tests__/copyFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import replacePathSepForRegex from '../replacePathSepForRegex';

const DIR = getTempDirectory('copyFiles-test');

beforeEach(() => {
cleanup(DIR);
beforeEach(async () => {
await cleanup(DIR);
fs.mkdirSync(DIR);
});

afterEach(() => {
cleanup(DIR);
afterEach(async () => {
await cleanup(DIR);
});

test('copies text and binary files from source to destination', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {

jest.mock('../resolveNodeModuleDir');

beforeEach(() => {
cleanup(DIR);
beforeEach(async () => {
await cleanup(DIR);
jest.resetModules();
});

afterEach(() => cleanup(DIR));
afterEach(async () => await cleanup(DIR));

const DIR = getTempDirectory('find_dependencies_test');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
getTempDirectory,
} from '../../../../../../jest/helpers';

beforeEach(() => {
cleanup(DIR);
beforeEach(async () => {
await cleanup(DIR);
jest.resetModules();
});

afterEach(() => cleanup(DIR));
afterEach(async () => await cleanup(DIR));

const DIR = getTempDirectory('find_project_root_test');

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/tools/config/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ const removeString = (config, str) =>
),
);

beforeEach(() => {
cleanup(DIR);
beforeEach(async () => {
await cleanup(DIR);
jest.resetModules();
jest.clearAllMocks();
});

afterEach(() => cleanup(DIR));
afterEach(async () => await cleanup(DIR));

test('should have a valid structure by default', () => {
writeFiles(DIR, {
Expand Down

0 comments on commit 5819a17

Please sign in to comment.