Class: Autotest::RunDependencies

Inherits:
Object
  • Object
show all
Defined in:
lib/autotest/run_dependencies.rb,
lib/autotest-run_dependencies.rb

Overview

Autotest::RunDependencies

FEATURES:

  • Allows an arbitrary number of dependencies to be set that must be satisfied before a test run is executed.

  • The dependencies take the form of scripts that output to stdout or stderr.

  • The dependency is considered satisfied if the output matches some user specified regular expression.

  • If the dependency is not satisfied, a list of errors is printed where each error is a match of the user supplied errors regular expression.

  • By default, output is colourised green on success, red on failure.

  • Colourisation can be turned off.

SYNOPSIS:

PROJECT_ROOT/.autotest

require 'autotest/run_dependencies.rb'

# Add a dependency to the run dependencies
Autotest::RunDependencies.add do |dependency|
  dependency.name = 'some dependency'
  dependency.command = 'echo "success"'
  dependency.satisfied_regexp = /success/
  dependency.errors_regexp = /error (.*)/
end

Constant Summary collapse

VERSION =
'0.1'
@@colourise_output =

By default the output produced is colourised.

true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RunDependencies

Creates an instance of Autotest::RunDependencies.

The optional options hash can contain values for the keys :name, :command, :satisfied_regexp and :errors_regexp. Any other entries will be ignored. The values of the parameters are used as default values for the instance’s attributes.



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

def initialize(options = {})
  self.name = options[:name]
  self.command = options[:command]
  self.satisfied_regexp = options[:satisfied_regexp]
  self.errors_regexp = options[:errors_regexp]
  self.last_dependency_check_time = Time.at(0)
end

Instance Attribute Details

#autotest_instanceObject

Returns the value of attribute autotest_instance.



31
32
33
# File 'lib/autotest/run_dependencies.rb', line 31

def autotest_instance
  @autotest_instance
end

#commandObject

Returns the value of attribute command.



31
32
33
# File 'lib/autotest/run_dependencies.rb', line 31

def command
  @command
end

#errors_regexpObject

Returns the value of attribute errors_regexp.



31
32
33
# File 'lib/autotest/run_dependencies.rb', line 31

def errors_regexp
  @errors_regexp
end

#last_dependency_check_timeObject

Returns the value of attribute last_dependency_check_time.



31
32
33
# File 'lib/autotest/run_dependencies.rb', line 31

def last_dependency_check_time
  @last_dependency_check_time
end

#nameObject

Returns the value of attribute name.



31
32
33
# File 'lib/autotest/run_dependencies.rb', line 31

def name
  @name
end

#satisfied_regexpObject

Returns the value of attribute satisfied_regexp.



31
32
33
# File 'lib/autotest/run_dependencies.rb', line 31

def satisfied_regexp
  @satisfied_regexp
end

Class Method Details

.add(&block) ⇒ Object

Adds a test dependency to any autotest test run.

Expects a block to be supplied that sets the dependency parameters, e.g.,

Autotest::RunDependencies.add do |dependency|
  dependency.name = 'some dependency'
  dependency.command = 'echo "success"'
  dependency.satisfied_regexp = /success/
  dependency.errors_regexp = /error (.*)/
end

Returns the dependency (an instance of Autotest::RunDependencies).

It is possible not to pass a block and set the parameters on the returned object but if a dependency object has no #command and #satisfied_regexp set then on calling #ensure_dependency_is_satisfied a RuntimeError is raised.

The method also registers the relevant hooks with Autotest so that the dependency is required to be satisfied before the test run.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/autotest/run_dependencies.rb', line 77

def self.add(&block)
  parameters = OpenStruct.new
  block.call(parameters) if block
  dependency = self.new(
    :name => parameters.name,
    :command => parameters.command,
    :satisfied_regexp => parameters.satisfied_regexp,
    :errors_regexp => parameters.errors_regexp
  )

  Autotest.add_hook(:initialize) do |autotest|
    dependency.autotest_instance = autotest
    false
  end

  Autotest.add_hook(:run_command) do |autotest|
    dependency.ensure_dependency_is_satisfied
    false
  end

  Autotest.add_hook(:interrupt) do |autotest|
    dependency.reset
    false
  end
  
  return dependency
end

.colourise_outputObject Also known as: colorize_output

Returns true if Autotest::RunDependencies is currently configured to colourise its output, false otherwise.



44
45
46
# File 'lib/autotest/run_dependencies.rb', line 44

def colourise_output
  @@colourise_output
end

.colourise_output=(boolean) ⇒ Object Also known as: colorize_output=

Sets whether or not Autotest::RunDependencies should colourise its output.



51
52
53
# File 'lib/autotest/run_dependencies.rb', line 51

def colourise_output=(boolean)
  @@colourise_output = boolean
end

Instance Method Details

#ensure_dependency_is_satisfiedObject

Runs the dependency test command if any files have been modified since the last time the dependency was checked. If the dependency is satisfied, i.e., the output of the command matches the regular expression in the #satisfied_regexp attribute, then the test run is allowed to continue, otherwise the method prints any lines in the output that match the regular expression in the #errors_regexp attribute and waits for further changes to the codebase before trying to test the dependency again.

This method raises a RuntimeError unless both the #command and #satisfied_regexp attributes are set.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/autotest/run_dependencies.rb', line 129

def ensure_dependency_is_satisfied
  unless self.command and self.satisfied_regexp
    raise(
      RuntimeError, 
      "Dependencies must have at least a command and a satisfied_regexp " +
      " set."
    )
  end
  if find_changed_files
    puts "\nChecking dependency: #{self.name}"
    loop do
      self.last_dependency_check_time = Time.now
      test_dependency
      if satisfied?
        puts "-> #{green_command}Dependency satisfied\n#{no_colour_command}"
        break
      else
        puts "-> #{red_command}Dependency not satisfied:\n" + 
          "#{errors}#{no_colour_command}"
        wait_for_changes
        puts "Rechecking dependency: #{self.name}"
      end
    end
  end
end

#resetObject

Resets the dependency state so that it will recheck the dependency during the next test run regardless of whether any files have changed.



157
158
159
# File 'lib/autotest/run_dependencies.rb', line 157

def reset
  self.last_dependency_check_time = Time.at(0)
end