Skip to content

Commit 105bde4

Browse files
committed
chore(test): rework env testing util to make cleanup easier
no issue - this adds a cleanupTestFolders function that will cleanup any folders generated by a call - rename setupEnv to setupTestFolder for clarity
1 parent c3979eb commit 105bde4

8 files changed

+80
-63
lines changed

test/acceptance/install-local-spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const expect = require('chai').expect;
3+
const {expect} = require('chai');
44
const fs = require('fs-extra');
55

66
const AcceptanceTest = require('../utils/acceptance-test');

test/unit/bootstrap-spec.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const expect = require('chai').expect;
33
const sinon = require('sinon');
4-
const env = require('../utils/env');
4+
const {setupTestFolder, cleanupTestFolders} = require('../utils/test-folder');
55
const path = require('path');
66
const proxyquire = require('proxyquire');
77

@@ -14,12 +14,16 @@ describe('Unit: Bootstrap', function () {
1414
sinon.restore();
1515
});
1616

17+
after(() => {
18+
cleanupTestFolders();
19+
});
20+
1721
describe('discoverCommands', function () {
1822
const bootstrap = require(modulePath);
1923

2024
it('loads basic command names into commands object', function () {
2125
let commands = {};
22-
const testEnv = env({
26+
const testEnv = setupTestFolder({
2327
dirs: ['commands/test3'],
2428
files: [{
2529
path: 'commands/test.js',
@@ -47,7 +51,7 @@ describe('Unit: Bootstrap', function () {
4751

4852
it('returns unmodified commands object if no commands dir exists for an extension', function () {
4953
let commands = {};
50-
const testEnv = env({});
54+
const testEnv = setupTestFolder({});
5155

5256
commands = bootstrap.discoverCommands(commands, testEnv.dir, 'testing');
5357

@@ -56,7 +60,7 @@ describe('Unit: Bootstrap', function () {
5660
});
5761

5862
it('ignores non-js files or folders without an index.js', function () {
59-
const testEnv = env({
63+
const testEnv = setupTestFolder({
6064
dirs: ['commands/test2', 'commands/test3'],
6165
files: [{
6266
path: 'commands/test.js',
@@ -85,7 +89,7 @@ describe('Unit: Bootstrap', function () {
8589
});
8690

8791
it('namespaces a command with the extension name if another command exists with the same basename', function () {
88-
const testEnv = env({
92+
const testEnv = setupTestFolder({
8993
dirs: ['commands/test2'],
9094
files: [{
9195
path: 'commands/test.js',

test/unit/commands/doctor/checks/validate-config-spec.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@ const expect = require('chai').expect;
33
const sinon = require('sinon');
44

55
const errors = require('../../../../../lib/errors');
6-
const setupEnv = require('../../../../utils/env');
6+
const {setupTestFolder, cleanupTestFolders} = require('../../../../utils/test-folder');
77
const advancedOpts = require('../../../../../lib/commands/config/advanced');
88

99
const check = require('../../../../../lib/commands/doctor/checks/validate-config');
1010
const validateConfig = check.task;
1111

1212
describe('Unit: Doctor Checks > validateConfig', function () {
13-
let env;
14-
1513
afterEach(function () {
1614
sinon.restore();
15+
});
1716

18-
if (env) {
19-
env.cleanup();
20-
env = null;
21-
}
17+
after(() => {
18+
cleanupTestFolders();
2219
});
2320

2421
it('skips if instance not set', function () {
@@ -39,7 +36,7 @@ describe('Unit: Doctor Checks > validateConfig', function () {
3936
});
4037

4138
it('rejects if environment is passed and no config exists for that environment', function () {
42-
env = setupEnv();
39+
const env = setupTestFolder();
4340
const cwdStub = sinon.stub(process, 'cwd').returns(env.dir);
4441
const runningStub = sinon.stub().resolves(false);
4542

@@ -58,7 +55,7 @@ describe('Unit: Doctor Checks > validateConfig', function () {
5855
});
5956

6057
it('rejects if environment is passed and the config file is not valid json', function () {
61-
env = setupEnv({files: [{path: 'config.testing.json', contents: 'not json'}]});
58+
const env = setupTestFolder({files: [{path: 'config.testing.json', contents: 'not json'}]});
6259
const cwdStub = sinon.stub(process, 'cwd').returns(env.dir);
6360
const runningStub = sinon.stub().resolves(false);
6461

@@ -78,7 +75,7 @@ describe('Unit: Doctor Checks > validateConfig', function () {
7875

7976
it('rejects with error if config values does not pass', function () {
8077
const config = {server: {port: 2368}};
81-
env = setupEnv({files: [{path: 'config.testing.json', content: config, json: true}]});
78+
const env = setupTestFolder({files: [{path: 'config.testing.json', content: config, json: true}]});
8279
const urlStub = sinon.stub(advancedOpts.url, 'validate').returns('Invalid URL');
8380
const portStub = sinon.stub(advancedOpts.port, 'validate').returns('Port is in use');
8481
sinon.stub(process, 'cwd').returns(env.dir);
@@ -101,7 +98,7 @@ describe('Unit: Doctor Checks > validateConfig', function () {
10198

10299
it('passes if all validate functions return true', function () {
103100
const config = {server: {port: 2368}};
104-
const env = setupEnv({files: [{path: 'config.testing.json', content: config, json: true}]});
101+
const env = setupTestFolder({files: [{path: 'config.testing.json', content: config, json: true}]});
105102
const urlStub = sinon.stub(advancedOpts.url, 'validate').returns(true);
106103
const portStub = sinon.stub(advancedOpts.port, 'validate').returns(true);
107104
sinon.stub(process, 'cwd').returns(env.dir);

test/unit/commands/update-spec.js

+12-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const expect = require('chai').expect;
33
const sinon = require('sinon');
44
const proxyquire = require('proxyquire').noCallThru();
55
const configStub = require('../../utils/config-stub');
6-
const setupEnv = require('../../utils/env');
6+
const {setupTestFolder, cleanupTestFolders} = require('../../utils/test-folder');
77
const Promise = require('bluebird');
88
const path = require('path');
99
const fs = require('fs-extra');
@@ -13,6 +13,10 @@ const errors = require('../../../lib/errors');
1313
const Instance = require('../../../lib/instance');
1414

1515
describe('Unit: Commands > Update', function () {
16+
after(() => {
17+
cleanupTestFolders();
18+
});
19+
1620
it('configureOptions adds setup & doctor options', function () {
1721
const superStub = sinon.stub().returnsArg(1);
1822
const doctorStub = sinon.stub().returnsArg(1);
@@ -339,7 +343,7 @@ describe('Unit: Commands > Update', function () {
339343
'../tasks/yarn-install': yarnInstallStub
340344
});
341345
const instance = new UpdateCommand({}, {});
342-
const env = setupEnv();
346+
const env = setupTestFolder();
343347
const ctx = {
344348
installPath: path.join(env.dir, 'versions/1.0.0'),
345349
version: '1.0.0'
@@ -349,7 +353,6 @@ describe('Unit: Commands > Update', function () {
349353
return instance.downloadAndUpdate(ctx, task).then(() => {
350354
expect(yarnInstallStub.calledOnce).to.be.true;
351355
expect(task.title).to.equal('Downloading and updating Ghost to v1.0.0');
352-
env.cleanup();
353356
});
354357
});
355358

@@ -363,7 +366,7 @@ describe('Unit: Commands > Update', function () {
363366
dirs: ['versions/1.0.0', 'versions/1.0.1'],
364367
links: [['versions/1.0.0', 'current']]
365368
};
366-
const env = setupEnv(envCfg);
369+
const env = setupTestFolder(envCfg);
367370
const ctx = {
368371
installPath: path.join(env.dir, 'versions/1.0.1'),
369372
force: false
@@ -376,8 +379,6 @@ describe('Unit: Commands > Update', function () {
376379
expect(fs.existsSync(ctx.installPath)).to.be.true;
377380
expect(yarnInstallStub.called).to.be.false;
378381
expect(task.skip.calledOnce).to.be.true;
379-
380-
env.cleanup();
381382
});
382383
});
383384

@@ -391,7 +392,7 @@ describe('Unit: Commands > Update', function () {
391392
dirs: ['versions/1.0.0', 'versions/1.0.1'],
392393
links: [['versions/1.0.0', 'current']]
393394
};
394-
const env = setupEnv(envCfg);
395+
const env = setupTestFolder(envCfg);
395396
const ctx = {
396397
installPath: path.join(env.dir, 'versions/1.0.1'),
397398
force: true
@@ -402,8 +403,6 @@ describe('Unit: Commands > Update', function () {
402403
return instance.downloadAndUpdate(ctx, {}).then(() => {
403404
expect(fs.existsSync(ctx.installPath)).to.be.false;
404405
expect(yarnInstallStub.calledOnce).to.be.true;
405-
406-
env.cleanup();
407406
});
408407
});
409408
});
@@ -480,7 +479,7 @@ describe('Unit: Commands > Update', function () {
480479
'versions/1.5.1',
481480
'versions/1.5.2'
482481
];
483-
const env = setupEnv({dirs: dirs});
482+
const env = setupTestFolder({dirs: dirs});
484483
const UpdateCommand = require(modulePath);
485484
const instance = new UpdateCommand({}, {});
486485
const cwdStub = sinon.stub(process, 'cwd').returns(env.dir);
@@ -493,8 +492,6 @@ describe('Unit: Commands > Update', function () {
493492
dirs.forEach((version) => {
494493
expect(fs.existsSync(path.join(env.dir, version))).to.be.true;
495494
});
496-
497-
env.cleanup();
498495
});
499496
});
500497

@@ -512,7 +509,7 @@ describe('Unit: Commands > Update', function () {
512509
'versions/1.5.0'
513510
]
514511
};
515-
const env = setupEnv(envCfg);
512+
const env = setupTestFolder(envCfg);
516513
const UpdateCommand = require(modulePath);
517514
const instance = new UpdateCommand({}, {});
518515
const cwdStub = sinon.stub(process, 'cwd').returns(env.dir);
@@ -540,8 +537,6 @@ describe('Unit: Commands > Update', function () {
540537
removedVersions.forEach((version) => {
541538
expect(fs.existsSync(path.join(env.dir, 'versions', version))).to.be.false;
542539
});
543-
544-
env.cleanup();
545540
});
546541
});
547542
});
@@ -665,7 +660,7 @@ describe('Unit: Commands > Update', function () {
665660
dirs: ['versions/1.0.0', 'versions/1.0.1'],
666661
links: [['versions/1.0.0', 'current']]
667662
}
668-
const env = setupEnv(envCfg);
663+
const env = setupTestFolder(envCfg);
669664
const cwdStub = sinon.stub(process, 'cwd').returns(env.dir);
670665
const config = configStub();
671666
config.get.withArgs('active-version').returns('1.0.0');
@@ -686,7 +681,6 @@ describe('Unit: Commands > Update', function () {
686681
expect(config.save.calledOnce).to.be.true;
687682

688683
cwdStub.restore();
689-
env.cleanup();
690684
});
691685

692686
it('does things correctly with rollback', function () {
@@ -695,7 +689,7 @@ describe('Unit: Commands > Update', function () {
695689
dirs: ['versions/1.0.0', 'versions/1.0.1'],
696690
links: [['versions/1.0.1', 'current']]
697691
}
698-
const env = setupEnv(envCfg);
692+
const env = setupTestFolder(envCfg);
699693
const cwdStub = sinon.stub(process, 'cwd').returns(env.dir);
700694
const config = configStub();
701695
config.get.withArgs('active-version').returns('1.0.1');
@@ -716,7 +710,6 @@ describe('Unit: Commands > Update', function () {
716710
expect(config.save.calledOnce).to.be.true;
717711

718712
cwdStub.restore();
719-
env.cleanup();
720713
});
721714
});
722715
});
+24-19
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
11
'use strict';
22
const expect = require('chai').expect;
33
const sinon = require('sinon');
4-
const setupEnv = require('../../utils/env');
4+
const {setupTestFolder, cleanupTestFolders} = require('../../utils/test-folder');
55
const fs = require('fs');
66
const path = require('path');
77

88
const ensureStructure = require('../../../lib/tasks/ensure-structure');
99

1010
describe('Unit: Tasks > ensure-structure', function () {
11-
const env = setupEnv();
12-
const cwdStub = sinon.stub(process, 'cwd').returns(env.dir);
11+
after(() => {
12+
cleanupTestFolders()
13+
});
1314

14-
ensureStructure();
15-
expect(cwdStub.calledOnce).to.be.true;
15+
it('works', function () {
16+
const env = setupTestFolder();
17+
const cwdStub = sinon.stub(process, 'cwd').returns(env.dir);
1618

17-
const expectedFiles = [
18-
'versions',
19-
'content/apps',
20-
'content/themes',
21-
'content/data',
22-
'content/images',
23-
'content/logs',
24-
'content/settings'
25-
];
19+
ensureStructure();
20+
expect(cwdStub.calledOnce).to.be.true;
2621

27-
expectedFiles.forEach((file) => {
28-
expect(fs.existsSync(path.join(env.dir, file))).to.be.true;
29-
});
22+
const expectedFiles = [
23+
'versions',
24+
'content/apps',
25+
'content/themes',
26+
'content/data',
27+
'content/images',
28+
'content/logs',
29+
'content/settings'
30+
];
3031

31-
cwdStub.restore();
32-
env.cleanup();
32+
expectedFiles.forEach((file) => {
33+
expect(fs.existsSync(path.join(env.dir, file))).to.be.true;
34+
});
35+
36+
cwdStub.restore();
37+
});
3338
});

test/unit/tasks/yarn-install-spec.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ const expect = require('chai').expect;
33
const sinon = require('sinon');
44
const proxyquire = require('proxyquire');
55
const Promise = require('bluebird');
6-
const setupEnv = require('../../utils/env');
6+
const {setupTestFolder, cleanupTestFolders} = require('../../utils/test-folder');
77
const path = require('path');
88
const fs = require('fs');
99

1010
const modulePath = '../../../lib/tasks/yarn-install';
1111
const errors = require('../../../lib/errors');
1212

1313
describe('Unit: Tasks > yarn-install', function () {
14+
after(() => {
15+
cleanupTestFolders();
16+
});
17+
1418
it('base function calls subtasks and yarn util', function () {
1519
const yarnStub = sinon.stub().resolves();
1620
const yarnInstall = proxyquire(modulePath, {
@@ -216,7 +220,7 @@ describe('Unit: Tasks > yarn-install', function () {
216220
});
217221

218222
it('creates dir, decompresses and maps files', function () {
219-
const env = setupEnv();
223+
const env = setupTestFolder();
220224
const downloadStub = sinon.stub().resolves({downloadedData: true});
221225
const shasumStub = sinon.stub().returns('asdf1234');
222226
const decompressStub = sinon.stub().resolves();

test/utils/acceptance-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const cp = require('child_process');
2727
const tmp = require('tmp');
2828
const find = require('lodash/find');
2929
const path = require('path');
30-
const env = require('./env');
30+
const {setupTestFolder} = require('./test-folder');
3131

3232
global.Promise = require('bluebird');
3333

@@ -41,7 +41,7 @@ module.exports = class AcceptanceTest {
4141
}
4242

4343
setup(type) {
44-
this.cleanupDir = env(type, this.dir).cleanup;
44+
this.cleanupDir = setupTestFolder(type, this.dir).cleanup;
4545
}
4646

4747
path(file) {

0 commit comments

Comments
 (0)