Skip to content

Commit da7a788

Browse files
committed
test: limit information stored in context
Store only objects set in `beforeEach` used in `test`. Avoid logging useless info on test failure.
1 parent 42a4856 commit da7a788

4 files changed

Lines changed: 40 additions & 31 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"prettier": "~1.9.2",
4040
"semantic-release": "^10.0.0",
4141
"sinon": "^4.0.0",
42+
"tempy": "^0.2.1",
4243
"xo": "^0.18.2"
4344
},
4445
"engines": {

test/integration.test.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import {escape} from 'querystring';
22
import test from 'ava';
33
import {stat} from 'fs-extra';
4-
import {stub, match} from 'sinon';
4+
import {stub} from 'sinon';
55
import clearModule from 'clear-module';
66
import SemanticReleaseError from '@semantic-release/error';
77
import {authenticate, upload} from './helpers/mock-github';
88

99
/* eslint camelcase: ["error", {properties: "never"}] */
1010

11+
// Save the current process.env
12+
const envBackup = Object.assign({}, process.env);
13+
1114
test.beforeEach(t => {
12-
// Save the current process.env
13-
t.context.env = Object.assign({}, process.env);
1415
// Delete env variables in case they are on the machine running the tests
1516
delete process.env.GH_TOKEN;
1617
delete process.env.GITHUB_TOKEN;
@@ -27,9 +28,9 @@ test.beforeEach(t => {
2728
t.context.logger = {log: t.context.log, error: t.context.error};
2829
});
2930

30-
test.afterEach.always(t => {
31+
test.afterEach.always(() => {
3132
// Restore process.env
32-
process.env = Object.assign({}, t.context.env);
33+
process.env = envBackup;
3334
});
3435

3536
test.serial('Verify Github auth', async t => {
@@ -157,9 +158,9 @@ test.serial('Publish a release with an array of assets', async t => {
157158

158159
await t.context.m.publish({githubToken, assets}, {nextRelease, options, logger: t.context.logger});
159160

160-
t.true(t.context.log.calledWith(match.string, releaseUrl));
161-
t.true(t.context.log.calledWith(match.string, assetUrl));
162-
t.true(t.context.log.calledWith(match.string, otherAssetUrl));
161+
t.deepEqual(t.context.log.args[0], ['Published Github release: %s', releaseUrl]);
162+
t.deepEqual(t.context.log.args[1], ['Published file %s', assetUrl]);
163+
t.deepEqual(t.context.log.args[2], ['Published file %s', otherAssetUrl]);
163164
t.true(github.isDone());
164165
t.true(githubUpload1.isDone());
165166
t.true(githubUpload2.isDone());
@@ -220,9 +221,9 @@ test.serial('Verify Github auth and release', async t => {
220221
await t.notThrows(t.context.m.verifyConditions({}, {options}));
221222
await t.context.m.publish({assets}, {nextRelease, options, logger: t.context.logger});
222223

223-
t.true(t.context.log.calledWith(match.string, releaseUrl));
224-
t.true(t.context.log.calledWith(match.string, assetUrl));
225-
t.true(t.context.log.calledWith(match.string, otherAssetUrl));
224+
t.deepEqual(t.context.log.args[0], ['Published Github release: %s', releaseUrl]);
225+
t.deepEqual(t.context.log.args[1], ['Published file %s', otherAssetUrl]);
226+
t.deepEqual(t.context.log.args[2], ['Published file %s', assetUrl]);
226227
t.true(github.isDone());
227228
t.true(githubUpload1.isDone());
228229
t.true(githubUpload2.isDone());

test/publish.test.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ import {escape} from 'querystring';
22
import test from 'ava';
33
import {stat} from 'fs-extra';
44
import nock from 'nock';
5-
import {stub, match} from 'sinon';
5+
import {stub} from 'sinon';
6+
import tempy from 'tempy';
67
import publish from '../lib/publish';
78
import {authenticate, upload} from './helpers/mock-github';
89

910
/* eslint camelcase: ["error", {properties: "never"}] */
1011

12+
// Save the current process.env
13+
const envBackup = Object.assign({}, process.env);
14+
1115
test.beforeEach(t => {
12-
// Save the current process.env
13-
t.context.env = Object.assign({}, process.env);
1416
// Delete env variables in case they are on the machine running the tests
1517
delete process.env.GH_TOKEN;
1618
delete process.env.GITHUB_TOKEN;
@@ -24,9 +26,9 @@ test.beforeEach(t => {
2426
t.context.logger = {log: t.context.log, error: t.context.error};
2527
});
2628

27-
test.afterEach.always(t => {
29+
test.afterEach.always(() => {
2830
// Restore process.env
29-
process.env = Object.assign({}, t.context.env);
31+
process.env = envBackup;
3032
// Clear nock
3133
nock.cleanAll();
3234
});
@@ -58,7 +60,7 @@ test.serial('Publish a release', async t => {
5860

5961
await publish(pluginConfig, options, nextRelease, t.context.logger);
6062

61-
t.true(t.context.log.calledWith(match.string, releaseUrl));
63+
t.deepEqual(t.context.log.args[0], ['Published Github release: %s', releaseUrl]);
6264
t.true(github.isDone());
6365
});
6466

@@ -87,7 +89,7 @@ test.serial('Publish a release with an existing tag', async t => {
8789

8890
await publish(pluginConfig, options, nextRelease, t.context.logger);
8991

90-
t.true(t.context.log.calledWith(match.string, releaseUrl));
92+
t.deepEqual(t.context.log.args[0], ['Published Github release: %s', releaseUrl]);
9193
t.true(github.isDone());
9294
});
9395

@@ -129,8 +131,8 @@ test.serial('Publish a release with one asset', async t => {
129131

130132
await publish(pluginConfig, options, nextRelease, t.context.logger);
131133

132-
t.true(t.context.log.calledWith(match.string, releaseUrl));
133-
t.true(t.context.log.calledWith(match.string, assetUrl));
134+
t.deepEqual(t.context.log.args[0], ['Published Github release: %s', releaseUrl]);
135+
t.deepEqual(t.context.log.args[1], ['Published file %s', assetUrl]);
134136
t.true(github.isDone());
135137
t.true(githubUpload.isDone());
136138
});
@@ -183,8 +185,8 @@ test.serial('Publish a release with one asset and custom github url', async t =>
183185

184186
await publish(pluginConfig, options, nextRelease, t.context.logger);
185187

186-
t.true(t.context.log.calledWith(match.string, releaseUrl));
187-
t.true(t.context.log.calledWith(match.string, assetUrl));
188+
t.deepEqual(t.context.log.args[0], ['Published Github release: %s', releaseUrl]);
189+
t.deepEqual(t.context.log.args[1], ['Published file %s', assetUrl]);
188190
t.true(github.isDone());
189191
t.true(githubUpload.isDone());
190192
});
@@ -193,9 +195,10 @@ test.serial('Publish a release with an array of missing assets', async t => {
193195
const owner = 'test_user';
194196
const repo = 'test_repo';
195197
const githubToken = 'github_token';
198+
const emptyDirectory = tempy.directory();
196199
const pluginConfig = {
197200
githubToken,
198-
assets: ['test/fixtures/empty', {path: 'test/fixtures/missing.txt', name: 'missing.txt'}],
201+
assets: [emptyDirectory, {path: 'test/fixtures/missing.txt', name: 'missing.txt'}],
199202
};
200203
const nextRelease = {version: '1.0.0', gitHead: '123', gitTag: 'v1.0.0', notes: 'Test release note body'};
201204
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
@@ -219,9 +222,12 @@ test.serial('Publish a release with an array of missing assets', async t => {
219222

220223
await publish(pluginConfig, options, nextRelease, t.context.logger);
221224

222-
t.true(t.context.log.calledWith(match.string, releaseUrl));
223-
t.true(t.context.error.calledWith(match.string, 'test/fixtures/missing.txt'));
224-
t.true(t.context.error.calledWith(match.string, 'test/fixtures/empty'));
225+
t.deepEqual(t.context.log.args[0], ['Published Github release: %s', releaseUrl]);
226+
t.deepEqual(t.context.error.args[0], [
227+
'The asset %s cannot be read, and will be ignored.',
228+
'test/fixtures/missing.txt',
229+
]);
230+
t.deepEqual(t.context.error.args[1], ['The asset %s is not a file, and will be ignored.', emptyDirectory]);
225231
t.true(github.isDone());
226232
});
227233

test/verify.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import SemanticReleaseError from '@semantic-release/error';
44
import verify from '../lib/verify';
55
import {authenticate} from './helpers/mock-github';
66

7-
test.beforeEach(t => {
8-
// Save the current process.env
9-
t.context.env = Object.assign({}, process.env);
7+
// Save the current process.env
8+
const envBackup = Object.assign({}, process.env);
9+
10+
test.beforeEach(() => {
1011
// Delete env variables in case they are on the machine running the tests
1112
delete process.env.GH_TOKEN;
1213
delete process.env.GITHUB_TOKEN;
@@ -16,9 +17,9 @@ test.beforeEach(t => {
1617
delete process.env.GITHUB_PREFIX;
1718
});
1819

19-
test.afterEach.always(t => {
20+
test.afterEach.always(() => {
2021
// Restore process.env
21-
process.env = Object.assign({}, t.context.env);
22+
process.env = envBackup;
2223
// Clear nock
2324
nock.cleanAll();
2425
});

0 commit comments

Comments
 (0)