@twilio-labs/languagetool-cli
Version:
Run LanguageTool for linting Markdown files during CI
94 lines (92 loc) • 2.85 kB
JavaScript
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { loadFiles } from "../lib/files.js";
import { ReportStats, } from "../lib/types.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export function getMarkdownFixturePath(name = "1") {
return path.resolve(__dirname, "fixtures", "markdown", name + ".md");
}
export function getPatchFixture(name = "1") {
const fullPath = path.resolve(__dirname, "fixtures", "patches", name + ".patch");
return fs.readFileSync(fullPath, { encoding: "utf8" });
}
export function getJSONFixture(name) {
const fullPath = path.resolve(__dirname, "fixtures", name);
const contents = fs.readFileSync(fullPath, { encoding: "utf8" });
return JSON.parse(contents);
}
export async function getFakeResult() {
const markdownPaths = [getMarkdownFixturePath()];
const markdown = (await loadFiles(markdownPaths))[0];
const fakeMatch = {
context: {
length: 3,
offset: 12,
text: "The word is foo.",
},
contextForSureMatch: 0,
ignoreForIncompleteSentence: false,
length: 3,
offset: 12,
message: "Consider a different word.",
replacements: [{ value: "bar" }],
rule: {
category: { id: "ID_CATEGORY", name: "Who Knows?" },
description: "Something something description",
id: "ID_STRING",
issueType: "who knows",
},
sentence: "The word is foo.",
shortMessage: "Bad word",
type: { typeName: "TYPE_NAME" },
};
const fakeResult = {
contents: markdown.contents,
path: "README.md",
matches: [],
annotatedText: undefined,
changedLines: [...markdown.contents.split("\n").keys()],
};
const fakeOptions = {
_: ["README.md"],
githubpr: "",
"pr-diff-only": false,
"max-pr-suggestions": 5,
"max-replacements": 5,
"custom-dict-file": "",
};
const fakeStats = new ReportStats();
const fakeItem = {
column: 13,
contextHighlighted: "foo",
contextPrefix: "The word is ",
contextPostfix: ".",
currentLine: "The word is foo.",
line: 1,
match: fakeMatch,
message: "Consider a different word.",
replacements: ["bar"],
result: fakeResult,
suggestedLine: "The word is bar.",
};
return {
markdown,
fakeItem,
fakeMatch,
fakeOptions,
fakeResult,
fakeStats,
};
}
export const MARKDOWN_RESPONSE = `**README.md** \`(1,13)\`
Consider a different word. \`foo\`
\`\`\`diff
- The word is foo.
+ The word is bar.
\`\`\`
**Suggestion(s):** \`bar\`
---
`;
//# sourceMappingURL=testUtilities.js.map