From b7c89658a034a42c7893a65157ce2959eaef74a2 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 17 Oct 2017 11:15:05 -0700 Subject: [PATCH] Async-ified linterTests.ts --- src/utils.ts | 26 ++++++++++++++++++++++++++ test/linterTests.ts | 16 +++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 65ef068a3df..98d4db04b89 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -15,6 +15,8 @@ * limitations under the License. */ +import * as fs from "fs"; + /** * Enforces the invariant that the input is an array. */ @@ -213,3 +215,27 @@ export function detectBufferEncoding(buffer: Buffer, length = buffer.length): En export function denormalizeWinPath(path: string): string { return path.replace(/\\/g, "/"); } + +export async function readFileAsync(fileName: string, encoding: string): Promise { + return await new Promise((resolve, reject) => { + fs.readFile(fileName, encoding, (error: Error | null, contents: string) => { + if (error !== null) { + reject(error); + } else { + resolve(contents); + } + }); + }); +} + +export async function writeFileAsync(fileName: string, data: string): Promise { + await new Promise((resolve, reject) => { + fs.writeFile(fileName, data, (error?: Error) => { + if (error !== null) { + reject(error); + } else { + resolve(); + } + }); + }); +} diff --git a/test/linterTests.ts b/test/linterTests.ts index d35fa4b2421..5c8edfc500c 100644 --- a/test/linterTests.ts +++ b/test/linterTests.ts @@ -16,9 +16,9 @@ */ import { assert } from "chai"; -import * as fs from "fs"; import { createSourceFile, ScriptTarget } from "typescript"; import { Replacement, RuleFailure } from "../src/language/rule/rule"; +import { readFileAsync, writeFileAsync } from "../src/utils"; import { createTempFile } from "./utils"; import Linter = require("../src/linter"); @@ -51,17 +51,23 @@ const templateDeclarationFixed = describe("Linter", () => { - it("apply fixes to correct files", () => { + it("apply fixes to correct files", async () => { const linter = new TestLinter({ fix: true }); const componentFile = createTempFile("ts"); const templateFile = createTempFile("ts"); - fs.writeFileSync(componentFile, componentDeclaration(templateFile)); - fs.writeFileSync(templateFile, templateDeclaration); + + await Promise.all([ + writeFileAsync(componentFile, componentDeclaration(templateFile)), + writeFileAsync(templateFile, templateDeclaration), + ]); + const sourceFile = createSourceFile(templateFile, `${templateDeclaration}`, ScriptTarget.ES2015); const replacement = new Replacement(6, 9, ""); const failure = new RuleFailure(sourceFile, 6, 15, "Declaration doesn't exist", "foo-bar", replacement); linter.applyFixesHelper(componentFile, componentDeclaration(templateFile), [failure]); - assert.equal(fs.readFileSync(templateFile, "utf-8"), templateDeclarationFixed); + + const actualTemplateFileContents = await readFileAsync(templateFile, "utf-8"); + assert.equal(actualTemplateFileContents, templateDeclarationFixed); }); });