Skip to content

Commit

Permalink
fix CDN repo URL handling with/without trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-makarov committed Jul 2, 2019
1 parent 7041bdd commit 1e2ff65
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/cocoapods-core/cdn_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def initialize(repo)
# @return [String] The URL of the source.
#
def url
@url ||= File.read(repo.join('.url'))
@url ||= File.read(repo.join('.url')).chomp.chomp('/') + '/'
end

# @return [String] The type of the source.
Expand Down
8 changes: 7 additions & 1 deletion lib/cocoapods-core/source/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def name_for_url(url)
end

case url.to_s.downcase
when Pod::TrunkSource::TRUNK_REPO_URL.downcase
when %r{https://#{Regexp.quote(trunk_repo_hostname)}}i
base = Pod::TrunkSource::TRUNK_REPO_NAME
when %r{github.com[:/]+(.+)/(.+)}
base = Regexp.last_match[1]
Expand All @@ -416,6 +416,12 @@ def name_for_url(url)
end
name
end

# Returns hostname for for `trunk` URL.
#
def trunk_repo_hostname
URI.parse(TrunkSource::TRUNK_REPO_URL).host.downcase.freeze
end
end
end
end
43 changes: 39 additions & 4 deletions spec/cdn_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,26 @@ def all_local_files
[@source.repo.join('**/*.yml'), @source.repo.join('**/*.txt'), @source.repo.join('**/*.json')].map(&Pathname.method(:glob)).flatten
end

@path = fixture('spec-repos/test_cdn_repo_local')
Pathname.glob(@path.join('*')).each(&:rmtree)
@source = CDNSource.new(@path)
def save_url(url)
File.open(@path.join('.url'), 'w') { |f| f.write(url) }
end

def cleanup
Pathname.glob(@path.join('*')).each(&:rmtree)
@path.join('.url').delete if @path.join('.url').exist?
end

@remote_dir = fixture('mock_cdn_repo_remote')

@path = fixture('spec-repos/test_cdn_repo_local')
cleanup
save_url('http://localhost:4321/')

@source = CDNSource.new(@path)
end

after do
Pathname.glob(@path.join('*')).each(&:rmtree)
cleanup
end

#-------------------------------------------------------------------------#
Expand All @@ -40,6 +51,30 @@ def all_local_files
it 'return its type' do
@source.type.should == 'CDN'
end

it 'works when the root URL has a trailing slash' do
save_url('http://localhost:4321/')
@source = CDNSource.new(@path)
@source.url.should == 'http://localhost:4321/'
end

it 'works when the root URL has a trailing path' do
save_url('http://localhost:4321/trail/ing/path/')
@source = CDNSource.new(@path)
@source.url.should == 'http://localhost:4321/trail/ing/path/'
end

it 'works when the root URL has no trailing slash' do
save_url('http://localhost:4321')
@source = CDNSource.new(@path)
@source.url.should == 'http://localhost:4321/'
end

it 'works when the root URL file has a newline' do
save_url("http://localhost:4321/\n")
@source = CDNSource.new(@path)
@source.url.should == 'http://localhost:4321/'
end
end

#-------------------------------------------------------------------------#
Expand Down
Empty file.
1 change: 0 additions & 1 deletion spec/fixtures/spec-repos/test_cdn_repo_local/.url

This file was deleted.

7 changes: 7 additions & 0 deletions spec/source/manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ module Pod
@sources_manager.send(:name_for_url, url).should == 'trunk'
end

it 'uses `trunk` for the CDN CocoaPods repository without a slash' do
url = 'https://cdn.cocoapods.org'
Pathname.any_instance.stubs(:exist?).
returns(false).then.returns(true)
@sources_manager.send(:name_for_url, url).should == 'trunk'
end

it 'uses the organization name for github.com URLs' do
url = 'https://github.com/segiddins/banana.git'
@sources_manager.send(:name_for_url, url).should == 'segiddins'
Expand Down

0 comments on commit 1e2ff65

Please sign in to comment.