Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Async-ified linterTests.ts #3338

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

import * as fs from "fs";

/**
* Enforces the invariant that the input is an array.
*/
Expand Down Expand Up @@ -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<string> {
return await new Promise<string>((resolve, reject) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as the other PR: remove the await here

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<void> {
await new Promise<void>((resolve, reject) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as the other PR: replace await with return

fs.writeFile(fileName, data, (error?: Error) => {
if (error !== null) {
reject(error);
} else {
resolve();
}
});
});
}
16 changes: 11 additions & 5 deletions test/linterTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one makes no sense to be async and awaited immediately

assert.equal(actualTemplateFileContents, templateDeclarationFixed);
});

});