semver_dialects 1.6.2 → 2.0.1

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: 39857e9e764ee20baf0fbc554b45189634fc792e026f8fe1ba6ff9a9bae2b742
4
- data.tar.gz: 5dcc13405766bf8fe25e4456dac42dcafe945e456e22d08d6063be59e977c80d
3
+ metadata.gz: 8f1e84f683c8e94cd71e65819cd9953ec91333b1286fe6d9c01ac9fbf2843d4c
4
+ data.tar.gz: 11ce3dbcdd748af0a5364d1ceb9569161a787cd4a6337056da60e1b73aa8d6dd
5
5
  SHA512:
6
- metadata.gz: 3b13a73b8fa16b037c7c7b224b8ce8ba3996fab09b98e0f979ae26f095537c19c459a061cf41ca9721ee6d0cd733dbbd9deaaf86210916507a57f57d7ed5c6a9
7
- data.tar.gz: 4b3967d51c1b9a61fee9dd31b88804268c142df322eaf08e731342aa672a5f37e1ad40847b1f70385de6dd1769cd58fdfea75a55c777a4f1072061a974c64ade
6
+ metadata.gz: e5b3b90ee346eb5649acab265702e1c4e563f53f4a539b95d2a84682c3d05507bc00c8b27399e43de3e00782d60c5264f2822a8370fc6b2ccd30bf7b11e364d3
7
+ data.tar.gz: 0e6b4ee40e4260a8a2cc0700379d0982a3029b58a76220f3ca89fd64ac3d3b3fe7e39227b660f5e8ce4220a6f15bd2dea26c4957b49479fa1aa9f1ff7d325eea
@@ -5,12 +5,26 @@ 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 = []
12
26
  @prefix_delimiter = 0
13
- version, _ = version_string.split('+')
27
+ version, _ = version_string.delete_prefix('v').split('+')
14
28
  if segments.nil?
15
29
  @segments = split_version_string!(version)
16
30
  else
@@ -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.2'
4
+ VERSION = '2.0.1'
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.2
4
+ version: 2.0.1
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-02-19 00:00:00.000000000 Z
13
+ date: 2024-03-19 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