Class: Corundum::RSpec

Inherits:
DocumentationTask show all
Defined in:
lib/corundum/rspec.rb

Instance Method Summary collapse

Methods inherited from DocumentationTask

#entry_point, title

Instance Method Details

#default_configuration(toolkit) ⇒ Object



26
27
28
29
30
31
# File 'lib/corundum/rspec.rb', line 26

def default_configuration(toolkit)
  super
  self.qa_finished_path = toolkit.finished_files.qa
  self.qa_rejections = toolkit.qa_rejections
  self.file_dependencies = file_lists.code + file_lists.test + file_lists.project
end

#defineObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/corundum/rspec.rb', line 50

def define
  super
  in_namespace do
    desc "Always run every spec"
    test_task(:all)

    desc "Generate specifications documentation"
    docn = doc_task(:doc => file_dependencies) do |t|
      t.rspec_opts = %w{-o /dev/null --failure-exit-code 0 -f h -o} + [t.doc_path]
    end
    file entry_path => :doc

    task :verify => entry_path do |task|
      require 'nokogiri'
      require 'corundum/qa-report'

      doc = Nokogiri::parse(File::read(entry_path))

      rejections = QA::Report.new("RSpec[#{entry_path}]")
      qa_rejections << rejections

      def class_xpath(name)
        "contains(concat(' ', normalize-space(@class), ' '), '#{name}')"
      end

      fails_path = "//*[" + %w{example failed}.map{|kind| class_xpath(kind)}.join(" and ") + "]"
      doc.xpath(fails_path).each do |node|
        backtrace_line =
          node.xpath(".//*[#{class_xpath("backtrace")}]").first.content.split("\n").first
        file,line,_ = backtrace_line.split(":")
        label = "fail"
        value = node.xpath(".//*[#{class_xpath("message")}]").first.content.gsub(/\s+/m, " ")

        rejections.add(label, file, line, value)
      end

      unless rejections.empty?
        rejections.fail "Spec fails, none allowed"
      end
    end

    desc "Run only failing examples listed in last_run"
    test_task(:quick) do |t|
      examples = []
      begin
        File.open("last_run", "r") do |fail_list|
          fail_list.each_line.grep(%r{^\s*\d+\)\s*(.*)}) do |line|
            examples << $1.gsub(/'/){"[']"}
          end
        end
      rescue
      end
      unless examples.empty?
        t.rspec_opts << "--example"
        t.rspec_opts << "\"#{examples.join("|")}\""
      end
      t.failure_message = "Spec examples failed."
    end
  end

  desc "Run failing examples if any exist, otherwise, run the whole suite"
  task root_task => in_namespace(:quick)

  task :run_quality_assurance => in_namespace(:verify)
  task :run_continuous_integration => in_namespace(:verify)
end

#doc_task(name) ⇒ Object



44
45
46
47
48
# File 'lib/corundum/rspec.rb', line 44

def doc_task(name)
  RSpecReportTask.define_task(self, name) do |t|
    yield(t) if block_given?
  end
end

#resolve_configurationObject



33
34
35
36
# File 'lib/corundum/rspec.rb', line 33

def resolve_configuration
  self.rspec_path ||= %x"which #{rspec_program}".chomp
  super
end

#test_task(name) ⇒ Object



38
39
40
41
42
# File 'lib/corundum/rspec.rb', line 38

def test_task(name)
  RSpecTask.define_task(self, name) do |t|
    yield(t) if block_given?
  end
end