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

Run and show test results in the editor #8

Merged
merged 47 commits into from
May 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
eb14239
Runner skeleton
jacobmendoza Mar 3, 2016
4d02424
Foundations of test runner
jacobmendoza Mar 5, 2016
ca43e68
First version of working UI without format
jacobmendoza Mar 6, 2016
f58fde7
Assigning x as key for running tests
jacobmendoza Mar 6, 2016
d1ac430
Conversion of main.js to ES6
jacobmendoza Mar 8, 2016
7986bbd
Conversion of panel.js to ES6
jacobmendoza Mar 8, 2016
7d8bec9
Conversion of test-runner-process.js to ES6
jacobmendoza Mar 8, 2016
b872b85
Conversion of test-runner-process.js to ES6
jacobmendoza Mar 8, 2016
77e54ee
Small redesign on runner process events
jacobmendoza Mar 11, 2016
596f5e5
Dependencies updated
jacobmendoza Mar 11, 2016
4fabeec
TestRunnerProcess emits events now with EventEmitter
jacobmendoza Mar 12, 2016
ddc1f8e
Conversion of terminal-command-executor to ES6
jacobmendoza Mar 12, 2016
af98d9a
TerminalCommandExecutor emits events now with EventEmitter
jacobmendoza Mar 12, 2016
469e2bf
Cancellation mechanism when the user toggles the package
jacobmendoza Mar 12, 2016
054827a
event-kit dependency removed
jacobmendoza Mar 12, 2016
c9ea899
Initial cancellation mechanism
jacobmendoza Mar 12, 2016
3955950
Toggling the plugin launches the test runner process
jacobmendoza Mar 12, 2016
2f5319d
Addressing issues marked by xo
jacobmendoza Mar 12, 2016
86b5ed2
Files use now /** @babel */ header
jacobmendoza Mar 13, 2016
f71d3ab
Items separated by lines in the environments for xo
jacobmendoza Mar 13, 2016
f35825a
Keymap uses now JSON
jacobmendoza Mar 13, 2016
f8675ed
Using new ES2015 import syntax
jacobmendoza Mar 13, 2016
33d8d6e
Fixing double quotes in ava.less
jacobmendoza Mar 13, 2016
90b97ad
Changing from spaces to tabs
jacobmendoza Mar 13, 2016
ec2f444
Removing cancelling process
jacobmendoza Apr 11, 2016
b4868b2
Fixing trailing spaces error
jacobmendoza Apr 11, 2016
e6a3385
Trying to fix errors due to outdated xo version
jacobmendoza Apr 11, 2016
b9a7069
Initial implementation of current execution check before running tests
jacobmendoza Apr 11, 2016
e7fe057
First step towards new rendering infrastructure
jacobmendoza Apr 23, 2016
bc8a08b
Refactoring the creation of the parser to a factory
jacobmendoza Apr 27, 2016
f8135c1
Initial extraction of test running logic from the panel
jacobmendoza Apr 27, 2016
dbe0a32
UI improvements
jacobmendoza Apr 29, 2016
83c0d2c
Injecting Panel dependency
jacobmendoza Apr 29, 2016
267805e
Update file-name header
jacobmendoza Apr 30, 2016
27304ac
Trying to fix xo errors that werent catch by my local version
jacobmendoza Apr 30, 2016
4059434
Initial extraction of template for the panel view
jacobmendoza May 2, 2016
87ce941
Initial changes in the UI towards supporting multiple files
jacobmendoza May 2, 2016
786465a
New loading indicator
jacobmendoza May 2, 2016
a35c73d
Removing the dependency on the status code
jacobmendoza May 2, 2016
dc5cc76
Adapting tests to new test runner api
jacobmendoza May 2, 2016
4458a8a
Solved error in the template: both columns had passed as text
jacobmendoza May 2, 2016
b2d0b8b
Chaging the close icon
jacobmendoza May 4, 2016
6d6449a
TODO and SKIP tests are not taken as failed/passed
jacobmendoza May 5, 2016
7d3aa5e
Fixes in the styles towards supporting better themes
jacobmendoza May 5, 2016
099a5ff
Preventing crashes when the user does not have an window with active …
jacobmendoza May 5, 2016
2bd121b
Fixed some discrepancy in the summary when the suite has skip/todo tests
jacobmendoza May 5, 2016
71b5372
AVA logo served from local asset
jacobmendoza May 8, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions keymaps/ava.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"atom-workspace": {
"ctrl-alt-r": "ava:run",
"ctrl-alt-a": "ava:toggle"
}
}
23 changes: 23 additions & 0 deletions lib/html-renderer-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** @babel */

