Skip to content

Commit

Permalink
Run prettier on test index.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Pratt committed Jun 2, 2020
1 parent 31491e0 commit 3bf9fa2
Showing 1 changed file with 119 additions and 86 deletions.
205 changes: 119 additions & 86 deletions test/index.js
Expand Up @@ -14,20 +14,23 @@ const webpackPkg = require('webpack/package.json');
const CaseSensitivePathsPlugin = require('../');

function webpackCompilerAtDir(dir, otherOpts, useBeforeEmitHook) {
const opts = Object.assign({
context: path.join(__dirname, 'fixtures', dir),
entry: './entry',
mode: 'development',
output: {
path: path.join(__dirname, 'js'),
filename: 'result.js',
const opts = Object.assign(
{
context: path.join(__dirname, 'fixtures', dir),
entry: './entry',
mode: 'development',
output: {
path: path.join(__dirname, 'js'),
filename: 'result.js',
},
plugins: [
new CaseSensitivePathsPlugin({
useBeforeEmitHook: useBeforeEmitHook,
}),
],
},
plugins: [
new CaseSensitivePathsPlugin({
useBeforeEmitHook: useBeforeEmitHook,
}),
],
}, otherOpts);
otherOpts,
);

if (webpackPkg.version[0] === '4') {
opts.mode = 'development';
Expand All @@ -37,8 +40,8 @@ function webpackCompilerAtDir(dir, otherOpts, useBeforeEmitHook) {
}

describe('CaseSensitivePathsPlugin', () => {
// This test will fail on case sensitive platforms, that's the whole point of this module.
// To ensure the rest of the library still works, disable *only this test.*
// These tests will fail on case sensitive platforms, that's the whole point of this module.
// To ensure the rest of the library still works, disable *only these tests.*
if (isPlatformCaseInsensitive) {
it('should compile and warn on wrong filename case', (done) => {
const compiler = webpackCompilerAtDir('wrong-case');
Expand Down Expand Up @@ -91,56 +94,77 @@ describe('CaseSensitivePathsPlugin', () => {
// For future reference: This test is somewhat of a race condition, these values seem to work well.
// If this test fails, sometimes just re-running will make it succeed.
it('should handle the deletion of a folder', (done) => {
const compiler = webpackCompilerAtDir('deleting-folder', { cache: false, watch: true });
const compiler = webpackCompilerAtDir('deleting-folder', {
cache: false,
watch: true,
});

// create folder and file to be deleted
const testFolder = path.join(__dirname, 'fixtures', 'deleting-folder', 'test-folder');
const testFolder = path.join(
__dirname,
'fixtures',
'deleting-folder',
'test-folder',
);
const testFile = path.join(testFolder, 'testfile.js');
if (!fs.existsSync(testFolder)) fs.mkdirSync(testFolder);
if (!fs.existsSync(testFile)) fs.writeFileSync(testFile, "module.exports = '';");
if (!fs.existsSync(testFile))
fs.writeFileSync(testFile, "module.exports = '';");

let watchCount = 0;
let resolved = false;
let jsonStats;
const watcher = compiler.watch({ poll: 500, aggregateTimeout: 500 }, (err, stats) => {
// We already detected the change and marked ourselves done, don't continue.
// Short circuits some intermittent test errors where this would be called again after
// test completion.
if (resolved) {
return;
}
const watcher = compiler.watch(
{ poll: 500, aggregateTimeout: 500 },
(err, stats) => {
// We already detected the change and marked ourselves done, don't continue.
// Short circuits some intermittent test errors where this would be called again after
// test completion.
if (resolved) {
return;
}

if (err) done(err);
watchCount += 1;

if (watchCount === 1) {
// First run should not error.
assert(!stats.hasErrors());
assert(!stats.hasWarnings());

setTimeout(() => {
// after initial compile delete test folder
fs.unlinkSync(testFile);
fs.rmdirSync(testFolder);
}, 500);
} else if (stats.hasErrors()) {
assert(!stats.hasWarnings());

jsonStats = stats.toJson();
assert.equal(jsonStats.errors.length, 1);

resolved = true;
watcher.close(done);
} else {
done(Error('Did not detect error when folder was deleted. Try rerunning the test.'));
}
});
if (err) done(err);
watchCount += 1;

if (watchCount === 1) {
// First run should not error.
assert(!stats.hasErrors());
assert(!stats.hasWarnings());

setTimeout(() => {
// after initial compile delete test folder
fs.unlinkSync(testFile);
fs.rmdirSync(testFolder);
}, 500);
} else if (stats.hasErrors()) {
assert(!stats.hasWarnings());

jsonStats = stats.toJson();
assert.equal(jsonStats.errors.length, 1);

resolved = true;
watcher.close(done);
} else {
done(
Error(
'Did not detect error when folder was deleted. Try rerunning the test.',
),
);
}
},
);
}).timeout(4000); // This sometimes times out.

it('should handle the creation of a new file', (done) => {
const compiler = webpackCompilerAtDir('file-creation');

const testFile = path.join(__dirname, 'fixtures', 'file-creation', 'testfile.js');
const testFile = path.join(
__dirname,
'fixtures',
'file-creation',
'testfile.js',
);
if (fs.existsSync(testFile)) fs.unlinkSync(testFile);

let compilationCount = 0;
Expand All @@ -156,13 +180,19 @@ describe('CaseSensitivePathsPlugin', () => {
if (error.message) error = error.message;

assert(stats.hasErrors());
assert(error.indexOf('Cannot resolve') !== -1 || error.indexOf('Can\'t resolve') !== -1);
assert(
error.indexOf('Cannot resolve') !== -1 ||
error.indexOf("Can't resolve") !== -1,
);
assert(!stats.hasWarnings());

fs.writeFileSync(testFile, 'module.exports = 0;');
} else if (compilationCount === 2) {
assert(fs.existsSync(testFile), 'Test file should exist');
assert(!stats.hasErrors(), `Should have no errors, but has: \n${stats.toJson().errors}`);
assert(
!stats.hasErrors(),
`Should have no errors, but has: \n${stats.toJson().errors}`,
);
assert(!stats.hasWarnings());

fs.unlinkSync(testFile);
Expand All @@ -177,41 +207,44 @@ describe('CaseSensitivePathsPlugin', () => {
it('should work with alternate fileSystems', (done) => {
let called = false;

webpack({
context: path.join(__dirname, 'fixtures', 'wrong-case'),
target: 'node',
output: {
path: path.join(__dirname, 'js'),
filename: 'result.js',
},
entry: './entry',
plugins: [
new CaseSensitivePathsPlugin(),
{
apply(compiler) {
let readdir;

const onCompile = () => {
readdir = readdir || compiler.inputFileSystem.readdir;
// eslint-disable-next-line no-param-reassign
compiler.inputFileSystem.readdir = function (p, cb) {
called = true;
fs.readdir(p, cb);
webpack(
{
context: path.join(__dirname, 'fixtures', 'wrong-case'),
target: 'node',
output: {
path: path.join(__dirname, 'js'),
filename: 'result.js',
},
entry: './entry',
plugins: [
new CaseSensitivePathsPlugin(),
{
apply(compiler) {
let readdir;

const onCompile = () => {
readdir = readdir || compiler.inputFileSystem.readdir;
// eslint-disable-next-line no-param-reassign
compiler.inputFileSystem.readdir = function(p, cb) {
called = true;
fs.readdir(p, cb);
};
};
};

if (compiler.hooks) {
compiler.hooks.compile.tap('test', onCompile);
} else {
compiler.plugin('compile', onCompile);
}
if (compiler.hooks) {
compiler.hooks.compile.tap('test', onCompile);
} else {
compiler.plugin('compile', onCompile);
}
},
},
},
],
}, (err) => {
if (err) done(err);
assert(called, 'should use compiler fs');
done();
});
],
},
(err) => {
if (err) done(err);
assert(called, 'should use compiler fs');
done();
},
);
});
});

0 comments on commit 3bf9fa2

Please sign in to comment.