Welcome to mirror list, hosted at ThFree Co, Russian Federation.

blame_spec.rb « git « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 676ea2663d2cd712394671ea70e3cbf764f702dd (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
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Gitlab::Git::Blame do
  let(:project) { create(:project, :repository) }
  let(:repository) { project.repository.raw }
  let(:sha) { TestEnv::BRANCH_SHA['master'] }
  let(:path) { 'CONTRIBUTING.md' }
  let(:range) { nil }

  subject(:blame) { described_class.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(: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(: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(: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