Class: TTNT::TestTask

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

Overview

TTNT version of Rake::TestTask. Uses configuration from 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) ⇒ TestTask

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

Parameters:

  • rake_testtask (Rake::TestTask)

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



18
19
20
21
22
23
24
25
26
# File 'lib/ttnt/testtask.rb', line 18

def initialize(rake_testtask)
  @rake_testtask = rake_testtask
  # Since test_files is not exposed in Rake::TestTask
  @test_files = @rake_testtask.instance_variable_get('@test_files')

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

Instance Attribute Details

#rake_testtaskObject

An instance of ‘Rake::TestTask` passed when TTNT::TestTask is initialized



13
14
15
# File 'lib/ttnt/testtask.rb', line 13

def rake_testtask
  @rake_testtask
end

Instance Method Details

#define_anchor_taskvoid (private)

This method returns an undefined value.

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ttnt/testtask.rb', line 86

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(@rake_testtask.verbose) do
      # Make it possible to require files in this gem
      gem_root = File.expand_path('../..', __FILE__)
      args =
        "-I#{gem_root} -r ttnt/anchor " +
        "#{@rake_testtask.ruby_opts_string}"

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

#define_run_taskvoid (private)

This method returns an undefined value.

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

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ttnt/testtask.rb', line 65

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
      args =
        "#{@rake_testtask.ruby_opts_string} #{@rake_testtask.run_code} " +
        "#{tests.to_a.join(' ')} #{@rake_testtask.option_list}"
      run_ruby args
    end
  end
end

#define_tasksvoid (private)

This method returns an undefined value.

Define TTNT tasks under namespace ‘ttnt:TESTNAME’



48
49
50
51
52
53
54
55
56
57
# File 'lib/ttnt/testtask.rb', line 48

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 @rake_testtask.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.


30
31
32
33
34
# File 'lib/ttnt/testtask.rb', line 30

def expanded_file_list
  test_files = Rake::FileList[@rake_testtask.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)


41
42
43
# File 'lib/ttnt/testtask.rb', line 41

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



111
112
113
114
115
116
117
# File 'lib/ttnt/testtask.rb', line 111

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