Skip to content

Commit

Permalink
Fix detection of non-existent commands
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmeuli committed May 13, 2020
1 parent b6520c0 commit fda5366
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/linters/black.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const commandExists = require("../../vendor/command-exists");
const { run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { parseErrorsFromDiff } = require("../utils/diff");
const { initLintResult } = require("../utils/lint-result");

Expand Down
2 changes: 1 addition & 1 deletion src/linters/eslint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const commandExists = require("../../vendor/command-exists");
const { run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { initLintResult } = require("../utils/lint-result");
const { getNpmBinCommand } = require("../utils/npm/get-npm-bin-command");
const { removeTrailingPeriod } = require("../utils/string");
Expand Down
2 changes: 1 addition & 1 deletion src/linters/flake8.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { sep } = require("path");

const commandExists = require("../../vendor/command-exists");
const { log, run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { initLintResult } = require("../utils/lint-result");
const { capitalizeFirstLetter } = require("../utils/string");

Expand Down
2 changes: 1 addition & 1 deletion src/linters/gofmt.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const commandExists = require("../../vendor/command-exists");
const { run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { parseErrorsFromDiff } = require("../utils/diff");
const { initLintResult } = require("../utils/lint-result");

Expand Down
2 changes: 1 addition & 1 deletion src/linters/golint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const commandExists = require("../../vendor/command-exists");
const { log, run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { initLintResult } = require("../utils/lint-result");
const { capitalizeFirstLetter } = require("../utils/string");

Expand Down
2 changes: 1 addition & 1 deletion src/linters/mypy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require("fs");
const { sep } = require("path");

const commandExists = require("../../vendor/command-exists");
const { log, run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { initLintResult } = require("../utils/lint-result");

const PARSE_REGEX = /^(.*):([0-9]+): (\w*): (.*)$/gm;
Expand Down
2 changes: 1 addition & 1 deletion src/linters/prettier.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const commandExists = require("../../vendor/command-exists");
const { run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { initLintResult } = require("../utils/lint-result");
const { getNpmBinCommand } = require("../utils/npm/get-npm-bin-command");

Expand Down
2 changes: 1 addition & 1 deletion src/linters/rubocop.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const commandExists = require("../../vendor/command-exists");
const { run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { initLintResult } = require("../utils/lint-result");
const { removeTrailingPeriod } = require("../utils/string");

Expand Down
2 changes: 1 addition & 1 deletion src/linters/stylelint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const commandExists = require("../../vendor/command-exists");
const { run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { initLintResult } = require("../utils/lint-result");
const { getNpmBinCommand } = require("../utils/npm/get-npm-bin-command");

Expand Down
2 changes: 1 addition & 1 deletion src/linters/swift-format-lockwood.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const commandExists = require("../../vendor/command-exists");
const { run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { initLintResult } = require("../utils/lint-result");

const PARSE_REGEX = /^(.*):([0-9]+):[0-9]+: \w+: \((\w+)\) (.*)\.$/gm;
Expand Down
2 changes: 1 addition & 1 deletion src/linters/swift-format-official.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const commandExists = require("../../vendor/command-exists");
const { run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { initLintResult } = require("../utils/lint-result");

const PARSE_REGEX = /^(.*):([0-9]+):([0-9]+): (warning|error): (.*)$/gm;
Expand Down
2 changes: 1 addition & 1 deletion src/linters/swiftlint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const commandExists = require("../../vendor/command-exists");
const { run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { initLintResult } = require("../utils/lint-result");

const PARSE_REGEX = /^(.*):([0-9]+):[0-9]+: (warning|error): (.*)$/gm;
Expand Down
2 changes: 1 addition & 1 deletion src/linters/xo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const commandExists = require("../../vendor/command-exists");
const { run } = require("../utils/action");
const commandExists = require("../utils/command-exists");
const { getNpmBinCommand } = require("../utils/npm/get-npm-bin-command");
const ESLint = require("./eslint");

Expand Down
19 changes: 19 additions & 0 deletions src/utils/command-exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const checkForCommand = require("../../vendor/command-exists");

/**
* Returns whether the provided shell command is available
* @param {string} command - Shell command to check for
* @returns {Promise<boolean>} - Whether the command is available
*/
async function commandExists(command) {
// The `command-exists` library throws an error if the command is not available. This function
// catches these errors and returns a boolean value instead
try {
await checkForCommand(command);
return true;
} catch (error) {
return false;
}
}

module.exports = commandExists;
11 changes: 11 additions & 0 deletions test/utils/command-exists.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const commandExists = require("../../src/utils/command-exists");

describe("commandExists()", () => {
test("should return `true` for existing command", async () => {
await expect(commandExists("cat")).resolves.toEqual(true);
});

test("should return `false` for non-existent command", async () => {
await expect(commandExists("nonexistentcommand")).resolves.toEqual(false);
});
});

0 comments on commit fda5366

Please sign in to comment.