class TTNT::TestTask

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.

Attributes

code_files[R]
rake_testtask[RW]
test_files[R]

Public Class Methods

new(rake_testtask = nil) { |self| ... } click to toggle source

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

@param rake_testtask [Rake::TestTask] an instance of Rake::TestTask

after user configuration is done
# 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

Public Instance Methods

code_files=(files) click to toggle source
# File lib/ttnt/testtask.rb, line 43
def code_files=(files)
  @code_files = files.kind_of?(String) ? FileList[files] : files
end
expanded_file_list() click to toggle source

Returns array of test file names.

Unlike Rake::TestTask#file_list, patterns are expanded.
# 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
method_missing(method, *args, &block) click to toggle source

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

# File lib/ttnt/testtask.rb, line 39
def method_missing(method, *args, &block)
  @rake_testtask.public_send(method, *args, &block)
end
test_files=(files) click to toggle source
# File lib/ttnt/testtask.rb, line 47
def test_files=(files)
  @test_files = files.kind_of?(String) ? FileList[files] : files
end

Private Instance Methods

define_anchor_task() click to toggle source

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

@return [void]

# 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_task() click to toggle source

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).

@return [void]

# 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_tasks() click to toggle source

Define TTNT tasks under namespace 'ttnt:TESTNAME'

@return [void]

# 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
repo() click to toggle source

Git repository discovered from current directory

@return [Rugged::Reposiotry]

# File lib/ttnt/testtask.rb, line 64
def repo
  @repo ||= Rugged::Repository.discover('.')
end
run_ruby(args) click to toggle source

Run ruby process with given args

@param args [String] argument to pass to ruby

# 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