Skip to content

Commit

Permalink
fix: use relative paths in github annotations when working directory …
Browse files Browse the repository at this point in the history
…is provided
  • Loading branch information
mskrajnowski committed May 29, 2024
1 parent 3bdde3b commit f90ec1c
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 26 deletions.
10 changes: 7 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10015,7 +10015,9 @@ async function getPylancePyrightVersion(pylanceVersion) {

// src/main.ts
function printInfo(pyrightVersion, node, cwd, command, args) {
core2.info(`pyright ${pyrightVersion.format()}, node ${node.version}, pyright-action ${getActionVersion()}`);
core2.info(
`pyright ${pyrightVersion.format()}, node ${node.version}, pyright-action ${getActionVersion()}`
);
core2.info(`Working directory: ${cwd}`);
core2.info(`Running: ${(0, import_shell_quote2.quote)([command, ...args])}`);
}
Expand Down Expand Up @@ -10078,7 +10080,7 @@ async function main() {
actionsCommand.issueCommand(
diag.severity,
{
file: diag.file,
file: workingDirectory ? path2.relative(workingDirectory, diag.file) : diag.file,
line: line + 1,
col: col + 1
},
Expand Down Expand Up @@ -10182,7 +10184,9 @@ function checkOverriddenFlags(version3, args) {
const flag = `--${key.toLowerCase()}`;
if (overriddenFlags.has(flag)) {
core2.warning(
`${configPath} contains ${(0, import_node_util.inspect)({ [key]: value })}; ${flag} as passed by pyright-action will have no effect.`
`${configPath} contains ${(0, import_node_util.inspect)({
[key]: value
})}; ${flag} as passed by pyright-action will have no effect.`
);
}
}
Expand Down
79 changes: 79 additions & 0 deletions src/__snapshots__/main.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1570,3 +1570,82 @@ exports[`with overridden flags > pyproject.toml bad toml > process.chdir 1`] = `
],
]
`;

exports[`working-directory > relative paths > command.issueCommand 1`] = `
[
[
"error",
{
"col": 1,
"file": "file1.py",
"line": 1,
},
"some error",
],
]
`;

exports[`working-directory > relative paths > core.error 1`] = `[]`;

exports[`working-directory > relative paths > core.info 1`] = `
[
[
"pyright 1.1.240, node v20.8.1, pyright-action 1.1.0",
],
[
"Working directory: /some/wd",
],
[
"Running: /path/to/node /path/to/pyright/dist/index.js --outputjson",
],
[
"/some/wd/file1.py:1:1 - error: some error",
],
[
"1 error, 0 warnings, 0 informations",
],
]
`;

exports[`working-directory > relative paths > core.setFailed 1`] = `
[
[
"1 error",
],
]
`;

exports[`working-directory > relative paths > core.warning 1`] = `[]`;

exports[`working-directory > relative paths > cp.spawnSync 1`] = `
[
[
"/path/to/node",
[
"/path/to/pyright/dist/index.js",
"--outputjson",
],
{
"encoding": "utf8",
"maxBuffer": 104857600,
"stdio": [
"ignore",
"pipe",
"inherit",
],
},
],
]
`;

exports[`working-directory > relative paths > fs.existsSync 1`] = `[]`;

exports[`working-directory > relative paths > fs.readFileSync 1`] = `[]`;

exports[`working-directory > relative paths > process.chdir 1`] = `
[
[
"/some/wd",
],
]
`;
122 changes: 106 additions & 16 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ afterEach(() => {
expect(mockedCore.info.mock.calls).toMatchSnapshot("core.info");
expect(mockedCore.warning.mock.calls).toMatchSnapshot("core.warning");
expect(mockedCore.error.mock.calls).toMatchSnapshot("core.error");
expect(mockedCommand.issueCommand.mock.calls).toMatchSnapshot("command.issueCommand");
expect(mockedCommand.issueCommand.mock.calls).toMatchSnapshot(
"command.issueCommand",
);
expect(mockedCp.spawnSync.mock.calls).toMatchSnapshot("cp.spawnSync");
expect(mockedFs.readFileSync.mock.calls).toMatchSnapshot("fs.readFileSync");
expect(mockedFs.existsSync.mock.calls).toMatchSnapshot("fs.existsSync");
Expand Down Expand Up @@ -188,19 +190,28 @@ describe("with comments", () => {
generalDiagnostics: [
{
file: "/path/to/file1.py",
range: { start: { line: 0, character: 0 }, end: { line: 1, character: 1 } },
range: {
start: { line: 0, character: 0 },
end: { line: 1, character: 1 },
},
severity: "error",
message: "some error",
},
{
file: "/path/to/file2.py",
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } },
range: {
start: { line: 0, character: 0 },
end: { line: 0, character: 0 },
},
severity: "warning",
message: "some warning",
},
{
file: "/path/to/file3.py",
range: { start: { line: 0, character: 0 }, end: { line: 1, character: 1 } },
range: {
start: { line: 0, character: 0 },
end: { line: 1, character: 1 },
},
severity: "information",
message: "some info",
rule: "reportSomeInformation",
Expand All @@ -213,7 +224,10 @@ describe("with comments", () => {
},
{
file: "/path/to/file1.py",
range: { start: { line: 5, character: 9 }, end: { line: 5, character: 15 } },
range: {
start: { line: 5, character: 9 },
end: { line: 5, character: 15 },
},
severity: "error",
message: "some error",
},
Expand Down Expand Up @@ -248,19 +262,28 @@ describe("with comments", () => {
generalDiagnostics: [
{
file: "/path/to/file1.py",
range: { start: { line: 0, character: 0 }, end: { line: 1, character: 1 } },
range: {
start: { line: 0, character: 0 },
end: { line: 1, character: 1 },
},
severity: "error",
message: "some error",
},
{
file: "/path/to/file2.py",
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } },
range: {
start: { line: 0, character: 0 },
end: { line: 0, character: 0 },
},
severity: "warning",
message: "some warning",
},
{
file: "/path/to/file3.py",
range: { start: { line: 0, character: 0 }, end: { line: 1, character: 1 } },
range: {
start: { line: 0, character: 0 },
end: { line: 1, character: 1 },
},
severity: "information",
message: "some info",
rule: "reportSomeInformation",
Expand All @@ -273,7 +296,10 @@ describe("with comments", () => {
},
{
file: "/path/to/file1.py",
range: { start: { line: 5, character: 9 }, end: { line: 5, character: 15 } },
range: {
start: { line: 5, character: 9 },
end: { line: 5, character: 15 },
},
severity: "error",
message: "some error",
},
Expand Down Expand Up @@ -308,19 +334,28 @@ describe("with comments", () => {
generalDiagnostics: [
{
file: "/path/to/file1.py",
range: { start: { line: 0, character: 0 }, end: { line: 1, character: 1 } },
range: {
start: { line: 0, character: 0 },
end: { line: 1, character: 1 },
},
severity: "error",
message: "some error",
},
{
file: "/path/to/file2.py",
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } },
range: {
start: { line: 0, character: 0 },
end: { line: 0, character: 0 },
},
severity: "warning",
message: "some warning",
},
{
file: "/path/to/file3.py",
range: { start: { line: 0, character: 0 }, end: { line: 1, character: 1 } },
range: {
start: { line: 0, character: 0 },
end: { line: 1, character: 1 },
},
severity: "information",
message: "some info",
rule: "reportSomeInformation",
Expand All @@ -333,7 +368,10 @@ describe("with comments", () => {
},
{
file: "/path/to/file1.py",
range: { start: { line: 5, character: 9 }, end: { line: 5, character: 15 } },
range: {
start: { line: 5, character: 9 },
end: { line: 5, character: 15 },
},
severity: "error",
message: "some error",
},
Expand All @@ -360,19 +398,28 @@ describe("with comments", () => {
generalDiagnostics: [
{
file: "/path/to/file1.py",
range: { start: { line: 0, character: 0 }, end: { line: 1, character: 1 } },
range: {
start: { line: 0, character: 0 },
end: { line: 1, character: 1 },
},
severity: "error",
message: "some error",
},
{
file: "/path/to/file2.py",
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } },
range: {
start: { line: 0, character: 0 },
end: { line: 0, character: 0 },
},
severity: "warning",
message: "some warning",
},
{
file: "/path/to/file3.py",
range: { start: { line: 0, character: 0 }, end: { line: 1, character: 1 } },
range: {
start: { line: 0, character: 0 },
end: { line: 1, character: 1 },
},
severity: "information",
message: "some info",
rule: "reportSomeInformation",
Expand Down Expand Up @@ -687,6 +734,49 @@ describe("with overridden flags", () => {
});
});

describe("working-directory", () => {
test("relative paths", async () => {
const args = ["/path/to/pyright/dist/index.js", "--outputjson"];
const wd = "/some/wd";

mockedHelpers.getArgs.mockResolvedValue({
annotate: new Set(["error"]),
workingDirectory: wd,
pyrightVersion,
command: nodeExecPath,
args,
});

mockedCp.spawnSync.mockImplementation(() => ({
pid: -1,
output: [],
stdout: reportToString({
generalDiagnostics: [
{
file: "/some/wd/file1.py",
range: {
start: { line: 0, character: 0 },
end: { line: 1, character: 1 },
},
severity: "error",
message: "some error",
},
],
summary: {
errorCount: 1,
warningCount: 0,
informationCount: 0,
},
}) as any,
stderr: "" as any,
status: 1,
signal: null,
}));

await main();
});
});

function reportToString(report: Report): string {
return JSON.stringify(report);
}
Loading

0 comments on commit f90ec1c

Please sign in to comment.