BigW Consortium Gitlab

closing_issue_extractor_spec.rb 6.8 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe Gitlab::ClosingIssueExtractor, lib: true do
4
  let(:project)   { create(:project) }
5
  let(:project2)   { create(:project) }
6
  let(:issue)     { create(:issue, project: project) }
7
  let(:issue2)     { create(:issue, project: project2) }
8
  let(:reference) { issue.to_reference }
9
  let(:cross_reference) { issue2.to_reference(project) }
10

Douwe Maan committed
11 12
  subject { described_class.new(project, project.creator) }

13 14 15 16
  before do
    project2.team << [project.creator, :master]
  end

Douwe Maan committed
17
  describe "#closed_by_message" do
18 19
    context 'with a single reference' do
      it do
20
        message = "Awesome commit (Closes #{reference})"
Douwe Maan committed
21
        expect(subject.closed_by_message(message)).to eq([issue])
22 23 24
      end

      it do
25
        message = "Awesome commit (closes #{reference})"
Douwe Maan committed
26
        expect(subject.closed_by_message(message)).to eq([issue])
27 28 29
      end

      it do
30
        message = "Closed #{reference}"
Douwe Maan committed
31
        expect(subject.closed_by_message(message)).to eq([issue])
32 33 34
      end

      it do
35
        message = "closed #{reference}"
Douwe Maan committed
36
        expect(subject.closed_by_message(message)).to eq([issue])
37 38
      end

39
      it do
40
        message = "Closing #{reference}"
Douwe Maan committed
41
        expect(subject.closed_by_message(message)).to eq([issue])
42 43 44
      end

      it do
45
        message = "closing #{reference}"
Douwe Maan committed
46
        expect(subject.closed_by_message(message)).to eq([issue])
47 48 49
      end

      it do
50
        message = "Close #{reference}"
Douwe Maan committed
51
        expect(subject.closed_by_message(message)).to eq([issue])
52 53 54
      end

      it do
55
        message = "close #{reference}"
Douwe Maan committed
56
        expect(subject.closed_by_message(message)).to eq([issue])
57 58 59
      end

      it do
60
        message = "Awesome commit (Fixes #{reference})"
Douwe Maan committed
61
        expect(subject.closed_by_message(message)).to eq([issue])
62 63
      end

64
      it do
65
        message = "Awesome commit (fixes #{reference})"
Douwe Maan committed
66
        expect(subject.closed_by_message(message)).to eq([issue])
67 68 69
      end

      it do
70
        message = "Fixed #{reference}"
Douwe Maan committed
71
        expect(subject.closed_by_message(message)).to eq([issue])
72 73 74
      end

      it do
75
        message = "fixed #{reference}"
Douwe Maan committed
76
        expect(subject.closed_by_message(message)).to eq([issue])
77 78 79
      end

      it do
80
        message = "Fixing #{reference}"
Douwe Maan committed
81
        expect(subject.closed_by_message(message)).to eq([issue])
82 83 84
      end

      it do
85
        message = "fixing #{reference}"
Douwe Maan committed
86
        expect(subject.closed_by_message(message)).to eq([issue])
87 88 89
      end

      it do
90
        message = "Fix #{reference}"
Douwe Maan committed
91
        expect(subject.closed_by_message(message)).to eq([issue])
92 93 94
      end

      it do
95
        message = "fix #{reference}"
Douwe Maan committed
96
        expect(subject.closed_by_message(message)).to eq([issue])
97 98 99
      end

      it do
100
        message = "Awesome commit (Resolves #{reference})"
Douwe Maan committed
101
        expect(subject.closed_by_message(message)).to eq([issue])
102 103 104
      end

      it do
105
        message = "Awesome commit (resolves #{reference})"
Douwe Maan committed
106
        expect(subject.closed_by_message(message)).to eq([issue])
107 108 109
      end

      it do
110
        message = "Resolved #{reference}"
Douwe Maan committed
111
        expect(subject.closed_by_message(message)).to eq([issue])
112 113 114
      end

      it do
115
        message = "resolved #{reference}"
Douwe Maan committed
116
        expect(subject.closed_by_message(message)).to eq([issue])
117 118 119
      end

      it do
120
        message = "Resolving #{reference}"
