Skip to content

Commit

Permalink
chore: move "wait" to utils + re-use (#4384)
Browse files Browse the repository at this point in the history
* chore: move wait to utils

* chore: re-use wait

* simplify test utils exports

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
dnalborczyk and lukastaegert committed Feb 5, 2022
1 parent 56c1514 commit 33a4278
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 43 deletions.
5 changes: 2 additions & 3 deletions test/leak/index.js
@@ -1,6 +1,7 @@
const path = require('path');
const weak = require('weak-napi');
const rollup = require('../..');
const { wait } = require('../utils');

var shouldCollect = false;
var isCollected = false;
Expand All @@ -9,13 +10,11 @@ function onCollect() {
isCollected = true;
}

const wait = () => new Promise(resolve => setTimeout(resolve));

async function waitForGC() {
const startTime = process.hrtime();
do {
global.gc();
await wait();
await wait(0);
} while (!isCollected && process.hrtime(startTime)[0] < 3);
}

Expand Down
68 changes: 35 additions & 33 deletions test/utils.js
Expand Up @@ -14,29 +14,19 @@ const { platform, version } = require('process');
const fixturify = require('fixturify');
const { removeSync } = require('fs-extra');

exports.compareError = compareError;
exports.compareWarnings = compareWarnings;
exports.deindent = deindent;
exports.executeBundle = executeBundle;
exports.getObject = getObject;
exports.loader = loader;
exports.normaliseOutput = normaliseOutput;
exports.runTestSuiteWithSamples = runTestSuiteWithSamples;
exports.assertDirectoriesAreEqual = assertDirectoriesAreEqual;
exports.assertFilesAreEqual = assertFilesAreEqual;
exports.assertIncludes = assertIncludes;
exports.atomicWriteFileSync = atomicWriteFileSync;
exports.writeAndSync = writeAndSync;
exports.getFileNamesAndRemoveOutput = getFileNamesAndRemoveOutput;
exports.writeAndRetry = writeAndRetry;
exports.wait = function wait(ms) {
return new Promise(fulfil => {
setTimeout(fulfil, ms);
});
};

function normaliseError(error) {
delete error.stack;
delete error.toString;
return { ...error, message: error.message };
}

function compareError(actual, expected) {
exports.compareError = function compareError(actual, expected) {
actual = normaliseError(actual);

if (actual.parserError) {
Expand All @@ -52,9 +42,9 @@ function compareError(actual, expected) {
}

assert.deepEqual(actual, expected);
}
};

function compareWarnings(actual, expected) {
exports.compareWarnings = function compareWarnings(actual, expected) {
assert.deepEqual(
actual.map(warning => {
const clone = { ...warning };
Expand All @@ -73,13 +63,15 @@ function compareWarnings(actual, expected) {
return warning;
})
);
}
};

function deindent(str) {
return str.slice(1).replace(/^\t+/gm, '').replace(/\s+$/gm, '').trim();
}

async function executeBundle(bundle, require) {
exports.deindent = deindent;

exports.executeBundle = async function executeBundle(bundle, require) {
const {
output: [cjs]
} = await bundle.generate({
Expand All @@ -90,17 +82,17 @@ async function executeBundle(bundle, require) {
const module = { exports: {} };
wrapper(module, module.exports, require);
return module.exports;
}
};

function getObject(entries) {
exports.getObject = function getObject(entries) {
const object = {};
for (const [key, value] of entries) {
object[key] = value;
}
return object;
}
};

function loader(modules) {
exports.loader = function loader(modules) {
modules = Object.assign(Object.create(null), modules);
return {
resolveId(id) {
Expand All @@ -111,11 +103,11 @@ function loader(modules) {
return modules[id];
}
};
}
};

function normaliseOutput(code) {
exports.normaliseOutput = function normaliseOutput(code) {
return code.toString().trim().replace(/\r\n/g, '\n');
}
};

function runTestSuiteWithSamples(suiteName, samplesDir, runTest, onTeardown) {
describe(suiteName, () => runSamples(samplesDir, runTest, onTeardown));
Expand All @@ -130,6 +122,8 @@ runTestSuiteWithSamples.skip = function (suiteName) {
describe.skip(suiteName, () => {});
};

exports.runTestSuiteWithSamples = runTestSuiteWithSamples;

function runSamples(samplesDir, runTest, onTeardown) {
if (onTeardown) {
afterEach(onTeardown);
Expand Down Expand Up @@ -181,6 +175,8 @@ function getFileNamesAndRemoveOutput(dir) {
}
}

exports.getFileNamesAndRemoveOutput = getFileNamesAndRemoveOutput;

function loadConfigAndRunTest(dir, runTest) {
const configFile = join(dir, '_config.js');
const config = require(configFile);
Expand All @@ -196,7 +192,7 @@ function loadConfigAndRunTest(dir, runTest) {
}
}

function assertDirectoriesAreEqual(actualDir, expectedDir) {
exports.assertDirectoriesAreEqual = function assertDirectoriesAreEqual(actualDir, expectedDir) {
const actualFiles = fixturify.readSync(actualDir);

let expectedFiles;
Expand All @@ -206,7 +202,7 @@ function assertDirectoriesAreEqual(actualDir, expectedDir) {
expectedFiles = [];
}
assertFilesAreEqual(actualFiles, expectedFiles);
}
};

function assertFilesAreEqual(actualFiles, expectedFiles, dirs = []) {
Object.keys({ ...actualFiles, ...expectedFiles }).forEach(fileName => {
Expand All @@ -223,7 +219,9 @@ function assertFilesAreEqual(actualFiles, expectedFiles, dirs = []) {
});
}

function assertIncludes(actual, expected) {
exports.assertFilesAreEqual = assertFilesAreEqual;

exports.assertIncludes = function assertIncludes(actual, expected) {
try {
assert.ok(
actual.includes(expected),
Expand All @@ -234,7 +232,7 @@ function assertIncludes(actual, expected) {
err.expected = expected;
throw err;
}
}
};

// Workaround a race condition in fs.writeFileSync that temporarily creates
// an empty file for a brief moment which may be read by rollup watch - even
Expand All @@ -245,13 +243,15 @@ function atomicWriteFileSync(filePath, contents) {
renameSync(stagingPath, filePath);
}

exports.atomicWriteFileSync = atomicWriteFileSync;

// It appears that on MacOS, it sometimes takes long for the file system to update
function writeAndSync(filePath, contents) {
exports.writeAndSync = function writeAndSync(filePath, contents) {
const file = openSync(filePath, 'w');
writeSync(file, contents);
fsyncSync(file);
closeSync(file);
}
};

// Sometimes, watchers on MacOS do not seem to fire. In those cases, it helps
// to write the same content again. This function returns a callback to stop
Expand All @@ -272,3 +272,5 @@ function writeAndRetry(filePath, contents) {
writeFile();
return () => clearTimeout(updateRetryTimeout);
}

exports.writeAndRetry = writeAndRetry;
8 changes: 1 addition & 7 deletions test/watch/index.js
Expand Up @@ -11,13 +11,7 @@ const { resolve } = require('path');
const { chdir, cwd, hrtime } = require('process');
const { copy, removeSync } = require('fs-extra');
const rollup = require('../../dist/rollup');
const { atomicWriteFileSync } = require('../utils');

function wait(ms) {
return new Promise(fulfil => {
setTimeout(fulfil, ms);
});
}
const { atomicWriteFileSync, wait } = require('../utils');

describe('rollup.watch', () => {
let watcher;
Expand Down

0 comments on commit 33a4278

Please sign in to comment.