Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 108 lines (87 sloc) 4.397 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 97 98 99 100 101 102 103 104 105 106 107
require 'helper'

class TestMergeHelpers < Test::Unit::TestCase
  on_ruby '1.9' do
    context "With two faked coverage resultsets" do
      setup do
        SimpleCov.use_merging true
        @resultset1 = {source_fixture('sample.rb') => [nil, 1, 1, 1, nil, nil, 1, 1, nil, nil],
            source_fixture('app/models/user.rb') => [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil],
            source_fixture('app/controllers/sample_controller.rb') => [nil, 1, 1, 1, nil, nil, 1, 0, nil, nil],
            source_fixture('resultset1.rb') => [1, 1, 1, 1]}

        @resultset2 = {source_fixture('sample.rb') => [1, nil, 1, 1, nil, nil, 1, 1, nil, nil],
            source_fixture('app/models/user.rb') => [nil, 1, 5, 1, nil, nil, 1, 0, nil, nil],
            source_fixture('app/controllers/sample_controller.rb') => [nil, 3, 1, nil, nil, nil, 1, 0, nil, nil],
            source_fixture('resultset2.rb') => [nil, 1, 1, nil]}
      end

      context "a merge" do
        setup do
          assert @merged = @resultset1.merge_resultset(@resultset2)
        end

        should "have proper results for sample.rb" do
          assert_equal [1, 1, 2, 2, nil, nil, 2, 2, nil, nil], @merged[source_fixture('sample.rb')]
        end

        should "have proper results for user.rb" do
          assert_equal [nil, 2, 6, 2, nil, nil, 2, 0, nil, nil], @merged[source_fixture('app/models/user.rb')]
        end

        should "have proper results for sample_controller.rb" do
          assert_equal [nil, 4, 2, 1, nil, nil, 2, 0, nil, nil], @merged[source_fixture('app/controllers/sample_controller.rb')]
        end

        should "have proper results for resultset1.rb" do
          assert_equal [1, 1, 1, 1], @merged[source_fixture('resultset1.rb')]
        end

        should "have proper results for resultset2.rb" do
          assert_equal [nil, 1, 1, nil], @merged[source_fixture('resultset2.rb')]
        end
      end

      # See Github issue #6
      should "return an empty hash when the resultset cache file is empty" do
        File.open(SimpleCov::ResultMerger.resultset_path, "w+") {|f| f.puts ""}
        assert_equal Hash.new, SimpleCov::ResultMerger.resultset
      end

      # See Github issue #6
      should "return an empty hash when the resultset cache file is not present" do
        system "rm #{SimpleCov::ResultMerger.resultset_path}" if File.exist?(SimpleCov::ResultMerger.resultset_path)
        assert_equal Hash.new, SimpleCov::ResultMerger.resultset
      end

      context "and results generated from those" do
        setup do
          system "rm #{SimpleCov::ResultMerger.resultset_path}" if File.exist?(SimpleCov::ResultMerger.resultset_path)
          @result1 = SimpleCov::Result.new(@resultset1)
          @result1.command_name = "result1"
          @result2 = SimpleCov::Result.new(@resultset2)
          @result2.command_name = "result2"
        end

        context "with stored results" do
          setup do
            assert SimpleCov::ResultMerger.store_result(@result1)
            assert SimpleCov::ResultMerger.store_result(@result2)
          end

          should "have stored data in resultset_path yaml file" do
            assert File.readlines(SimpleCov::ResultMerger.resultset_path).length > 50
          end

          should "return a hash containing keys ['result1' and 'result2'] for resultset" do
            assert_equal ['result1', 'result2'], SimpleCov::ResultMerger.resultset.keys.sort
          end

          should "return proper values for merged_result" do
            assert_equal [nil, 2, 6, 2, nil, nil, 2, 0, nil, nil], SimpleCov::ResultMerger.merged_result.source_files.find {|s| s.filename =~ /user/}.lines.map(&:coverage)
          end

          context "with second result way above the merge_timeout" do
            setup do
              @result2.created_at = Time.now - 172800 # two days ago
              assert SimpleCov::ResultMerger.store_result(@result2)
            end

            should "have only one result in SimpleCov::ResultMerger.results" do
              assert_equal 1, SimpleCov::ResultMerger.results.length
            end
          end

          context "with merging disabled" do
            setup { SimpleCov.use_merging false }

            should "return nil for SimpleCov.result" do
              assert_nil SimpleCov.result
            end
          end
        end

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