rubocops/patches: autocorrect some offenses

This commit is contained in:
nandahkrishna 2021-02-26 00:48:03 +05:30
parent 9d62a13a31
commit d68452f232
No known key found for this signature in database
GPG Key ID: 067E5FCD58ADF3AA
2 changed files with 48 additions and 68 deletions

View File

@ -11,6 +11,7 @@ module RuboCop
# TODO: Many of these could be auto-corrected. # TODO: Many of these could be auto-corrected.
class Patches < FormulaCop class Patches < FormulaCop
extend T::Sig extend T::Sig
extend AutoCorrector
def audit_formula(node, _class_node, _parent_class_node, body) def audit_formula(node, _class_node, _parent_class_node, body)
@full_source_content = source_buffer(node).source @full_source_content = source_buffer(node).source
@ -40,37 +41,36 @@ module RuboCop
private private
def patch_problems(patch) def patch_problems(patch_url_node)
patch_url = string_content(patch) patch_url = string_content(patch_url_node)
if regex_match_group(patch, %r{https://github.com/[^/]*/[^/]*/pull}) if regex_match_group(patch_url_node, %r{https://github.com/[^/]*/[^/]*/pull})
problem "Use a commit hash URL rather than an unstable pull request URL: #{patch_url}" problem "Use a commit hash URL rather than an unstable pull request URL: #{patch_url}"
end end
if regex_match_group(patch, %r{.*gitlab.*/merge_request.*}) if regex_match_group(patch_url_node, %r{.*gitlab.*/merge_request.*})
problem "Use a commit hash URL rather than an unstable merge request URL: #{patch_url}" problem "Use a commit hash URL rather than an unstable merge request URL: #{patch_url}"
end end
if regex_match_group(patch, %r{https://github.com/[^/]*/[^/]*/commit/[a-fA-F0-9]*\.diff}) if regex_match_group(patch_url_node, %r{https://github.com/[^/]*/[^/]*/commit/[a-fA-F0-9]*\.diff})
problem <<~EOS.chomp problem "GitHub patches should end with .patch, not .diff: #{patch_url}" do |corrector|
GitHub patches should end with .patch, not .diff: correct = patch_url_node.source.gsub(/\.diff/, ".patch")
#{patch_url} corrector.replace(patch_url_node.source_range, correct)
EOS end
end end
if regex_match_group(patch, %r{.*gitlab.*/commit/[a-fA-F0-9]*\.diff}) if regex_match_group(patch_url_node, %r{.*gitlab.*/commit/[a-fA-F0-9]*\.diff})
problem <<~EOS.chomp problem "GitLab patches should end with .patch, not .diff: #{patch_url}" do |corrector|
GitLab patches should end with .patch, not .diff: correct = patch_url_node.source.gsub(/\.diff/, ".patch")
#{patch_url} corrector.replace(patch_url_node.source_range, correct)
EOS end
end end
gh_patch_param_pattern = %r{https?://github\.com/.+/.+/(?:commit|pull)/[a-fA-F0-9]*.(?:patch|diff)} gh_patch_param_pattern = %r{https?://github\.com/.+/.+/(?:commit|pull)/[a-fA-F0-9]*.(?:patch|diff)}
if regex_match_group(patch, gh_patch_param_pattern) && !patch_url.match?(/\?full_index=\w+$/) if regex_match_group(patch_url_node, gh_patch_param_pattern) && !patch_url.match?(/\?full_index=\w+$/)
problem <<~EOS problem "GitHub patches should use the full_index parameter: #{patch_url}?full_index=1" do |corrector|
GitHub patches should use the full_index parameter: corrector.replace(patch_url_node.source_range, "#{patch_url}?full_index=1")
#{patch_url}?full_index=1 end
EOS
end end
gh_patch_patterns = Regexp.union([%r{/raw\.github\.com/}, gh_patch_patterns = Regexp.union([%r{/raw\.github\.com/},
@ -78,39 +78,33 @@ module RuboCop
%r{gist\.github\.com/raw}, %r{gist\.github\.com/raw},
%r{gist\.github\.com/.+/raw}, %r{gist\.github\.com/.+/raw},
%r{gist\.githubusercontent\.com/.+/raw}]) %r{gist\.githubusercontent\.com/.+/raw}])
if regex_match_group(patch, gh_patch_patterns) && !patch_url.match?(%r{/[a-fA-F0-9]{6,40}/}) if regex_match_group(patch_url_node, gh_patch_patterns) && !patch_url.match?(%r{/[a-fA-F0-9]{6,40}/})
problem <<~EOS.chomp problem "GitHub/Gist patches should specify a revision: #{patch_url}"
GitHub/Gist patches should specify a revision:
#{patch_url}
EOS
end end
gh_patch_diff_pattern = gh_patch_diff_pattern =
%r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)} %r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)}
if regex_match_group(patch, gh_patch_diff_pattern) if regex_match_group(patch_url_node, gh_patch_diff_pattern)
problem "Use a commit hash URL rather than patch-diff: #{patch_url}" problem "Use a commit hash URL rather than patch-diff: #{patch_url}"
end end
if regex_match_group(patch, %r{macports/trunk}) if regex_match_group(patch_url_node, %r{macports/trunk})
problem <<~EOS.chomp problem "MacPorts patches should specify a revision instead of trunk: #{patch_url}"
MacPorts patches should specify a revision instead of trunk:
#{patch_url}
EOS
end end
if regex_match_group(patch, %r{^http://trac\.macports\.org}) if regex_match_group(patch_url_node, %r{^http://trac\.macports\.org})
problem <<~EOS.chomp problem "Patches from MacPorts Trac should be https://, not http: #{patch_url}" do |corrector|
Patches from MacPorts Trac should be https://, not http: correct = patch_url_node.source.gsub(%r{^http://}, "https://")
#{patch_url} corrector.replace(patch_url_node.source_range, correct)
EOS end
end end
return unless regex_match_group(patch, %r{^http://bugs\.debian\.org}) return unless regex_match_group(patch_url_node, %r{^http://bugs\.debian\.org})
problem <<~EOS.chomp problem "Patches from Debian should be https://, not http: #{patch_url}" do |corrector|
Patches from Debian should be https://, not http: correct = patch_url_node.source.gsub(%r{^http://}, "https://")
#{patch_url} corrector.replace(patch_url_node.source_range, correct)
EOS end
end end
def inline_patch_problems(patch) def inline_patch_problems(patch)

View File

@ -54,23 +54,19 @@ describe RuboCop::Cop::FormulaAudit::Patches do
expected_offense = if patch_url.include?("/raw.github.com/") expected_offense = if patch_url.include?("/raw.github.com/")
expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source: source expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source: source
GitHub/Gist patches should specify a revision: GitHub/Gist patches should specify a revision: #{patch_url}
#{patch_url}
EOS EOS
elsif patch_url.include?("macports/trunk") elsif patch_url.include?("macports/trunk")
expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source: source expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source: source
MacPorts patches should specify a revision instead of trunk: MacPorts patches should specify a revision instead of trunk: #{patch_url}
#{patch_url}
EOS EOS
elsif patch_url.start_with?("http://trac.macports.org") elsif patch_url.start_with?("http://trac.macports.org")
expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source: source expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source: source
Patches from MacPorts Trac should be https://, not http: Patches from MacPorts Trac should be https://, not http: #{patch_url}
#{patch_url}
EOS EOS
elsif patch_url.start_with?("http://bugs.debian.org") elsif patch_url.start_with?("http://bugs.debian.org")
expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source: source expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source: source
Patches from Debian should be https://, not http: Patches from Debian should be https://, not http: #{patch_url}
#{patch_url}
EOS EOS
# rubocop:disable Layout/LineLength # rubocop:disable Layout/LineLength
elsif patch_url.match?(%r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)}) elsif patch_url.match?(%r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)})
@ -78,9 +74,8 @@ describe RuboCop::Cop::FormulaAudit::Patches do
expect_offense_hash message: "Use a commit hash URL rather than patch-diff: #{patch_url}", expect_offense_hash message: "Use a commit hash URL rather than patch-diff: #{patch_url}",
severity: :convention, line: 5, column: 4, source: source severity: :convention, line: 5, column: 4, source: source
elsif patch_url.match?(%r{https?://github\.com/.+/.+/(?:commit|pull)/[a-fA-F0-9]*.(?:patch|diff)}) elsif patch_url.match?(%r{https?://github\.com/.+/.+/(?:commit|pull)/[a-fA-F0-9]*.(?:patch|diff)})
expect_offense_hash message: <<~EOS, severity: :convention, line: 5, column: 4, source: source expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 4, source: source
GitHub patches should use the full_index parameter: GitHub patches should use the full_index parameter: #{patch_url}?full_index=1
#{patch_url}?full_index=1
EOS EOS
end end
expected_offense.zip([inspect_source(source).last]).each do |expected, actual| expected_offense.zip([inspect_source(source).last]).each do |expected, actual|
@ -112,11 +107,8 @@ describe RuboCop::Cop::FormulaAudit::Patches do
line: 4, line: 4,
column: 2, column: 2,
source: source }, source: source },
{ message: { message: "Patches from MacPorts Trac should be https://, not http: " \
<<~EOS.chomp, "http://trac.macports.org/export/68507/trunk/dports/net/trafshow/files/",
Patches from MacPorts Trac should be https://, not http:
http://trac.macports.org/export/68507/trunk/dports/net/trafshow/files/
EOS
severity: :convention, severity: :convention,
line: 8, line: 8,
column: 25, column: 25,
@ -205,23 +197,19 @@ describe RuboCop::Cop::FormulaAudit::Patches do
expected_offense = if patch_url.include?("/raw.github.com/") expected_offense = if patch_url.include?("/raw.github.com/")
expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source
GitHub/Gist patches should specify a revision: GitHub/Gist patches should specify a revision: #{patch_url}
#{patch_url}
EOS EOS
elsif patch_url.include?("macports/trunk") elsif patch_url.include?("macports/trunk")
expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source
MacPorts patches should specify a revision instead of trunk: MacPorts patches should specify a revision instead of trunk: #{patch_url}
#{patch_url}
EOS EOS
elsif patch_url.start_with?("http://trac.macports.org") elsif patch_url.start_with?("http://trac.macports.org")
expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source
Patches from MacPorts Trac should be https://, not http: Patches from MacPorts Trac should be https://, not http: #{patch_url}
#{patch_url}
EOS EOS
elsif patch_url.start_with?("http://bugs.debian.org") elsif patch_url.start_with?("http://bugs.debian.org")
expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source
Patches from Debian should be https://, not http: Patches from Debian should be https://, not http: #{patch_url}
#{patch_url}
EOS EOS
elsif patch_url.match?(%r{https://github.com/[^/]*/[^/]*/pull}) elsif patch_url.match?(%r{https://github.com/[^/]*/[^/]*/pull})
expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source
@ -233,13 +221,11 @@ describe RuboCop::Cop::FormulaAudit::Patches do
EOS EOS
elsif patch_url.match?(%r{https://github.com/[^/]*/[^/]*/commit/}) elsif patch_url.match?(%r{https://github.com/[^/]*/[^/]*/commit/})
expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source
GitHub patches should end with .patch, not .diff: GitHub patches should end with .patch, not .diff: #{patch_url}
#{patch_url}
EOS EOS
elsif patch_url.match?(%r{.*gitlab.*/commit/}) elsif patch_url.match?(%r{.*gitlab.*/commit/})
expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source expect_offense_hash message: <<~EOS.chomp, severity: :convention, line: 5, column: 8, source: source
GitLab patches should end with .patch, not .diff: GitLab patches should end with .patch, not .diff: #{patch_url}
#{patch_url}
EOS EOS
# rubocop:disable Layout/LineLength # rubocop:disable Layout/LineLength
elsif patch_url.match?(%r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)}) elsif patch_url.match?(%r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)})