|
| 1 | +# typed: false |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "spec_helper" |
| 5 | +require "dependabot/sbt/requirement" |
| 6 | +require "dependabot/sbt/version" |
| 7 | + |
| 8 | +RSpec.describe Dependabot::Sbt::Requirement do |
| 9 | + subject(:requirement) { described_class.new(requirement_string) } |
| 10 | + |
| 11 | + let(:requirement_string) { ">=1.0.0" } |
| 12 | + |
| 13 | + describe ".new" do |
| 14 | + subject(:requirement) { described_class.new(requirement_string) } |
| 15 | + |
| 16 | + context "with an exact version (typical SBT usage)" do |
| 17 | + let(:requirement_string) { "1.0.0" } |
| 18 | + |
| 19 | + it { is_expected.to eq(Gem::Requirement.new("= 1.0.0")) } |
| 20 | + end |
| 21 | + |
| 22 | + context "with a pre-release version" do |
| 23 | + let(:requirement_string) { "1.3.alpha" } |
| 24 | + |
| 25 | + it { is_expected.to be_satisfied_by(Gem::Version.new("1.3.alpha")) } |
| 26 | + end |
| 27 | + |
| 28 | + context "with a range requirement" do |
| 29 | + let(:requirement_string) { "[1.0.0,)" } |
| 30 | + |
| 31 | + it { is_expected.to eq(Gem::Requirement.new(">= 1.0.0")) } |
| 32 | + |
| 33 | + context "when needing a > operator" do |
| 34 | + let(:requirement_string) { "(1.0.0,)" } |
| 35 | + |
| 36 | + it { is_expected.to eq(Gem::Requirement.new("> 1.0.0")) } |
| 37 | + end |
| 38 | + |
| 39 | + context "when needing a > and a < operator" do |
| 40 | + let(:requirement_string) { "(1.0.0, 2.0.0)" } |
| 41 | + |
| 42 | + it { is_expected.to eq(Gem::Requirement.new("> 1.0.0", "< 2.0.0")) } |
| 43 | + end |
| 44 | + |
| 45 | + context "when needing a >= and a <= operator" do |
| 46 | + let(:requirement_string) { "[1.0.0, 2.0.0]" } |
| 47 | + |
| 48 | + it { is_expected.to eq(Gem::Requirement.new(">= 1.0.0", "<= 2.0.0")) } |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + context "with a soft requirement" do |
| 53 | + let(:requirement_string) { "[1.0.0]" } |
| 54 | + |
| 55 | + it "treats it as an equality matcher" do |
| 56 | + expect(requirement).to be_satisfied_by( |
| 57 | + Dependabot::Sbt::Version.new("1.0.0") |
| 58 | + ) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + context "with a wildcard version ending in +" do |
| 63 | + let(:requirement_string) { "1.0.+" } |
| 64 | + |
| 65 | + it "converts to a pessimistic constraint" do |
| 66 | + expect(requirement).to be_satisfied_by(Dependabot::Sbt::Version.new("1.0.1")) |
| 67 | + expect(requirement).to be_satisfied_by(Dependabot::Sbt::Version.new("1.0.9")) |
| 68 | + expect(requirement).not_to be_satisfied_by(Dependabot::Sbt::Version.new("1.1.0")) |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + context "with a bare wildcard +" do |
| 73 | + let(:requirement_string) { "+" } |
| 74 | + |
| 75 | + it "matches any version" do |
| 76 | + expect(requirement).to be_satisfied_by(Dependabot::Sbt::Version.new("1.0.0")) |
| 77 | + expect(requirement).to be_satisfied_by(Dependabot::Sbt::Version.new("99.0.0")) |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + describe ".requirements_array" do |
| 83 | + subject(:requirements) { described_class.requirements_array(requirement_string) } |
| 84 | + |
| 85 | + context "with a single requirement" do |
| 86 | + let(:requirement_string) { "1.0.0" } |
| 87 | + |
| 88 | + it "returns an array with one equality requirement" do |
| 89 | + expect(requirements.length).to eq(1) |
| 90 | + expect(requirements.first).to eq(Gem::Requirement.new("= 1.0.0")) |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + context "with multiple OR requirements" do |
| 95 | + let(:requirement_string) { "[1.0,2.0),(3.0,4.0)" } |
| 96 | + |
| 97 | + it "returns an array with the correct split requirements" do |
| 98 | + expect(requirements.length).to eq(2) |
| 99 | + expect(requirements.first).to eq(Gem::Requirement.new(">= 1.0", "< 2.0")) |
| 100 | + expect(requirements.last).to eq(Gem::Requirement.new("> 3.0", "< 4.0")) |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + describe "#satisfied_by?" do |
| 106 | + subject { requirement.satisfied_by?(version) } |
| 107 | + |
| 108 | + let(:requirement_string) { ">=1.0.0" } |
| 109 | + |
| 110 | + context "with a version that satisfies the requirement" do |
| 111 | + let(:version) { Dependabot::Sbt::Version.new("1.5.0") } |
| 112 | + |
| 113 | + it { is_expected.to be(true) } |
| 114 | + end |
| 115 | + |
| 116 | + context "with a version that does not satisfy the requirement" do |
| 117 | + let(:version) { Dependabot::Sbt::Version.new("0.9.0") } |
| 118 | + |
| 119 | + it { is_expected.to be(false) } |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments