semver_dialects 1.6.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6cc2bd801dde6d272799f9d459e65dd035a8b2ae65e66a635d6ae423d7667a9f
4
- data.tar.gz: bc3692ba314609875927812115cca95199a88af38eb6b69a652dc69f22183099
3
+ metadata.gz: 4bf47952cc234d33748963e58c0705c505099123cc31c3add7ae0327ae71602a
4
+ data.tar.gz: f98d8ae7b8132c48c3e532829dcd681ab05fa5c75114ff640704ee9c3b08ee35
5
5
  SHA512:
6
- metadata.gz: 6417b415513cbc8a0c579303630ac62971cba025d4ee5b8a47fe3d819e538344dfb69f645dc852c188a1afee2cc753c9ffc9454b59454e152fb7060aa1f9b711
7
- data.tar.gz: 8ec38eb372ff41181bd336d7724008296a18cca71d461c2484df2b86dee7e554540f76182f0f40496b4ddba0be0e6fcba4511df6ba897ad41e7a1f7d870f2d9a
6
+ metadata.gz: f679d527db82d52b690e0e055c3313f45f0b27ecd79e72f98a0ed0f0b79a7d3345cc322eb4d02ca2522bcaa6ddf1de319d2c09cd876e85cdc739249a77664410
7
+ data.tar.gz: 6a82935c5c66b18d7a61fa99df1ff176976b5d48b72297289b14c4123e628e15b0462cd8fa2a5cb0f21448c34e7fb5818879d1c157d554470be5de0781854f15
@@ -5,7 +5,21 @@ require_relative '../../utils.rb'
5
5
  class SemanticVersion
6
6
  attr_reader :version_string, :prefix_delimiter, :prefix_segments, :suffix_segments, :segments
7
7
 
8
+ # String to build a regexp that matches a version.
9
+ #
10
+ # A version might start with a leading "v", then it must have a digit,
11
+ # then it might have any sequence made of alphanumerical characters,
12
+ # underscores, dots, dashes, and wildcards.
13
+ VERSION_PATTERN = "v?[0-9][a-zA-Z0-9_.*+-]*"
14
+
15
+ # Regexp for a string that only contains a single version string.
16
+ VERSION_ONLY_REGEXP = Regexp.new("\\A#{VERSION_PATTERN}\\z").freeze
17
+
8
18
  def initialize(version_string, segments = nil)
19
+ unless VERSION_ONLY_REGEXP.match version_string
20
+ raise SemverDialects::InvalidVersionError, version_string
21
+ end
22
+
9
23
  @version_string = version_string
10
24
  @prefix_segments = []
11
25
  @suffix_segments = []
@@ -2,6 +2,9 @@ require_relative "version_cut"
2
2
  require_relative "version_interval"
3
3
 
4
4
  module VersionParser
5
+ # A constraint is made of an operator followed by a version string.
6
+ CONSTRAINT_REGEXP = Regexp.new("(?<op>[><=]+) *(?<version>#{SemanticVersion::VERSION_PATTERN})").freeze
7
+
5
8
  def self.parse(versionstring)
6
9
  if (versionstring == "=*")
7
10
  # special case = All Versions
@@ -10,9 +13,9 @@ module VersionParser
10
13
 
11
14
  version_items = versionstring.split(" ")
12
15
  interval = VersionInterval.new(IntervalType::LEFT_OPEN | IntervalType::RIGHT_OPEN, BelowAll.new(), AboveAll.new())
13
- version_items.each do
14
- |version_item|
15
- matches = version_item.match /(?<op>[><=]+) *(?<version>[a-zA-Z0-9\-_\.\*]+)/
16
+ version_items.each do |version_item|
17
+ matches = version_item.match CONSTRAINT_REGEXP
18
+ raise SemverDialects::InvalidConstraintError, versionstring if matches.nil?
16
19
  version_string = matches[:version]
17
20
  case matches[:op]
18
21
  when ">="
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SemverDialects
4
- VERSION = '1.6.1'
4
+ VERSION = '2.0.0'
5
5
  end
@@ -14,6 +14,36 @@ module SemverDialects
14
14
  end
15
15
  end
16
16
 
17
+ class UnsupportedPackageTypeError < Error
18
+ def initialize(pkgType)
19
+ @pkgType = pkgType
20
+ end
21
+
22
+ def message
23
+ "unsupported package type '#{@pkgType}'"
24
+ end
25
+ end
26
+
27
+ class InvalidVersionError < Error
28
+ def initialize(raw_version)
29
+ @raw_version = raw_version
30
+ end
31
+
32
+ def message
33
+ "invalid version '#{@raw_version}'"
34
+ end
35
+ end
36
+
37
+ class InvalidConstraintError < Error
38
+ def initialize(raw_constraint)
39
+ @raw_constraint = raw_constraint
40
+ end
41
+
42
+ def message
43
+ "invalid constraint '#{@raw_constraint}'"
44
+ end
45
+ end
46
+
17
47
  # A utiltity module that helps with version matching
