summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/changelog/release_spec.rb
blob: f95244d67500b5aa2ac5f6be4f637345e4e9d253 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Changelog::Release do
  describe '#to_markdown' do
    let(:config) { Gitlab::Changelog::Config.new(build_stubbed(:project)) }
    let(:commit) { build_stubbed(:commit) }
    let(:author) { build_stubbed(:user) }
    let(:mr) { build_stubbed(:merge_request) }
    let(:release) do
      described_class
        .new(version: '1.0.0', date: Time.utc(2021, 1, 5), config: config)
    end

    context 'when there are no entries' do
      it 'includes a notice about the lack of entries' do
        expect(release.to_markdown).to eq(<<~OUT)
          ## 1.0.0 (2021-01-05)

          No changes.

        OUT
      end
    end

    context 'when all data is present' do
      it 'includes all data' do
        allow(config).to receive(:contributor?).with(author).and_return(true)

        release.add_entry(
          title: 'Entry title',
          commit: commit,
          category: 'fixed',
          author: author,
          merge_request: mr
        )

        expect(release.to_markdown).to eq(<<~OUT)
          ## 1.0.0 (2021-01-05)

          ### fixed (1 change)

          - [Entry title](#{commit.to_reference(full: true)}) \
          by #{author.to_reference(full: true)} \
          ([merge request](#{mr.to_reference(full: true)}))

        OUT
      end
    end

    context 'when no merge request is present' do
      it "doesn't include a merge request link" do
        allow(config).to receive(:contributor?).with(author).and_return(true)

        release.add_entry(
          title: 'Entry title',
          commit: commit,
          category: 'fixed',
          author: author
        )

        expect(release.to_markdown).to eq(<<~OUT)
          ## 1.0.0 (2021-01-05)

          ### fixed (1 change)

          - [Entry title](#{commit.to_reference(full: true)}) \
          by #{author.to_reference(full: true)}

        OUT
      end
    end

    context 'when the author is not a contributor' do
      it "doesn't include the author" do
        allow(config).to receive(:contributor?).with(author).and_return(false)

        release.add_entry(
          title: 'Entry title',
          commit: commit,
          category: 'fixed',
          author: author
        )

        expect(release.to_markdown).to eq(<<~OUT)
          ## 1.0.0 (2021-01-05)

          ### fixed (1 change)

          - [Entry title](#{commit.to_reference(full: true)})

        OUT
      end
    end

    context 'when a category has no entries' do
      it "isn't included in the output" do
        config.categories['kittens'] = 'Kittens'
        config.categories['fixed'] = 'Bug fixes'

        release.add_entry(
          title: 'Entry title',
          commit: commit,
          category: 'fixed'
        )

        expect(release.to_markdown).to eq(<<~OUT)
          ## 1.0.0 (2021-01-05)

          ### Bug fixes (1 change)

          - [Entry title](#{commit.to_reference(full: true)})

        OUT
      end
    end
  end

  describe '#header_start_position' do
    it 'returns a regular expression for finding the start of a release section' do
      config = Gitlab::Changelog::Config.new(build_stubbed(:project))
      release = described_class
        .new(version: '1.0.0', date: Time.utc(2021, 1, 5), config: config)

      expect(release.header_start_pattern).to eq(/^##\s*1\.0\.0/)
    end
  end
end