Skip to content

Commit 20977e6

Browse files
bvaughnn8schloss
authored andcommitted
Add basic release script snapshot test (facebook#14280)
Added regression test for release scripts
1 parent 63f2264 commit 20977e6

File tree

5 files changed

+957
-2
lines changed

5 files changed

+957
-2
lines changed

scripts/release/prepare-stable-commands/check-out-packages.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@
33
'use strict';
44

55
const {exec} = require('child-process-promise');
6+
const {existsSync} = require('fs');
67
const {join} = require('path');
78
const {logPromise} = require('../utils');
89
const theme = require('../theme');
910

10-
const run = async ({cwd, packages, version}) => {
11+
const run = async ({cwd, local, packages, version}) => {
12+
if (local) {
13+
// Sanity test
14+
if (!existsSync(join(cwd, 'build', 'node_modules', 'react'))) {
15+
console.error(theme.error`No local build exists.`);
16+
process.exit(1);
17+
}
18+
return;
19+
}
20+
1121
// Cleanup from previous builds
1222
await exec(`rm -rf ./build/node_modules*`, {cwd});
1323
await exec(`mkdir ./build/node_modules`, {cwd});

scripts/release/prepare-stable-commands/parse-params.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ const commandLineArgs = require('command-line-args');
66
const commandLineUsage = require('command-line-usage');
77

88
const paramDefinitions = [
9+
{
10+
name: 'local',
11+
type: Boolean,
12+
description:
13+
'Skip NPM and use the build already present in "build/node_modules".',
14+
defaultValue: false,
15+
},
916
{
1017
name: 'version',
1118
type: String,

scripts/release/prepare-stable-commands/update-stable-version-numbers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ const run = async ({cwd, packages, version}, versionsMap) => {
158158
}
159159
if (beforeContents !== afterContents) {
160160
numFilesModified++;
161-
diff += printDiff(path, beforeContents, afterContents);
161+
// Using a relative path for diff helps with the snapshot test
162+
diff += printDiff(relative(cwd, path), beforeContents, afterContents);
162163
writeFileSync(path, afterContents, {cwd});
163164
}
164165
});

scripts/release/test.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
const {exec, spawn} = require('child-process-promise');
6+
const {join} = require('path');
7+
const {readFileSync} = require('fs');
8+
const theme = require('./theme');
9+
const {logPromise, printDiff} = require('./utils');
10+
11+
const cwd = join(__dirname, '..', '..');
12+
13+
const CIRCLE_CI_BUILD = 12707;
14+
const COMMIT = 'b3d1a81a9';
15+
const VERSION = '1.2.3';
16+
17+
const run = async () => {
18+
const defaultOptions = {
19+
cwd,
20+
env: process.env,
21+
};
22+
23+
try {
24+
// Start with a known build/revision:
25+
// https://circleci.com/gh/facebook/react/12707
26+
let promise = spawn(
27+
'node',
28+
['./scripts/release/prepare-canary.js', `--build=${CIRCLE_CI_BUILD}`],
29+
defaultOptions
30+
);
31+
logPromise(
32+
promise,
33+
theme`Checking out canary build {version ${CIRCLE_CI_BUILD}}`
34+
);
35+
await promise;
36+
37+
// Upgrade the above build top a known React version.
38+
// Note that using the --local flag skips NPM checkout.
39+
// This isn't totally necessary but is useful if we want to test an unpublished canary.
40+
promise = spawn(
41+
'node',
42+
[
43+
'./scripts/release/prepare-stable.js',
44+
`--version=0.0.0-${COMMIT}`,
45+
'--local',
46+
],
47+
defaultOptions
48+
);
49+
promise.childProcess.stdin.setEncoding('utf-8');
50+
promise.childProcess.stdout.setEncoding('utf-8');
51+
promise.childProcess.stdout.on('data', data => {
52+
if (data.includes('✓ Version for')) {
53+
// Update all packages to a stable version
54+
promise.childProcess.stdin.write(VERSION);
55+
} else if (data.includes('(y/N)')) {
56+
// Accept all of the confirmation prompts
57+
promise.childProcess.stdin.write('y');
58+
}
59+
});
60+
logPromise(promise, theme`Preparing stable release {version ${VERSION}}`);
61+
await promise;
62+
63+
const beforeContents = readFileSync(
64+
join(cwd, 'scripts/release/test.snapshot'),
65+
'utf-8'
66+
);
67+
await exec('cp build/temp.diff scripts/release/test.snapshot', {cwd});
68+
const afterContents = readFileSync(
69+
join(cwd, 'scripts/release/test.snapshot'),
70+
'utf-8'
71+
);
72+
73+
if (beforeContents === afterContents) {
74+
console.log(theme.header`Snapshot test passed.`);
75+
} else {
76+
printDiff('scripts/release/test.snapshot', beforeContents, afterContents);
77+
console.log();
78+
console.error(theme.error('Snapshot test failed!'));
79+
console.log();
80+
console.log(
81+
'If this failure was expected, please update the contents of the snapshot file:'
82+
);
83+
console.log(
84+
theme` {command git add} {path scripts/release/test.snapshot}`
85+
);
86+
console.log(
87+
theme` {command git commit -m "Updating release script snapshot file."}`
88+
);
89+
process.exit(1);
90+
}
91+
} catch (error) {
92+
console.error(theme.error(error));
93+
process.exit(1);
94+
}
95+
};
96+
97+
run();

0 commit comments

Comments
 (0)