BigW Consortium Gitlab

blob_helper_spec.rb 3.93 KB
Newer Older
1 2 3
require 'spec_helper'

describe BlobHelper do
4 5
  include TreeHelper

6 7 8 9 10 11 12 13 14 15 16 17
  let(:blob_name) { 'test.lisp' }
  let(:no_context_content) { ":type \"assem\"))" }
  let(:blob_content) { "(make-pathname :defaults name\n#{no_context_content}" }
  let(:split_content) { blob_content.split("\n") }
  let(:multiline_content) do
    %q(
    def test(input):
      """This is line 1 of a multi-line comment.
      This is line 2.
      """
    )
  end
18

19
  describe '#highlight' do
20
    it 'returns plaintext for unknown lexer context' do
21
      result = helper.highlight(blob_name, no_context_content)
22
      expect(result).to eq(%[<pre class="code highlight"><code><span id="LC1" class="line">:type "assem"))</span></code></pre>])
23 24
    end

25
    it 'highlights single block' do
26 27
      expected = %Q[<pre class="code highlight"><code><span id="LC1" class="line"><span class="p">(</span><span class="nb">make-pathname</span> <span class="ss">:defaults</span> <span class="nv">name</span></span>
<span id="LC2" class="line"><span class="ss">:type</span> <span class="s">"assem"</span><span class="p">))</span></span></code></pre>]
28

29
      expect(helper.highlight(blob_name, blob_content)).to eq(expected)
30
    end
31

32
    it 'highlights multi-line comments' do
33
      result = helper.highlight(blob_name, multiline_content)
34 35 36 37
      html = Nokogiri::HTML(result)
      lines = html.search('.s')
      expect(lines.count).to eq(3)
      expect(lines[0].text).to eq('"""This is line 1 of a multi-line comment.')
38 39
      expect(lines[1].text).to eq('      This is line 2.')
      expect(lines[2].text).to eq('      """')
40
    end
Stan Hu committed
41 42 43 44 45

    context 'diff highlighting' do
      let(:blob_name) { 'test.diff' }
      let(:blob_content) { "+aaa\n+bbb\n- ccc\n ddd\n"}
      let(:expected) do
46
        %q(<pre class="code highlight"><code><span id="LC1" class="line"><span class="gi">+aaa</span></span>
Stan Hu committed
47 48
<span id="LC2" class="line"><span class="gi">+bbb</span></span>
<span id="LC3" class="line"><span class="gd">- ccc</span></span>
49
<span id="LC4" class="line"> ddd</span></code></pre>)
Stan Hu committed
50 51
      end

52
      it 'highlights each line properly' do
53
        result = helper.highlight(blob_name, blob_content)
Stan Hu committed
54 55 56
        expect(result).to eq(expected)
      end
    end
57
  end
58

59 60 61 62 63 64
  describe "#sanitize_svg" do
    let(:input_svg_path) { File.join(Rails.root, 'spec', 'fixtures', 'unsanitized.svg') }
    let(:data) { open(input_svg_path).read }
    let(:expected_svg_path) { File.join(Rails.root, 'spec', 'fixtures', 'sanitized.svg') }
    let(:expected) { open(expected_svg_path).read }

65
    it 'retains essential elements' do
66 67 68 69
      blob = OpenStruct.new(data: data)
      expect(sanitize_svg(blob).data).to eq(expected)
    end
  end
70 71

  describe "#edit_blob_link" do
72 73
    let(:namespace) { create(:namespace, name: 'gitlab' )}
    let(:project) { create(:project, namespace: namespace) }
74 75 76

    before do
      allow(self).to receive(:current_user).and_return(double)
77
      allow(self).to receive(:can_collaborate_with_project?).and_return(true)
78 79 80
    end

    it 'verifies blob is text' do
81
      expect(helper).not_to receive(:blob_text_viewable?)
82 83 84 85 86

      button = edit_blob_link(project, 'refs/heads/master', 'README.md')

      expect(button).to start_with('<button')
    end
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

    it 'uses the passed blob instead retrieve from repository' do
      blob = project.repository.blob_at('refs/heads/master', 'README.md')

      expect(project.repository).not_to receive(:blob_at)

      edit_blob_link(project, 'refs/heads/master', 'README.md', blob: blob)
    end

    it 'returns a link with the proper route' do
      link = edit_blob_link(project, 'master', 'README.md')

      expect(Capybara.string(link).find_link('Edit')[:href]).to eq('/gitlab/gitlabhq/edit/master/README.md')
    end

    it 'returns a link with the passed link_opts on the expected route' do
      link = edit_blob_link(project, 'master', 'README.md', link_opts: { mr_id: 10 })

      expect(Capybara.string(link).find_link('Edit')[:href]).to eq('/gitlab/gitlabhq/edit/master/README.md?mr_id=10')
    end
107
  end
108
end