Skip to content

Commit 0bbdbdd

Browse files
author
Brian Vaughn
committed
Add release script snapshot test
1 parent 3585929 commit 0bbdbdd

File tree

5 files changed

+959
-2
lines changed

5 files changed

+959
-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
@@ -150,7 +150,8 @@ const run = async ({cwd, packages, version}, versionsMap) => {
150150
}
151151
if (beforeContents !== afterContents) {
152152
numFilesModified++;
153-
diff += printDiff(path, beforeContents, afterContents);
153+
// Using a relative path for diff helps with the snapshot test
154+
diff += printDiff(relative(cwd, path), beforeContents, afterContents);
154155
writeFileSync(path, afterContents, {cwd});
155156
}
156157
});

scripts/release/test.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
'scripts/release/test.snapshot',
65+
'utf-8',
66+
{cwd}
67+
);
68+
await exec('cp build/temp.diff scripts/release/test.snapshot');
69+
const afterContents = readFileSync(
70+
'scripts/release/test.snapshot',
71+
'utf-8',
72+
{cwd}
73+
);
74+
75+
if (beforeContents === afterContents) {
76+
console.log(theme.header`Snapshot test passed.`);
77+
} else {
78+
printDiff('scripts/release/test.snapshot', beforeContents, afterContents);
79+
console.log();
80+
console.error(theme.error('Snapshot test failed!'));
81+
console.log();
82+
console.log(
83+
'If this failure was expected, please update the contents of the snapshot file:'
84+
);
85+
console.log(
86+
theme` {command git add} {path scripts/release/test.snapshot}`
87+
);
88+
console.log(
89+
theme` {command git commit -m "Updating release script snapshot file."}`
90+
);
91+
process.exit(1);
92+
}
93+
} catch (error) {
94+
console.error(theme.error(error));
95+
process.exit(1);
96+
}
97+
};
98+
99+
run();

0 commit comments

Comments
 (0)