Class: Kicker::Recipes::Ruby
- Inherits:
-
Object
- Object
- Kicker::Recipes::Ruby
- Defined in:
- lib/kicker/recipes/ruby.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.runner_bin ⇒ Object
Returns the ruby command to run the tests with.
-
.test_cases_root ⇒ Object
Returns the root directory of where test cases will be looked up.
-
.test_options ⇒ Object
Assigns extra options that are to be passed on to the runner_bin.
-
.test_type ⇒ Object
Returns the type of tests to run.
Instance Attribute Summary collapse
-
#tests ⇒ Object
readonly
The list of collected tests.
Class Method Summary collapse
-
.call(files) ⇒ Object
:nodoc:.
- .reset! ⇒ Object
-
.run_tests(tests) ⇒ Object
Runs the given tests, if there are any, with the method defined by test_type.
-
.run_with_spec_runner(tests) ⇒ Object
Runs the given tests with ‘spec’ as RSpec tests.
-
.run_with_test_runner(tests) ⇒ Object
Runs the given tests with ‘ruby’ as unit-test tests.
- .runner_command(*parts) ⇒ Object
- .spec_runner_command(tests) ⇒ Object
- .test_runner_command(tests) ⇒ Object
Instance Method Summary collapse
-
#handle! ⇒ Object
This method is called to collect tests.
-
#initialize(files) ⇒ Ruby
constructor
:nodoc:.
-
#runner_bin ⇒ Object
A shortcut to Ruby.runner_bin.
-
#test_cases_root ⇒ Object
A shortcut to Ruby.test_cases_root.
-
#test_file(name) ⇒ Object
Returns the file for
nameif it exists. -
#test_type ⇒ Object
A shortcut to Ruby.test_type.
Constructor Details
#initialize(files) ⇒ Ruby
:nodoc:
99 100 101 102 |
# File 'lib/kicker/recipes/ruby.rb', line 99 def initialize(files) #:nodoc: @files = files @tests = [] end |
Class Attribute Details
.runner_bin ⇒ Object
Returns the ruby command to run the tests with. Eg: ‘ruby’ or ‘spec’.
Defaults to ‘ruby’ if test_type is ‘test’ and ‘spec’ if test_type is ‘spec’.
22 23 24 |
# File 'lib/kicker/recipes/ruby.rb', line 22 def runner_bin @runner_bin ||= test_type == 'test' ? 'ruby' : 'rspec' end |
.test_cases_root ⇒ Object
Returns the root directory of where test cases will be looked up.
Defaults to the value of test_type. Eg: ‘test’ or ‘spec’.
32 33 34 |
# File 'lib/kicker/recipes/ruby.rb', line 32 def test_cases_root @test_cases_root ||= test_type end |
.test_options ⇒ Object
Assigns extra options that are to be passed on to the runner_bin.
Ruby. << '-I ./lib/foo'
41 42 43 |
# File 'lib/kicker/recipes/ruby.rb', line 41 def @test_options ||= [] end |
.test_type ⇒ Object
Returns the type of tests to run. Eg: ‘test’ or ‘spec’.
Defaults to ‘test’ if no ‘spec’ directory exists.
9 10 11 |
# File 'lib/kicker/recipes/ruby.rb', line 9 def test_type @test_type ||= File.exist?('spec') ? 'spec' : 'test' end |
Instance Attribute Details
#tests ⇒ Object (readonly)
The list of collected tests.
97 98 99 |
# File 'lib/kicker/recipes/ruby.rb', line 97 def tests @tests end |
Class Method Details
.call(files) ⇒ Object
:nodoc:
90 91 92 93 94 |
# File 'lib/kicker/recipes/ruby.rb', line 90 def self.call(files) #:nodoc: handler = new(files) handler.handle! run_tests(handler.tests) end |
.reset! ⇒ Object
45 46 47 48 49 50 |
# File 'lib/kicker/recipes/ruby.rb', line 45 def reset! @test_type = nil @runner_bin = nil @test_cases_root = nil @test_options = nil end |
.run_tests(tests) ⇒ Object
Runs the given tests, if there are any, with the method defined by test_type. If test_type is ‘test’ the run_with_test_runner method is used. The same applies when test_type is ‘spec’.
66 67 68 |
# File 'lib/kicker/recipes/ruby.rb', line 66 def run_tests(tests) send("run_with_#{test_type}_runner", tests) unless tests.empty? end |
.run_with_spec_runner(tests) ⇒ Object
Runs the given tests with ‘spec’ as RSpec tests.
85 86 87 |
# File 'lib/kicker/recipes/ruby.rb', line 85 def run_with_spec_runner(tests) execute(spec_runner_command(tests)) end |
.run_with_test_runner(tests) ⇒ Object
Runs the given tests with ‘ruby’ as unit-test tests.
76 77 78 |
# File 'lib/kicker/recipes/ruby.rb', line 76 def run_with_test_runner(tests) execute(test_runner_command(tests)) end |
.runner_command(*parts) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/kicker/recipes/ruby.rb', line 52 def runner_command(*parts) parts.map do |part| case part when Array part.empty? ? nil : part.join(' ') else part.to_s end end.compact.join(' ') end |
.spec_runner_command(tests) ⇒ Object
80 81 82 |
# File 'lib/kicker/recipes/ruby.rb', line 80 def spec_runner_command(tests) runner_command(runner_bin, , tests) end |
.test_runner_command(tests) ⇒ Object
70 71 72 73 |
# File 'lib/kicker/recipes/ruby.rb', line 70 def test_runner_command(tests) tests_without_ext = tests.map { |f| f[0,f.size-3] } runner_command(runner_bin, %w{ -I. } + , '-r', tests_without_ext.join(' -r '), "-e ''") end |
Instance Method Details
#handle! ⇒ Object
This method is called to collect tests. Override this if you’re subclassing and make sure to call super.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/kicker/recipes/ruby.rb', line 131 def handle! @tests.concat(@files.take_and_map do |file| case file # Match any ruby test file when /^#{test_cases_root}\/.+_#{test_type}\.rb$/ file # A file such as ./lib/namespace/foo.rb is mapped to: # * ./test/namespace/foo_test.rb # * ./test/foo_test.rb when /^lib\/(.+)\.rb$/ if namespaced = test_file($1) namespaced elsif in_test_root = test_file(File.basename(file, '.rb')) in_test_root end end end) end |
#runner_bin ⇒ Object
A shortcut to Ruby.runner_bin.
110 111 112 |
# File 'lib/kicker/recipes/ruby.rb', line 110 def runner_bin self.class.runner_bin end |
#test_cases_root ⇒ Object
A shortcut to Ruby.test_cases_root.
115 116 117 |
# File 'lib/kicker/recipes/ruby.rb', line 115 def test_cases_root self.class.test_cases_root end |
#test_file(name) ⇒ Object
Returns the file for name if it exists.
test_file('foo') # => "test/foo_test.rb"
test_file('foo/bar') # => "test/foo/bar_test.rb"
test_file('does/not/exist') # => nil
124 125 126 127 |
# File 'lib/kicker/recipes/ruby.rb', line 124 def test_file(name) file = File.join(test_cases_root, "#{name}_#{test_type}.rb") file if File.exist?(file) end |
#test_type ⇒ Object
A shortcut to Ruby.test_type.
105 106 107 |
# File 'lib/kicker/recipes/ruby.rb', line 105 def test_type self.class.test_type end |