Class: ServerspecLauncherRakeTasks

Inherits:
Object
  • Object
show all
Includes:
ExampleHelper, Rake::DSL
Defined in:
lib/serverspec_launcher/rake_tasks.rb

Overview

loads up the rake tasks

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExampleHelper

#load_bundled_examples, #load_examples_from_gem, #load_shared_examples, #shared_examples

Constructor Details

#initialize(properties = nil) ⇒ ServerspecLauncherRakeTasks

Returns a new instance of ServerspecLauncherRakeTasks.



15
16
17
18
19
20
# File 'lib/serverspec_launcher/rake_tasks.rb', line 15

def initialize(properties = nil)
  @properties = properties ? properties : YAML.load_file('properties.yml')
  options = @properties[:options] || {}
  @fail_on_err = options[:fail_on_err]
  @formatters = options[:formatters] || []
end

Class Method Details

.load(properties = nil) ⇒ Object



112
113
114
115
# File 'lib/serverspec_launcher/rake_tasks.rb', line 112

def self.load(properties = nil)
  tasks = ServerspecLauncherRakeTasks.new properties
  tasks.load_tasks
end

Instance Method Details

#debug_tasksObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/serverspec_launcher/rake_tasks.rb', line 99

def debug_tasks
  namespace :debug do
    desc 'View loaded Shared examples'
    task :shared_examples do
      puts 'Loaded Shared Examples:\n======================='
      load_shared_examples @properties[:shared_example_gems] || []
      shared_examples.each do |ex|
        puts "\t#{ex}"
      end
    end
  end
end

#load_tasksObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/serverspec_launcher/rake_tasks.rb', line 22

def load_tasks
  task serverspec: 'serverspec:all'

  debug_tasks

  namespace :serverspec do
    task all: @properties[:targets].keys.map { |key| 'serverspec:' + key.split('.')[0] }
    @properties[:targets].keys.each do |key|
      target = @properties[:targets][key]
      options = {
        fail_on_err: target[:fail_on_err] || @fail_on_err,
        formatters: target[:formatters] || @formatters
      }
      spec_type = target[:spec_type] || 'default'
      if target[:hosts].is_a?(Array)

        task_array(key, spec_type, target, options)
      elsif target[:hosts]
        host = target[:hosts]
        task_name = (key || target[:name]).to_s
        rake_task(host, key, task_name, spec_type, options)
      else
        task_name = (key || target[:name]).to_s
        rake_task(key, key, task_name, spec_type, options)
      end
    end
  end
end

#rake_task(host, key, task_name, spec_type, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/serverspec_launcher/rake_tasks.rb', line 63

def rake_task(host, key, task_name, spec_type, options = {})
  desc "Run serverspec to #{key}"
  RSpec::Core::RakeTask.new(host.split('.')[0].to_sym) do |t|
    ENV['TARGET_HOST'] = host
    ENV['TARGET'] = key
    ENV['TASK_NAME'] = task_name
    t.pattern = "spec/#{spec_type}_spec.rb"
    t.fail_on_error = options[:fail_on_err]
    set_formatters(key, options, t)
  end
end

#set_formatters(key, options, t) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/serverspec_launcher/rake_tasks.rb', line 75

def set_formatters(key, options, t)
  opts = t.rspec_opts
  if options[:formatters].include? 'junit'
    opts = "#{opts}  --format RspecJunitFormatter --out reports/#{key}.xml"
  end
  if options[:formatters].include?('docs') || options[:formatters].include?('documentation')
    opts = "#{opts}  --format documentation --out reports/#{key}.txt"
  end
  if options[:formatters].include?('docs_screen')
    opts = "#{opts}  --format documentation"
  end
  if options[:formatters].include?('progress')
    opts = "#{opts}  --format progress"
  end
  if options[:formatters].include?('html')
    opts = "#{opts}  --format html --out reports/#{key}.html"
  end
  if File.exist?('.rspec')
    opts = "#{opts}  --options .rspec"
  end
  t.rspec_opts = opts
  puts "opts #{t.rspec_opts}"
end

#task_array(key, spec_type, target, options) ⇒ Object



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

def task_array(key, spec_type, target, options)
  desc "Run serverspec to #{key}"
  task key.to_sym => "serverspec:#{key}:all"
  namespace key.to_sym do
    task all: target[:hosts].map { |host| "serverspec:#{key}:#{host.split('.')[0].to_sym}" }
    target[:hosts].each do |host|
      task_name = "#{key}:#{host || target[:name]}"
      rake_task(host, key, task_name, spec_type, options)
    end
  end
end