Skip to content
This repository was 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 4 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
3 changes: 3 additions & 0 deletions keymaps/ava.cson
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'atom-workspace':
'ctrl-alt-a': 'ava:toggle'
Copy link
Member

Choose a reason for hiding this comment

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

Can you use a JSON file here instead?

'ctrl-alt-x': 'ava:run'
Copy link
Member

Choose a reason for hiding this comment

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

Is there really any point in having two separate shortcuts? I would think it's enough with just Ctrl+Alt+A which toggles and runs tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is no big reason for having two today. I used two because they allowed me to easily test in isolation the two steps. I'll compact them in one.

If in the future we have a parsing of the test file (something that I'd like to discuss after having a nice stable first version) phase we'll need two. No need to worry about this now.

30 changes: 30 additions & 0 deletions lib/main.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{CompositeDisposable} = require 'atom'
Panel = require './panel'

module.exports = TestingForAva =
testingForAvaView: null
modalPanel: null
subscriptions: null

activate: (state) ->
@panel = new Panel(state.testingForAvaViewState)
@atomPanel = atom.workspace.addRightPanel(item: @panel, visible: false)

@subscriptions = new CompositeDisposable

@subscriptions.add atom.commands.add 'atom-workspace',
'ava:toggle': => @toggle()
'ava:run': => @panel.run()

deactivate: ->
@subscriptions.dispose()
@panel.destroy()

serialize: ->
atomAva: @panel.serialize()

toggle: ->
if @atomPanel.isVisible()
@atomPanel.hide()
else
@atomPanel.show()
66 changes: 66 additions & 0 deletions lib/panel.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
TestRunnerProcess = require './test-runner-process'

module.exports =
class Panel
constructor: (
serializedState,
testRunnerProcess = new TestRunnerProcess) ->
@testRunnerProcess = testRunnerProcess
@renderBase()

renderBase: ->
@element = @createElement('div', 'ava')

message = @createElement('div', 'message')
message.textContent = "AVA test runner"
@element.appendChild(message)

@executing = @createElement('div', 'executing')
@executing.textContent = 'Loading'
@executing.style.display = 'none'
@element.appendChild(@executing)

@testsContainer = @createElement('div', 'test-container')
@element.appendChild(@testsContainer)

run: ->
@toggleExecutingIndicator()
@testsContainer.innerHTML = ''
editor = atom.workspace.getActiveTextEditor()
currentFileName = editor.buffer.file.path
@testRunnerProcess.run currentFileName, @renderAssert, @renderFinalReport

renderAssert: (result) =>
newTest = @createElement('div', 'test')
status = if result.ok then 'OK' else 'NO'
newTest.textContent = "#{status} - #{result.name}"
@testsContainer.appendChild newTest

renderFinalReport: (results) =>
@toggleExecutingIndicator()
summary = @createElement('div', 'summary')
percentage = Math.round((results.pass/results.count)*100)
summary.textContent = "#{results.count} total - #{percentage}% passed"
@testsContainer.appendChild summary

createElement: (elementType, cssClass = null) ->
element = document.createElement(elementType)
if (cssClass?)
element.classList.add(cssClass)
element

toggleExecutingIndicator: =>
if (@executing.style.display is 'block')
@executing.style.display = 'none'
else
@executing.style.display = 'block'

# Returns an object that can be retrieved when package is activated
serialize: ->

# Tear down any state and detach
destroy: ->
@element.remove()

getElement: ->
@element
43 changes: 43 additions & 0 deletions lib/terminal-command-executor.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{Emitter} = require 'event-kit'
ChildProcess = require 'child_process'

module.exports =
class TerminalCommandExecutor
constructor: ->
@emitter = new Emitter

run: (command, destinyFolder = null) ->
@command = command
@destinyFolder = destinyFolder

spawn = ChildProcess.spawn

terminal = spawn("bash", ["-l"])
terminal.on 'close', @streamClosed
terminal.stdout.on 'data', @stdOutDataReceived
terminal.stderr.on 'data', @stdErrDataReceived

terminalCommand = if @destinyFolder then "cd \"#{@destinyFolder}\" && #{@command}\n" else "#{@command}\n"

console.log "Launching command to terminal: #{terminalCommand}"

terminal.stdin.write(terminalCommand)
terminal.stdin.write("exit\n")

stdOutDataReceived: (newData) =>
@emitter.emit 'onStdOutData', newData.toString()

stdErrDataReceived: (newData) =>
@emitter.emit 'onStdErrData', newData.toString()

streamClosed: (code) =>
@emitter.emit 'onFinishData', code

onDataReceived: (callback) ->
@emitter.on 'onStdOutData', callback

onDataFinished: (callback) ->
@emitter.on 'onFinishData', callback

destroy: ->
@emitter.dispose()
31 changes: 31 additions & 0 deletions lib/test-runner-process.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Parser = require 'tap-parser'
TerminalCommandExecutor = require './terminal-command-executor'

module.exports =
class TestRunnerProcess
constructor: (executor = new TerminalCommandExecutor) ->
@terminalCommandExecutor = executor
@terminalCommandExecutor.onDataReceived (data) => @addAvaOutput(data)
@terminalCommandExecutor.onDataFinished => @endAvaOutput()

run: (filePath, assertCallback, completeCallback) ->
@parser = @getParser()
@parser.on('assert', assertCallback)
@parser.on('complete', completeCallback)

#TODO: Fix parsing of folders and files
folder = filePath.substring(0, filePath.lastIndexOf("/") + 1);
file = filePath.split("/").pop()

command = "ava #{file} --tap"
Copy link
Member

Choose a reason for hiding this comment

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

Would be better to use the AVA programmatic API. Example: https://github.com/pine613/fly-ava/blob/master/index.js

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Of course! Will do!.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@sindresorhus, sorry if I'm missing something here.

When trying to use the programmatic API providing an absolute path for running a test I'm getting 'No tests found in ../x/y.js, make sure to import "ava" at the top of your test file'. Same result if I use the CLI in a folder different from the one that hosts the file, as:

  • ava /Users/Me/Folder/test.js --verbose

I have been able to trace the code till test-worker.js but being honest, I don't know why it's failing. Any idea?. Maybe I'm doing something wrong...

Thank you! :)

