Skip to content

Commit

Permalink
Refactor open handling and add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schnittstabil committed May 3, 2017
1 parent a1ae203 commit 80bcbf5
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 60 deletions.
62 changes: 2 additions & 60 deletions cli.js
Expand Up @@ -15,8 +15,6 @@ if (!hasFlag('no-local') && localCLI && localCLI !== __filename) {
return;
}

const path = require('path');
const spawn = require('child_process').spawn;
const updateNotifier = require('update-notifier');
const getStdin = require('get-stdin');
const meow = require('meow');
Expand Down Expand Up @@ -91,63 +89,7 @@ const log = report => {

process.stdout.write(reporter(report.results));
process.exit(report.errorCount === 0 ? 0 : 1);
}

const open = report => {
if (report.errorCount === 0) {
return;
}

const editor = process.env.EDITOR;

if (!editor) {
console.log(`
\`open\` option was used, but your $EDITOR environment variable is empty.
Fix it by setting path to your editor of choice in ~/.bashrc or ~/.zshrc:
export EDITOR=atom
`);
return;
}

const executableName = editor.split(path.sep).pop();
const lineColumn = message => `${message.line}:${message.column}`;
const args = [];

report.results
.filter(file => file.errorCount > 0)
.forEach(file => {
// VS Code requires --goto option for path:line:column
if (executableName === 'code') {
args.push('--goto');
}

// Sublime Text, Atom, and VS Code support opening file at exact position
if (['subl', 'atom', 'code'].indexOf(executableName) >= 0) {
args.push(file.filePath + ':' + lineColumn(file.messages[0]));
return;
}

// WebStorm supports opening file on a specific line (no column support)
if (executableName === 'wstorm') {
args.push(file.filePath + ':' + file.messages[0].line);
return;
}

// TextMate requires a `--line` option
if (executableName === 'mate') {
args.push('--line', lineColumn(file.messages[0]), file.filePath);
return;
}

args.push(file.filePath);
});

spawn(editor, args, {
detached: true,
stdio: 'ignore'
}).unref();
}
};

// `xo -` => `xo --stdin`
if (input[0] === '-') {
Expand Down Expand Up @@ -178,7 +120,7 @@ if (opts.init) {
}

if (opts.open) {
open(report);
require('./open-report')(report);
}

log(report);
Expand Down
65 changes: 65 additions & 0 deletions open-report.js
@@ -0,0 +1,65 @@
'use strict';
const path = require('path');
const spawn = require('child_process').spawn;

const lineColumn = message => `${message.line}:${message.column}`;

const openReport = (report, opts) => {
opts = Object.assign({
editor: process.env.EDITOR,
spawn
}, opts);

if (report.errorCount === 0) {
return;
}

if (!opts.editor) {
console.log(`
\`open\` option was used, but your $EDITOR environment variable is empty.
Fix it by setting path to your editor of choice in ~/.bashrc or ~/.zshrc:
export EDITOR=atom
`);
return;
}

const executableName = opts.editor.split(path.sep).pop();
const args = [];

report.results
.filter(result => result.errorCount > 0)
.forEach(result => {
// VS Code requires --goto option for path:line:column
if (executableName === 'code') {
args.push('--goto');
}

// Sublime Text, Atom, and VS Code support opening file at exact position
if (['subl', 'atom', 'code'].indexOf(executableName) >= 0) {
args.push(result.filePath + ':' + lineColumn(result.messages[0]));
return;
}

// WebStorm supports opening file on a specific line (no column support)
if (executableName === 'wstorm') {
args.push(result.filePath + ':' + result.messages[0].line);
return;
}

// TextMate requires a `--line` option
if (executableName === 'mate') {
args.push('--line', lineColumn(result.messages[0]), result.filePath);
return;
}

args.push(result.filePath);
});

opts.spawn(opts.editor, args, {
detached: true,
stdio: 'ignore'
}).unref();
};

module.exports = openReport;
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -33,6 +33,7 @@
"gitignore-filter.js",
"index.js",
"cli.js",
"open-report.js",
"options-manager.js",
"config"
],
Expand Down
77 changes: 77 additions & 0 deletions test/open-report.js
@@ -0,0 +1,77 @@
import path from 'path';
import test from 'ava';
import fn from '../open-report';

const singleFileReportFixture = {
errorCount: 1,
results: [
{
errorCount: 1,
filePath: 'foo.js',
messages:[
{
line: 42,
column: 43
}
]
}
]
};

const singleSpawnMacro = (t, input, expected) => {
t.plan(3);

fn(input.report, {
editor: input.editor,
spawn: (cmd, args, opts) => {
t.is(cmd, expected.cmd);
t.deepEqual(args, expected.args);

return {
unref: () => {
t.pass();
}
}
}
});
}

test('VS Code - single file report', singleSpawnMacro, {
editor: 'code',
report: singleFileReportFixture
}, {
cmd: 'code',
args: ['--goto', 'foo.js:42:43']
});

test('Sublime Text - single file report', singleSpawnMacro, {
editor: 'subl',
report: singleFileReportFixture
}, {
cmd: 'subl',
args: ['foo.js:42:43']
});

test('Atom - single file report', singleSpawnMacro, {
editor: 'atom',
report: singleFileReportFixture
}, {
cmd: 'atom',
args: ['foo.js:42:43']
});

test('WebStorm - single file report', singleSpawnMacro, {
editor: 'wstorm',
report: singleFileReportFixture
}, {
cmd: 'wstorm',
args: ['foo.js:42']
});

test('TextMate - single file report', singleSpawnMacro, {
editor: 'mate',
report: singleFileReportFixture
}, {
cmd: 'mate',
args: ['--line', '42:43', 'foo.js']
});

0 comments on commit 80bcbf5

Please sign in to comment.