Module: Regtest

Extended by:
Regtest, Colors
Included in:
Regtest
Defined in:
lib/regtest.rb,
lib/regtest/colors.rb,
lib/regtest/version.rb

Defined Under Namespace

Modules: Colors

Constant Summary collapse

VERSION =
'2.5.0'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colors

apply, codes, report

Class Attribute Details

.exit_codesObject (readonly)

Returns the value of attribute exit_codes.



102
103
104
# File 'lib/regtest.rb', line 102

def exit_codes
  @exit_codes
end

.log_filenamesObject (readonly)

Returns the value of attribute log_filenames.



102
103
104
# File 'lib/regtest.rb', line 102

def log_filenames
  @log_filenames
end

.resultsObject (readonly)

Returns the value of attribute results.



102
103
104
# File 'lib/regtest.rb', line 102

def results
  @results
end

.show_exceptionsObject

Flag to show exceptions on STDERR



105
106
107
# File 'lib/regtest.rb', line 105

def show_exceptions
  @show_exceptions
end

.startObject (readonly)

Returns the value of attribute start.



102
103
104
# File 'lib/regtest.rb', line 102

def start
  @start
end

Class Method Details

.check_resultsObject

Checking results, should be overwritten by SCM plugins e.g. regtest/git

Returns:

  • Symbol with check result (one of :success, :unknown_result or :fail)



136
137
138
139
# File 'lib/regtest.rb', line 136

def check_results
  report "\nPlease check result files manually. Regtest isn't able to do that.", type: :unknown_result
  :unknown_result
end

.determine_filename_from_caller(ext) ⇒ Object

Determine a filename which is derived from the filename of the “real” caller of the calling method

Parameters:

  • ext

    new extension (i.e. ‘.yml’)



110
111
112
# File 'lib/regtest.rb', line 110

def determine_filename_from_caller ext
  caller_locations(2, 1).first.path.sub(/\.rb$/, '') << ext.to_s
end

.report(*args, type: nil) ⇒ Object

Report text to output with possible type, could be overwritten by plugins e.g. regtest/colors.



143
144
145
# File 'lib/regtest.rb', line 143

def report *args, type: nil
  puts *args
end

.report_statisticsObject

Report some statistics, could be overwritten by plugins.



115
116
117
118
119
120
# File 'lib/regtest.rb', line 115

def report_statistics
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  time = now - start
  sample_count = results.values.map(&:size).reduce(0, &:+)
  report format("\n\n%d samples executed in %.2f s (%d samples/s)", sample_count, time, sample_count / time), type: :statistics
end

.saveObject

Save all results to the corresponding files.



123
124
125
126
127
128
129
130
131
# File 'lib/regtest.rb', line 123

def save
  results.each_pair do |filename, arr|
    File.open(filename, 'w') do |f|
      arr.each do |h|
        f.write h.to_yaml
      end
    end
  end
end

Instance Method Details

#combinations(hashy) ⇒ Object

Build all combinations of a Hash-like object with arrays as values. Return value is an array of OpenStruct instances.

Example:

require 'ostruct'
require 'regtest'

o = OpenStruct.new
o.a = [1,2,3]
o.b = [:x, :y]
Regtest.combinations(o)
# => [#<OpenStruct a=1, b=:x>, #<OpenStruct a=1, b=:y>,
#     #<OpenStruct a=2, b=:x>, #<OpenStruct a=2, b=:y>,
#     #<OpenStruct a=3, b=:x>, #<OpenStruct a=3, b=:y>]


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/regtest.rb', line 65

def combinations hashy
  h = hashy.to_h
  a = h.values[0].product(*h.values[1..-1])
  res = []
  a.each do |e|
    o = OpenStruct.new
    h.keys.zip(e) do |k, v|
      o[k] = v
    end
    res << o
  end
  res
end

#log(s, mode: nil) ⇒ Object

Write (temporary) informations to a log file By default the log file is truncated at the first call of Regtest.log for each run of regtest, and all following calls appends to the log file. So you have a log for one run of regtest. You can use mode (‘a’ or ‘w’) to change this behaviour for each call of Regtest.log.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/regtest.rb', line 84

def log s, mode: nil
  log_filename = Regtest.determine_filename_from_caller('.log')
  case mode
  when nil
    mode = Regtest.log_filenames.include?(log_filename) ? 'a' : 'w'
  when 'a', 'w'
    # ok
  else
    raise ArgumentError.new(format('Mode %s is not allowed.', mode))
  end
  Regtest.log_filenames << log_filename
  File.open log_filename, mode do |f|
    f.puts s
  end
end

#sample(name) ⇒ Object

Define a sample



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

def sample name
  h = {}
  name = name.to_s if name.kind_of?(Symbol)
  h['sample'] = name
  begin
    h['result'] = yield
  rescue Exception => e
    h['exception'] = e.message
    if show_exceptions
      $stderr.puts nil, e.full_message
    end
  end
  output_filename = Regtest.determine_filename_from_caller('.yml')
  unless Regtest.results[output_filename]
    Regtest.report "\n", type: :filename unless Regtest.results.empty?
    Regtest.report output_filename, type: :filename
    Regtest.results[output_filename] = []
  end
  Regtest.results[output_filename] << h
  print '.'; $stdout.flush
  h
end