Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 56d7c0a4ea
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 97 lines (80 sloc) 3.182 kb
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
# encoding: utf-8
require 'spec_helper'
require 'rspec/core/formatters/text_mate_formatter'
require 'nokogiri'

module RSpec
  module Core
    module Formatters
      describe TextMateFormatter do
        let(:suffix) {
          if ::RUBY_PLATFORM == 'java'
            "-jruby"
          elsif defined?(Rubinius)
            "-rbx"
          else
            ""
          end
        }

        let(:root) { File.expand_path("#{File.dirname(__FILE__)}/../../../..") }
        let(:expected_file) do
          "#{File.dirname(__FILE__)}/text_mate_formatted-#{::RUBY_VERSION}#{suffix}.html"
        end

        let(:generated_html) do
          options = RSpec::Core::ConfigurationOptions.new(
            %w[spec/rspec/core/resources/formatter_specs.rb --format textmate --order default]
          )
          options.parse_options

          err, out = StringIO.new, StringIO.new
          err.set_encoding("utf-8") if err.respond_to?(:set_encoding)
          out.set_encoding("utf-8") if out.respond_to?(:set_encoding)

          command_line = RSpec::Core::CommandLine.new(options)
          command_line.instance_variable_get("@configuration").backtrace_cleaner.inclusion_patterns = []
          command_line.run(err, out)
          out.string.gsub(/\d+\.\d+(s| seconds)/, "n.nnnn\\1")
        end

        let(:expected_html) do
          unless File.file?(expected_file)
            raise "There is no HTML file with expected content for this platform: #{expected_file}"
          end
          File.read(expected_file)
        end

        before do
          RSpec.configuration.stub(:load_spec_files) do
            RSpec.configuration.files_to_run.map {|f| load File.expand_path(f) }
          end
        end

        # Uncomment this group temporarily in order to overwrite the expected
        # with actual. Use with care!!!
        describe "file generator", :if => ENV['GENERATE'] do
          it "generates a new comparison file" do
            Dir.chdir(root) do
              File.open(expected_file, 'w') {|io| io.write(generated_html)}
            end
          end
        end

        it "produces HTML identical to the one we designed manually" do
          Dir.chdir(root) do
            actual_doc = Nokogiri::HTML(generated_html)
            backtrace_lines = actual_doc.search("div.backtrace a")
            actual_doc.search("div.backtrace").remove

            expected_doc = Nokogiri::HTML(expected_html)
            expected_doc.search("div.backtrace").remove

            expect(actual_doc.inner_html).to eq(expected_doc.inner_html)

            backtrace_lines.each do |backtrace_line|
              expect(backtrace_line['href']).to include("txmt://open?url=")
            end
          end
        end

        it "has a backtrace line from the raw erb evaluation" do
          Dir.chdir(root) do
            actual_doc = Nokogiri::HTML(generated_html)

            expect(actual_doc.inner_html).to include('(erb):1')
          end
        end

        it "has a backtrace line from a erb source file we forced to appear" do
          expect(generated_html).to include('open?url=file:///foo.html.erb')
        end

      end
    end
  end
end
Something went wrong with that request. Please try again.