debian-mirror-gitlab/spec/lib/gitlab/git/blame_spec.rb

82 lines
2.7 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require "spec_helper"
2022-08-13 15:12:31 +05:30
RSpec.describe Gitlab::Git::Blame do
let(:project) { create(:project, :repository) }
let(:repository) { project.repository.raw }
let(:sha) { TestEnv::BRANCH_SHA['master'] }
2022-06-21 17:19:12 +05:30
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
2017-08-17 22:00:37 +05:30
end
2018-11-08 19:23:39 +05:30
describe 'blaming a file' do
2022-06-21 17:19:12 +05:30
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
2017-08-17 22:00:37 +05:30
2022-06-21 17:19:12 +05:30
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.', ''])
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
context "ISO-8859 encoding" do
2022-06-21 17:19:12 +05:30
let(:path) { 'encoding/iso8859.txt' }
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
it 'converts to UTF-8' do
2022-06-21 17:19:12 +05:30
expect(result.size).to eq(1)
expect(result.first[:commit]).to be_kind_of(Gitlab::Git::Commit)
2022-08-13 15:12:31 +05:30
expect(result.first[:line]).to eq("Äü")
2022-06-21 17:19:12 +05:30
expect(result.first[:line]).to be_utf8
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
context "unknown encoding" do
2022-06-21 17:19:12 +05:30
let(:path) { 'encoding/iso8859.txt' }
2017-09-10 17:25:29 +05:30
it 'converts to UTF-8' do
2021-06-08 01:23:25 +05:30
expect_next_instance_of(CharlockHolmes::EncodingDetector) do |detector|
expect(detector).to receive(:detect).and_return(nil)
end
2022-06-21 17:19:12 +05:30
expect(result.size).to eq(1)
expect(result.first[:commit]).to be_kind_of(Gitlab::Git::Commit)
2022-08-13 15:12:31 +05:30
expect(result.first[:line]).to eq("")
2022-06-21 17:19:12 +05:30
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)
2017-08-17 22:00:37 +05:30
2022-06-21 17:19:12 +05:30
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)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
end
end
end