Skip to content
Open
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
16 changes: 16 additions & 0 deletions lib/rubygems/commands/sources_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def initialize
end

def add_source(source_uri) # :nodoc:
source_uri = add_trailing_slash(source_uri)
check_rubygems_https source_uri

source = Gem::Source.new source_uri
Expand All @@ -76,6 +77,7 @@ def add_source(source_uri) # :nodoc:
end

def append_source(source_uri) # :nodoc:
source_uri = add_trailing_slash(source_uri)
check_rubygems_https source_uri

source = Gem::Source.new source_uri
Expand Down Expand Up @@ -103,6 +105,7 @@ def append_source(source_uri) # :nodoc:
end

def prepend_source(source_uri) # :nodoc:
source_uri = add_trailing_slash(source_uri)
check_rubygems_https source_uri

source = Gem::Source.new source_uri
Expand Down Expand Up @@ -141,6 +144,19 @@ def check_typo_squatting(source)
end
end

def add_trailing_slash(source_uri) # :nodoc:
# Ensure the source URI has a trailing slash for proper RFC 2396 path merging
# Without a trailing slash, the last path segment is treated as a file and removed
# during relative path resolution (e.g., "/blish" + "gems/foo.gem" = "/gems/foo.gem")
# With a trailing slash, it's treated as a directory (e.g., "/blish/" + "gems/foo.gem" = "/blish/gems/foo.gem")
uri = Gem::URI.parse(source_uri)
uri.path = uri.path.gsub(%r{/+$}, "") + "/" if uri.path && !uri.path.empty?
uri.to_s
rescue Gem::URI::Error
# If parsing fails, return the original URI and let later validation handle it
source_uri
end

def check_rubygems_https(source_uri) # :nodoc:
uri = Gem::URI source_uri

Expand Down
99 changes: 97 additions & 2 deletions test/rubygems/test_gem_commands_sources_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,82 @@ def test_execute_add
assert_equal "", @ui.error
end

def test_execute_add_without_trailing_slash
setup_fake_source('https://rubygems.pkg.github.com/my-org')

@cmd.handle_options %W[--add https://rubygems.pkg.github.com/my-org]

use_ui @ui do
@cmd.execute
end

assert_equal [@gem_repo, 'https://rubygems.pkg.github.com/my-org/'], Gem.sources

expected = <<-EOF
https://rubygems.pkg.github.com/my-org/ added to sources
EOF

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

def test_execute_add_multiple_trailing_slash
setup_fake_source('https://rubygems.pkg.github.com/my-org/')

@cmd.handle_options %W[--add https://rubygems.pkg.github.com/my-org///]

use_ui @ui do
@cmd.execute
end

assert_equal [@gem_repo, 'https://rubygems.pkg.github.com/my-org/'], Gem.sources

expected = <<-EOF
https://rubygems.pkg.github.com/my-org/ added to sources
EOF

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

def test_execute_append_without_trailing_slash
setup_fake_source('https://rubygems.pkg.github.com/my-org')

@cmd.handle_options %W[--append https://rubygems.pkg.github.com/my-org]

use_ui @ui do
@cmd.execute
end

assert_equal [@gem_repo, 'https://rubygems.pkg.github.com/my-org/'], Gem.sources

expected = <<-EOF
https://rubygems.pkg.github.com/my-org/ added to sources
EOF

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

def test_execute_prepend_without_trailing_slash
setup_fake_source('https://rubygems.pkg.github.com/my-org')

@cmd.handle_options %W[--prepend https://rubygems.pkg.github.com/my-org]

use_ui @ui do
@cmd.execute
end

assert_equal [@gem_repo, 'https://rubygems.pkg.github.com/my-org/'], Gem.sources

expected = <<-EOF
https://rubygems.pkg.github.com/my-org/ added to sources
EOF

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

def test_execute_append
setup_fake_source(@new_repo)

Expand Down Expand Up @@ -583,7 +659,7 @@ def test_execute_add_bad_uri
assert_equal [@gem_repo], Gem.sources

expected = <<-EOF
beta-gems.example.com is not a URI
beta-gems.example.com/ is not a URI
EOF

assert_equal expected, @ui.output
Expand All @@ -602,7 +678,26 @@ def test_execute_append_bad_uri
assert_equal [@gem_repo], Gem.sources

expected = <<-EOF
beta-gems.example.com is not a URI
beta-gems.example.com/ is not a URI
EOF

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

def test_execute_prepend_bad_uri
@cmd.handle_options %w[--prepend beta-gems.example.com]

use_ui @ui do
assert_raise Gem::MockGemUi::TermError do
@cmd.execute
end
end

assert_equal [@gem_repo], Gem.sources

expected = <<-EOF
beta-gems.example.com/ is not a URI
EOF

assert_equal expected, @ui.output
Expand Down
Loading