Module: Autotest::CucumberMixin

Included in:
Cucumber, CucumberRails, CucumberRailsRspec, CucumberRspec
Defined in:
lib/gems/cucumber-0.1.15/lib/autotest/cucumber_mixin.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#scenarios_to_runObject

Returns the value of attribute scenarios_to_run.



10
11
12
# File 'lib/gems/cucumber-0.1.15/lib/autotest/cucumber_mixin.rb', line 10

def scenarios_to_run
  @scenarios_to_run
end

Class Method Details

.included(receiver) ⇒ Object



6
7
8
# File 'lib/gems/cucumber-0.1.15/lib/autotest/cucumber_mixin.rb', line 6

def self.included(receiver)
  receiver::ALL_HOOKS << [:run_features, :ran_features]
end

Instance Method Details

#all_features_goodObject



47
48
49
# File 'lib/gems/cucumber-0.1.15/lib/autotest/cucumber_mixin.rb', line 47

def all_features_good
  scenarios_to_run == []
end

#get_to_greenObject



51
52
53
54
55
56
57
# File 'lib/gems/cucumber-0.1.15/lib/autotest/cucumber_mixin.rb', line 51

def get_to_green
  begin
    super
    run_features
    wait_for_changes unless all_features_good
  end until all_features_good
end

#initializeObject



12
13
14
15
# File 'lib/gems/cucumber-0.1.15/lib/autotest/cucumber_mixin.rb', line 12

def initialize
  super
  reset_features
end

#make_cucumber_cmd(scenarios_to_run, dirty_scenarios_filename) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gems/cucumber-0.1.15/lib/autotest/cucumber_mixin.rb', line 103

def make_cucumber_cmd(scenarios_to_run, dirty_scenarios_filename)
  return '' if scenarios_to_run == []
  
  profiles = YAML.load_file("cucumber.yml").keys rescue []
  
  profile ||= "autotest-all" if profiles.include?("autotest-all") and scenarios_to_run == :all
  profile ||= "autotest"     if profiles.include?("autotest")
  profile ||= nil
  
  if profile
    args = ["--profile", profile]
  else
    args = %w{features --format} << (scenarios_to_run == :all ? "progress" : "pretty")
  end
  args += %w{--format autotest --color --out} << dirty_scenarios_filename
  args = args.join(' ')
  
  if scenarios_to_run == :all
    scenario_args = nil
  else
    # We escape scenario names for the shell by wrapping them in $'...' and replacing
    # every single quote with an escaped version, "\'".  We use a double backslash for
    # Ruby, and we put it in a block or gsub interpolates the sequence "\'" as $'.
    scenario_args = scenarios_to_run.map { |s| "-s $'#{s.gsub("'") {"\\'"} }'" }.join(' ')
  end
  return "#{Cucumber::RUBY_BINARY} #{Cucumber::BINARY} #{args} #{scenario_args}"
end

#rerun_all_featuresObject



59
60
61
62
# File 'lib/gems/cucumber-0.1.15/lib/autotest/cucumber_mixin.rb', line 59

def rerun_all_features
  reset_features
  run_features
end

#reset_featuresObject



64
65
66
# File 'lib/gems/cucumber-0.1.15/lib/autotest/cucumber_mixin.rb', line 64

def reset_features
  self.scenarios_to_run = :all
end

#runObject



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
# File 'lib/gems/cucumber-0.1.15/lib/autotest/cucumber_mixin.rb', line 17

def run
  hook :initialize
  reset
  reset_features
  add_sigint_handler

  self.last_mtime = Time.now if $f

  loop do # ^c handler
    begin
      get_to_green
      if self.tainted then
        rerun_all_tests
        rerun_all_features if all_good
      else
        hook :all_good
      end
      wait_for_changes
      # Once tests and features are green, reset features every
      # time a file is changed to see if anything breaks.
      reset_features
    rescue Interrupt
      break if self.wants_to_quit
      reset
      reset_features
    end
  end
  hook :quit
end

#run_featuresObject



68
69
70
71
72
73
74
75
76
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
# File 'lib/gems/cucumber-0.1.15/lib/autotest/cucumber_mixin.rb', line 68

def run_features
  hook :run_features
  Tempfile.open('autotest-cucumber') do |dirty_scenarios_file|
    cmd = self.make_cucumber_cmd self.scenarios_to_run, dirty_scenarios_file.path
    return if cmd.empty?
    puts cmd unless $q
    old_sync = $stdout.sync
    $stdout.sync = true
    self.results = []
    line = []
    begin
      open("| #{cmd}", "r") do |f|
        until f.eof? do
          c = f.getc
          putc c
          line << c
          if c == ?\n then
            self.results << if RUBY_VERSION >= "1.9" then
                              line.join
                            else
                              line.pack "c*"
                            end
            line.clear
          end
        end
      end
    ensure
      $stdout.sync = old_sync
    end
    self.scenarios_to_run = dirty_scenarios_file.readlines.map { |l| l.chomp }
    self.tainted = true unless self.scenarios_to_run == []
  end
  hook :ran_features
end