Skip to content

Commit

Permalink
Merge pull request #49 from roychoo/minitest
Browse files Browse the repository at this point in the history
adding support for minitest
  • Loading branch information
moxley committed May 25, 2015
2 parents a96dc90 + 04135c2 commit ccc8ffb
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 3 deletions.
9 changes: 9 additions & 0 deletions lib/ruby-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ RubyTestView = require './ruby-test-view'

module.exports =
config:
minitestAllCommand:
type: 'string'
default: "ruby -I test test"
minitestFileCommand:
type: 'string'
default: "ruby -I test {relative_path}"
minitestSingleCommand:
type: 'string'
default: "ruby {relative_path} -n \"/{regex}/\""
testAllCommand:
type: 'string'
default: "ruby -I test test"
Expand Down
59 changes: 57 additions & 2 deletions lib/source-info.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module.exports =
test: 'test'
spec: 'rspec'
feature: 'cucumber'
minitest: 'minitest'

matchers:
method: /def\s(.*?)$/
spec: /(?:"|')(.*?)(?:"|')/

currentShell: ->
atom.config.get('ruby-test.shell') || 'bash'
Expand Down Expand Up @@ -37,6 +42,53 @@ module.exports =
else
null

minitestRegExp: (text, type)->
@_minitestRegExp ||= unless @_minitestRegExp
value = text.match(@matchers[type]) if text?
if value
value[1]
else
""

isMiniTest: ->
editor = atom.workspace.getActiveTextEditor()
i = @currentLine() - 1
regExp = null
isSpec = false
isUnit = false
isRSpec = false
specRegExp = new RegExp(/^(\s+)(should|test|it)\s+['""'](.*)['""']\s+do\s*(?:#.*)?$/)
rspecRequireRegExp = new RegExp(/^require(\s+)['"]spec_helper['"]$/)
minitestClassRegExp = new RegExp(/class\s(.*)<(\s?|\s+)Minitest::Test/)
minitestMethodRegExp = new RegExp(/^(\s+)def\s(.*)$/)
while i >= 0
text = editor.lineTextForBufferRow(i)
# check if it is rspec or minitest spec
if !regExp && specRegExp.test(text)
isSpec = true
regExp = text
# check if it is minitest unit
else if !regExp && minitestMethodRegExp.test(text)
isUnit = true
regExp = text

# if it is spec and has require spec_helper which means it is rspec spec
else if rspecRequireRegExp.test(text)
isRSpec = true
break
# if it is unit test and inherit from Minitest::Unit
else if isUnit && minitestClassRegExp.test(text)
@minitestRegExp(regExp, "method")
return true

i--

if !isRSpec && isSpec
@minitestRegExp(regExp, "spec")
return true

return false

testFramework: ->
@_testFramework ||= unless @_testFramework
(t = @fileType()) and @frameworkLookup[t] or
Expand All @@ -46,8 +98,11 @@ module.exports =
@_fileType ||= if @_fileType == undefined
if not @activeFile()
null
else if matches = @activeFile().match(/_(test|spec)\.rb$/)
matches[1]
else if matches = @activeFile().match(/_?(test|spec)_?(.*)\.rb$/)
if @isMiniTest()
"minitest"
else
matches[1]
else if matches = @activeFile().match(/\.(feature)$/)
matches[1]

Expand Down
3 changes: 2 additions & 1 deletion lib/test-runner.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ module.exports =
else
@testParams.testFileCommand()
cmd.replace('{relative_path}', @testParams.activeFile()).
replace('{line_number}', @testParams.currentLine())
replace('{line_number}', @testParams.currentLine()).
replace('{regex}', @testParams.minitestRegExp())

cancel: ->
@shell.kill()
76 changes: 76 additions & 0 deletions spec/source-info-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ describe "SourceInfo", ->

setUpOpenFile = ->
editor = {buffer: {file: {path: "foo_test.rb"}}}
cursor =
getBufferRow: ->
99
editor.getLastCursor = -> cursor
editor.lineTextForBufferRow = (line) ->
""
spyOn(atom.workspace, 'getActiveTextEditor').andReturn(editor)

setUpWithOpenFile = ->
Expand All @@ -52,6 +58,13 @@ describe "SourceInfo", ->
setUpWithoutOpenFile()
expect(sourceInfo.cwd()).toBe("fooPath")

describe "::fileType", ->
it "correctly detects a minitest file", ->
setUpWithOpenFile()
editor.lineTextForBufferRow = (line) ->
" it \"test something\" do"
expect(sourceInfo.fileType()).toBe("minitest")

describe "::projectType", ->
it "correctly detects a test directory", ->
spyOn(fs, 'existsSync').andCallFake (filePath) ->
Expand Down Expand Up @@ -126,6 +139,69 @@ describe "SourceInfo", ->
setUpWithoutOpenFile()
expect(sourceInfo.currentLine()).toBeNull()

describe "::minitestRegExp", ->
it "correctly returns the matching regex for spec", ->
setUpWithoutOpenFile()
expect(sourceInfo.minitestRegExp(" it \"test something\" do", "spec")).toBe("test something")

it "correctly returns the matching regex for minitest unit", ->
setUpWithoutOpenFile()
expect(sourceInfo.minitestRegExp(" def test_something", "method")).toBe("test_something")

it "should return empty string if no match", ->
setUpWithoutOpenFile()
expect(sourceInfo.minitestRegExp("test something", "spec")).toBe("")

describe "::isMiniTest", ->
it "correctly returns true if it is minitest spec file", ->
setUpWithOpenFile()
cursor =
getBufferRow: ->
99
editor.lineTextForBufferRow = (line) ->
if line == 99
" it \"test something\" do"
expect(sourceInfo.isMiniTest("")).toBe(true)

it "correctly returns true if it is a minitest unit file", ->
setUpWithOpenFile()
cursor =
getBufferRow: ->
10
editor.getLastCursor = -> cursor
editor.lineTextForBufferRow = (line) ->
if line == 10
" def something"
else if line == 5
"class sometest < Minitest::Test"
expect(sourceInfo.isMiniTest("")).toBe(true)

it "correctly returns false if it is a rspec file", ->
setUpWithOpenFile()
cursor =
getBufferRow: ->
10
editor.getLastCursor = -> cursor
editor.lineTextForBufferRow = (line) ->
if line == 10
" it \"test something\" do"
else if line == 5
"require \"spec_helper\""
expect(sourceInfo.isMiniTest("")).toBe(false)

it "correctly returns false if it is a unit test file", ->
setUpWithOpenFile()
cursor =
getBufferRow: ->
10
editor.getLastCursor = -> cursor
editor.lineTextForBufferRow = (line) ->
if line == 10
" def something"
else if line == 5
"class sometest < Unit::Test"
expect(sourceInfo.isMiniTest("")).toBe(false)

describe "::currentShell", ->
it "when ruby-test.shell is null", ->
setUpWithoutOpenFile()
Expand Down
8 changes: 8 additions & 0 deletions spec/test-runner-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe "TestRunner", ->
spyOn(@testRunnerParams, 'setTestInfo')
spyOn(SourceInfo.prototype, 'activeFile').andReturn('fooTestFile')
spyOn(SourceInfo.prototype, 'currentLine').andReturn(100)
spyOn(SourceInfo.prototype, 'minitestRegExp').andReturn('test foo')
spyOn(SourceInfo.prototype, 'testFileCommand').andReturn('fooTestCommand {relative_path}')
spyOn(SourceInfo.prototype, 'testSingleCommand').andReturn('fooTestCommand {relative_path}:{line_number}')

Expand All @@ -31,3 +32,10 @@ describe "TestRunner", ->
runner = new TestRunner(@testRunnerParams)
runner.run()
expect(@testRunnerParams.setTestInfo).toHaveBeenCalledWith("fooTestCommand fooTestFile:100")

it "constructs a single-minitest command when testScope is 'single'", ->
SourceInfo.prototype.testSingleCommand.andReturn('fooTestCommand {relative_path} -n \"/{regex}/\"')
@testRunnerParams.testScope = "single"
runner = new TestRunner(@testRunnerParams)
runner.run()
expect(@testRunnerParams.setTestInfo).toHaveBeenCalledWith("fooTestCommand fooTestFile -n \"/test foo/\"")

0 comments on commit ccc8ffb

Please sign in to comment.