Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #482 from lucasdf/stdin_filename
Browse files Browse the repository at this point in the history
Pass filename to Flake8
  • Loading branch information
Arcanemagus authored Dec 6, 2017
2 parents 6408200 + 5b3a09e commit 4311397
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
55 changes: 50 additions & 5 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@

// eslint-disable-next-line import/extensions, import/no-extraneous-dependencies
import { CompositeDisposable } from 'atom';
import fs from 'fs-plus';
import path from 'path';
import * as helpers from 'atom-linter';

let fs;
let path;
let helpers;
let semver;

function loadDeps() {
if (!semver) {
semver = require('semver');
}
if (!fs) {
fs = require('fs-plus');
}
if (!helpers) {
helpers = require('atom-linter');
}
if (!path) {
path = require('path');
}
}

// Local variables
const parseRegex = /(\d+):(\d+):\s(([A-Z])\d{2,3})\s+(.*)/g;
const execPathVersions = new Map();

const applySubstitutions = (givenExecPath, projDir) => {
let execPath = givenExecPath;
Expand Down Expand Up @@ -75,6 +93,22 @@ const generateInvalidPointTrace = async (execPath, match, filePath, textEditor,
};
};

const determineExecVersion = async (execPath) => {
const versionString = await helpers.exec(execPath, ['--version'], { ignoreExitCode: true });
const versionPattern = /^[^\s]+/g;
const match = versionString.match(versionPattern);
if (match !== null) {
execPathVersions.set(execPath, match[0]);
}
};

const getFlake8Version = async (execPath) => {
if (!execPathVersions.has(execPath)) {
await determineExecVersion(execPath);
}
return execPathVersions.get(execPath);
};

export default {
activate() {
this.idleCallbacks = new Set();
Expand All @@ -92,6 +126,7 @@ export default {
if (typeof atom.config.get('linter-flake8.disableTimeout') !== 'undefined') {
atom.config.unset('linter-flake8.disableTimeout');
}
loadDeps();
};
packageDepsID = window.requestIdleCallback(linterFlake8Deps);
this.idleCallbacks.add(packageDepsID);
Expand Down Expand Up @@ -144,6 +179,8 @@ export default {
scope: 'file',
lintOnFly: true,
lint: async (textEditor) => {
loadDeps();

const filePath = textEditor.getPath();
const fileText = textEditor.getText();

Expand All @@ -152,6 +189,15 @@ export default {
const projectPath = atom.project.relativizePath(filePath)[0];
const baseDir = projectPath !== null ? projectPath : path.dirname(filePath);
const configFilePath = await helpers.findCachedAsync(baseDir, this.projectConfigFile);
const execPath = fs.normalize(applySubstitutions(this.executablePath, baseDir));

// get the version of Flake8
const version = await getFlake8Version(execPath);

// stdin-display-name available since 3.0.0
if (semver.valid(version) && semver.gte(version, '3.0.0')) {
parameters.push('--stdin-display-name', filePath);
}

if (this.projectConfigFile && baseDir !== null && configFilePath !== null) {
parameters.push('--config', configFilePath);
Expand All @@ -178,11 +224,10 @@ export default {

parameters.push('-');

const execPath = fs.normalize(applySubstitutions(this.executablePath, baseDir));
const forceTimeout = 1000 * 60 * 5; // (ms * s * m) = Five minutes
const options = {
stdin: fileText,
cwd: path.dirname(textEditor.getPath()),
cwd: baseDir,
ignoreExitCode: true,
timeout: forceTimeout,
uniqueKey: `linter-flake8:${filePath}`,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
"dependencies": {
"atom-linter": "^10.0.0",
"atom-package-deps": "^4.0.1",
"fs-plus": "^3.0.0"
"fs-plus": "^3.0.0",
"semver": "^5.4.1"
},
"devDependencies": {
"eslint": "^4.6.0",
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/with-config-file/bad-ignored.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asfd
1 change: 1 addition & 0 deletions spec/fixtures/with-config-file/subdir/bad-ignored.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asfd
2 changes: 2 additions & 0 deletions spec/fixtures/with-config-file/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
exclude = bad-ignored.py, subdir/bad-ignored.py
23 changes: 23 additions & 0 deletions spec/linter-flake8-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import { it, fit, wait, beforeEach, afterEach } from 'jasmine-fix';
const { lint } = require('../lib/main.js').provideLinter();

const fixturePath = join(__dirname, 'fixtures');
const fixtureWithConfigPath = join(__dirname, 'fixtures', 'with-config-file');
const goodPath = join(fixturePath, 'good.py');
const badPath = join(fixturePath, 'bad.py');
const badIgnoredPath = join(fixtureWithConfigPath, 'bad-ignored.py');
const badSubdirIgnoredPath = join(fixtureWithConfigPath, 'subdir', 'bad-ignored.py');
const errwarnPath = join(fixturePath, 'errwarn.py');
const builtinsPath = join(fixturePath, 'builtins.py');

Expand Down Expand Up @@ -83,6 +86,26 @@ describe('The flake8 provider for Linter', () => {
});
});

describe('use configuration file and', () => {
let editor = null;

beforeEach(async () => {
atom.project.addPath(fixtureWithConfigPath);
editor = await atom.workspace.open(badIgnoredPath);
});

it('ignores file excluded in configuration file', async () => {
const messages = await lint(editor);
expect(messages.length).toBe(0);
});

it('ignores file excluded in configuration file using relative path', async () => {
editor = await atom.workspace.open(badSubdirIgnoredPath);
const messages = await lint(editor);
expect(messages.length).toBe(0);
});
});

it('finds nothing wrong with a valid file', async () => {
const editor = await atom.workspace.open(goodPath);
const messages = await lint(editor);
Expand Down

0 comments on commit 4311397

Please sign in to comment.