Class: TTNT::TestTask

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/ttnt/testtask.rb

Overview

TTNT version of Rake::TestTask.

You can use the configuration from a Rake::TestTask to minimize user configuration.

Defines TTNT related rake tasks when instantiated.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rake_testtask = nil) {|_self| ... } ⇒ TestTask

Create an instance of TTNT::TestTask and define TTNT rake tasks.

Parameters:

  • rake_testtask (Rake::TestTask) (defaults to: nil)

    an instance of Rake::TestTask after user configuration is done

Yields:

  • (_self)

Yield Parameters:



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ttnt/testtask.rb', line 22

def initialize(rake_testtask = nil)
  @rake_testtask = rake_testtask || Rake::TestTask.new

  # There's no `test_files` method so we can't delegate it
  # to the internal task through `method_missing`.
  @test_files = @rake_testtask.instance_variable_get('@test_files')

  yield self if block_given?

  @anchor_description = 'Generate test-to-code mapping' + (name == :test ? '' : " for #{name}")
  @run_description = 'Run selected tests' + (name = :test ? '' : " for #{name}")
  define_tasks
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Delegate missing methods to the internal task so we can override the defaults during the block execution.



39
40
41
# File 'lib/ttnt/testtask.rb', line 39

def method_missing(method, *args, &block)
  @rake_testtask.public_send(method, *args, &block)
end

Instance Attribute Details

#code_filesObject

Returns the value of attribute code_files.



16
17
18
# File 'lib/ttnt/testtask.rb', line 16

def code_files
  @code_files
end

#rake_testtaskObject

Returns the value of attribute rake_testtask.



15
16
17
# File 'lib/ttnt/testtask.rb', line 15

def rake_testtask
  @rake_testtask
end

#test_filesObject

Returns the value of attribute test_files.



16
17
18
# File 'lib/ttnt/testtask.rb', line 16

def test_files
  @test_files
end

Instance Method Details

#define_anchor_taskvoid (private)

This method returns an undefined value.

Define a task which runs tests file by file, and generate and save test-to-code mapping.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ttnt/testtask.rb', line 118

def define_anchor_task
  desc @anchor_description
  task 'anchor' do
    # In order to make it possible to stop coverage services like Coveralls
    # which interferes with ttnt/anchor because both use coverage library.
    # See test/test_helper.rb
    ENV['ANCHOR_TASK'] = '1'

    Rake::FileUtilsExt.verbose(verbose) do
      # Make it possible to require files in this gem
      gem_root = File.expand_path('../..', __FILE__)
      args =
        "-I#{gem_root} -r ttnt/anchor " +
        "#{ruby_opts_string}"

      expanded_file_list.each do |test_file|
        run_ruby "#{args} #{test_file}"
      end
    end

    if @code_files
      mapping = TestToCodeMapping.new(repo)
      mapping.select_code_files!(@code_files)
      mapping.write!
    end
  end
end

#define_run_taskvoid (private)

This method returns an undefined value.

Define a task which runs only tests which might have been affected from changes between anchored commit and TARGET_SHA.

TARGET_SHA can be specified as an environment variable (defaults to HEAD).



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ttnt/testtask.rb', line 88

def define_run_task
  desc @run_description
  task 'run' do
    target_sha = ENV['TARGET_SHA']
    ts = TTNT::TestSelector.new(repo, target_sha, expanded_file_list)
    tests = ts.select_tests!

    if tests.empty?
      STDERR.puts 'No test selected.'
    else
      if ENV['ISOLATED']
        tests.each do |test|
          args = "#{ruby_opts_string} #{test} #{option_list}"
          run_ruby args
          break if @failed && ENV['FAIL_FAST']
        end
      else
        args =
          "#{ruby_opts_string} #{run_code} " +
          "#{tests.to_a.join(' ')} #{option_list}"
        run_ruby args
      end
    end
  end
end

#define_tasksvoid (private)

This method returns an undefined value.

Define TTNT tasks under namespace ‘ttnt:TESTNAME’



71
72
73
74
75
76
77
78
79
80
# File 'lib/ttnt/testtask.rb', line 71

def define_tasks
  # Task definitions are taken from Rake::TestTask
  # https://github.com/ruby/rake/blob/e644af3/lib/rake/testtask.rb#L98-L112
  namespace :ttnt do
    namespace name do
      define_run_task
      define_anchor_task
    end
  end
end

#expanded_file_listObject

Returns array of test file names.

Unlike Rake::TestTask#file_list, patterns are expanded.


53
54
55
56
57
# File 'lib/ttnt/testtask.rb', line 53

def expanded_file_list
  test_files = Rake::FileList[pattern].compact
  test_files += @test_files.to_a if @test_files
  test_files
end

#repoRugged::Reposiotry (private)

Git repository discovered from current directory

Returns:

  • (Rugged::Reposiotry)


64
65
66
# File 'lib/ttnt/testtask.rb', line 64

def repo
  @repo ||= Rugged::Repository.discover('.')
end

#run_ruby(args) ⇒ Object (private)

Run ruby process with given args

Parameters:

  • args (String)

    argument to pass to ruby



149
150
151
152
153
154
155
156
# File 'lib/ttnt/testtask.rb', line 149

def run_ruby(args)
  ruby "#{args}" do |ok, status|
    @failed = true if !ok
    if !ok && status.respond_to?(:signaled?) && status.signaled?
      raise SignalException.new(status.termsig)
    end
  end
end