class HtmlRendererHelper {
createContainer(cssClass = null, textContent = null) {
const element = document.createElement('div');
if (cssClass) {
element.classList.add(cssClass);
}
if (textContent) {
element.textContent = textContent;
}
return element;
}

createImage(src, cssClass = null) {
const img = document.createElement('img');
img.src = src;
img.classList.add(cssClass);
return img;
}
}

module.exports = HtmlRendererHelper;
68 changes: 68 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/** @babel */

import path from 'path';
import {CompositeDisposable} from 'atom';
import Panel from './panel.js';
import TestRunnerProcess from './test-runner-process';

module.exports = {
activate(state) {
this.initRunnerProcess();
this.initUI(state);

this.subscriptions = new CompositeDisposable();

this.subscriptions.add(
atom.commands.add('atom-workspace', 'ava:toggle', () => this.toggle()));

this.subscriptions.add(
atom.commands.add('atom-workspace', 'ava:run', () => this.run()));
},
initRunnerProcess() {
this.testRunnerProcess = new TestRunnerProcess();
this.testRunnerProcess.on('assert', result => this.panel.renderAssert(result));
this.testRunnerProcess.on('complete', results => this.panel.renderFinalReport(results));
},
initUI() {
this.panel = new Panel(this);
this.panel.renderBase();
this.atomPanel = atom.workspace.addRightPanel({item: this.panel, visible: false});
},
canRun() {
return (atom.workspace.getActiveTextEditor() && this.testRunnerProcess.canRun());
},
run() {
if (!this.atomPanel.isVisible()) {
this.toggle();
}
if (!this.canRun()) {
return;
}

const editor = atom.workspace.getActiveTextEditor();
const currentFileName = editor.buffer.file.path;
const folder = path.dirname(currentFileName);
const file = path.basename(currentFileName);

this.panel.renderStartProcess(file);
this.testRunnerProcess.run(folder, file);
},
toggle() {
if (this.atomPanel.isVisible()) {
this.atomPanel.hide();
} else {
this.atomPanel.show();
this.run();
}
},
closePanel() {
this.atomPanel.hide();
},
deactivate() {
this.subscriptions.dispose();
this.panel.destroy();
},
serialize() {
this.atomAva = this.panel.serialize();
}
};
96 changes: 96 additions & 0 deletions lib/panel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/** @babel */
/* global __dirname */

import path from 'path';
import fs from 'fs';
import HtmlRendererHelper from './html-renderer-helper';

class Panel {
constructor(pluginInstance, htmlRendererHelper = new HtmlRendererHelper()) {
this.htmlRendererHelper = htmlRendererHelper;
this.pluginInstance = pluginInstance;
this.loadingSelector = 'sk-three-bounce';
}

renderBase() {
this.element = document.createElement('div');
this.element.classList.add('ava');

const resolvedPath = path.resolve(__dirname, '../views/panel.html');
this.element.innerHTML = fs.readFileSync(resolvedPath);

this.testsContainer = this.element.getElementsByClassName('tests-container')[0];
const closeIcon = this.element.getElementsByClassName('close-icon')[0];
closeIcon.addEventListener('click', e => this.pluginInstance.closePanel(e), false);
}

renderAssert(assertResult) {
const assert = assertResult.assert;

const newTest = this.htmlRendererHelper.createContainer('test');
newTest.classList.add(this._getCssClassForAssert(assert));
newTest.textContent = `${assert.name}`;
this.testsContainer.appendChild(newTest);

this._updateTestStatisticSection(assertResult);
}

_getCssClassForAssert(assert) {
if (assert.ok) {
return (assert.skip) ? 'skipped' : 'ok';
}
return (assert.todo) ? 'todo' : 'ko';
}

renderFinalReport(results) {
this.hideExecutingIndicator();

const summary = this.htmlRendererHelper.createContainer('summary');
const passed = results.pass - (results.skip ? results.skip : 0);
const percentage = Math.round((passed / results.count) * 100);
summary.textContent = `${results.count} total - ${percentage}% passed`;

this.testsContainer.appendChild(summary);
}

cleanTestsContainer() {
this.testsContainer.innerHTML = '';
}

renderStartProcess(fileName) {
const fileHeader = document.getElementById('file-header');
fileHeader.textContent = fileName;

this.displayExecutingIndicator();
this.cleanTestsContainer();
}

displayExecutingIndicator() {
const executing = document.getElementById(this.loadingSelector);
if (executing) {
executing.style.display = 'block';
}
}

hideExecutingIndicator() {
const executing = document.getElementById(this.loadingSelector);
if (executing) {
executing.style.display = 'none';
}
}

_updateTestStatisticSection(assertResult) {
const passedContainer = document.getElementById('passed');
const failedContainer = document.getElementById('failed');
passedContainer.textContent = assertResult.currentExecution.passed;
failedContainer.textContent = assertResult.currentExecution.failed;
}

serialize() { }

destroy() {
this.element.remove();
}
}

