From 04135c2e0521b1f72048e52584a0d8be69a284d6 Mon Sep 17 00:00:00 2001 From: Roy Choo Date: Mon, 20 Apr 2015 17:10:14 +0800 Subject: [PATCH] removing trailing white space fixing bugs that causes rspec file to match minitest adding more tests adding initial tests adding support for minitest removing deprecated api removing console logs --- lib/ruby-test.coffee | 9 +++++ lib/source-info.coffee | 59 +++++++++++++++++++++++++++- lib/test-runner.coffee | 3 +- spec/source-info-spec.coffee | 76 ++++++++++++++++++++++++++++++++++++ spec/test-runner-spec.coffee | 8 ++++ 5 files changed, 152 insertions(+), 3 deletions(-) diff --git a/lib/ruby-test.coffee b/lib/ruby-test.coffee index 3b8b927..e5684a5 100644 --- a/lib/ruby-test.coffee +++ b/lib/ruby-test.coffee @@ -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" diff --git a/lib/source-info.coffee b/lib/source-info.coffee index 4b72032..49e90ee 100644 --- a/lib/source-info.coffee +++ b/lib/source-info.coffee @@ -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' @@ -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 @@ -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] diff --git a/lib/test-runner.coffee b/lib/test-runner.coffee index d8ff84f..c9aa42f 100644 --- a/lib/test-runner.coffee +++ b/lib/test-runner.coffee @@ -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() diff --git a/spec/source-info-spec.coffee b/spec/source-info-spec.coffee index 292d50a..e91186a 100644 --- a/spec/source-info-spec.coffee +++ b/spec/source-info-spec.coffee @@ -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 = -> @@ -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) -> @@ -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() diff --git a/spec/test-runner-spec.coffee b/spec/test-runner-spec.coffee index 9aaa5d2..2ded1b8 100644 --- a/spec/test-runner-spec.coffee +++ b/spec/test-runner-spec.coffee @@ -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}') @@ -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/\"")