forked from actions-rs/clippy-check
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eac7042
commit 5a19ca7
Showing
6 changed files
with
279 additions
and
28 deletions.
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,186 @@ | ||
import { OutputParser } from "outputParser"; | ||
import { type CargoMessage, type MaybeCargoMessage, type Stats } from "schema"; | ||
|
||
jest.mock("@actions/core"); | ||
|
||
describe("outputParser", () => { | ||
const emptyStats: Stats = { | ||
error: 0, | ||
warning: 0, | ||
note: 0, | ||
ice: 0, | ||
help: 0, | ||
}; | ||
|
||
const defaultMessage: CargoMessage = { | ||
reason: "compiler-message", | ||
message: { | ||
code: "code", | ||
message: "message", | ||
rendered: "rendered", | ||
level: "warning", | ||
spans: [ | ||
{ | ||
is_primary: true, | ||
column_start: 10, | ||
column_end: 15, | ||
line_start: 30, | ||
line_end: 30, | ||
file_name: "main.rs", | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
it("ignores invalid json", () => { | ||
const outputParser = new OutputParser(); | ||
|
||
outputParser.tryParseClippyLine("I am not valid json"); | ||
|
||
expect(outputParser.stats).toEqual(emptyStats); | ||
}); | ||
|
||
it("ignores non-compiler-messages", () => { | ||
const outputParser = new OutputParser(); | ||
|
||
const output: MaybeCargoMessage = { | ||
reason: "not-a-compiler-message", | ||
}; | ||
|
||
outputParser.tryParseClippyLine(JSON.stringify(output)); | ||
|
||
expect(outputParser.stats).toEqual(emptyStats); | ||
}); | ||
|
||
it("ignores when compiler-message doesn't have a code", () => { | ||
const outputParser = new OutputParser(); | ||
|
||
const output: MaybeCargoMessage = { | ||
reason: "compiler-message", | ||
message: { | ||
code: null, | ||
message: "", | ||
rendered: "", | ||
level: "", | ||
spans: [], | ||
}, | ||
}; | ||
|
||
outputParser.tryParseClippyLine(JSON.stringify(output)); | ||
|
||
expect(outputParser.stats).toEqual(emptyStats); | ||
}); | ||
|
||
test.each([ | ||
["help", undefined], | ||
["note", undefined], | ||
["warning", undefined], | ||
["error", undefined], | ||
["error: internal compiler error", "ice"], | ||
])("bumps %s when message level is %s", (level, test) => { | ||
const outputParser = new OutputParser(); | ||
|
||
const output: CargoMessage = { | ||
reason: defaultMessage.reason, | ||
message: { | ||
...defaultMessage.message, | ||
level, | ||
}, | ||
}; | ||
|
||
outputParser.tryParseClippyLine(JSON.stringify(output)); | ||
|
||
expect(outputParser.stats).toEqual({ ...emptyStats, [test ?? level]: 1 }); | ||
}); | ||
|
||
it("ignores when level is not help, note, warning, error, ice", () => { | ||
const outputParser = new OutputParser(); | ||
|
||
const output: CargoMessage = { | ||
reason: defaultMessage.reason, | ||
message: { | ||
...defaultMessage.message, | ||
level: "it's my birthday", | ||
}, | ||
}; | ||
|
||
outputParser.tryParseClippyLine(JSON.stringify(output)); | ||
|
||
expect(outputParser.stats).toEqual({ ...emptyStats }); | ||
}); | ||
|
||
it("ignores duplicate", () => { | ||
const outputParser = new OutputParser(); | ||
|
||
outputParser.tryParseClippyLine(JSON.stringify(defaultMessage)); | ||
outputParser.tryParseClippyLine(JSON.stringify(defaultMessage)); | ||
|
||
expect(outputParser.stats).toEqual({ ...emptyStats, [defaultMessage.message.level]: 1 }); | ||
}); | ||
|
||
it("fails when primary span cannot be found", () => { | ||
const outputParser = new OutputParser(); | ||
|
||
const output: CargoMessage = { | ||
reason: defaultMessage.reason, | ||
message: { | ||
...defaultMessage.message, | ||
spans: [], | ||
}, | ||
}; | ||
|
||
expect(() => { | ||
outputParser.tryParseClippyLine(JSON.stringify(output)); | ||
}).toThrow(/Unable to find primary span for message/); | ||
}); | ||
|
||
it("parses annotations into AnnotationWithMessageAndLevel", () => { | ||
const outputParser = new OutputParser(); | ||
|
||
outputParser.tryParseClippyLine( | ||
JSON.stringify({ | ||
reason: defaultMessage.reason, | ||
message: { | ||
...defaultMessage.message, | ||
level: "error", | ||
}, | ||
}), | ||
); | ||
outputParser.tryParseClippyLine( | ||
JSON.stringify({ | ||
reason: defaultMessage.reason, | ||
message: { | ||
...defaultMessage.message, | ||
level: "warning", | ||
}, | ||
}), | ||
); | ||
|
||
expect(outputParser.annotations).toEqual([ | ||
{ | ||
level: 0, | ||
message: "rendered", | ||
properties: { | ||
endColumn: 15, | ||
endLine: 30, | ||
file: "main.rs", | ||
startColumn: 10, | ||
startLine: 30, | ||
title: "message", | ||
}, | ||
}, | ||
{ | ||
level: 1, | ||
message: "rendered", | ||
properties: { | ||
endColumn: 15, | ||
endLine: 30, | ||
file: "main.rs", | ||
startColumn: 10, | ||
startLine: 30, | ||
title: "message", | ||
}, | ||
}, | ||
]); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Reporter } from "reporter"; | ||
import { AnnotationLevel } from "schema"; | ||
|
||
jest.mock("@actions/core"); | ||
|
||
describe("reporter", () => { | ||
it("works", async () => { | ||
const r = new Reporter(); | ||
|
||
await expect( | ||
r.report( | ||
{ | ||
error: 0, | ||
help: 0, | ||
ice: 0, | ||
note: 0, | ||
warning: 0, | ||
}, | ||
[ | ||
{ | ||
level: AnnotationLevel.Error, | ||
message: "I'm an error", | ||
properties: {}, | ||
}, | ||
{ | ||
level: AnnotationLevel.Warning, | ||
message: "I'm a warning", | ||
properties: {}, | ||
}, | ||
{ | ||
level: AnnotationLevel.Notice, | ||
message: "I'm a notice", | ||
properties: {}, | ||
}, | ||
], | ||
{ | ||
cargo: "cargo", | ||
clippy: "clippy", | ||
rustc: "rustc", | ||
}, | ||
), | ||
).resolves.toBeUndefined(); | ||
}); | ||
}); |