This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
Async-ified linterTests.ts #3338
Closed
JoshuaKGoldberg
wants to merge
1
commit into
palantir:master
from
JoshuaKGoldberg:linter-tests-async
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<string> { | ||
return await new Promise<string>((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<void> { | ||
await new Promise<void>((resolve, reject) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as the other PR: replace |
||
fs.writeFile(fileName, data, (error?: Error) => { | ||
if (error !== null) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
|
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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