summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/git/blame_spec.rb
blob: 7dd7460b142daaee21e1108bd231c24c92fe4cb6 (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
76
77
78
79
80
81
82
83
84
85
# 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(:sha) { SeedRepo::Commit::ID }
  let(:path) { 'CONTRIBUTING.md' }
  let(:range) { nil }

  subject(:blame) { Gitlab::Git::Blame.new(repository, sha, path, range: range) }

  let(:result) do
    [].tap do |data|
      blame.each do |commit, line, previous_path|
        data << { commit: commit, line: line, previous_path: previous_path }
      end
    end
  end

  describe 'blaming a file' do
    it 'has the right number of lines' do
      expect(result.size).to eq(95)
      expect(result.first[:commit]).to be_kind_of(Gitlab::Git::Commit)
      expect(result.first[:line]).to eq("# Contribute to GitLab")
      expect(result.first[:line]).to be_utf8
    end

    context 'blaming a range' do
      let(:range) { 2..4 }

      it 'only returns the range' do
        expect(result.size).to eq(range.size)
        expect(result.map {|r| r[:line] }).to eq(['', 'This guide details how contribute to GitLab.', ''])
      end
    end

    context "ISO-8859 encoding" do
      let(:sha) { SeedRepo::EncodingCommit::ID }
      let(:path) { 'encoding/iso8859.txt' }

      it 'converts to UTF-8' do
        expect(result.size).to eq(1)
        expect(result.first[:commit]).to be_kind_of(Gitlab::Git::Commit)
        expect(result.first[:line]).to eq("Ä ü")
        expect(result.first[:line]).to be_utf8
      end
    end

    context "unknown encoding" do
      let(:sha) { SeedRepo::EncodingCommit::ID }
      let(:path) { 'encoding/iso8859.txt' }

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

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

    context "renamed file" do
      let(:project) { create(:project, :repository) }
      let(:repository) { project.repository.raw_repository }
      let(:commit) { project.commit('blame-on-renamed') }
      let(:sha) { commit.id }
      let(:path) { 'files/plain_text/renamed' }

      it 'includes the previous path' do
        expect(result.size).to eq(5)

        expect(result[0]).to include(line: 'Initial commit', previous_path: nil)
        expect(result[1]).to include(line: 'Initial commit', previous_path: nil)
        expect(result[2]).to include(line: 'Renamed as "filename"', previous_path: 'files/plain_text/initial-commit')
        expect(result[3]).to include(line: 'Renamed as renamed', previous_path: 'files/plain_text/"filename"')
        expect(result[4]).to include(line: 'Last edit, no rename', previous_path: path)
      end
    end
  end
end