Skip to content
Merged
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
66 changes: 33 additions & 33 deletions __tests__/batch-lint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ describe("batchLint", () => {
await rm(tmpDir, { recursive: true, force: true });
});

test("returns empty array when no files are provided", async () => {
const result = await batchLint(2, [], false, false, RULES_NO_EMPTY_LIST);
expect(result).toEqual([]);
test("returns empty result when no files are provided", async () => {
const result = await batchLint(2, [], false, RULES_NO_EMPTY_LIST);
expect(result).toEqual({ allResults: [], actionableResults: [] });
});

describe("路径 payload", () => {
Expand All @@ -83,16 +83,18 @@ describe("batchLint", () => {
await writeFile(fileA, TRIGGER_CONTENT, "utf8");
await writeFile(fileB, TRIGGER_CONTENT, "utf8");

const result = await batchLint(
const { actionableResults } = await batchLint(
2,
[fileA, fileB],
false,
false,
RULES_NO_EMPTY_LIST
);

expect(result.map((item) => item.path)).toEqual([fileA, fileB]);
result.forEach((item) => {
expect(actionableResults.map((item) => item.path)).toEqual([
fileA,
fileB,
]);
actionableResults.forEach((item) => {
expect(Array.isArray(item.lintResult)).toBe(true);
expect(item.lintResult.length).toBeGreaterThan(0);
expect(item.fixedResult == null).toBe(true);
Expand All @@ -103,17 +105,16 @@ describe("batchLint", () => {
const file = path.join(tmpDir, "read-in-worker.md");
await writeFile(file, TRIGGER_CONTENT, "utf8");

const result = await batchLint(
const { actionableResults } = await batchLint(
1,
[file],
false,
false,
RULES_NO_EMPTY_LIST
);

expect(result).toHaveLength(1);
expect(result[0].path).toBe(file);
expect(result[0].lintResult[0].name).toBe("no-empty-list");
expect(actionableResults).toHaveLength(1);
expect(actionableResults[0].path).toBe(file);
expect(actionableResults[0].lintResult[0].name).toBe("no-empty-list");
});
});

Expand All @@ -127,31 +128,29 @@ describe("batchLint", () => {
})
);

const result = await batchLint(
const { actionableResults } = await batchLint(
3,
files,
false,
false,
RULES_NO_EMPTY_LIST
);

expect(result).toHaveLength(fileCount);
expect(result.map((item) => item.path)).toEqual(files);
expect(actionableResults).toHaveLength(fileCount);
expect(actionableResults.map((item) => item.path)).toEqual(files);
});

test("threads greater than files does not error", async () => {
const file = path.join(tmpDir, "single.md");
await writeFile(file, TRIGGER_CONTENT, "utf8");

const result = await batchLint(
const { actionableResults } = await batchLint(
16,
[file],
false,
false,
RULES_NO_EMPTY_LIST
);

expect(result).toHaveLength(1);
expect(actionableResults).toHaveLength(1);
});
});

Expand All @@ -164,15 +163,18 @@ describe("batchLint", () => {
await writeFile(fileB, TRIGGER_CONTENT, "utf8");
await writeFile(fileC, TRIGGER_CONTENT, "utf8");

const result = await batchLint(
const { actionableResults } = await batchLint(
2,
[fileA, fileB, fileC],
false,
false,
RULES_NO_EMPTY_LIST
);

expect(result.map((item) => item.path)).toEqual([fileA, fileB, fileC]);
expect(actionableResults.map((item) => item.path)).toEqual([
fileA,
fileB,
fileC,
]);
});
});

Expand All @@ -182,7 +184,7 @@ describe("batchLint", () => {
await writeFile(file, "# Clean content\n", "utf8");

await expect(
batchLint(2, [file], false, false, RULES_NO_EMPTY_LIST)
batchLint(2, [file], false, RULES_NO_EMPTY_LIST)
).resolves.toBeDefined();
});

Expand All @@ -192,7 +194,7 @@ describe("batchLint", () => {

try {
await expect(
batchLint(1, [file], false, false, RULES_NO_EMPTY_LIST)
batchLint(1, [file], false, RULES_NO_EMPTY_LIST)
).rejects.toThrow();
expect(destroySpy).toHaveBeenCalled();
} finally {
Expand All @@ -206,33 +208,31 @@ describe("batchLint", () => {
const file = path.join(tmpDir, "fixable.md");
await writeFile(file, TRIGGER_CONTENT, "utf8");

const result = await batchLint(
const { actionableResults } = await batchLint(
1,
[file],
false,
true,
RULES_NO_EMPTY_LIST
);

expect(result).toHaveLength(1);
expect(result[0].fixedResult).not.toBeNull();
expect(result[0].fixedResult?.result).toBeDefined();
expect(actionableResults).toHaveLength(1);
expect(actionableResults[0].fixedResult).not.toBeNull();
expect(actionableResults[0].fixedResult?.result).toBeDefined();
});

test("does not return fixedResult when fix mode is disabled", async () => {
const file = path.join(tmpDir, "no-fix.md");
await writeFile(file, TRIGGER_CONTENT, "utf8");

const result = await batchLint(
const { actionableResults } = await batchLint(
1,
[file],
false,
false,
RULES_NO_EMPTY_LIST
);

expect(result).toHaveLength(1);
expect(result[0].fixedResult == null).toBe(true);
expect(actionableResults).toHaveLength(1);
expect(actionableResults[0].fixedResult == null).toBe(true);
});
});
});
Expand Down
91 changes: 91 additions & 0 deletions __tests__/keep-lint-item.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { keepLintItem } from "../src/utils/batch-lint";
import { FixConvergence } from "@lint-md/core";
import type { FixedResult } from "@lint-md/core";
import type { BatchLintItem } from "../src/types";

const baseItem = (
overrides: Partial<BatchLintItem> & {
convergence?: FixConvergence;
}
): BatchLintItem => {
const { convergence, ...rest } = overrides;
const fixedResult: FixedResult = {
result: "",
notAppliedFixes: [],
};
if (convergence !== undefined) {
fixedResult.convergence = convergence;
}
return {
path: "a.md",
lintResult: [],
fixedResult,
...rest,
};
};

describe("keepLintItem", () => {
test("keeps items with lint findings", () => {
expect(
keepLintItem(
baseItem({
lintResult: [
{
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 2 },
},
message: "x",
name: "r",
content: "x",
severity: 2 as any,
},
],
})
)
).toBe(true);
});

test("keeps items with unapplied fixes", () => {
expect(
keepLintItem(
baseItem({
fixedResult: {
result: "",
notAppliedFixes: [{ range: [0, 1], text: "x" }],
},
})
)
).toBe(true);
});

test("keeps items with cycle convergence so #98 warning has a target", () => {
expect(
keepLintItem(baseItem({ convergence: FixConvergence.CYCLE_DETECTED }))
).toBe(true);
});

test("keeps items with max convergence so #98 warning has a target", () => {
expect(
keepLintItem(baseItem({ convergence: FixConvergence.MAX_ROUNDS }))
).toBe(true);
});

test("drops items with stable convergence and no other signal", () => {
expect(keepLintItem(baseItem({ convergence: FixConvergence.STABLE }))).toBe(
false
);
});

test("drops items with fixedResult: null", () => {
expect(keepLintItem(baseItem({ fixedResult: null }))).toBe(false);
});

test("drops items with fixedResult: undefined", () => {
expect(keepLintItem(baseItem({ fixedResult: undefined }))).toBe(false);
});

test("treats pre-#182 cores (no convergence field) like the old behaviour", () => {
expect(keepLintItem(baseItem({}))).toBe(false);
});
});
Loading