Douwe Maan committed
121
        expect(subject.closed_by_message(message)).to eq([issue])
122 123 124
      end

      it do
125
        message = "resolving #{reference}"
Douwe Maan committed
126
        expect(subject.closed_by_message(message)).to eq([issue])
127 128 129
      end

      it do
130
        message = "Resolve #{reference}"
Douwe Maan committed
131
        expect(subject.closed_by_message(message)).to eq([issue])
132 133 134
      end

      it do
135
        message = "resolve #{reference}"
Douwe Maan committed
136
        expect(subject.closed_by_message(message)).to eq([issue])
137 138 139
      end
    end

140 141 142 143 144 145 146 147 148
    context "with a cross-project reference" do
      it do
        message = "Closes #{cross_reference}"
        expect(subject.closed_by_message(message)).to eq([issue2])
      end
    end

    context "with a cross-project URL" do
      it do
Douwe Maan committed
149
        message = "Closes #{urls.namespace_project_issue_url(issue2.project.namespace, issue2.project, issue2)}"
150 151 152 153 154 155
        expect(subject.closed_by_message(message)).to eq([issue2])
      end
    end

    context "with an invalid URL" do
      it do
Douwe Maan committed
156
        message = "Closes https://google.com#{urls.namespace_project_issue_path(issue2.project.namespace, issue2.project, issue2)}"
157 158 159 160
        expect(subject.closed_by_message(message)).to eq([])
      end
    end

161 162 163
    context 'with multiple references' do
      let(:other_issue) { create(:issue, project: project) }
      let(:third_issue) { create(:issue, project: project) }
164 165
      let(:reference2) { other_issue.to_reference }
      let(:reference3) { third_issue.to_reference }
166 167

      it 'fetches issues in single line message' do
168
        message = "Closes #{reference} and fix #{reference2}"
169

Douwe Maan committed
170
        expect(subject.closed_by_message(message)).
Douwe Maan committed
171
            to match_array([issue, other_issue])
172 173 174
      end

      it 'fetches comma-separated issues references in single line message' do
175
        message = "Closes #{reference}, closes #{reference2}"
176

Douwe Maan committed
177
        expect(subject.closed_by_message(message)).
Douwe Maan committed
178
            to match_array([issue, other_issue])
179 180 181
      end

      it 'fetches comma-separated issues numbers in single line message' do
182
        message = "Closes #{reference}, #{reference2} and #{reference3}"
183

Douwe Maan committed
184
        expect(subject.closed_by_message(message)).
Douwe Maan committed
185
            to match_array([issue, other_issue, third_issue])
186 187 188
      end

      it 'fetches issues in multi-line message' do
189
        message = "Awesome commit (closes #{reference})\nAlso fixes #{reference2}"
190

Douwe Maan committed
191
        expect(subject.closed_by_message(message)).
Douwe Maan committed
192
            to match_array([issue, other_issue])
193 194 195
      end

      it 'fetches issues in hybrid message' do
196
        message = "Awesome commit (closes #{reference})\n"\
197
                  "Also fixing issues #{reference2}, #{reference3} and #4"
198

Douwe Maan committed
199
        expect(subject.closed_by_message(message)).
Douwe Maan committed
200
            to match_array([issue, other_issue, third_issue])
201
      end
202 203 204 205 206 207 208 209 210

      it "fetches cross-project references" do
        message = "Closes #{reference} and #{cross_reference}"

        expect(subject.closed_by_message(message)).
            to match_array([issue, issue2])
      end

      it "fetches cross-project URL references" do
Douwe Maan committed
211
        message = "Closes #{urls.namespace_project_issue_url(issue2.project.namespace, issue2.project, issue2)} and #{reference}"
212 213 214 215 216 217

        expect(subject.closed_by_message(message)).
            to match_array([issue, issue2])
      end

      it "ignores invalid cross-project URL references" do
Douwe Maan committed
218
        message = "Closes https://google.com#{urls.namespace_project_issue_path(issue2.project.namespace, issue2.project, issue2)} and #{reference}"
219 220 221 222

        expect(subject.closed_by_message(message)).
            to match_array([issue])
      end
223 224
    end
  end
Douwe Maan committed
225 226 227 228

  def urls
    Gitlab::Application.routes.url_helpers
  end
229
end