Skip to content

Commit

Permalink
Resolve ruby-head uri deprecation warning
Browse files Browse the repository at this point in the history
> URI::RFC3986_PARSER.make_regexp is obsoleted. Use URI::RFC2396_PARSER.make_regexp explicitly

The `uri` gem is switching over its default parser from RFC 2396 to 3986. The new parser doesn't have `make_regexp` available but for compatibility a fallback was added.

Additionally, old versions of `uri` don't have the RFC2396 parser available.
  • Loading branch information
Earlopain authored and bbatsov committed Aug 24, 2024
1 parent 5b6a0dd commit 566ba5b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
3 changes: 1 addition & 2 deletions lib/rubocop/config_loader_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ def inherited_file(path, inherit_from, file)
end

def remote_file?(uri)
regex = URI::DEFAULT_PARSER.make_regexp(%w[http https])
/\A#{regex}\z/.match?(uri)
uri.start_with?('http://', 'https://')
end

def handle_disabled_by_default(config, new_default_configuration)
Expand Down
9 changes: 7 additions & 2 deletions lib/rubocop/cop/mixin/line_length_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@ def tab_indentation_width
end

def uri_regexp
@uri_regexp ||=
URI::DEFAULT_PARSER.make_regexp(config.for_cop('Layout/LineLength')['URISchemes'])
@uri_regexp ||= begin
# Ruby 3.4 changes the default parser to RFC3986 which warns on make_regexp.
# Additionally, the RFC2396_PARSER alias is only available on 3.4 for now.
# Extra info at https://github.com/ruby/uri/issues/118
parser = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::DEFAULT_PARSER
parser.make_regexp(config.for_cop('Layout/LineLength')['URISchemes'])
end
end

def valid_uri?(uri_ish_string)
Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/remote_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ class RemoteConfig
CACHE_LIFETIME = 24 * 60 * 60

def initialize(url, base_dir)
@uri = URI.parse(url)
begin
@uri = URI.parse(url)
rescue URI::InvalidURIError
raise ConfigNotFoundError, "Failed to resolve configuration: '#{url}' is not a valid URI"
end
@base_dir = base_dir
end

Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/remote_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
assert_requested :get, remote_config_url
end

context 'when the remote URL is not a valid URI' do
let(:remote_config_url) { 'http://example.com/rübocop.yml' }

it 'raises a configuration error' do
expect do
remote_config
end.to raise_error(RuboCop::ConfigNotFoundError, /is not a valid URI/)
end
end

context 'when remote URL is configured with token auth' do
let(:token) { 'personal_access_token' }
let(:remote_config_url) { "http://#{token}@example.com/rubocop.yml" }
Expand Down

0 comments on commit 566ba5b

Please sign in to comment.