File: reporting.rb

package info (click to toggle)
ruby-aruba 0.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 492 kB
  • ctags: 192
  • sloc: ruby: 1,427; makefile: 10
file content (122 lines) | stat: -rw-r--r-- 3,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
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
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
116
117
118
119
120
121
122
if(ENV['ARUBA_REPORT_DIR'])
  ENV['ARUBA_REPORT_TEMPLATES'] ||= File.dirname(__FILE__) + '/../../../../share/ruby-aruba/templates'
  
  require 'fileutils'
  require 'erb'
  require 'cgi'
  require 'bcat/ansi'
  require 'rdiscount'
  require 'aruba/spawn_process'

  module Aruba
    module Reporting
      class << self
        def reports
          @reports ||= Hash.new do |hash, feature|
            hash[feature] = []
          end
        end
      end
      
      def pygmentize(file)
        pygmentize = SpawnProcess.new(%{pygmentize -f html -O encoding=utf-8 "#{file}"}, 3, 0.5)
        pygmentize.run! do |p|
          exit_status = p.stop(false)
          if(exit_status == 0)
            p.stdout(false)
          elsif(p.stderr(false) =~ /no lexer/) # Pygment's didn't recognize it
            IO.read(file)
          else
            STDERR.puts "\e[31m#{p.stderr} - is pygments installed?\e[0m"
            exit $?.exitstatus
          end
        end
      end

      def title
        @scenario.title
      end

      def description
        unescaped_description = @scenario.description.gsub(/^(\s*)\\/, '\1')
        markdown = RDiscount.new(unescaped_description)
        markdown.to_html
      end

      def commands
        @commands || []
      end

      def output
        @aruba_keep_ansi = true # We want the output coloured!
        escaped_stdout = CGI.escapeHTML(all_stdout)
        html = Bcat::ANSI.new(escaped_stdout).to_html
        Bcat::ANSI::STYLES.each do |name, style|
          html.gsub!(/style='#{style}'/, %{class="xterm_#{name}"})
        end
        html
      end

      def report
        erb = ERB.new(template('main.erb'), nil, '-')
        erb.result(binding)
      end
      
      def files
        erb = ERB.new(template('files.erb'), nil, '-')
        file = current_dir
        erb.result(binding)
      end
      
      def again(erb, _erbout, file)
        _erbout.concat(erb.result(binding))
      end

      def children(dir)
        Dir["#{dir}/*"].sort
      end

      def template(path)
        IO.read(File.join(ENV['ARUBA_REPORT_TEMPLATES'], path))
      end

      def depth
        File.dirname(@scenario.feature.file).split('/').length
      end

      def index
        erb = ERB.new(template('index.erb'), nil, '-')
        erb.result(binding)
      end

      def index_title
        "Examples"
      end
    end
  end
  World(Aruba::Reporting)

  After do |scenario|
    @scenario = scenario
    html_file = "#{scenario.feature.file}:#{scenario.line}.html"
    report_file = File.join(ENV['ARUBA_REPORT_DIR'], html_file)
    _mkdir(File.dirname(report_file))
    File.open(report_file, 'w') do |io|
      io.write(report)
    end

    Aruba::Reporting.reports[scenario.feature] << [scenario, html_file]

    FileUtils.cp_r(File.join(ENV['ARUBA_REPORT_TEMPLATES'], '.'), ENV['ARUBA_REPORT_DIR'])
    Dir["#{ENV['ARUBA_REPORT_DIR']}/**/*.erb"].each{|f| FileUtils.rm(f)}
  end

  at_exit do
    index_file = File.join(ENV['ARUBA_REPORT_DIR'], "index.html")
    extend(Aruba::Reporting)
    File.open(index_file, 'w') do |io|
      io.write(index)
    end
  end
end