Class: Autowatchr

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

Defined Under Namespace

Classes: Config, Tee

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script, options = {}) {|@config| ... } ⇒ Autowatchr

Returns a new instance of Autowatchr.

Yields:



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/autowatchr.rb', line 104

def initialize(script, options = {})
  @config = Config.new(options)
  yield @config  if block_given?
  @script = script
  @test_files = []
  @failed_tests = {}

  discover_files
  run_all_tests
  start_watching_files
  start_sigint_handler
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



101
102
103
# File 'lib/autowatchr.rb', line 101

def config
  @config
end

#interrupt_receivedObject

Returns the value of attribute interrupt_received.



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

def interrupt_received
  @interrupt_received
end

Instance Method Details

#classname_to_path(s) ⇒ Object



174
175
176
# File 'lib/autowatchr.rb', line 174

def classname_to_path(s)
  File.join(@config.test_dir, underscore(s)+".rb")
end

#run_lib_file(file) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/autowatchr.rb', line 135

def run_lib_file(file)
  md = file.match(%r{^#{@config.lib_dir}#{File::SEPARATOR}?(.+)$})
  parts = md[1].split(File::SEPARATOR)
  parts[-1] = config.test_file % File.basename(parts[-1],'.rb')
  file = "#{@config.test_dir}/" + File.join(parts)
  run_test_file(file)
end

#run_test_file(files) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/autowatchr.rb', line 143

def run_test_file(files)
  files = [files]   unless files.is_a?(Array)

  passing  = []
  commands = []
  files.each do |file|
    tests = @failed_tests[file]
    if tests && !tests.empty?
      predicate = %!#{file} -n "/^(#{tests.join("|")})$/"!
      commands << @config.eval_command(predicate)
    else
      passing << file
    end
  end

  if !passing.empty?
    predicate = if passing.length > 1
                  "-e \"%w[#{passing.join(" ")}].each do |f| require f end\""
                else
                  passing[0]
                end
    commands.unshift(@config.eval_command(predicate))
  end

  cmd = commands.join("; ")
  puts cmd

  results = execute_cmd(cmd)
  handle_results(results, files)
end

#start_sigint_handlerObject

Traps any INT signals (like Control-C) and re-runs all the tests, unless the user sends the INT signal again within 2 seconds.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/autowatchr.rb', line 120

def start_sigint_handler
  Signal.trap 'INT' do
    if self.interrupt_received
      exit 0
    else
      self.interrupt_received = true
      puts "\nInterrupt a second time to quit"
      Kernel.sleep 2
      self.interrupt_received = false
      puts "Running all tests..."
      run_all_tests
    end
  end
end