Class: Crew::Task::Test

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/crew/task/test.rb

Defined Under Namespace

Classes: FailedException, Result

Instance Method Summary collapse

Methods included from Util

#assert, #escape, #poll, #retryable

Constructor Details

#initialize(task, test_path_prefix, index, skip, test_blk) ⇒ Test

Returns a new instance of Test.



40
41
42
# File 'lib/crew/task/test.rb', line 40

def initialize(task, test_path_prefix, index, skip, test_blk)
  @task, @test_path_prefix, @index, @skip, @test_blk = task, test_path_prefix, index, skip, test_blk
end

Instance Method Details

#clear_cached_result!Object



108
109
110
# File 'lib/crew/task/test.rb', line 108

def clear_cached_result!
  FileUtils.rm_rf(test_cache_path)
end

#has_cached_result?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/crew/task/test.rb', line 93

def has_cached_result?
  File.exist?(test_cache_path)
end

#loggerObject



119
120
121
# File 'lib/crew/task/test.rb', line 119

def logger
  @task.home.logger
end

#record_test_result(status, extras = nil) ⇒ Object



101
102
103
104
105
106
# File 'lib/crew/task/test.rb', line 101

def record_test_result(status, extras = nil)
  FileUtils.mkdir_p(File.dirname(test_cache_path))
  File.open(test_cache_path, "w") do |f|
    f << {status: status, extras: extras}.to_json
  end
end

#resultObject



44
45
46
47
48
49
50
51
# File 'lib/crew/task/test.rb', line 44

def result
  if File.exist?(test_cache_path)
    result = JSON.parse(File.read(test_cache_path))
    Result.new(@task.context.name, @task.name, @index, result['status'], result['extras'])
  else
    Result.new(@task.context.name, @task.name, @index, "missing", {})
  end
end

#run_test!(fail_fast = false) ⇒ Object



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
# File 'lib/crew/task/test.rb', line 53

def run_test!(fail_fast = false)
  test_name = "#{@task.name} ##{@index + 1}"
  logger.test(test_name) do
    if has_cached_result?
      logger.info "Test already has a cached result"
    else
      clear_cached_result!
      if @skip
        logger.skip_test test_name
        record_test_result "skip"
      else
        status = "error"
        extra = {}
        @task.reset!
        extra[:start_time] = Time.new.to_f
        begin
          @task.context.with_callbacks do
            @task.instance_eval(&@test_blk)
          end
        rescue => e
          extra.merge!(exception: {backtrace: e.backtrace, message: e.message, class: e.class.to_s})
          status = "fail"
          logger.fail_test(test_name)
        else
          status = "pass"
          logger.pass_test(test_name)
        end
        extra[:end_time] = Time.new.to_f
        record_test_result status, extra
        if status == 'fail'
          puts
          puts result
          raise FailedException if fail_fast
        end
      end
    end
  end
  result
end

#test_cache_pathObject



97
98
99
# File 'lib/crew/task/test.rb', line 97

def test_cache_path
  File.join(@test_path_prefix, "#{@index}.json")
end