module.exports = Panel;
11 changes: 11 additions & 0 deletions lib/parser-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @babel */

import parser from 'tap-parser';

class ParserFactory {
getParser() {
return parser();
}
}

module.exports = ParserFactory;
47 changes: 47 additions & 0 deletions lib/terminal-command-executor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** @babel */

import EventEmitter from 'events';
import ChildProcess from 'child_process';

class TerminalCommandExecutor extends EventEmitter {
constructor() {
super();
EventEmitter.call(this);

this.dataReceivedEventName = 'dataReceived';
this.dataFinishedEventName = 'dataFinished';
}

run(command, destinyFolder = null) {
this.command = command;
this.destinyFolder = destinyFolder;

const spawn = ChildProcess.spawn;

this.terminal = spawn('bash', ['-l']);
this.terminal.on('close', statusCode => this._streamClosed(statusCode));
this.terminal.stdout.on('data', data => this._stdOutDataReceived(data));
this.terminal.stderr.on('data', data => this._stdErrDataReceived(data));

const terminalCommand = this.destinyFolder ?
`cd \"${this.destinyFolder}\" && ${this.command}\n` :
`${this.command}\n`;

this.terminal.stdin.write(terminalCommand);
this.terminal.stdin.write('exit\n');
}

_stdOutDataReceived(newData) {
this.emit(this.dataReceivedEventName, newData.toString());
}

_stdErrDataReceived(newData) {
this.emit(this.dataReceivedEventName, newData.toString());
}

_streamClosed(code) {
this.emit(this.dataFinishedEventName, code);
}
}

module.exports = TerminalCommandExecutor;
81 changes: 81 additions & 0 deletions lib/test-runner-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/** @babel */

import EventEmitter from 'events';
import TerminalCommandExecutor from './terminal-command-executor';
import ParserFactory from './parser-factory';

class TestRunnerProcess extends EventEmitter {
constructor(
executor = new TerminalCommandExecutor(),
parserFactory = new ParserFactory()) {
super();

this.eventHandlers = {};
this.terminalCommandExecutor = executor;
this.parserFactory = parserFactory;

this.terminalCommandExecutor.on('dataReceived', data => this._addAvaOutput(data));
this.terminalCommandExecutor.on('dataFinished', () => this._endAvaOutput());

EventEmitter.call(this);
}

canRun() {
return !this.isRunning;
}

run(folder, file) {
if (!this.canRun()) {
return;
}

this.isRunning = true;
this.currentExecution = {passed: 0, failed: 0};

this.parser = this.parserFactory.getParser();
this._setHandlersOnParser(this.parser);

const command = `ava ${file} --tap`;

this.terminalCommandExecutor.run(command, folder);
}

_setHandlersOnParser(parser) {
const instance = this;
parser.on('assert', assert => {
instance._updateCurrentExecution(assert);
const result = {
currentExecution: this.currentExecution, assert
};
instance.emit('assert', result);
});

parser.on('complete', results => this.emit('complete', results));
}

_updateCurrentExecution(assert) {
if (assert.ok) {
if (!assert.skip) {
this.currentExecution.passed++;
}
} else if (!assert.todo) {
this.currentExecution.failed++;
}
}

_addAvaOutput(data) {
this.parser.write(data);
}

_endAvaOutput() {
this.parser.end();
this.isRunning = false;
}

destroy() {
this.isRunning = false;
this.terminalCommandExecutor.destroy();
}
}

module.exports = TestRunnerProcess;
Binary file added media/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "ava",
"version": "0.2.0",
"description": "Snippets for AVA",
"main": "./lib/main",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just rename main.js to index.js and use "main": "lib",

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not working for me... not sure if I'm missing something.

"license": "MIT",
"repository": "sindresorhus/atom-ava",
"private": true,
Expand All @@ -28,17 +29,33 @@
"scripts": {
"test": "xo"
},
"activationCommands": {
"atom-workspace": [
"ava:toggle",
"ava:run"
]
},
"keywords": [
"snippets",
"test",
"runner",
"ava",
"mocha"
],
"dependencies": {
"tap-parser": "^1.2.2",
"ava": "^0.13.0"
},
"devDependencies": {
"xo": "*"
},
"xo": {
"envs": [
"browser",
"node",
"jasmine",
"atomtest"
],
"esnext": true,
"globals": [
"atom"
Expand Down
Loading