From 3adc141a79cdab83671b7f68301901dd7726e7c4 Mon Sep 17 00:00:00 2001 From: Daniel Niknam Date: Sun, 22 Aug 2021 01:40:21 +1000 Subject: [rubygems/rubygems] Refactor `Gem::RemoteFetcher::FetchError` initializer to `build` method The `initialize` method is already doing a lot and by adding the `Gem::PrintableUri` to redact sensitive information, things are getting complicated and hard to read here. For the start, I have refactored the `initialize` method into a class method called `build`. https://github.com/rubygems/rubygems/commit/4312e8fdf5 --- test/rubygems/test_gem_commands_sources_command.rb | 2 +- test/rubygems/test_gem_remote_fetcher.rb | 32 +++++++++++++++++++++- test/rubygems/test_gem_resolver_best_set.rb | 6 ++-- test/rubygems/test_gem_spec_fetcher.rb | 2 +- test/rubygems/test_remote_fetch_error.rb | 6 ++-- test/rubygems/utilities.rb | 4 +-- 6 files changed, 41 insertions(+), 11 deletions(-) (limited to 'test') diff --git a/test/rubygems/test_gem_commands_sources_command.rb b/test/rubygems/test_gem_commands_sources_command.rb index 7bca0f3803..6fcbbf657b 100644 --- a/test/rubygems/test_gem_commands_sources_command.rb +++ b/test/rubygems/test_gem_commands_sources_command.rb @@ -182,7 +182,7 @@ class TestGemCommandsSourcesCommand < Gem::TestCase uri = "http://beta-gems.example.com/specs.#{@marshal_version}.gz" @fetcher.data[uri] = proc do - raise Gem::RemoteFetcher::FetchError.new('it died', uri) + raise Gem::RemoteFetcher::FetchError.build('it died', uri) end @cmd.handle_options %w[--add http://beta-gems.example.com] diff --git a/test/rubygems/test_gem_remote_fetcher.rb b/test/rubygems/test_gem_remote_fetcher.rb index 1bae9247fe..b4c82ce588 100644 --- a/test/rubygems/test_gem_remote_fetcher.rb +++ b/test/rubygems/test_gem_remote_fetcher.rb @@ -204,7 +204,7 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== @test_data end - raise Gem::RemoteFetcher::FetchError.new("haha!", '') + raise Gem::RemoteFetcher::FetchError.build("haha!", '') end end @@ -241,6 +241,36 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== assert File.exist?(a1_cache_gem) end + def test_download_with_token + a1_data = nil + File.open @a1_gem, 'rb' do |fp| + a1_data = fp.read + end + + fetcher = util_fuck_with_fetcher a1_data + + a1_cache_gem = @a1.cache_file + assert_equal a1_cache_gem, fetcher.download(@a1, 'http://token@gems.example.com') + assert_equal("http://token@gems.example.com/gems/a-1.gem", + fetcher.instance_variable_get(:@test_arg).to_s) + assert File.exist?(a1_cache_gem) + end + + def test_download_with_x_oauth_basic + a1_data = nil + File.open @a1_gem, 'rb' do |fp| + a1_data = fp.read + end + + fetcher = util_fuck_with_fetcher a1_data + + a1_cache_gem = @a1.cache_file + assert_equal a1_cache_gem, fetcher.download(@a1, 'http://token:x-oauth-basic@gems.example.com') + assert_equal("http://token:x-oauth-basic@gems.example.com/gems/a-1.gem", + fetcher.instance_variable_get(:@test_arg).to_s) + assert File.exist?(a1_cache_gem) + end + def test_download_with_encoded_auth a1_data = nil File.open @a1_gem, 'rb' do |fp| diff --git a/test/rubygems/test_gem_resolver_best_set.rb b/test/rubygems/test_gem_resolver_best_set.rb index 0e279d16a8..a448ec05a4 100644 --- a/test/rubygems/test_gem_resolver_best_set.rb +++ b/test/rubygems/test_gem_resolver_best_set.rb @@ -106,7 +106,7 @@ class TestGemResolverBestSet < Gem::TestCase error_uri = api_uri + 'a' - error = Gem::RemoteFetcher::FetchError.new 'bogus', error_uri + error = Gem::RemoteFetcher::FetchError.build 'bogus', error_uri set.replace_failed_api_set error @@ -124,7 +124,7 @@ class TestGemResolverBestSet < Gem::TestCase set.sets << index_set - error = Gem::RemoteFetcher::FetchError.new 'bogus', @gem_repo + error = Gem::RemoteFetcher::FetchError.build 'bogus', @gem_repo e = assert_raise Gem::RemoteFetcher::FetchError do set.replace_failed_api_set error @@ -145,7 +145,7 @@ class TestGemResolverBestSet < Gem::TestCase error_uri = api_uri + 'a' - error = Gem::RemoteFetcher::FetchError.new 'bogus', error_uri + error = Gem::RemoteFetcher::FetchError.build 'bogus', error_uri set.replace_failed_api_set error diff --git a/test/rubygems/test_gem_spec_fetcher.rb b/test/rubygems/test_gem_spec_fetcher.rb index afae46e120..be862aa138 100644 --- a/test/rubygems/test_gem_spec_fetcher.rb +++ b/test/rubygems/test_gem_spec_fetcher.rb @@ -144,7 +144,7 @@ class TestGemSpecFetcher < Gem::TestCase def test_spec_for_dependency_bad_fetch_spec src = Gem::Source.new(@gem_repo) def src.fetch_spec(name) - raise Gem::RemoteFetcher::FetchError.new("bad news from the internet", @uri) + raise Gem::RemoteFetcher::FetchError.build("bad news from the internet", @uri) end Gem.sources.replace [src] diff --git a/test/rubygems/test_remote_fetch_error.rb b/test/rubygems/test_remote_fetch_error.rb index b9e58389d3..cdc9ddad77 100644 --- a/test/rubygems/test_remote_fetch_error.rb +++ b/test/rubygems/test_remote_fetch_error.rb @@ -3,17 +3,17 @@ require_relative 'helper' class TestRemoteFetchError < Gem::TestCase def test_password_redacted - error = Gem::RemoteFetcher::FetchError.new('There was an error fetching', 'https://user:secret@gemsource.org') + error = Gem::RemoteFetcher::FetchError.build('There was an error fetching', 'https://user:secret@gemsource.org') refute_match %r{secret}, error.to_s end def test_invalid_url - error = Gem::RemoteFetcher::FetchError.new('There was an error fetching', 'https://::gemsource.org') + error = Gem::RemoteFetcher::FetchError.build('There was an error fetching', 'https://::gemsource.org') assert_equal error.to_s, 'There was an error fetching (https://::gemsource.org)' end def test_to_s - error = Gem::RemoteFetcher::FetchError.new('There was an error fetching', 'https://gemsource.org') + error = Gem::RemoteFetcher::FetchError.build('There was an error fetching', 'https://gemsource.org') assert_equal error.to_s, 'There was an error fetching (https://gemsource.org)' end end diff --git a/test/rubygems/utilities.rb b/test/rubygems/utilities.rb index 20416fe70b..36513dcd31 100644 --- a/test/rubygems/utilities.rb +++ b/test/rubygems/utilities.rb @@ -51,7 +51,7 @@ class Gem::FakeFetcher raise ArgumentError, 'need full URI' unless path.start_with?("https://", "http://") unless @data.key? path - raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path) + raise Gem::RemoteFetcher::FetchError.build("no data for #{path}", path) end if @data[path].kind_of?(Array) && @data[path].first.kind_of?(Array) @@ -124,7 +124,7 @@ class Gem::FakeFetcher raise ArgumentError, 'need full URI' unless path =~ %r{^http://} unless @data.key? path - raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path) + raise Gem::RemoteFetcher::FetchError.build("no data for #{path}", path) end data = @data[path] -- cgit v1.2.3