File: ref_matcher_shared_examples.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (110 lines) | stat: -rw-r--r-- 3,041 bytes parent folder | download
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
# frozen_string_literal: true

# All of these examples expect a `ref_matcher` which has been initialized using
# the `ref_pattern` var e.g.
#   subject(:ref_matcher) { RefMatcher.new(ref_pattern) }
#   subject(:ref_matcher) { ProtectedBranch.new(name: ref_pattern) }
RSpec.shared_examples 'RefMatcher#matching' do
  subject(:matching) { ref_matcher.matching(refs) }

  shared_examples 'returns `refs` matching `ref_pattern`' do
    context 'when there is a match' do
      let(:ref_pattern) { exact_ref_pattern }

      it { is_expected.to match_array(exact_matches) }
    end

    context 'when there is no match' do
      let(:ref_pattern) { 'unknown' }

      it { is_expected.to be_empty }
    end

    context 'when ref pattern is a wildcard' do
      let(:ref_pattern) { wildcard_ref_pattern }

      it { is_expected.to match_array(wildcard_matches) }
    end
  end

  context 'when refs are strings' do
    let(:refs) { ['v1.0', 'v1.1', 'v2.0'] }
    let(:exact_ref_pattern) { 'v1.0' }
    let(:exact_matches) { ['v1.0'] }
    let(:wildcard_ref_pattern) { 'v1.*' }
    let(:wildcard_matches) { ['v1.0', 'v1.1'] }

    it_behaves_like 'returns `refs` matching `ref_pattern`'
  end

  context 'when refs are ref objects' do
    let(:v_one) { instance_double('Gitlab::Git::Ref', name: 'v1.0') }
    let(:v_one_one) { instance_double('Gitlab::Git::Ref', name: 'v1.1') }
    let(:v_two) { instance_double('Gitlab::Git::Ref', name: 'v2.0') }
    let(:refs) { [v_one, v_one_one, v_two] }
    let(:exact_ref_pattern) { 'v1.0' }
    let(:exact_matches) { [v_one] }
    let(:wildcard_ref_pattern) { 'v1.*' }
    let(:wildcard_matches) { [v_one, v_one_one] }

    it_behaves_like 'returns `refs` matching `ref_pattern`'
  end
end

RSpec.shared_examples 'RefMatcher#matches?' do
  let(:ref_pattern) { 'v1.0' }
  let(:ref_name) { 'v1.0' }

  subject(:matches) { ref_matcher.matches?(ref_name) }

  context 'when ref_pattern matches ref_name' do
    it { is_expected.to be_truthy }
  end

  context 'when ref_name is empty' do
    let(:ref_name) { '' }

    it { is_expected.to be_falsey }
  end

  context 'when ref_pattern wildcard matches ref_name' do
    let(:ref_pattern) { 'v*' }

    it { is_expected.to be_truthy }
  end

  context 'when ref_pattern wildcard does not match ref_name' do
    let(:ref_pattern) { 'v2.*' }

    it { is_expected.to be_falsey }
  end

  context 'when ref_pattern with ReDoS' do
    let(:ref_pattern) { '**************a' }
    let(:ref_name) { 'aaaaaaaaaaaaaaaaaaaaa' }

    it 'does not cause catastrophic backtracking' do
      expect do
        Timeout.timeout(10.seconds) do
          is_expected.to be_truthy
        end
      end.not_to raise_error
    end
  end
end

RSpec.shared_examples 'RefMatcher#wildcard?' do
  subject(:wildcard) { ref_matcher.wildcard? }

  context 'when pattern is not a wildcard' do
    let(:ref_pattern) { 'v1' }

    it { is_expected.to be_falsey }
  end

  context 'when pattern is a wildcard' do
    let(:ref_pattern) { 'v*' }

    it { is_expected.to be_truthy }
  end
end