Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions maven/lib/dependabot/maven/file_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@

module Dependabot
module Maven
class FileUpdater < Dependabot::FileUpdaters::Base
class FileUpdater < Dependabot::FileUpdaters::Base # rubocop:disable Metrics/ClassLength
XML_TERMINATORS = T.let(
{ "<!--" => "-->", "<?" => "?>", "<![CDATA[" => "]]>" }.freeze,
T::Hash[String, String]
)

XML_TAG_SCANNER = %r{
<!--.*?-->
|<\?.*?\?>
|<!\[CDATA\[.*?\]\]>
|<(/?)[^\s>/!?](?:"[^"]*"|'[^']*'|[^>"'])*?(/?)>
}mx

extend T::Sig

require_relative "file_updater/declaration_finder"
Expand Down Expand Up @@ -331,10 +343,48 @@ def declaration_finder(dependency, requirement)
def updated_file_declaration(old_declaration, previous_req, requirement)
original_req_string = T.must(previous_req.requirement_string)

old_declaration.gsub(
/(?<=\s|>)#{Regexp.quote(original_req_string)}(?=\s|<)/,
T.must(requirement.requirement_string)
)
filler = /(?:\s|<!--(?:(?!-->).)*-->|<\?(?:(?!\?>).)*\?>)*/m
own_version = %r{
(<(?:[\w.-]+:)?version(?:\s(?:"[^"]*"|'[^']*'|[^>"'])*)?>#{filler}(?:<!\[CDATA\[\s*)?)
#{Regexp.quote(original_req_string)}
((?:\s*\]\]>)?#{filler}</(?:[\w.-]+:)?version\s*>)
}mx

# Only rewrite the declaration's own <version> child; nested elements
# (e.g. plugin <configuration>) may contain the same text.
old_declaration.gsub(own_version) do
match = T.must(Regexp.last_match)
prefix = old_declaration[0...match.begin(0)].to_s
next match[0].to_s if inside_comment_or_cdata?(prefix) || element_depth(prefix) != 1

"#{match[1]}#{requirement.requirement_string}#{match[2]}"
end
end

sig { params(xml_prefix: String).returns(T::Boolean) }
def inside_comment_or_cdata?(xml_prefix)
pos = 0
while (match = xml_prefix.match(/<!--|<\?|<!\[CDATA\[/, pos))
terminator = XML_TERMINATORS.fetch(match[0].to_s)
finish = xml_prefix.index(terminator, match.end(0))
return true unless finish

pos = finish + terminator.length
end
false
end

sig { params(xml_prefix: String).returns(Integer) }
def element_depth(xml_prefix)
depth = 0
xml_prefix.scan(XML_TAG_SCANNER) do |closing, self_closing|
next if Regexp.last_match(0).to_s.start_with?("<!", "<?")

if closing == "/" then depth -= 1
elsif self_closing.to_s.empty? then depth += 1
end
end
depth
end

sig { returns(T::Array[Dependabot::DependencyFile]) }
Expand Down
306 changes: 306 additions & 0 deletions maven/spec/dependabot/maven/file_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,312 @@
its(:content) { is_expected.to include("<version> 4.6.1 </version>") }
end

context "with a plugin whose configuration holds the same version" do
let(:pom_body) { fixture("poms", "plugin_config_same_version.xml") }
let(:dependency) do
Dependabot::Dependency.new(
name: "org.apache.maven.plugins:maven-enforcer-plugin",
version: "3.7.0",
requirements: [{
file: "pom.xml",
requirement: "3.7.0",
groups: ["plugin"],
source: nil,
metadata: { packaging_type: "jar" }
}],
previous_requirements: [{
file: "pom.xml",
requirement: "3.6.3",
groups: ["plugin"],
source: nil,
metadata: { packaging_type: "jar" }
}],
package_manager: "maven"
)
end

its(:content) do
is_expected.to include("<artifactId>maven-enforcer-plugin</artifactId>\n <version>3.7.0</version>")
is_expected.to include("<requireMavenVersion><version>3.6.3</version></requireMavenVersion>")
end
end

context "with a plugin whose own version has attributes and follows a self-closing tag" do
let(:pom_body) do
fixture("poms", "plugin_config_same_version.xml")
.sub("<version>3.6.3</version>\n <executions>", "<executions>")
.sub("<id>require-maven</id>", "<id>require-maven</id><skip/><note a=\"x/\"/>")
.sub("</executions>", "</executions>\n <version id=\"v\">3.6.3</version>")
end
let(:dependency) do
Dependabot::Dependency.new(
name: "org.apache.maven.plugins:maven-enforcer-plugin",
version: "3.7.0",
requirements: [{
file: "pom.xml",
requirement: "3.7.0",
groups: ["plugin"],
source: nil,
metadata: { packaging_type: "jar" }
}],
previous_requirements: [{
file: "pom.xml",
requirement: "3.6.3",
groups: ["plugin"],
source: nil,
metadata: { packaging_type: "jar" }
}],
package_manager: "maven"
)
end

its(:content) do
is_expected.to include('<version id="v">3.7.0</version>')
is_expected.to include("<requireMavenVersion><version>3.6.3</version></requireMavenVersion>")
end
end

context "with a commented-out copy of the plugin version" do
let(:pom_body) do
fixture("poms", "plugin_config_same_version.xml")
.sub(
"<artifactId>maven-enforcer-plugin</artifactId>",
"<artifactId>maven-enforcer-plugin</artifactId>\n <!-- <version>3.6.3</version> -->"
)
end
let(:dependency) do
Dependabot::Dependency.new(
name: "org.apache.maven.plugins:maven-enforcer-plugin",
version: "3.7.0",
requirements: [{
file: "pom.xml", requirement: "3.7.0", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
previous_requirements: [{
file: "pom.xml", requirement: "3.6.3", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
package_manager: "maven"
)
end

its(:content) do
is_expected.to include("<!-- <version>3.6.3</version> -->")
is_expected.to include("</executions>").and include("<version>3.7.0</version>")
is_expected.to include("<requireMavenVersion><version>3.6.3</version></requireMavenVersion>")
end
end

context "with CDATA comments, processing instructions and quoted > before the plugin version" do
let(:pom_body) do
fixture("poms", "plugin_config_same_version.xml")
.sub("<version>3.6.3</version>\n <executions>", "<executions>")
.sub(
"<id>require-maven</id>",
"<id>require-maven</id><arg value=\"a > b\"/><?pi <x> ?>" \
"<![CDATA[<version><!-- example -->1.0</version>]]>"
)
.sub("</executions>", "</executions>\n <version><!-- plugin -->3.6.3</version>")
end
let(:dependency) do
Dependabot::Dependency.new(
name: "org.apache.maven.plugins:maven-enforcer-plugin",
version: "3.7.0",
requirements: [{
file: "pom.xml", requirement: "3.7.0", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
previous_requirements: [{
file: "pom.xml", requirement: "3.6.3", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
package_manager: "maven"
)
end

its(:content) do
is_expected.to include("<version><!-- plugin -->3.7.0</version>")
is_expected.to include("<requireMavenVersion><version>3.6.3</version></requireMavenVersion>")
end
end

context "with a processing instruction containing the plugin version" do
let(:pom_body) do
fixture("poms", "plugin_config_same_version.xml")
.sub(
"<artifactId>maven-enforcer-plugin</artifactId>",
"<artifactId>maven-enforcer-plugin</artifactId>\n<?pi <version>3.6.3</version> ?>"
)
end
let(:dependency) do
Dependabot::Dependency.new(
name: "org.apache.maven.plugins:maven-enforcer-plugin",
version: "3.7.0",
requirements: [{
file: "pom.xml", requirement: "3.7.0", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
previous_requirements: [{
file: "pom.xml", requirement: "3.6.3", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
package_manager: "maven"
)
end

its(:content) do
is_expected.to include("<?pi <version>3.6.3</version> ?>")
is_expected.to include("<version>3.7.0</version>")
end
end

context "with a CDATA-wrapped plugin version" do
let(:pom_body) do
fixture("poms", "plugin_config_same_version.xml")
.sub(
"<version>3.6.3</version>\n <executions>",
"<version><![CDATA[3.6.3]]></version>\n <executions>"
)
end
let(:dependency) do
Dependabot::Dependency.new(
name: "org.apache.maven.plugins:maven-enforcer-plugin",
version: "3.7.0",
requirements: [{
file: "pom.xml", requirement: "3.7.0", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
previous_requirements: [{
file: "pom.xml", requirement: "3.6.3", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
package_manager: "maven"
)
end

its(:content) do
is_expected.to include("<version><![CDATA[3.7.0]]></version>")
is_expected.to include("<requireMavenVersion><version>3.6.3</version></requireMavenVersion>")
end
end

context "with a whitespace-padded CDATA plugin version" do
let(:pom_body) do
fixture("poms", "plugin_config_same_version.xml")
.sub(
"<version>3.6.3</version>\n <executions>",
"<version><![CDATA[ 3.6.3 ]]></version>\n <executions>"
)
end
let(:dependency) do
Dependabot::Dependency.new(
name: "org.apache.maven.plugins:maven-enforcer-plugin",
version: "3.7.0",
requirements: [{
file: "pom.xml", requirement: "3.7.0", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
previous_requirements: [{
file: "pom.xml", requirement: "3.6.3", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
package_manager: "maven"
)
end

its(:content) do
is_expected.to include("<version><![CDATA[ 3.7.0 ]]></version>")
is_expected.to include("<requireMavenVersion><version>3.6.3</version></requireMavenVersion>")
end
end

context "with a namespace-prefixed plugin version" do
let(:pom_body) do
fixture("poms", "plugin_config_same_version.xml")
.sub("<version>3.6.3</version>\n <executions>", "<m:version>3.6.3</m:version>\n <executions>")
end
let(:dependency) do
Dependabot::Dependency.new(
name: "org.apache.maven.plugins:maven-enforcer-plugin",
version: "3.7.0",
requirements: [{
file: "pom.xml", requirement: "3.7.0", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
previous_requirements: [{
file: "pom.xml", requirement: "3.6.3", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
package_manager: "maven"
)
end

its(:content) do
is_expected.to include("<m:version>3.7.0</m:version>")
is_expected.to include("<requireMavenVersion><version>3.6.3</version></requireMavenVersion>")
end
end

context "with a plugin version with a quoted > in an attribute" do
let(:pom_body) do
fixture("poms", "plugin_config_same_version.xml")
.sub(
"<version>3.6.3</version>\n <executions>",
"<version note='a > b'>3.6.3</version>\n <executions>"
)
end
let(:dependency) do
Dependabot::Dependency.new(
name: "org.apache.maven.plugins:maven-enforcer-plugin",
version: "3.7.0",
requirements: [{
file: "pom.xml", requirement: "3.7.0", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
previous_requirements: [{
file: "pom.xml", requirement: "3.6.3", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
package_manager: "maven"
)
end

its(:content) do
is_expected.to include("<version note='a > b'>3.7.0</version>")
is_expected.to include("<requireMavenVersion><version>3.6.3</version></requireMavenVersion>")
end
end

context "with a plugin version adjacent to processing instructions" do
let(:pom_body) do
fixture("poms", "plugin_config_same_version.xml")
.sub(
"<version>3.6.3</version>\n <executions>",
"<version><?a x?>3.6.3<?b y?></version>\n <executions>"
)
end
let(:dependency) do
Dependabot::Dependency.new(
name: "org.apache.maven.plugins:maven-enforcer-plugin",
version: "3.7.0",
requirements: [{
file: "pom.xml", requirement: "3.7.0", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
previous_requirements: [{
file: "pom.xml", requirement: "3.6.3", groups: ["plugin"], source: nil,
metadata: { packaging_type: "jar" }
}],
package_manager: "maven"
)
end

its(:content) do
is_expected.to include("<version><?a x?>3.7.0<?b y?></version>")
is_expected.to include("<requireMavenVersion><version>3.6.3</version></requireMavenVersion>")
end
end

context "with a comment on the version" do
let(:pom_body) { fixture("poms", "version_with_comment.xml") }

Expand Down
Loading
Loading