Skip to content

Commit

Permalink
attempt to make tests more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 5, 2021
1 parent 4c4cf22 commit 6a995fb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
6 changes: 4 additions & 2 deletions lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,15 @@ export function createChannel(streamIn: StreamIn): StreamOut {

case 'resolve': {
let callback = pluginCallbacks.get(request.key);
sendResponse(id, await callback!(request) as any);
if (!callback) sendResponse(id, {});
else sendResponse(id, await callback!(request) as any);
break;
}

case 'load': {
let callback = pluginCallbacks.get(request.key);
sendResponse(id, await callback!(request) as any);
if (!callback) sendResponse(id, {});
else sendResponse(id, await callback!(request) as any);
break;
}

Expand Down
9 changes: 9 additions & 0 deletions scripts/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ module.exports = ${JSON.stringify(exit0Map, null, 2)};
await goBuildPromise;
}

// Writing a file atomically is important for watch mode tests since we don't
// want to read the file after it has been truncated but before the new contents
// have been written.
exports.writeFileAtomic = (where, contents) => {
const file = path.join(os.tmpdir(), 'esbuild-atomic-file-' + Math.random().toString(36).slice(2))
fs.writeFileSync(file, contents)
fs.renameSync(file, where)
}

exports.buildBinary = () => {
childProcess.execFileSync('go', ['build', '-ldflags=-s -w', './cmd/esbuild'], { cwd: repoDir, stdio: 'ignore' })
return path.join(repoDir, process.platform === 'win32' ? 'esbuild.exe' : 'esbuild')
Expand Down
16 changes: 8 additions & 8 deletions scripts/js-api-tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { installForTests, removeRecursiveSync } = require('./esbuild')
const { installForTests, removeRecursiveSync, writeFileAtomic } = require('./esbuild')
const { SourceMapConsumer } = require('source-map')
const assert = require('assert')
const path = require('path')
Expand Down Expand Up @@ -1655,7 +1655,7 @@ let watchTests = {
// First rebuild: edit
{
const [error2, result2] = await rebuildUntil(
() => writeFileAsync(input, `throw 2`),
() => writeFileAtomic(input, `throw 2`),
() => fs.readFileSync(outfile, 'utf8') === 'throw 2;\n',
)
assert.strictEqual(error2, null)
Expand All @@ -1666,7 +1666,7 @@ let watchTests = {
// Second rebuild: edit
{
const [error2, result2] = await rebuildUntil(
() => writeFileAsync(input, `throw 3`),
() => writeFileAtomic(input, `throw 3`),
() => fs.readFileSync(outfile, 'utf8') === 'throw 3;\n',
)
assert.strictEqual(error2, null)
Expand All @@ -1677,7 +1677,7 @@ let watchTests = {
// Third rebuild: syntax error
{
const [error2, result2] = await rebuildUntil(
() => writeFileAsync(input, `throw 1 2`),
() => writeFileAtomic(input, `throw 1 2`),
err => err,
)
assert.notStrictEqual(error2, null)
Expand All @@ -1691,7 +1691,7 @@ let watchTests = {
// Fourth rebuild: edit
{
const [error2, result2] = await rebuildUntil(
() => writeFileAsync(input, `throw 4`),
() => writeFileAtomic(input, `throw 4`),
() => fs.readFileSync(outfile, 'utf8') === 'throw 4;\n',
)
assert.strictEqual(error2, null)
Expand All @@ -1715,7 +1715,7 @@ let watchTests = {
// Sixth rebuild: restore
{
const [error2, result2] = await rebuildUntil(
() => writeFileAsync(input, `throw 5`),
() => writeFileAtomic(input, `throw 5`),
() => fs.readFileSync(outfile, 'utf8') === 'throw 5;\n',
)
assert.strictEqual(error2, null)
Expand Down Expand Up @@ -1770,7 +1770,7 @@ let watchTests = {
// First rebuild: edit
{
const [error2, result2] = await rebuildUntil(
() => writeFileAsync(input, `throw 2`),
() => writeFileAtomic(input, `throw 2`),
(err, res) => res.outputFiles[0].text === 'throw 2;\n',
)
assert.strictEqual(error2, null)
Expand All @@ -1781,7 +1781,7 @@ let watchTests = {
// Second rebuild: edit
{
const [error2, result2] = await rebuildUntil(
() => writeFileAsync(input, `throw 3`),
() => writeFileAtomic(input, `throw 3`),
(err, res) => res.outputFiles[0].text === 'throw 3;\n',
)
assert.strictEqual(error2, null)
Expand Down
4 changes: 2 additions & 2 deletions scripts/plugin-tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { installForTests, removeRecursiveSync } = require('./esbuild')
const { installForTests, removeRecursiveSync, writeFileAtomic } = require('./esbuild')
const assert = require('assert')
const path = require('path')
const util = require('util')
Expand Down Expand Up @@ -1338,7 +1338,7 @@ let pluginTests = {
// First rebuild: edit
{
const [error2, result2] = await rebuildUntil(
() => writeFileAsync(example, `export let x = 2`),
() => writeFileAtomic(example, `export let x = 2`),
() => fs.readFileSync(outfile, 'utf8') !== code,
)
code = await readFileAsync(outfile, 'utf8')
Expand Down

0 comments on commit 6a995fb

Please sign in to comment.