Class: ParallelTests::RuntimeLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/parallel_tests/runtime_logger.rb

Constant Summary collapse

@@has_started =
false

Class Method Summary collapse

Class Method Details

.class_directory(suspect) ⇒ Object

Note: this is a best guess at conventional test directory structure, and may need tweaking / post-processing to match correctly for any given project



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/parallel_tests/runtime_logger.rb', line 30

def self.class_directory(suspect)
  result = "test/"

  if defined?(Rails)
    result += case suspect.superclass.name
              when "ActionDispatch::IntegrationTest"
                "integration/"
              when "ActionDispatch::PerformanceTest"
                "performance/"
              when "ActionController::TestCase"
                "functional/"
              when "ActionView::TestCase"
                "unit/helpers/"
              else
                "unit/"
              end
  end
  result
end

.class_to_filename(suspect) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/parallel_tests/runtime_logger.rb', line 51

def self.class_to_filename(suspect)
  word = suspect.to_s.dup
  return word unless word.match /^[A-Z]/ and not word.match %r{/[a-z]}

  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.gsub!(/\:\:/,'/')
  word.tr!("-", "_")
  word.downcase!
  word
end

.log(test, start_time, end_time) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/parallel_tests/runtime_logger.rb', line 4

def self.log(test, start_time, end_time)
  return if test.is_a? Test::Unit::TestSuite  # don't log for suites-of-suites

  if !@@has_started # make empty log file 
    File.open(ParallelTests.runtime_log, 'w') do end
    @@has_started = true
  end

  File.open(ParallelTests.runtime_log, 'a') do |output|
    begin
      output.flock File::LOCK_EX
      output.puts(self.message(test, start_time, end_time))
    ensure
      output.flock File::LOCK_UN
    end
  end
end

.message(test, start_time, end_time) ⇒ Object



22
23
24
25
26
# File 'lib/parallel_tests/runtime_logger.rb', line 22

def self.message(test, start_time, end_time)
  delta="%.2f" % (end_time.to_f-start_time.to_f)
  filename=class_directory(test.class) + class_to_filename(test.class) + ".rb"
  message="#{filename}:#{delta}"
end