Skip to content

Commit

Permalink
refactor: use fs-extra.readFileSync
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Dec 26, 2021
1 parent 2a899d5 commit dc21e7e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
53 changes: 41 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -88,6 +88,7 @@
"eslint-plugin-prettier": "^4.0.0",
"execa": "^5.1.1",
"fixturify": "^2.1.1",
"fs-extra": "^10.0.0",
"hash.js": "^1.1.7",
"husky": "^7.0.4",
"is-reference": "^3.0.0",
Expand Down
3 changes: 2 additions & 1 deletion test/cli/index.js
@@ -1,6 +1,7 @@
const assert = require('assert');
const { exec } = require('child_process');
const path = require('path');
const { readFileSync } = require('fs-extra');
const sander = require('sander');
const {
normaliseOutput,
Expand Down Expand Up @@ -109,7 +110,7 @@ runTestSuiteWithSamples(
done(err);
}
} else {
const expected = sander.readFileSync('_expected.js').toString();
const expected = readFileSync('_expected.js', 'utf8');
try {
assert.equal(normaliseOutput(code), normaliseOutput(expected));
done();
Expand Down
9 changes: 5 additions & 4 deletions test/form/index.js
@@ -1,5 +1,6 @@
const assert = require('assert');
const path = require('path');
const { readFileSync } = require('fs-extra');
const sander = require('sander');
const rollup = require('../../dist/rollup');
const { normaliseOutput, runTestSuiteWithSamples } = require('../utils.js');
Expand Down Expand Up @@ -79,19 +80,19 @@ runTestSuiteWithSamples('form', path.resolve(__dirname, 'samples'), (dir, config

async function generateAndTestBundle(bundle, outputOptions, expectedFile, { show }) {
await bundle.write(outputOptions);
const actualCode = normaliseOutput(sander.readFileSync(outputOptions.file));
const actualCode = normaliseOutput(readFileSync(outputOptions.file, 'utf8'));
let expectedCode;
let actualMap;
let expectedMap;

try {
expectedCode = normaliseOutput(sander.readFileSync(expectedFile));
expectedCode = normaliseOutput(readFileSync(expectedFile, 'utf8'));
} catch (err) {
expectedCode = 'missing file';
}

try {
actualMap = JSON.parse(sander.readFileSync(outputOptions.file + '.map').toString());
actualMap = JSON.parse(readFileSync(outputOptions.file + '.map', 'utf8'));
actualMap.sourcesContent = actualMap.sourcesContent
? actualMap.sourcesContent.map(normaliseOutput)
: null;
Expand All @@ -100,7 +101,7 @@ async function generateAndTestBundle(bundle, outputOptions, expectedFile, { show
}

try {
expectedMap = JSON.parse(sander.readFileSync(expectedFile + '.map').toString());
expectedMap = JSON.parse(readFileSync(expectedFile + '.map', 'utf8'));
expectedMap.sourcesContent = actualMap.sourcesContent
? expectedMap.sourcesContent.map(normaliseOutput)
: null;
Expand Down
16 changes: 6 additions & 10 deletions test/watch/index.js
@@ -1,5 +1,6 @@
const assert = require('assert');
const path = require('path');
const { readFileSync } = require('fs-extra');
const sander = require('sander');
const rollup = require('../../dist/rollup');

Expand Down Expand Up @@ -1083,9 +1084,7 @@ describe('rollup.watch', () => {
'BUNDLE_END',
'END',
() => {
const generated = sander.readFileSync('test/_tmp/output/bundle.js', {
encoding: 'utf-8'
});
const generated = readFileSync('test/_tmp/output/bundle.js', 'utf8');
assert.ok(/jQuery/.test(generated));
}
]);
Expand Down Expand Up @@ -1505,7 +1504,7 @@ describe('rollup.watch', () => {
plugins: {
load() {
this.addWatchFile(WATCHED_ID);
return `export default "${sander.readFileSync(WATCHED_ID).toString().trim()}"`;
return `export default "${readFileSync(WATCHED_ID, 'utf8').trim()}"`;
}
}
});
Expand Down Expand Up @@ -1562,10 +1561,7 @@ describe('rollup.watch', () => {
if (addWatchFile) {
this.addWatchFile(WATCHED_ID);
}
return `export const value = "${sander
.readFileSync(WATCHED_ID)
.toString()
.trim()}"`;
return `export const value = "${readFileSync(WATCHED_ID, 'utf8').trim()}"`;
}
}
}
Expand Down Expand Up @@ -1613,7 +1609,7 @@ describe('rollup.watch', () => {
transform(code, id) {
if (id.endsWith('dep1.js')) {
this.addWatchFile(path.resolve('test/_tmp/input/dep2.js'));
const text = sander.readFileSync('test/_tmp/input/dep2.js').toString().trim();
const text = readFileSync('test/_tmp/input/dep2.js', 'utf8').trim();
return `export default ${JSON.stringify(text)}`;
}
}
Expand Down Expand Up @@ -1800,7 +1796,7 @@ describe('rollup.watch', () => {
transform() {
transformRuns++;
this.addWatchFile(WATCHED_ID);
return `export default "${sander.readFileSync(WATCHED_ID).toString().trim()}"`;
return `export default "${readFileSync(WATCHED_ID, 'utf8').trim()}"`;
}
}
});
Expand Down

0 comments on commit dc21e7e

Please sign in to comment.