Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use async FS in tests (#1437)
  • Loading branch information
Jasper De Moor authored and devongovett committed May 27, 2018
1 parent dc11325 commit 181a63f
Show file tree
Hide file tree
Showing 30 changed files with 520 additions and 507 deletions.
4 changes: 2 additions & 2 deletions test/asset.js
@@ -1,5 +1,5 @@
const assert = require('assert');
const fs = require('fs');
const fs = require('../src/utils/fs');
const path = require('path');
const Asset = require('../src/Asset');
const {bundle} = require('./utils');
Expand Down Expand Up @@ -28,7 +28,7 @@ describe('Asset', () => {
outFile
});

assert(fs.existsSync(__dirname, `/dist/${outFile}`));
assert(await fs.exists(__dirname, `/dist/${outFile}`));
});

it('should have backward compatibility for package field', function() {
Expand Down
19 changes: 8 additions & 11 deletions test/autoinstall.js
@@ -1,16 +1,13 @@
const assert = require('assert');
const install = require('../src/utils/installPackage');
const fs = require('fs');
const rimraf = require('rimraf');
const promisify = require('../src/utils/promisify');
const primraf = promisify(rimraf);
const ncp = promisify(require('ncp'));
const fs = require('../src/utils/fs');
const {ncp, rimraf} = require('./utils');
const inputDirPath = __dirname + '/input';

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

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

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

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

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

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

afterEach(async function() {
await primraf(inputDirPath);
await rimraf(inputDirPath);
});
});
4 changes: 2 additions & 2 deletions test/bundler.js
Expand Up @@ -56,7 +56,7 @@ describe('bundler', function() {
__dirname + '/integration/multi-entry/two.html'
]);

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

assertBundleTree(b, [
await assertBundleTree(b, [
{
type: 'html',
assets: ['one.html'],
Expand Down
31 changes: 14 additions & 17 deletions test/contentHashing.js
@@ -1,13 +1,10 @@
const assert = require('assert');
const fs = require('fs');
const {bundle} = require('./utils');
const rimraf = require('rimraf');
const promisify = require('../src/utils/promisify');
const ncp = promisify(require('ncp'));
const fs = require('../src/utils/fs');
const {bundle, rimraf, ncp} = require('./utils');

describe('content hashing', function() {
beforeEach(function() {
rimraf.sync(__dirname + '/input');
beforeEach(async function() {
await rimraf(__dirname + '/input');
});

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

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

fs.writeFileSync(
await fs.writeFile(
__dirname + '/input/index.css',
'body { background: green }'
);
Expand All @@ -32,11 +29,11 @@ describe('content hashing', function() {
production: true
});

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

assert.notEqual(filename, newFilename);
});
Expand All @@ -48,19 +45,19 @@ describe('content hashing', function() {
production: true
});

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

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

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

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

assert.notEqual(filename, newFilename);
});
Expand Down
57 changes: 27 additions & 30 deletions test/css.js
@@ -1,15 +1,12 @@
const assert = require('assert');
const fs = require('fs');
const {bundle, run, assertBundleTree} = require('./utils');
const promisify = require('../src/utils/promisify');
const ncp = promisify(require('ncp'));
const rimraf = require('rimraf');
const fs = require('../src/utils/fs');
const {bundle, run, assertBundleTree, rimraf, ncp} = require('./utils');

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

assertBundleTree(b, {
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'index.css', 'local.js', 'local.css'],
childBundles: [
Expand All @@ -24,15 +21,15 @@ describe('css', function() {
]
});

let output = run(b);
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(output(), 3);
});

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

assertBundleTree(b, {
await assertBundleTree(b, {
name: 'index.js',
assets: [
'index.js',
Expand Down Expand Up @@ -68,15 +65,15 @@ describe('css', function() {
]
});

let output = run(b);
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(await output(), 3);
});

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

assertBundleTree(b, {
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'index.css', 'other.css', 'local.css'],
childBundles: [
Expand All @@ -92,11 +89,11 @@ describe('css', function() {
]
});

let output = run(b);
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(output(), 2);

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

assertBundleTree(b, {
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'index.css'],
childBundles: [
Expand All @@ -126,11 +123,11 @@ describe('css', function() {
]
});

let output = run(b);
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(output(), 2);

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

assert(
fs.existsSync(
await fs.exists(
__dirname + '/dist/' + css.match(/url\("(test\.[0-9a-f]+\.woff2)"\)/)[1]
)
);
Expand All @@ -151,7 +148,7 @@ describe('css', function() {
production: true
});

assertBundleTree(b, {
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'index.css'],
childBundles: [
Expand All @@ -171,11 +168,11 @@ describe('css', function() {
]
});

let output = run(b);
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(output(), 2);

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

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

assertBundleTree(b, {
await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'index.css'],
childBundles: [
Expand All @@ -209,15 +206,15 @@ describe('css', function() {
]
});

let output = run(b);
let output = await run(b);
assert.equal(typeof output, 'function');

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

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

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

Expand All @@ -226,18 +223,18 @@ describe('css', function() {
production: true
});

let output = run(b);
let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(output(), 3);

let css = fs.readFileSync(__dirname + '/dist/index.css', 'utf8');
let css = await fs.readFile(__dirname + '/dist/index.css', 'utf8');
assert(css.includes('.local'));
assert(css.includes('.index'));
assert(!css.includes('\n'));
});

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

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

// cssnext is applied
let css = fs.readFileSync(__dirname + '/dist/index.css', 'utf8');
let css = await fs.readFile(__dirname + '/dist/index.css', 'utf8');
assert(css.includes('rgba'));
});

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

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

// cssnext is applied
let css = fs.readFileSync(__dirname + '/dist/index.css', 'utf8');
let css = await fs.readFile(__dirname + '/dist/index.css', 'utf8');
assert(css.includes('rgba'));
});
});
8 changes: 4 additions & 4 deletions test/encodedURI.js
@@ -1,12 +1,12 @@
const assert = require('assert');
const fs = require('fs');
const fs = require('../src/utils/fs');
const {bundle, assertBundleTree} = require('./utils');

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

assertBundleTree(b, {
await assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
Expand All @@ -18,8 +18,8 @@ describe('encodedURI', function() {
]
});

let files = fs.readdirSync(__dirname + '/dist');
let html = fs.readFileSync(__dirname + '/dist/index.html');
let files = await fs.readdir(__dirname + '/dist');
let html = await fs.readFile(__dirname + '/dist/index.html');
for (let file of files) {
if (file !== 'index.html') {
assert(html.includes(file));
Expand Down

0 comments on commit 181a63f

Please sign in to comment.