-
Notifications
You must be signed in to change notification settings - Fork 142
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
f0a9dc8
commit ebd4f4f
Showing
9 changed files
with
186 additions
and
2 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,66 @@ | ||
const { run } = require("../utils/action"); | ||
const commandExists = require("../utils/command-exists"); | ||
const { parseErrorsFromDiff } = require("../utils/diff"); | ||
const { initLintResult } = require("../utils/lint-result"); | ||
|
||
/** | ||
* https://pypi.org/project/oitnb/ | ||
*/ | ||
class Oitnb { | ||
static get name() { | ||
return "oitnb"; | ||
} | ||
|
||
/** | ||
* 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 oitnb) | ||
if (!(await commandExists("python"))) { | ||
throw new Error("Python is not installed"); | ||
} | ||
|
||
// Verify that oitnb is installed | ||
try { | ||
run(`${prefix} oitnb --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 = "") { | ||
const files = `^.*\\.(${extensions.join("|")})$`; | ||
const fixArg = fix ? "" : "--check --diff"; | ||
return run(`${prefix} oitnb ${fixArg} --include "${files}" ${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 {import('../utils/lint-result').LintResult} - Parsed lint result | ||
*/ | ||
static parseOutput(dir, output) { | ||
const lintResult = initLintResult(); | ||
lintResult.error = parseErrorsFromDiff(output.stdout); | ||
lintResult.isSuccess = output.status === 0; | ||
return lintResult; | ||
} | ||
} | ||
|
||
module.exports = Oitnb; |
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,65 @@ | ||
const Oitnb = require("../../../src/linters/oitnb"); | ||
const { TEST_DATE } = require("../../test-utils"); | ||
|
||
const testName = "oitnb"; | ||
const linter = Oitnb; | ||
const commandPrefix = ""; | ||
const extensions = ["py"]; | ||
|
||
// Linting without auto-fixing | ||
function getLintParams(dir) { | ||
const stdoutFile1 = `--- file1.py ${TEST_DATE}\n+++ file1.py ${TEST_DATE}\n@@ -1,10 +1,10 @@\n var_1 = "hello"\n var_2 = "world"\n \n \n-def main (): # Whitespace error\n+def main(): # Whitespace error\n print("hello " + var_2)\n \n \n def add(num_1, num_2):\n return num_1 + num_2\n@@ -19,8 +19,9 @@\n \n \n def divide(num_1, num_2):\n return num_1 / num_2\n \n+\n # Blank lines error\n \n main()`; | ||
const stdoutFile2 = `--- file2.py ${TEST_DATE}\n+++ file2.py ${TEST_DATE}\n@@ -1,2 +1,2 @@\n def add(num_1, num_2):\n- return num_1 + num_2 # Indentation error\n+ return num_1 + num_2 # Indentation error`; | ||
return { | ||
// Expected output of the linting function | ||
cmdOutput: { | ||
status: 1, | ||
stdoutParts: [stdoutFile1, stdoutFile2], | ||
stdout: `${stdoutFile1}\n \n${stdoutFile2}`, | ||
}, | ||
// Expected output of the parsing function | ||
lintResult: { | ||
isSuccess: false, | ||
warning: [], | ||
error: [ | ||
{ | ||
path: "file1.py", | ||
firstLine: 1, | ||
lastLine: 11, | ||
message: ` -var_1 = "hello"\n+var_1 = 'hello'\n -var_2 = "world"\n+-var_2 = 'world'\n \n \n-def main (): # Whitespace error\n+def main(): # Whitespace error\n -print("hello " + var_2)\n+print('hello ' + var_2)\n \n \n def add(num_1, num_2):\n return num_1 + num_2`, | ||
}, | ||
{ | ||
path: "file1.py", | ||
firstLine: 19, | ||
lastLine: 27, | ||
message: ` \n \n def divide(num_1, num_2):\n return num_1 / num_2\n \n+\n # Blank lines error\n \n main()\n `, | ||
}, | ||
{ | ||
path: "file2.py", | ||
firstLine: 1, | ||
lastLine: 3, | ||
message: ` def add(num_1, num_2):\n- return num_1 + num_2 # Indentation error\n+ return num_1 + num_2 # Indentation error`, | ||
}, | ||
], | ||
}, | ||
}; | ||
} | ||
|
||
// Linting with auto-fixing | ||
function getFixParams(dir) { | ||
return { | ||
// Expected output of the linting function | ||
cmdOutput: { | ||
status: 0, | ||
stdout: "", | ||
}, | ||
// Expected output of the parsing function | ||
lintResult: { | ||
isSuccess: true, | ||
warning: [], | ||
error: [], | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = [testName, linter, commandPrefix, extensions, 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,26 @@ | ||
var_1 = "hello" | ||
var_2 = "world" | ||
|
||
|
||
def main (): # Whitespace error | ||
print("hello " + var_2) | ||
|
||
|
||
def add(num_1, num_2): | ||
return num_1 + num_2 | ||
|
||
|
||
def subtract(num_1, num_2): | ||
return num_1 - num_2 | ||
|
||
|
||
def multiply(num_1, num_2): | ||
return num_1 * num_2 | ||
|
||
|
||
def divide(num_1, num_2): | ||
return num_1 / num_2 | ||
|
||
# Blank lines error | ||
|
||
main() |
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,2 @@ | ||
def add(num_1, num_2): | ||
return num_1 + num_2 # Indentation error |
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 @@ | ||
oitnb>=0.2.2 |