summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/git/blame_spec.rb
blob: 495cb16ebab40a79a75dcb98cea899b2e5bd6d67 (plain)
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
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Gitlab::Git::Blame, :seed_helper do
  let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '', 'group/project') }
  let(:blame) do
    Gitlab::Git::Blame.new(repository, SeedRepo::Commit::ID, "CONTRIBUTING.md")
  end

  describe 'blaming a file' do
    context "each count" do
      it do
        data = []
        blame.each do |commit, line|
          data << {
            commit: commit,
            line: line
          }
        end

        expect(data.size).to eq(95)
        expect(data.first[:commit]).to be_kind_of(Gitlab::Git::Commit)
        expect(data.first[:line]).to eq("# Contribute to GitLab")
        expect(data.first[:line]).to be_utf8
      end
    end

    context "ISO-8859 encoding" do
      let(:blame) do
        Gitlab::Git::Blame.new(repository, SeedRepo::EncodingCommit::ID, "encoding/iso8859.txt")
      end

      it 'converts to UTF-8' do
        data = []
        blame.each do |commit, line|
          data << {
            commit: commit,
            line: line
          }
        end

        expect(data.size).to eq(1)
        expect(data.first[:commit]).to be_kind_of(Gitlab::Git::Commit)
        expect(data.first[:line]).to eq("Ä ü")
        expect(data.first[:line]).to be_utf8
      end
    end

    context "unknown encoding" do
      let(:blame) do
        Gitlab::Git::Blame.new(repository, SeedRepo::EncodingCommit::ID, "encoding/iso8859.txt")
      end

      it 'converts to UTF-8' do
        expect_next_instance_of(CharlockHolmes::EncodingDetector) do |detector|
          expect(detector).to receive(:detect).and_return(nil)
        end

        data = []
        blame.each do |commit, line|
          data << {
            commit: commit,
            line: line
          }
        end

        expect(data.size).to eq(1)
        expect(data.first[:commit]).to be_kind_of(Gitlab::Git::Commit)
        expect(data.first[:line]).to eq(" ")
        expect(data.first[:line]).to be_utf8
      end
    end
  end
end