diff_helper_spec.rb 4.6 KB
Newer Older
M
Marin Jankovski 已提交
1 2 3 4 5 6
require 'spec_helper'

describe DiffHelper do
  include RepoHelpers

  let(:project) { create(:project) }
R
Rubén Dávila 已提交
7
  let(:repository) { project.repository }
8
  let(:commit) { project.commit(sample_commit.id) }
A
Alex Lossent 已提交
9 10
  let(:diffs) { commit.diffs }
  let(:diff) { diffs.first }
11 12
  let(:diff_refs) { [commit.parent, commit] }
  let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs) }
M
Marin Jankovski 已提交
13 14 15

  describe 'diff_hard_limit_enabled?' do
    it 'should return true if param is provided' do
J
Jeroen van Baarsen 已提交
16
      allow(controller).to receive(:params) { { force_show_diff: true } }
17
      expect(diff_hard_limit_enabled?).to be_truthy
M
Marin Jankovski 已提交
18 19 20
    end

    it 'should return false if param is not provided' do
21
      expect(diff_hard_limit_enabled?).to be_falsey
M
Marin Jankovski 已提交
22 23 24 25 26
    end
  end

  describe 'allowed_diff_size' do
    it 'should return hard limit for a diff if force diff is true' do
J
Jeroen van Baarsen 已提交
27
      allow(controller).to receive(:params) { { force_show_diff: true } }
28
      expect(allowed_diff_size).to eq(1000)
M
Marin Jankovski 已提交
29 30 31
    end

    it 'should return safe limit for a diff if force diff is false' do
32
      expect(allowed_diff_size).to eq(100)
M
Marin Jankovski 已提交
33 34 35
    end
  end

A
Alex Lossent 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
  describe 'allowed_diff_lines' do
    it 'should return hard limit for number of lines in a diff if force diff is true' do
      allow(controller).to receive(:params) { { force_show_diff: true } }
      expect(allowed_diff_lines).to eq(50000)
    end

    it 'should return safe limit for numbers of lines a diff if force diff is false' do
      expect(allowed_diff_lines).to eq(5000)
    end
  end

  describe 'safe_diff_files' do
    it 'should return all files from a commit that is smaller than safe limits' do
49
      expect(safe_diff_files(diffs, diff_refs).length).to eq(2)
A
Alex Lossent 已提交
50 51 52
    end

    it 'should return only the first file if the diff line count in the 2nd file takes the total beyond safe limits' do
53
      allow(diffs[1].diff).to receive(:lines).and_return([""] * 4999) #simulate 4999 lines
54
      expect(safe_diff_files(diffs, diff_refs).length).to eq(1)
A
Alex Lossent 已提交
55 56 57 58
    end

    it 'should return all files from a commit that is beyond safe limit for numbers of lines if force diff is true' do
      allow(controller).to receive(:params) { { force_show_diff: true } }
59
      allow(diffs[1].diff).to receive(:lines).and_return([""] * 4999) #simulate 4999 lines
60
      expect(safe_diff_files(diffs, diff_refs).length).to eq(2)
A
Alex Lossent 已提交
61 62 63 64
    end

    it 'should return only the first file if the diff line count in the 2nd file takes the total beyond hard limits' do
      allow(controller).to receive(:params) { { force_show_diff: true } }
65
      allow(diffs[1].diff).to receive(:lines).and_return([""] * 49999) #simulate 49999 lines
66
      expect(safe_diff_files(diffs, diff_refs).length).to eq(1)
A
Alex Lossent 已提交
67 68 69 70
    end

    it 'should return only a safe number of file diffs if a commit touches more files than the safe limits' do
      large_diffs = diffs * 100 #simulate 200 diffs
71
      expect(safe_diff_files(large_diffs, diff_refs).length).to eq(100)
A
Alex Lossent 已提交
72 73 74 75 76
    end

    it 'should return all file diffs if a commit touches more files than the safe limits but force diff is true' do
      allow(controller).to receive(:params) { { force_show_diff: true } }
      large_diffs = diffs * 100 #simulate 200 diffs
77
      expect(safe_diff_files(large_diffs, diff_refs).length).to eq(200)
A
Alex Lossent 已提交
78 79 80 81 82
    end

    it 'should return a limited file diffs if a commit touches more files than the hard limits and force diff is true' do
      allow(controller).to receive(:params) { { force_show_diff: true } }
      very_large_diffs = diffs * 1000 #simulate 2000 diffs
83
      expect(safe_diff_files(very_large_diffs, diff_refs).length).to eq(1000)
A
Alex Lossent 已提交
84 85 86
    end
  end

M
Marin Jankovski 已提交
87 88
  describe 'unfold_bottom_class' do
    it 'should return empty string when bottom line shouldnt be unfolded' do
89
      expect(unfold_bottom_class(false)).to eq('')
M
Marin Jankovski 已提交
90 91 92
    end

    it 'should return js class when bottom lines should be unfolded' do
93
      expect(unfold_bottom_class(true)).to eq('js-unfold-bottom')
M
Marin Jankovski 已提交
94 95 96
    end
  end

97 98 99 100 101 102 103 104 105 106
  describe 'unfold_class' do
    it 'returns empty on false' do
      expect(unfold_class(false)).to eq('')
    end

    it 'returns a class on true' do
      expect(unfold_class(true)).to eq('unfold js-unfold')
    end
  end

M
Marin Jankovski 已提交
107 108 109
  describe 'diff_line_content' do

    it 'should return non breaking space when line is empty' do
J
Jeroen van Baarsen 已提交
110
      expect(diff_line_content(nil)).to eq('  ')
M
Marin Jankovski 已提交
111 112 113
    end

    it 'should return the line itself' do
J
Jeroen van Baarsen 已提交
114 115 116
      expect(diff_line_content(diff_file.diff_lines.first.text)).
        to eq('@@ -6,12 +6,18 @@ module Popen')
      expect(diff_line_content(diff_file.diff_lines.first.type)).to eq('match')
117 118 119 120 121
      expect(diff_file.diff_lines.first.new_pos).to eq(6)
    end

    it 'should return safe HTML' do
      expect(diff_line_content(diff_file.diff_lines.first.text)).to be_html_safe
M
Marin Jankovski 已提交
122 123 124
    end
  end
end