Class: Rake::RubyBreakerTestTask

Inherits:
TestTask
  • Object
show all
Defined in:
lib/rubybreaker/task.rb

Overview

This class can be used as a replacement for Rake::TestTask. It is a subclass of Rake::TestTask and maintains additional information for running RubyBreaker as a Rake test task.

For example, the following shows how to run RubyBreaker in a test task:

desc “Run testtask test” Rake::RubyBreakerTestTask.new(:“testtask_test”) do |t|

t.libs << "lib"
t.test_files = ["test/testtask/tc_testtask.rb"]
t.breakable = ["SampleClassA"]

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(taskname = "", *args, &blk) ⇒ RubyBreakerTestTask

This overrides the testtask’s constructor. In addition to the original behavior, it keeps track of RubyBreaker options and store them in a yaml file.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubybreaker/task.rb', line 33

def initialize(taskname="", *args, &blk)

  # Initialize extra instance variables
  @rubybreaker_opts = []
  @breakable = nil

  # Call the original constructor first
  super(taskname, *args, &blk)

  # Parse the RubyBreaker options
  case @rubybreaker_opts
  when Array
    opts = @rubybreaker_opts
  when String
    opts = @rubybreaker_opts.split(" ").select {|v| v != ""}
  else
    opts = []
  end

  # Construct the task configuration hash
  config = {
    name: taskname,
    rubybreaker_opts: opts,
    breakable: [], # Set doesn't work well with YAML; just use an array
    test_files: @test_files,
  }

  # This allows a bulk declaration of Breakable modules/classes
  @breakable.each { |b| config[:breakable] << b } if @breakable

  # This code segment is a clever way to store yaml data in a ruby file
  # that reads its own yaml data after __END__ when loaded.
  code_data = <<-EOS
require "yaml"
f = File.new(__FILE__, "r")
while !(f.readline.match("^__END__.*$"))
  # do nothing
end
data = f.read
$__rubybreaker_task = YAML.load(data)
__END__
#{YAML.dump(config)}
  EOS

  tmp_path = ""
  # Tests are run different processes, so we must export this
  # information to an external yaml file.
  f = Tempfile.new(["#{taskname}",".rb"]) 
  tmp_path = f.path
  f.write(code_data)
  f.close()

  # Inject the -r option to load this yaml file
  if @ruby_opts && @ruby_opts.empty?
    @ruby_opts << "-r" << tmp_path
  else
    @ruby_opts = ["-r", tmp_path]
  end

  return self
end

Instance Attribute Details

#breakableObject

List of Breakable modules/classes



25
26
27
# File 'lib/rubybreaker/task.rb', line 25

def breakable
  @breakable
end

#rubybreaker_optsObject

RubyBreaker options



28
29
30
# File 'lib/rubybreaker/task.rb', line 28

def rubybreaker_opts
  @rubybreaker_opts
end