Copy link
Member

Choose a reason for hiding this comment

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

AVA 0.13.0 changed the API regarding how files are added. Ensure you pass them to the .run() method: https://github.com/sindresorhus/ava/blob/6d5f3225cc712f10e7a0a8a9c6e40d574fd4b251/test/api.js#L23-L27

Same result if I use the CLI in a folder different from the one that hosts the file, as:

I can reproduce too. Would you mind opening an issue on AVA?

~/dev/chalk master
❯ ava /Users/sindresorhus/dev/acosh/test.js

  ✖ No tests found in ../acosh/test.js, make sure to import "ava" at the top of your test file

As a dirty workaround, for now you could probably do this sindresorhus/atom-linter-xo@5d85618.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No worries, I will open the issue!. I thought that it was more likely to be something wrong with my environment. I saw the change in the API and was providing the files to the run() method.

I'll take a look at the workaround. Thanks :).


@terminalCommandExecutor.run(command, folder)

addAvaOutput: (data) ->
@parser.write(data)

endAvaOutput: ->
@parser.end()

getParser: ->
Parser()
8 changes: 8 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,13 +29,20 @@
"scripts": {
"test": "xo"
},
"activationCommands": {
"atom-workspace": "ava:toggle"
},
"keywords": [
"snippets",
"test",
"runner",
"ava",
"mocha"
],
"dependencies": {
"event-kit": "^1.1.0",
"tap-parser": ">=v1.2.2"
Copy link
Member

Choose a reason for hiding this comment

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

>=v1.2.2 => ^1.2.2

},
"devDependencies": {
"xo": "*"
},
Expand Down
31 changes: 31 additions & 0 deletions spec/fake-spawn.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports =
class FakeSpawn
self = []
constructor: ->
self = @
@commandsReceived = []

on: (event, callback) ->
@mainCallBack = callback

emulateClose: ->
@mainCallBack(0)

stdout: {
write: (data) ->
@stdOutCallBack data
on: (event, callback) ->
@stdOutCallBack = callback
}

stderr: {
write: (data) ->
@stdErrCallBack data
on: (event, callback) ->
@stdErrCallBack = callback
}

