Skip to content

Commit 181a63f

Browse files
Jasper De Moordevongovett
authored andcommitted
Use async FS in tests (#1437)
1 parent dc11325 commit 181a63f

30 files changed

Lines changed: 520 additions & 507 deletions

test/asset.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const assert = require('assert');
2-
const fs = require('fs');
2+
const fs = require('../src/utils/fs');
33
const path = require('path');
44
const Asset = require('../src/Asset');
55
const {bundle} = require('./utils');
@@ -28,7 +28,7 @@ describe('Asset', () => {
2828
outFile
2929
});
3030

31-
assert(fs.existsSync(__dirname, `/dist/${outFile}`));
31+
assert(await fs.exists(__dirname, `/dist/${outFile}`));
3232
});
3333

3434
it('should have backward compatibility for package field', function() {

test/autoinstall.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
const assert = require('assert');
22
const install = require('../src/utils/installPackage');
3-
const fs = require('fs');
4-
const rimraf = require('rimraf');
5-
const promisify = require('../src/utils/promisify');
6-
const primraf = promisify(rimraf);
7-
const ncp = promisify(require('ncp'));
3+
const fs = require('../src/utils/fs');
4+
const {ncp, rimraf} = require('./utils');
85
const inputDirPath = __dirname + '/input';
96

107
describe('autoinstall', function() {
118
beforeEach(async function() {
129
// Setup (clear the input dir and move integration test in)
13-
await primraf(inputDirPath, {});
10+
await rimraf(inputDirPath, {});
1411
await ncp(__dirname + '/integration/babel-default', inputDirPath);
1512
});
1613

@@ -23,9 +20,9 @@ describe('autoinstall', function() {
2320
});
2421

2522
let expectedModulePath = inputDirPath + '/node_modules/' + pkgName;
26-
assert(fs.existsSync(expectedModulePath), 'lodash is in node_modules');
23+
assert(await fs.exists(expectedModulePath), 'lodash is in node_modules');
2724

28-
let pkg = fs.readFileSync(inputDirPath + '/package.json');
25+
let pkg = await fs.readFile(inputDirPath + '/package.json');
2926
pkg = JSON.parse(pkg);
3027
assert(pkg.devDependencies[pkgName], 'lodash is saved as a dev dep');
3128
});
@@ -39,14 +36,14 @@ describe('autoinstall', function() {
3936
});
4037

4138
let expectedModulePath = inputDirPath + '/node_modules/' + pkgName;
42-
assert(fs.existsSync(expectedModulePath), 'lodash is in node_modules');
39+
assert(await fs.exists(expectedModulePath), 'lodash is in node_modules');
4340

44-
let pkg = fs.readFileSync(inputDirPath + '/package.json');
41+
let pkg = await fs.readFile(inputDirPath + '/package.json');
4542
pkg = JSON.parse(pkg);
4643
assert(pkg.devDependencies[pkgName], 'lodash is saved as a dev dep');
4744
});
4845

4946
afterEach(async function() {
50-
await primraf(inputDirPath);
47+
await rimraf(inputDirPath);
5148
});
5249
});

test/bundler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('bundler', function() {
5656
__dirname + '/integration/multi-entry/two.html'
5757
]);
5858

