File: range_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (175 lines) | stat: -rw-r--r-- 6,180 bytes parent folder | download | duplicates (2)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require 'spec_helper'
require 'puppet/util/package/version/range'

class IntegerVersion
  class ValidationFailure < ArgumentError; end
  include Comparable
  REGEX_FULL    = '(\d+)'.freeze
  REGEX_FULL_RX = /\A#{REGEX_FULL}\Z/.freeze

  def self.parse(ver)
    match, version = *ver.match(REGEX_FULL_RX)
    raise ValidationFailure, "Unable to parse '#{ver}' as a version identifier" unless match

    new(version).freeze
  end

  attr_reader :version

  def initialize(version)
    @version = version.to_i
  end

  def <=>(other)
    @version <=> other.version
  end
end

describe Puppet::Util::Package::Version::Range do
  context 'when creating new version range' do
    it 'should raise unless String is passed' do
      expect { Puppet::Util::Package::Version::Range.parse(:abc, IntegerVersion) }.to raise_error(Puppet::Util::Package::Version::Range::ValidationFailure)
    end
    it 'should raise if operator is not implemented' do
      expect { Puppet::Util::Package::Version::Range.parse('=a', IntegerVersion) }.to raise_error(Puppet::Util::Package::Version::Range::ValidationFailure)
    end
    it 'should raise if operator cannot be parsed' do
      expect { Puppet::Util::Package::Version::Range.parse('~=a', IntegerVersion) }.to raise_error(IntegerVersion::ValidationFailure)
    end
    it 'should raise if version cannot be parsed' do
      expect { Puppet::Util::Package::Version::Range.parse('>=a', IntegerVersion) }.to raise_error(IntegerVersion::ValidationFailure)
    end
  end

  context 'when creating new version range with regular version' do
    it 'it does not include greater version' do
      vr = Puppet::Util::Package::Version::Range.parse('3', IntegerVersion)
      v = IntegerVersion.parse('4')
      expect(vr.include?(v)).to eql(false)
    end

    it 'it includes specified version' do
      vr = Puppet::Util::Package::Version::Range.parse('3', IntegerVersion)
      v = IntegerVersion.parse('3')
      expect(vr.include?(v)).to eql(true)
    end

    it 'it does not include lower version' do
      vr = Puppet::Util::Package::Version::Range.parse('3', IntegerVersion)
      v = IntegerVersion.parse('2')
      expect(vr.include?(v)).to eql(false)
    end
  end

  context 'when creating new version range with greater or equal operator' do
    it 'it includes greater version' do
      vr = Puppet::Util::Package::Version::Range.parse('>=3', IntegerVersion)
      v = IntegerVersion.parse('4')
      expect(vr.include?(v)).to eql(true)
    end

    it 'it includes specified version' do
      vr = Puppet::Util::Package::Version::Range.parse('>=3', IntegerVersion)
      v = IntegerVersion.parse('3')
      expect(vr.include?(v)).to eql(true)
    end

    it 'it does not include lower version' do
      vr = Puppet::Util::Package::Version::Range.parse('>=3', IntegerVersion)
      v = IntegerVersion.parse('2')
      expect(vr.include?(v)).to eql(false)
    end
  end

  context 'when creating new version range with greater operator' do
    it 'it includes greater version' do
      vr = Puppet::Util::Package::Version::Range.parse('>3', IntegerVersion)
      v = IntegerVersion.parse('10')
      expect(vr.include?(v)).to eql(true)
    end

    it 'it does not include specified version' do
      vr = Puppet::Util::Package::Version::Range.parse('>3', IntegerVersion)
      v = IntegerVersion.parse('3')
      expect(vr.include?(v)).to eql(false)
    end

    it 'it does not include lower version' do
      vr = Puppet::Util::Package::Version::Range.parse('>3', IntegerVersion)
      v = IntegerVersion.parse('1')
      expect(vr.include?(v)).to eql(false)
    end
  end

  context 'when creating new version range with lower or equal operator' do
    it 'it does not include greater version' do
      vr = Puppet::Util::Package::Version::Range.parse('<=3', IntegerVersion)
      v = IntegerVersion.parse('5')
      expect(vr.include?(v)).to eql(false)
    end

    it 'it includes specified version' do
      vr = Puppet::Util::Package::Version::Range.parse('<=3', IntegerVersion)
      v = IntegerVersion.parse('3')
      expect(vr.include?(v)).to eql(true)
    end

    it 'it includes lower version' do
      vr = Puppet::Util::Package::Version::Range.parse('<=3', IntegerVersion)
      v = IntegerVersion.parse('1')
      expect(vr.include?(v)).to eql(true)
    end
  end

  context 'when creating new version range with lower operator' do
    it 'it does not include greater version' do
      vr = Puppet::Util::Package::Version::Range.parse('<3', IntegerVersion)
      v = IntegerVersion.parse('8')
      expect(vr.include?(v)).to eql(false)
    end

    it 'it does not include specified version' do
      vr = Puppet::Util::Package::Version::Range.parse('<3', IntegerVersion)
      v = IntegerVersion.parse('3')
      expect(vr.include?(v)).to eql(false)
    end

    it 'it includes lower version' do
      vr = Puppet::Util::Package::Version::Range.parse('<3', IntegerVersion)
      v = IntegerVersion.parse('2')
      expect(vr.include?(v)).to eql(true)
    end
  end

  context 'when creating new version range with interval' do
    it 'it does not include greater version' do
      vr = Puppet::Util::Package::Version::Range.parse('>3 <=5', IntegerVersion)
      v = IntegerVersion.parse('7')
      expect(vr.include?(v)).to eql(false)
    end

    it 'it includes specified max interval value' do
      vr = Puppet::Util::Package::Version::Range.parse('>3 <=5', IntegerVersion)
      v = IntegerVersion.parse('5')
      expect(vr.include?(v)).to eql(true)
    end

    it 'it includes in interval version' do
      vr = Puppet::Util::Package::Version::Range.parse('>3 <=5', IntegerVersion)
      v = IntegerVersion.parse('4')
      expect(vr.include?(v)).to eql(true)
    end

    it 'it does not include min interval value ' do
      vr = Puppet::Util::Package::Version::Range.parse('>3 <=5', IntegerVersion)
      v = IntegerVersion.parse('3')
      expect(vr.include?(v)).to eql(false)
    end

    it 'it does not include lower value ' do
      vr = Puppet::Util::Package::Version::Range.parse('>3 <=5', IntegerVersion)
      v = IntegerVersion.parse('2')
      expect(vr.include?(v)).to eql(false)
    end
  end
end