class TddDeploy::Server

TddDeploy::Server

implements a simple 'rack' server. Methods are either used internally or called from the web page during page reloads.

It only displays one page - which is defined in the gem in lib/tdd_deploy/server-templates/test_results.html.erb.

Constants

HOST_TESTS_DIR
LIB_DIR
LOCAL_TESTS_DIR
SITE_TESTS_DIR
TEMPLATE_PATH

Attributes

query_hash[RW]
test_classes[RW]

Public Class Methods

new(*args) click to toggle source
Calls superclass method TddDeploy::Base::new
# File lib/tdd_deploy/server.rb, line 27
def initialize *args
  raise RuntimeError.new("No Environment File") unless File.exists? TddDeploy::Environ::ENV_FNAME
  super
  load_all_tests
  @request_count = 0
end

Public Instance Methods

call(env) click to toggle source

rack interface. takes an env Hash and returns [code, headers, body]

# File lib/tdd_deploy/server.rb, line 58
def call(env)
  self.query_hash = parse_query_string(env['QUERY_STRING'])

  if self.query_hash['run_configurator']
    require 'tdd_deploy/configurator'
    configurator = TddDeploy::Configurator.new
    configurator.make_configuration_files
  end
  
  if self.query_hash['install_special']
    require 'tdd_deploy/installer'
    installer = TddDeploy::Installer.new
    [:app_hosts, :balance_hosts, :db_hosts, :web_hosts].each do |host_list|
      installer.empty_special_dir self.site_user, host_list
    end
    [:app_hosts, :balance_hosts, :db_hosts, :web_hosts].each do |host_list|
      installer.install_special_files_on_host_list_as self.site_user, host_list
    end
    query_hash['failed-tests'] = failed_tests.join(',')
  end
  
  if self.query_hash['install_configs']
    require 'tdd_deploy/installer'
    installer ||= TddDeploy::Installer.new
    [:app_hosts, :balance_hosts, :db_hosts, :web_hosts].each do |host_list|
      installer.install_config_files_on_host_list_as self.site_user, host_list
    end
    query_hash['failed-tests'] = failed_tests.join(',')
  end
  
  if self.query_hash['run_cap_deploy']
    require 'tdd_deploy/installer'
    installer ||= TddDeploy::Installer.new
    installer.run_cap_deploy
    query_hash['failed-tests'] = true
  end

  load_all_tests
  
  if query_hash['failed-tests'] && @request_count > 0
    remove_failed_tests
    run_selected_tests(query_hash['failed-tests'])
  else
    run_all_tests
  end
  @request_count += 1
  
  query_string = new_query_string
  body = [
    render_results,
    # "#{env.inspect}"
    ]
  return [200, {'Content-Length' => body.join('').length.to_s, 'Content-Type' => 'text/html'}, body]
end
failed_tests() click to toggle source

failed_tests returns a unique, sorted list of strings. It just seemed easier to do it in the accessors than take the chance of using push, pop, etc and mucking it up - like I did before.

# File lib/tdd_deploy/server.rb, line 37
def failed_tests
  @failed_tests ||= []
  raise RuntimeError.new("@failed_tests is not an Array: #{@failed_tests.inspect}") unless @failed_tests.is_a? Array
  @failed_tests = @failed_tests.map { |x| x.to_s }.uniq.sort
end
failed_tests=(value) click to toggle source

failed_tests= does the right thing for all kinds of input variations.

# File lib/tdd_deploy/server.rb, line 44
def failed_tests=(value)
  begin
    value = value.split(/[\s,]+/) if value.is_a? String
    value = [value] unless value.is_a? Array
  
    @failed_tests = @failed_tests.to_a unless @failed_tests.is_a? Array

    @failed_tests = (@failed_tests + value.map { |x| x.to_s }).uniq
  rescue
    @failed_tests = (@failed_tests.to_a.map { |x| x.to_s } + value.to_a.map { |x| x.to_s }).uniq.sort
  end
end
load_all_tests() click to toggle source

loads all files in 'lib/tdd_deploy/host_tests | site_tests | local_tests. both host_tests and site_tests are clobbered by the rake install task. local_tests is safe.

# File lib/tdd_deploy/server.rb, line 116
def load_all_tests
  # discard any already defined tests
  TddDeploy::TestBase.flush_children_methods
  
  # reload all tests
  [TddDeploy::Server::HOST_TESTS_DIR, TddDeploy::Server::SITE_TESTS_DIR,
      TddDeploy::Server::LOCAL_TESTS_DIR].each do |dir|
    if File.exists?(dir)
      # puts "gathering tests from #{dir}"
      Dir.new(dir).each do |fname|
        load File.join(dir, fname) if fname =~ /\.rb$/
      end
    else
      puts "skipping #{dir} - no such directory"
    end
  end

  self.test_classes = TddDeploy::TestBase.children

  @test_to_class_map = {}
  self.test_classes.each do |klass|
    klass.instance_methods(false).each do |func|
      @test_to_class_map[func.to_s] = klass
    end
  end
end
run_all_tests() click to toggle source

Re-reads the environment and then runs all known tests.

# File lib/tdd_deploy/server.rb, line 144
def run_all_tests
  read_env
  reset_tests

  ret = true
  self.failed_tests = []
  self.test_classes.each do |klass|
    ret &= run_all_tests_in_class(klass)
  end
  ret
end
run_selected_tests(test_list) click to toggle source

Re-reads the environment and then runs tests from 'test_list'

# File lib/tdd_deploy/server.rb, line 157
def run_selected_tests(test_list)
  read_env
  ret = true
  test_list = test_list.split(/[\s,]+/) if test_list.is_a? String
  self.failed_tests -= test_list
  test_list.each do |test|
    ret &= run_a_test test
  end
  ret
end

Private Instance Methods

new_query_string() click to toggle source
# File lib/tdd_deploy/server.rb, line 207
def new_query_string
  "failed-tests=" + URI.escape(self.failed_tests.join(',')) unless @failed_tests.empty?
end
parse_query_string(query_string) click to toggle source
# File lib/tdd_deploy/server.rb, line 192
def parse_query_string(query_string)
  return '' unless query_string.is_a? String
  Hash[query_string.split('&').map { |tmp| key,value = tmp.split('='); value ? [key, URI.decode(value)] : [key, 'true']}]
end
render_results() click to toggle source
# File lib/tdd_deploy/server.rb, line 197
def render_results
  f = File.new(TEMPLATE_PATH)
  template = ERB.new f.read, nil, '<>'
  f.close

  # add 'server_obj' so accessors are accessible from erb template
  server_obj = self
  template.result(binding)
end
run_a_test(test) click to toggle source
# File lib/tdd_deploy/server.rb, line 184
def run_a_test test
  return false unless (klass = @test_to_class_map[test])
  obj = klass.new
  test_result = obj.send test.to_sym
  self.failed_tests.push(test) unless test_result
  test_result
end
run_all_tests_in_class(klass) click to toggle source

used by

# File lib/tdd_deploy/server.rb, line 171
def run_all_tests_in_class klass
  read_env
  obj = klass.new
  ret = true
  # puts "#{klass}.instance_methods: #{klass.instance_methods(false)}"
  klass.instance_methods(false).each do |func|
    test_result = obj.send func.to_sym
    self.failed_tests.push(func) unless test_result
    ret &= test_result
  end
  ret
end