summaryrefslogtreecommitdiff
path: root/spec/models/tree_spec.rb
blob: 1522d836f760f4c9675c47b756d787e49c1d06fc (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Tree do
  let(:repository) { create(:project, :repository).repository }
  let(:sha) { repository.root_ref }

  subject { described_class.new(repository, '54fcc214') }

  describe '#readme' do
    before do
      stub_const('FakeBlob', Class.new)
      FakeBlob.class_eval do
        attr_reader :name

        def initialize(name)
          @name = name
        end

        def readme?
          name =~ /^readme/i
        end
      end
    end

    it 'returns nil when repository does not contains a README file' do
      files = [FakeBlob.new('file'), FakeBlob.new('license'), FakeBlob.new('copying')]
      expect(subject).to receive(:blobs).and_return(files)

      expect(subject.readme).to eq nil
    end

    it 'returns nil when repository does not contains a previewable README file' do
      files = [FakeBlob.new('file'), FakeBlob.new('README.pages'), FakeBlob.new('README.png')]
      expect(subject).to receive(:blobs).and_return(files)

      expect(subject.readme).to eq nil
    end

    it 'returns README when repository contains a previewable README file' do
      files = [FakeBlob.new('README.png'), FakeBlob.new('README'), FakeBlob.new('file')]
      expect(subject).to receive(:blobs).and_return(files)

      expect(subject.readme.name).to eq 'README'
    end

    it 'returns first previewable README when repository contains more than one' do
      files = [FakeBlob.new('file'), FakeBlob.new('README.md'), FakeBlob.new('README.asciidoc')]
      expect(subject).to receive(:blobs).and_return(files)

      expect(subject.readme.name).to eq 'README.md'
    end

    it 'returns first plain text README when repository contains more than one' do
      files = [FakeBlob.new('file'), FakeBlob.new('README'), FakeBlob.new('README.txt')]
      expect(subject).to receive(:blobs).and_return(files)

      expect(subject.readme.name).to eq 'README'
    end

    it 'prioritizes previewable README file over one in plain text' do
      files = [FakeBlob.new('file'), FakeBlob.new('README'), FakeBlob.new('README.md')]
      expect(subject).to receive(:blobs).and_return(files)

      expect(subject.readme.name).to eq 'README.md'
    end
  end
end