Skip to content

Commit

Permalink
[rubygems/rubygems] Warn for duplicate meta data links
Browse files Browse the repository at this point in the history
Match order of METADATA_LINK_KEYS to order used by rubygems.org in Links model.
Add missing download_uri key.

rubygems/rubygems@d2922cd6e9
  • Loading branch information
etherbob authored and matzbot committed Dec 14, 2023
1 parent d7dad64 commit beefce1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
27 changes: 23 additions & 4 deletions lib/rubygems/specification_policy.rb
Expand Up @@ -12,13 +12,14 @@ class Gem::SpecificationPolicy
VALID_URI_PATTERN = %r{\Ahttps?:\/\/([^\s:@]+:[^\s:@]*@)?[A-Za-z\d\-]+(\.[A-Za-z\d\-]+)+\.?(:\d{1,5})?([\/?]\S*)?\z} # :nodoc:

METADATA_LINK_KEYS = %w[
bug_tracker_uri
changelog_uri
documentation_uri
homepage_uri
mailing_list_uri
changelog_uri
source_code_uri
documentation_uri
wiki_uri
mailing_list_uri
bug_tracker_uri
download_uri
funding_uri
].freeze # :nodoc:

Expand Down Expand Up @@ -106,6 +107,8 @@ def validate_optional(strict)

validate_removed_attributes

validate_unique_links

if @warnings > 0
if strict
error "specification has warnings"
Expand Down Expand Up @@ -501,6 +504,22 @@ def validate_rake_extensions(builder) # :nodoc:
WARNING
end

def validate_unique_links
links = @specification.metadata.slice(*METADATA_LINK_KEYS)
grouped = links.group_by {|_key, uri| uri }
grouped.each do |uri, copies|
next unless copies.length > 1
keys = copies.map(&:first).join("\n ")
warning <<~WARNING
You have specified the uri:
#{uri}
for all of the following keys:
#{keys}
Only the first one will be shown on rubygems.org
WARNING
end
end

def warning(statement) # :nodoc:
@warnings += 1

Expand Down
32 changes: 32 additions & 0 deletions test/rubygems/test_gem_specification.rb
Expand Up @@ -3644,6 +3644,38 @@ def test_metadata_link_validation_fails
end
end

def test_metadata_link_validation_warns_for_duplicates
util_setup_validate

Dir.chdir @tempdir do
@m2 = quick_gem "m", "2" do |s|
s.files = %w[lib/code.rb]
s.licenses = "BSD-2-Clause"
s.metadata = {
"source_code_uri" => "http://example.com",
"homepage_uri" => "http://example.com",
"changelog_uri" => "http://example.com/changelog",
}
end

use_ui @ui do
@m2.validate
end

expected = <<~EXPECTED
#{w}: You have specified the uri:
http://example.com
for all of the following keys:
homepage_uri
source_code_uri
Only the first one will be shown on rubygems.org
#{w}: See https://guides.rubygems.org/specification-reference/ for help
EXPECTED

assert_equal expected, @ui.error, "warning"
end
end

def test_metadata_specs
@m1 = quick_gem "m", "1" do |s|
s.files = %w[lib/code.rb]
Expand Down

0 comments on commit beefce1

Please sign in to comment.