stdin: {
write: (command) =>
self.commandsReceived.push command
}
28 changes: 28 additions & 0 deletions spec/main-spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Main = require '../lib/main'

describe "TestingForAva", ->
packageName = 'ava'
mainSelector = '.ava'
toggleCommand = 'ava:toggle'
[workspaceElement, activationPromise] = []

beforeEach ->
workspaceElement = atom.views.getView(atom.workspace)
activationPromise = atom.packages.activatePackage(packageName)

describe "when the ava:toggle event is triggered", ->
it "hides and shows the view", ->
jasmine.attachToDOM(workspaceElement)

expect(workspaceElement.querySelector(mainSelector)).not.toExist()

atom.commands.dispatch workspaceElement, toggleCommand

waitsForPromise ->
activationPromise

runs ->
mainElement = workspaceElement.querySelector(mainSelector)
expect(mainElement).toBeVisible()
atom.commands.dispatch workspaceElement, toggleCommand
expect(mainElement).not.toBeVisible()
38 changes: 38 additions & 0 deletions spec/terminal-command-executor-spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
TerminalCommandExecutor = require '../lib/terminal-command-executor'
ChildProcess = require 'child_process'
FakeSpawn = require './fake-spawn'

describe 'TerminalCommandExecutor', ->
[executor, fake, exec, stdOutData, exitCode] = {}

beforeEach ->
stdOutData = ''
exitCode = -1
fake = new FakeSpawn
executor = new TerminalCommandExecutor
spyOn(ChildProcess, 'spawn').andReturn(fake)

it 'can be created', ->
expect(executor).not.toBeNull()

it 'writes the command and exits if not destination folder is provided', ->
executor.run 'command'
expect(fake.commandsReceived[0]).toBe('command\n')
expect(fake.commandsReceived[1]).toBe('exit\n')

it 'writes the folder, command and exits if folder is provided', ->
executor.run 'command', 'dir'
expect(fake.commandsReceived[0]).toBe('cd "dir" && command\n')
expect(fake.commandsReceived[1]).toBe('exit\n')

it 'calls the callback when new data appears in stdout', ->
executor.run 'command'
executor.onDataReceived (data) -> stdOutData = data
fake.stdout.write('some data')
expect(stdOutData).toBe('some data')

it 'calls the callback when the stream is closed', ->
executor.run 'command'
executor.onDataFinished (code) -> exitCode = code
fake.emulateClose()
expect(exitCode).toBe(0)
36 changes: 36 additions & 0 deletions spec/test-runner-process-spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
TestRunnerProcess = require '../lib/test-runner-process'
TerminalCommandExecutor = require '../lib/terminal-command-executor'

describe 'TestRunnerProcess', ->
[runner, executor, parser] = {}

beforeEach ->
executor = new TerminalCommandExecutor
parser = ((completeCallBack) ->
on: (eventName, callBack) ->
write: ->
end: () ->)()

runner = new TestRunnerProcess(executor)
spyOn(runner, 'getParser').andReturn(parser)

it 'can be created', ->
expect(runner).not.toBeNull()

it 'runs the executor with the appropriate parameters', ->
spyOn(atom.project, 'getPaths').andReturn(['path'])
spyOn(executor, 'run')
runner.run('/somefolder/filename')
expect(executor.run).toHaveBeenCalledWith('ava filename --tap', '/somefolder/')

it 'redirects the output for the parser when is received', ->
spyOn(parser, 'write')
runner.run('/somefolder/filename')
executor.stdOutDataReceived 'newdata'
expect(parser.write).toHaveBeenCalledWith('newdata')

it 'closes the parser stream when the output is over', ->
spyOn(parser, 'end')
runner.run('/somefolder/filename')
executor.streamClosed 0
expect(parser.end).toHaveBeenCalled()
11 changes: 11 additions & 0 deletions styles/ava.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@import "ui-variables";
Copy link
Member

Choose a reason for hiding this comment

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

single-quotes


.ava {
padding: 30px;
font-size: 15px;

.message { padding-bottom: 10px; }
.summary { font-size: 12px; padding-top: 10px; }
.test { font-size: 11px; font-weight: bold; }
.executing { font-size: 12px; font-weight: bold; padding-bottom: 10px;}
Copy link
Member

Choose a reason for hiding this comment

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

use tab indentation

}