-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ruff #620
Closed
+227
−24
Closed
Add ruff #620
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,91 @@ | ||
const { sep } = require("path"); | ||
|
||
const { run } = require("../utils/action"); | ||
const commandExists = require("../utils/command-exists"); | ||
const { initLintResult } = require("../utils/lint-result"); | ||
const { capitalizeFirstLetter } = require("../utils/string"); | ||
|
||
const PARSE_REGEX = /^(.*):([0-9]+):[0-9]+: (\w*) (.*)$/gm; | ||
|
||
/** @typedef {import('../utils/lint-result').LintResult} LintResult */ | ||
|
||
/** | ||
* https://beta.ruff.rs | ||
*/ | ||
class Ruff { | ||
static get name() { | ||
return "Ruff"; | ||
} | ||
|
||
/** | ||
* Verifies that all required programs are installed. Throws an error if programs are missing | ||
* @param {string} dir - Directory to run the linting program in | ||
* @param {string} prefix - Prefix to the lint command | ||
*/ | ||
static async verifySetup(dir, prefix = "") { | ||
// Verify that Python is installed (required to execute Ruff) | ||
if (!(await commandExists("python"))) { | ||
throw new Error("Python is not installed"); | ||
} | ||
|
||
// Verify that Ruff is installed | ||
try { | ||
run(`${prefix} ruff --version`, { dir }); | ||
} catch (err) { | ||
throw new Error(`${this.name} is not installed`); | ||
} | ||
} | ||
|
||
/** | ||
* Runs the linting program and returns the command output | ||
* @param {string} dir - Directory to run the linter in | ||
* @param {string[]} extensions - File extensions which should be linted | ||
* @param {string} args - Additional arguments to pass to the linter | ||
* @param {boolean} fix - Whether the linter should attempt to fix code style issues automatically | ||
* @param {string} prefix - Prefix to the lint command | ||
* @returns {{status: number, stdout: string, stderr: string}} - Output of the lint command | ||
*/ | ||
static lint(dir, extensions, args = "", fix = false, prefix = "") { | ||
if (extensions.length !== 1 || extensions[0] !== "py") { | ||
throw new Error(`${this.name} error: File extensions are not configurable`); | ||
} | ||
const fixArg = fix ? "--fix-only --exit-non-zero-on-fix" : ""; | ||
return run(`${prefix} ruff check --quiet ${fixArg} ${args} .`, { | ||
dir, | ||
ignoreErrors: true, | ||
}); | ||
} | ||
|
||
/** | ||
* Parses the output of the lint command. Determines the success of the lint process and the | ||
* severity of the identified code style violations | ||
* @param {string} dir - Directory in which the linter has been run | ||
* @param {{status: number, stdout: string, stderr: string}} output - Output of the lint command | ||
* @returns {LintResult} - Parsed lint result | ||
*/ | ||
static parseOutput(dir, output) { | ||
const lintResult = initLintResult(); | ||
lintResult.isSuccess = output.status === 0; | ||
|
||
const matches = output.stdout.matchAll(PARSE_REGEX); | ||
for (const match of matches) { | ||
const [_, pathFull, line, rule, text] = match; | ||
const leadingSep = `.${sep}`; | ||
let path = pathFull; | ||
if (path.startsWith(leadingSep)) { | ||
path = path.substring(2); // Remove "./" or ".\" from start of path | ||
} | ||
const lineNr = parseInt(line, 10); | ||
lintResult.error.push({ | ||
path, | ||
firstLine: lineNr, | ||
lastLine: lineNr, | ||
message: `${capitalizeFirstLetter(text)} (${rule})`, | ||
}); | ||
} | ||
|
||
return lintResult; | ||
} | ||
} | ||
|
||
module.exports = Ruff; |
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,67 @@ | ||
const { EOL } = require("os"); | ||
|
||
const Ruff = require("../../../src/linters/ruff"); | ||
|
||
const testName = "ruff"; | ||
const linter = Ruff; | ||
const args = ""; | ||
const commandPrefix = ""; | ||
const extensions = ["py"]; | ||
|
||
// Linting without auto-fixing | ||
function getLintParams(dir) { | ||
const stdoutFile1 = `file1.py:3:8: F401 [*] \`os\` imported but unused`; | ||
const stdoutFile2 = `file2.py:1:4: F821 Undefined name \`a\`${EOL}file2.py:1:5: E701 Multiple statements on one line (colon)`; | ||
return { | ||
// Expected output of the linting function | ||
cmdOutput: { | ||
status: 1, | ||
stdoutParts: [stdoutFile1, stdoutFile2], | ||
stdout: `${stdoutFile1}${EOL}${stdoutFile2}`, | ||
}, | ||
// Expected output of the parsing function | ||
lintResult: { | ||
isSuccess: false, | ||
warning: [], | ||
error: [ | ||
{ | ||
path: "file1.py", | ||
firstLine: 3, | ||
lastLine: 3, | ||
message: "[*] `os` imported but unused (F401)", | ||
}, | ||
{ | ||
path: "file2.py", | ||
firstLine: 1, | ||
lastLine: 1, | ||
message: "Undefined name `a` (F821)", | ||
}, | ||
{ | ||
path: "file2.py", | ||
firstLine: 1, | ||
lastLine: 1, | ||
message: "Multiple statements on one line (colon) (E701)", | ||
}, | ||
], | ||
}, | ||
}; | ||
} | ||
|
||
// Linting with auto-fixing | ||
function getFixParams(dir) { | ||
return { | ||
// Expected output of the linting function | ||
cmdOutput: { | ||
status: 1, | ||
stdout: "", | ||
}, | ||
// Expected output of the parsing function | ||
lintResult: { | ||
isSuccess: false, | ||
warning: [], | ||
error: [], | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = [testName, linter, commandPrefix, extensions, args, getLintParams, getFixParams]; |
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,8 @@ | ||
from typing import List | ||
|
||
import os | ||
|
||
|
||
def sum_even_numbers(numbers: List[int]) -> int: | ||
"""Given a list of integers, return the sum of all even numbers in the list.""" | ||
return sum(num for num in numbers if num % 2 == 0) |
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 @@ | ||
if a: a = False |
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 @@ | ||
ruff>=0.0.257 |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pickfire is it possible that the expected values in the
ruff
+fix
are wrong?I ran the tests on my machine for
ruff
and without the following change the tests were failing:the thing is the option
--fix-only
atlint-action/src/linters/ruff.js
Lines 52 to 53 in 5981801
ruff
doesn't report the violations:Here is my context:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't looked at this PR closely, but FYI we recently changed
--fix-only
to always return a code of 0, unless you provide--exit-non-zero-on-fix
, in which case, it'll exit 1 if it fixes any violations.Relevant link to the breaking change list: https://github.com/charliermarsh/ruff/blob/main/BREAKING_CHANGES.md#--fix-only-now-exits-with-a-zero-exit-code-unless---exit-non-zero-on-fix-is-specified-4146