Skip to content

Commit 52ca270

Browse files
fix: consolidate toolchain building
1 parent 9381f01 commit 52ca270

File tree

4 files changed

+75
-35
lines changed

4 files changed

+75
-35
lines changed

dist/index.js

+22-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/clippy.ts

+24-17
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,24 @@ async function buildContext(program: Program, toolchain: string | undefined): Pr
2424
rustc: "",
2525
};
2626

27-
toolchain = `+${toolchain}` ?? "";
28-
2927
await Promise.all([
30-
await exec.exec("rustc", [toolchain, "-V"], {
28+
await exec.exec("rustc", buildToolchainArguments(toolchain, ["-V"]), {
3129
silent: false,
3230
listeners: {
3331
stdout: (buffer: Buffer) => {
3432
return (context.rustc = buffer.toString().trim());
3533
},
3634
},
3735
}),
38-
await program.call([toolchain, "-V"], {
36+
await program.call(buildToolchainArguments(toolchain, ["-V"]), {
3937
silent: false,
4038
listeners: {
4139
stdout: (buffer: Buffer) => {
4240
return (context.cargo = buffer.toString().trim());
4341
},
4442
},
4543
}),
46-
await program.call([toolchain, "clippy", "-V"], {
44+
await program.call(buildToolchainArguments(toolchain, ["clippy", "-V"]), {
4745
silent: false,
4846
listeners: {
4947
stdout: (buffer: Buffer) => {
@@ -57,7 +55,7 @@ async function buildContext(program: Program, toolchain: string | undefined): Pr
5755
}
5856

5957
async function runClippy(actionInput: input.ParsedInput, program: Program): Promise<ClippyResult> {
60-
const args = buildArgs(actionInput);
58+
const args = buildClippyArguments(actionInput);
6159
const outputParser = new OutputParser();
6260

6361
const options: exec.ExecOptions = {
@@ -112,20 +110,29 @@ export async function run(actionInput: input.ParsedInput): Promise<void> {
112110
}
113111
}
114112

115-
function buildArgs(actionInput: input.ParsedInput): string[] {
116-
const args: string[] = [];
113+
function buildToolchainArguments(toolchain: string | undefined, after: string[]): string[] {
114+
const args = [];
117115

118-
// Toolchain selection MUST go first in any condition
119-
if (actionInput.toolchain) {
120-
args.push(`+${actionInput.toolchain}`);
116+
if (toolchain) {
117+
args.push(`+${toolchain}`);
121118
}
122119

123-
args.push("clippy");
120+
args.push(...after);
121+
122+
return args;
123+
}
124124

125-
// `--message-format=json` should just right after the `cargo clippy`
126-
// because usually people are adding the `-- -D warnings` at the end
127-
// of arguments and it will mess up the output.
128-
args.push("--message-format=json");
125+
function buildClippyArguments(actionInput: input.ParsedInput): string[] {
126+
// Toolchain selection MUST go first in any condition!
127+
return buildToolchainArguments(actionInput.toolchain, [
128+
"clippy",
129129

130-
return args.concat(actionInput.args);
130+
// `--message-format=json` should just right after the `cargo clippy`
131+
// because usually people are adding the `-- -D warnings` at the end
132+
// of arguments and it will mess up the output.
133+
"--message-format=json",
134+
135+
// and the rest
136+
...actionInput.args,
137+
]);
131138
}

src/tests/clippy.test.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,33 @@ describe("clippy", () => {
6262
await expect(run(actionInput)).rejects.toThrow(/Clippy had exited with the (\d)+ exit code/);
6363
});
6464

65+
it("records versions with toolchain", async () => {
66+
const reportSpy = jest.spyOn(Reporter.prototype, "report");
67+
jest.spyOn(exec, "exec").mockImplementation((commandline: string, args?: string[], options?: exec.ExecOptions) => {
68+
if (commandline.endsWith("cargo")) {
69+
if (args?.[0] === "+nightly" && args?.[1] === "-V") {
70+
options?.listeners?.stdout?.(Buffer.from("cargo version"));
71+
} else if (args?.[0] === "+nightly" && args?.[1] === "clippy" && args?.[2] === "-V") {
72+
options?.listeners?.stdout?.(Buffer.from("clippy version"));
73+
}
74+
} else if (commandline === "rustc" && args?.[0] === "+nightly" && args?.[1] === "-V") {
75+
options?.listeners?.stdout?.(Buffer.from("rustc version"));
76+
}
77+
return Promise.resolve(0);
78+
});
79+
80+
const actionInput: ParsedInput = {
81+
toolchain: "nightly",
82+
args: [],
83+
useCross: false,
84+
workingDirectory: undefined,
85+
};
86+
87+
await expect(run(actionInput)).resolves.toBeUndefined();
88+
89+
expect(reportSpy).toBeCalledWith({ error: 0, help: 0, ice: 0, note: 0, warning: 0 }, [], { cargo: "cargo version", clippy: "clippy version", rustc: "rustc version" });
90+
});
91+
6592
it("records versions", async () => {
6693
const reportSpy = jest.spyOn(Reporter.prototype, "report");
6794
jest.spyOn(exec, "exec").mockImplementation((commandline: string, args?: string[], options?: exec.ExecOptions) => {
@@ -78,7 +105,7 @@ describe("clippy", () => {
78105
});
79106

80107
const actionInput: ParsedInput = {
81-
toolchain: "stable",
108+
toolchain: undefined,
82109
args: [],
83110
useCross: false,
84111
workingDirectory: undefined,

0 commit comments

Comments
 (0)