Class: Squcumber::Postgres::Rake::Task

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/squcumber-postgres/rake/task.rb

Instance Method Summary collapse

Instance Method Details

#install_tasksObject



11
12
13
14
15
16
17
18
19
20
21
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
50
51
52
53
54
55
56
57
58
# File 'lib/squcumber-postgres/rake/task.rb', line 11

def install_tasks
  namespace :test do
    # Auto-generate Rake tasks for each feature and each of their parent directories
    @features_dir = File.join(FileUtils.pwd, 'features')
    features = Dir.glob("#{@features_dir}/**/*.feature")
    parent_directories = features.map { |f| f.split('/')[0..-2].join('/') }.uniq
    features.each do |feature|
      feature_name = feature.gsub(@features_dir + '/', '').gsub('.feature', '')
      task_name = feature_name.gsub('/', ':')
      desc "Run SQL tests for feature #{feature_name}"
      task "sql:#{task_name}".to_sym, [:scenario_line_number] do |_, args|
        cucumber_task_name = "cucumber_#{task_name}".to_sym
        ::Cucumber::Rake::Task.new(cucumber_task_name) do |t|
          line_number = args[:scenario_line_number].nil? ? '' : ":#{args[:scenario_line_number]}"
          output_dir = ENV['CUSTOM_OUTPUT_DIR'] ? ENV['CUSTOM_OUTPUT_DIR'] : '/tmp'
          output_file = ENV['CUSTOM_OUTPUT_NAME'] ? ENV['CUSTOM_OUTPUT_NAME'] : feature_name.gsub('/', '_')
          output_path = output_dir + '/' + output_file
          output_opts = "--format html --out #{output_path}.html --format json --out #{output_path}.json"
          extra_config = ENV['SQUCUMBER_OPTIONS_EXTRA']
          t.cucumber_opts = "#{feature}#{line_number} --format pretty #{output_opts} --require #{File.dirname(__FILE__)}/../support --require #{File.dirname(__FILE__)}/../step_definitions #{ENV['CUSTOM_STEPS_DIR'] ? '--require ' + ENV['CUSTOM_STEPS_DIR'] : ''} #{extra_config}"
        end
        ::Rake::Task[cucumber_task_name].execute
      end
    end

    parent_directories.each do |feature|
      feature_name = feature.gsub(@features_dir + '/', '')
      task_name = feature_name.gsub('/', ':')
      if feature_name.eql?(@features_dir)
        feature_name = 'features'
        task_name = 'all'
      end
      desc "Run SQL tests for all features in /#{feature_name}"
      task "sql:#{task_name}".to_sym do
        cucumber_task_name = "cucumber_#{task_name}".to_sym
        ::Cucumber::Rake::Task.new(cucumber_task_name) do |t|
          output_dir = ENV['CUSTOM_OUTPUT_DIR'] ? ENV['CUSTOM_OUTPUT_DIR'] : '/tmp'
          output_file = ENV['CUSTOM_OUTPUT_NAME'] ? ENV['CUSTOM_OUTPUT_NAME'] : feature_name.gsub('/', '_')
          output_path = output_dir + '/' + output_file
          output_opts = "--format html --out #{output_path}.html --format json --out #{output_path}.json"
          extra_config = ENV['SQUCUMBER_OPTIONS_EXTRA']
          t.cucumber_opts = "#{feature} --format pretty #{output_opts} --require #{File.dirname(__FILE__)}/../support --require #{File.dirname(__FILE__)}/../step_definitions #{ENV['CUSTOM_STEPS_DIR'] ? '--require ' + ENV['CUSTOM_STEPS_DIR'] : ''} #{extra_config}"
        end
        ::Rake::Task[cucumber_task_name].execute
      end
    end
  end
end