59-
assertBundleTree(b, [
59+
await assertBundleTree(b, [
6060
{
6161
type: 'html',
6262
assets: ['one.html'],
@@ -78,7 +78,7 @@ describe('bundler', function() {
7878
it('should support multiple entry points as a glob', async function() {
7979
let b = await bundle(__dirname + '/integration/multi-entry/*.html');
8080

81-
assertBundleTree(b, [
81+
await assertBundleTree(b, [
8282
{
8383
type: 'html',
8484
assets: ['one.html'],

test/contentHashing.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
const assert = require('assert');
2-
const fs = require('fs');
3-
const {bundle} = require('./utils');
4-
const rimraf = require('rimraf');
5-
const promisify = require('../src/utils/promisify');
6-
const ncp = promisify(require('ncp'));
2+
const fs = require('../src/utils/fs');
3+
const {bundle, rimraf, ncp} = require('./utils');
74

85
describe('content hashing', function() {
9-
beforeEach(function() {
10-
rimraf.sync(__dirname + '/input');
6+
beforeEach(async function() {
7+
await rimraf(__dirname + '/input');
118
});
129

1310
it('should update content hash when content changes', async function() {
@@ -17,13 +14,13 @@ describe('content hashing', function() {
1714
production: true
1815
});
1916

20-
let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
17+
let html = await fs.readFile(__dirname + '/dist/index.html', 'utf8');
2118
let filename = html.match(
2219
/<link rel="stylesheet" href="[/\\]{1}(input\.[a-f0-9]+\.css)">/
2320
)[1];
24-
assert(fs.existsSync(__dirname + '/dist/' + filename));
21+
assert(await fs.exists(__dirname + '/dist/' + filename));
2522

26-
fs.writeFileSync(
23+
await fs.writeFile(
2724
__dirname + '/input/index.css',
2825
'body { background: green }'
2926
);
@@ -32,11 +29,11 @@ describe('content hashing', function() {
3229
production: true
3330
});
3431

35-
html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
32+
html = await fs.readFile(__dirname + '/dist/index.html', 'utf8');
3633
let newFilename = html.match(
3734
/<link rel="stylesheet" href="[/\\]{1}(input\.[a-f0-9]+\.css)">/
3835
)[1];
39-
assert(fs.existsSync(__dirname + '/dist/' + newFilename));
36+
assert(await fs.exists(__dirname + '/dist/' + newFilename));
4037

4138
assert.notEqual(filename, newFilename);
4239
});
@@ -48,19 +45,19 @@ describe('content hashing', function() {
4845
production: true
4946
});
5047

51-
let js = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
48+
let js = await fs.readFile(__dirname + '/dist/index.js', 'utf8');
5249
let filename = js.match(/\/(test\.[0-9a-f]+\.txt)/)[1];
53-
assert(fs.existsSync(__dirname + '/dist/' + filename));
50+
assert(await fs.exists(__dirname + '/dist/' + filename));
5451

55-
fs.writeFileSync(__dirname + '/input/test.txt', 'hello world');
52+
await fs.writeFile(__dirname + '/input/test.txt', 'hello world');
5653

5754
await bundle(__dirname + '/input/index.js', {
5855
production: true
5956
});
6057

61-
js = fs.readFileSync(__dirname + '/dist/index.js', 'utf8');
58+
js = await fs.readFile(__dirname + '/dist/index.js', 'utf8');
6259
let newFilename = js.match(/\/(test\.[0-9a-f]+\.txt)/)[1];
63-
assert(fs.existsSync(__dirname + '/dist/' + newFilename));
60+
assert(await fs.exists(__dirname + '/dist/' + newFilename));
6461

6562
assert.notEqual(filename, newFilename);
6663
});

test/css.js

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
const assert = require('assert');
2-
const fs = require('fs');
3-
const {bundle, run, assertBundleTree} = require('./utils');
4-
const promisify = require('../src/utils/promisify');
5-
const ncp = promisify(require('ncp'));
6-
const rimraf = require('rimraf');
2+
const fs = require('../src/utils/fs');
3+
const {bundle, run, assertBundleTree, rimraf, ncp} = require('./utils');
74

85
describe('css', function() {
96
it('should produce two bundles when importing a CSS file', async function() {
107
let b = await bundle(__dirname + '/integration/css/index.js');
118

12-
assertBundleTree(b, {
9+
await assertBundleTree(b, {
1310
name: 'index.js',
1411
assets: ['index.js', 'index.css', 'local.js', 'local.css'],
1512
childBundles: [
@@ -24,15 +21,15 @@ describe('css', function() {
2421
]
2522
});
2623

27-
let output = run(b);
24+
let output = await run(b);
2825
assert.equal(typeof output, 'function');
2926
assert.equal(output(), 3);
3027
});
3128

3229
it('should support loading a CSS bundle along side dynamic imports', async function() {
3330
let b = await bundle(__dirname + '/integration/dynamic-css/index.js');
3431

35-
assertBundleTree(b, {
32+
await assertBundleTree(b, {
3633
name: 'index.js',
3734
assets: [
3835
'index.js',
@@ -68,15 +65,15 @@ describe('css', function() {
6865
]
6966
});
7067

71-
let output = run(b);
68+
let output = await run(b);
7269
assert.equal(typeof output, 'function');
7370
assert.equal(await output(), 3);
7471
});
7572

7673
it('should support importing CSS from a CSS file', async function() {
7774
let b = await bundle(__dirname + '/integration/css-import/index.js');
7875

79-
assertBundleTree(b, {
76+
await assertBundleTree(b, {
8077
name: 'index.js',
8178
assets: ['index.js', 'index.css', 'other.css', 'local.css'],
8279
childBundles: [
@@ -92,11 +89,11 @@ describe('css', function() {
9289
]
9390
});
9491

95-
let output = run(b);
92+
let output = await run(b);
9693
assert.equal(typeof output, 'function');
9794
assert.equal(output(), 2);
9895

99-
let css = fs.readFileSync(__dirname + '/dist/index.css', 'utf8');
96+
let css = await fs.readFile(__dirname + '/dist/index.css', 'utf8');
10097
assert(css.includes('.local'));
10198
assert(css.includes('.other'));
10299
assert(/@media print {\s*.other/.test(css));
@@ -106,7 +103,7 @@ describe('css', function() {
106103
it('should support linking to assets with url() from CSS', async function() {
107104
let b = await bundle(__dirname + '/integration/css-url/index.js');
108105

109-
assertBundleTree(b, {
106+
await assertBundleTree(b, {
110107
name: 'index.js',
111108
assets: ['index.js', 'index.css'],
112109
childBundles: [
@@ -126,11 +123,11 @@ describe('css', function() {
126123
]
127124
});
128125

129-
let output = run(b);
126+
let output = await run(b);
130127
assert.equal(typeof output, 'function');
131128
assert.equal(output(), 2);
132129

133-
let css = fs.readFileSync(__dirname + '/dist/index.css', 'utf8');
130+
let css = await fs.readFile(__dirname + '/dist/index.css', 'utf8');
134131
assert(/url\("test\.[0-9a-f]+\.woff2"\)/.test(css));
135132
assert(css.includes('url("http://google.com")'));
136133
assert(css.includes('.index'));
@@ -140,7 +137,7 @@ describe('css', function() {
140137
assert(css.includes('.no-quote'));
141138

142139
assert(
143-
fs.existsSync(
140+
await fs.exists(
144141
__dirname + '/dist/' + css.match(/url\("(test\.[0-9a-f]+\.woff2)"\)/)[1]
145142
)
146143
);
@@ -151,7 +148,7 @@ describe('css', function() {
151148
production: true
152149
});
153150

154-
assertBundleTree(b, {
151+
await assertBundleTree(b, {
155152
name: 'index.js',
156153
assets: ['index.js', 'index.css'],
157154
childBundles: [
@@ -171,11 +168,11 @@ describe('css', function() {
171168
]
172169
});
173170

174-
let output = run(b);
171+
let output = await run(b);
175172
assert.equal(typeof output, 'function');
176173
assert.equal(output(), 2);
177174

178-
let css = fs.readFileSync(__dirname + '/dist/index.css', 'utf8');
175+
let css = await fs.readFile(__dirname + '/dist/index.css', 'utf8');
179176
assert(/url\(test\.[0-9a-f]+\.woff2\)/.test(css), 'woff ext found in css');
180177
assert(css.includes('url(http://google.com)'), 'url() found');
181178
assert(css.includes('.index'), '.index found');
@@ -185,7 +182,7 @@ describe('css', function() {
185182
assert(css.includes('.no-quote'));
186183

187184
assert(
188-
fs.existsSync(
185+
await fs.exists(
189186
__dirname + '/dist/' + css.match(/url\((test\.[0-9a-f]+\.woff2)\)/)[1]
190187
)
191188
);
@@ -194,7 +191,7 @@ describe('css', function() {
194191
it('should support transforming with postcss', async function() {
195192
let b = await bundle(__dirname + '/integration/postcss/index.js');
196193

197-
assertBundleTree(b, {
194+
await assertBundleTree(b, {
198195
name: 'index.js',
199196
assets: ['index.js', 'index.css'],
200197
childBundles: [
@@ -209,15 +206,15 @@ describe('css', function() {
209206
]
210207
});
211208

212-
let output = run(b);
209+
let output = await run(b);
213210
assert.equal(typeof output, 'function');
214211

215212
let value = output();
216213
assert(/_index_[0-9a-z]+_1/.test(value));
217214

218215
let cssClass = value.match(/(_index_[0-9a-z]+_1)/)[1];
219216

220-
let css = fs.readFileSync(__dirname + '/dist/index.css', 'utf8');
217+
let css = await fs.readFile(__dirname + '/dist/index.css', 'utf8');
221218
assert(css.includes(`.${cssClass}`));
222219
});
223220

@@ -226,18 +223,18 @@ describe('css', function() {
226223
production: true
227224
});
228225

229-
let output = run(b);
226+
let output = await run(b);
230227
assert.equal(typeof output, 'function');
231228
assert.equal(output(), 3);
232229

233-
let css = fs.readFileSync(__dirname + '/dist/index.css', 'utf8');
230+
let css = await fs.readFile(__dirname + '/dist/index.css', 'utf8');
234231
assert(css.includes('.local'));
235232
assert(css.includes('.index'));
236233
assert(!css.includes('\n'));
237234
});
238235

239236
it('should automatically install postcss plugins with npm if needed', async function() {
240-
rimraf.sync(__dirname + '/input');
237+
await rimraf(__dirname + '/input');
241238
await ncp(__dirname + '/integration/autoinstall/npm', __dirname + '/input');
242239
await bundle(__dirname + '/input/index.css');
243240

@@ -249,12 +246,12 @@ describe('css', function() {
249246
assert(pkg.devDependencies['caniuse-lite']);
250247

251248
// cssnext is applied
252-
let css = fs.readFileSync(__dirname + '/dist/index.css', 'utf8');
249+
let css = await fs.readFile(__dirname + '/dist/index.css', 'utf8');
253250
assert(css.includes('rgba'));
254251
});
255252

256253
it('should automatically install postcss plugins with yarn if needed', async function() {
257-
rimraf.sync(__dirname + '/input');
254+
await rimraf(__dirname + '/input');
258255
await ncp(
259256
__dirname + '/integration/autoinstall/yarn',
260257
__dirname + '/input'
@@ -269,11 +266,11 @@ describe('css', function() {
269266
assert(pkg.devDependencies['caniuse-lite']);
270267

271268
// appveyor is not currently writing to the yarn.lock file and will require further investigation
272-
// let lockfile = fs.readFileSync(__dirname + '/input/yarn.lock', 'utf8');
269+
// let lockfile = await fs.readFile(__dirname + '/input/yarn.lock', 'utf8');
273270
// assert(lockfile.includes('postcss-cssnext'));
274271

275272
// cssnext is applied
276-
let css = fs.readFileSync(__dirname + '/dist/index.css', 'utf8');
273+
let css = await fs.readFile(__dirname + '/dist/index.css', 'utf8');
277274
assert(css.includes('rgba'));
278275
});
279276
});

test/encodedURI.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const assert = require('assert');
2-
const fs = require('fs');
2+
const fs = require('../src/utils/fs');
33
const {bundle, assertBundleTree} = require('./utils');
44

55
describe('encodedURI', function() {
66
it('should support bundling files which names in encoded URI', async function() {
77
let b = await bundle(__dirname + '/integration/encodedURI/index.html');
88

9-
assertBundleTree(b, {
9+
await assertBundleTree(b, {
1010
name: 'index.html',
1111
assets: ['index.html'],
1212
childBundles: [
@@ -18,8 +18,8 @@ describe('encodedURI', function() {
1818
]
1919
});
2020

21-
let files = fs.readdirSync(__dirname + '/dist');
22-
let html = fs.readFileSync(__dirname + '/dist/index.html');
21+
let files = await fs.readdir(__dirname + '/dist');
22+
let html = await fs.readFile(__dirname + '/dist/index.html');
2323
for (let file of files) {
2424
if (file !== 'index.html') {
2525
assert(html.includes(file));

0 commit comments

Comments
 (0)