18
48
  module VersionChecker
19
49
  def self.version_translate(typ, version_string)
@@ -35,26 +65,32 @@ module SemverDialects
35
65
  when 'packagist'
36
66
  VersionTranslator.translate_packagist(version_string)
37
67
  else
38
- raise SemverDialects::Error, "unsupported package type '#{typ}'"
68
+ raise UnsupportedPackageTypeError, typ
39
69
  end
40
70
  end
41
71
 
72
+ # Determines if a version of a given package type satisfies a constraint.
73
+ #
74
+ # On normal execution, this method might raise the following exceptions:
75
+ #
76
+ # - UnsupportedPackageTypeError if the package type is not supported
77
+ # - InvalidVersionError if the version is invalid
78
+ # - InvalidConstraintError if the constraint is invalid or contains invalid versions
79
+ #
42
80
  def self.version_sat?(typ, raw_ver, raw_constraint)
43
81
  # os package versions are handled very differently from application package versions
44
82
  return os_pkg_version_sat?(typ, raw_ver, raw_constraint) if os_purl_type?(typ)
45
83
 
46
- version_constraint = version_translate(typ, raw_constraint)
47
- raise SemverDialects::Error, 'malformed constraint' if version_constraint.nil? || version_constraint.empty?
48
-
49
- version = VersionParser.parse('=' + raw_ver)
50
- raise SemverDialects::Error, 'malformed constraint' if version.nil? || version.empty?
84
+ # build an interval that only contains the version
85
+ version = VersionCut.new(raw_ver)
86
+ version_as_interval = VersionInterval.new(IntervalType::LEFT_CLOSED | IntervalType::RIGHT_CLOSED, version, version)
51
87
 
52
88
  constraint = VersionRange.new
53
- version_constraint.each do |version_interval_str|
89
+ version_translate(typ, raw_constraint).each do |version_interval_str|
54
90
  constraint << VersionParser.parse(version_interval_str)
55
91
  end
56
92
 
57
- constraint.overlaps_with?(version)
93
+ constraint.overlaps_with?(version_as_interval)
58
94
  end
59
95
 
60
96
  def self.os_purl_type?(typ)
@@ -65,7 +101,7 @@ module SemverDialects
65
101
  if typ == 'deb'
66
102
  # we only support the less than operator, because that's the only one currently output
67
103
  # by the advisory exporter for operating system packages.
68
- raise SemverDialects::Error, 'malformed constraint' unless raw_constraint[0] == '<'
104
+ raise SemverDialects::InvalidConstraintError, raw_constraint unless raw_constraint[0] == '<'
69
105
 
70
106
  v1 = DebVersion.new(raw_ver)
71
107
  v2 = DebVersion.new(raw_constraint[1..-1])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: semver_dialects
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Thome
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2024-01-26 00:00:00.000000000 Z
13
+ date: 2024-03-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: pastel
@@ -74,14 +74,14 @@ dependencies:
74
74
  requirements:
75
75
  - - "~>"
76
76
  - !ruby/object:Gem::Version
77
- version: 2.4.9
77
+ version: '2.4'
78
78
  type: :development
79
79
  prerelease: false
80
80
  version_requirements: !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - - "~>"
83
83
  - !ruby/object:Gem::Version
84
- version: 2.4.9
84
+ version: '2.4'
85
85
  - !ruby/object:Gem::Dependency
86
86
  name: rake
87
87
  requirement: !ruby/object:Gem::Requirement
@@ -152,8 +152,8 @@ licenses:
152
152
  metadata:
153
153
  allowed_push_host: https://rubygems.org
154
154
  homepage_uri: https://rubygems.org/gems/semver_dialects
155
- source_code_uri: https://gitlab.com/gitlab-org/vulnerability-research/foss/semver_dialects
156
- changelog_uri: https://gitlab.com/gitlab-org/vulnerability-research/foss/semver_dialects/-/blob/master/CHANGELOG.md
155
+ source_code_uri: https://gitlab.com/gitlab-org/ruby/gems/semver_dialects
156
+ changelog_uri: https://gitlab.com/gitlab-org/ruby/gems/semver_dialects/-/blob/master/CHANGELOG.md
157
157
  post_install_message:
158
158
  rdoc_options: []
159
159
  require_paths: