Skip to content

Commit

Permalink
Fix malformed version number error for terraform and github actions (d…
Browse files Browse the repository at this point in the history
…ependabot#10222)

* Fix malformed version number error for terraform and github actions

* Fix malformed version number in github actions

* Reduce duplication and add more tests for initialize method

* Strip string spaces before checking emptiness
  • Loading branch information
amazimbe authored Jul 18, 2024
1 parent bf137a9 commit e90eb49
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions github_actions/lib/dependabot/github_actions/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def self.remove_leading_v(version)

sig { override.params(version: VersionParameter).returns(T::Boolean) }
def self.correct?(version)
return false if version.to_s.strip.empty?

version = Version.remove_leading_v(version)
super
end
Expand Down
4 changes: 4 additions & 0 deletions github_actions/spec/dependabot/github_actions/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
semver_without_v = "1.2.3"

describe "#correct?" do
it "rejects nil" do
expect(described_class.correct?(nil)).to be(false)
end

it "accepts semver" do
expect(described_class.correct?(semver_version)).to be(true)
end
Expand Down
27 changes: 27 additions & 0 deletions terraform/lib/dependabot/terraform/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Version < Dependabot::Version
sig { override.params(version: VersionParameter).void }
def initialize(version)
@version_string = T.let(version.to_s, String)
version = Version.remove_leading_v(version)
version = Version.remove_backport(version)

super
end

Expand All @@ -31,6 +34,30 @@ def self.new(version)
def to_s
@version_string
end

sig { override.params(version: VersionParameter).returns(T::Boolean) }
def self.correct?(version)
version = Version.remove_leading_v(version)
version = Version.remove_backport(version)

return false if version.to_s.strip.empty?

super
end

sig { params(version: VersionParameter).returns(VersionParameter) }
def self.remove_leading_v(version)
return version.gsub(/^v/, "") if version.is_a?(String)

version
end

sig { params(version: VersionParameter).returns(VersionParameter) }
def self.remove_backport(version)
return version.split("+").first if version.is_a?(String) && version.include?("+")

version
end
end
end
end
Expand Down
52 changes: 52 additions & 0 deletions terraform/spec/dependabot/terraform/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,57 @@

it { is_expected.to eq "1.0.0-pre1" }
end

context "with a leading v" do
let(:version_string) { "v0.3.2" }

it { is_expected.to eq "v0.3.2" }
end

context "with a backported version" do
let(:version_string) { "1.17.2+backport-1" }

it { is_expected.to eq "1.17.2+backport-1" }
end
end

describe "#initialize" do
subject { version.inspect }

context "with a leading v" do
let(:version_string) { "v0.3.2" }
let(:expected) { "#<#{described_class} \"0.3.2\">" }

it { is_expected.to eq expected }
end

context "with a backported version" do
let(:version_string) { "1.17.2+backport-1" }
let(:expected) { "#<#{described_class} \"1.17.2\">" }

it { is_expected.to eq expected }
end
end

describe ".correct?" do
subject { described_class.correct?(version_string) }

valid = %w(1.0.0 v0.3.2 1.17.2+backport-1)
valid.each do |version|
context "with version #{version}" do
let(:version_string) { version }

it { is_expected.to be(true) }
end
end

invalid = ["", nil]
invalid.each do |version|
context "with version #{version}" do
let(:version_string) { version }

it { is_expected.to be(false) }
end
end
end
end

0 comments on commit e90eb49

Please sign in to comment.