Class: Ruby

Inherits:
Object
  • Object
show all
Defined in:
lib/kicker/recipes/ruby.rb

Direct Known Subclasses

Rails

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files) ⇒ Ruby

:nodoc:



96
97
98
99
# File 'lib/kicker/recipes/ruby.rb', line 96

def initialize(files) #:nodoc:
  @files = files
  @tests = []
end

Class Attribute Details

.runner_binObject

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' : 'spec'
end

.test_cases_rootObject

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_optionsObject

Assigns extra options that are to be passed on to the runner_bin.

Ruby.test_options << '-I ./lib/foo'


41
42
43
# File 'lib/kicker/recipes/ruby.rb', line 41

def test_options
  @test_options ||= []
end

.test_typeObject

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

#testsObject (readonly)

The list of collected tests.



94
95
96
# File 'lib/kicker/recipes/ruby.rb', line 94

def tests
  @tests
end

Class Method Details

.call(files) ⇒ Object

:nodoc:



87
88
89
90
91
# File 'lib/kicker/recipes/ruby.rb', line 87

def self.call(files) #:nodoc:
  handler = new(files)
  handler.handle!
  run_tests(handler.tests)
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’.



48
49
50
# File 'lib/kicker/recipes/ruby.rb', line 48

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.

If you want to adjust the logging, stdout and growl, override this, call spec_runner_command with the tests to get the command and call execute with the custom logging block.



78
79
80
81
82
83
84
# File 'lib/kicker/recipes/ruby.rb', line 78

def run_with_spec_runner(tests)
  execute(spec_runner_command(tests)) do |status|
    if status.after? && status.growl?
      status.output.split("\n").last
    end
  end
end

.run_with_test_runner(tests) ⇒ Object

Runs the given tests with ‘ruby’ as unit-test tests.

If you want to adjust the logging, stdout and growl, override this, call test_runner_command with the tests to get the command and call execute with the custom logging block.



61
62
63
64
65
66
67
# File 'lib/kicker/recipes/ruby.rb', line 61

def run_with_test_runner(tests)
  execute(test_runner_command(tests)) do |status|
    if status.after? && status.growl?
      status.output.split("\n").last
    end
  end
end

.spec_runner_command(tests) ⇒ Object



69
70
71
# File 'lib/kicker/recipes/ruby.rb', line 69

def spec_runner_command(tests)
  "#{runner_bin} #{test_options.join(' ')} #{tests.join(' ')}"
end

.test_runner_command(tests) ⇒ Object



52
53
54
# File 'lib/kicker/recipes/ruby.rb', line 52

def test_runner_command(tests)
  "#{runner_bin} #{test_options.join(' ')} -r #{tests.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.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/kicker/recipes/ruby.rb', line 128

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_binObject

A shortcut to Ruby.runner_bin.



107
108
109
# File 'lib/kicker/recipes/ruby.rb', line 107

def runner_bin
  self.class.runner_bin
end

#test_cases_rootObject

A shortcut to Ruby.test_cases_root.



112
113
114
# File 'lib/kicker/recipes/ruby.rb', line 112

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


121
122
123
124
# File 'lib/kicker/recipes/ruby.rb', line 121

def test_file(name)
  file = File.join(test_cases_root, "#{name}_#{test_type}.rb")
  file if File.exist?(file)
end

#test_typeObject

A shortcut to Ruby.test_type.



102
103
104
# File 'lib/kicker/recipes/ruby.rb', line 102

def test_type
  self.class.test_type
end