New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rubocops/cask: Disallow protocol in cask URL verified stanza #14886
Merged
issyl0
merged 5 commits into
Homebrew:master
from
issyl0:rubocop-cask-url-verified-disallow-protocol
Mar 6, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
79db987
rubocops/cask: Disallow protocol in cask URL verified stanza
issyl0 f4a0bc7
rubocops/cask: Appease Sorbet with the correct inheritance
issyl0 4791224
rubocops/cask/url: Remove unnecessary `require "forwardable"`
issyl0 4e06756
rubocops/cask/url: Fix test expectations; regexp for `starts_with?`
issyl0 d995afc
test/rubocops/cask/url: Ensure that autocorrection works
issyl0 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# typed: strict | ||
|
||
module RuboCop::Cop::Cask::OnUrlStanza | ||
requires_ancestor { RuboCop::Cop::Cask::UrlLegacyCommaSeparators } | ||
requires_ancestor { RuboCop::Cop::Cask::Url } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Cask | ||
# This cop checks that a cask's `url` stanza is formatted correctly. | ||
# | ||
# @example | ||
# # bad | ||
# url "https://example.com/foo.dmg", | ||
# verified: "https://example.com" | ||
# | ||
# | ||
# # good | ||
# url "https://example.com/foo.dmg", | ||
# verified: "example.com" | ||
# | ||
class Url < Base | ||
extend AutoCorrector | ||
extend Forwardable | ||
include OnUrlStanza | ||
|
||
def on_url_stanza(stanza) | ||
return if stanza.stanza_node.block_type? | ||
|
||
hash_node = stanza.stanza_node.last_argument | ||
return unless hash_node.hash_type? | ||
|
||
hash_node.each_pair do |key_node, value_node| | ||
next unless key_node.source == "verified" | ||
next unless value_node.str_type? | ||
next unless value_node.source.start_with?(%r{^"https?://}) | ||
|
||
add_offense( | ||
value_node.source_range, | ||
message: "Verified URL parameter value should not start with https:// or http://.", | ||
) do |corrector| | ||
corrector.replace(value_node.source_range, value_node.source.gsub(%r{^"https?://}, "\"")) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
require "rubocops/rubocop-cask" | ||
require "test/rubocops/cask/shared_examples/cask_cop" | ||
|
||
describe RuboCop::Cop::Cask::Url do | ||
include CaskCop | ||
|
||
subject(:cop) { described_class.new } | ||
|
||
context "when url 'verified' value does not start with a protocol" do | ||
let(:source) do | ||
<<~CASK | ||
cask "foo" do | ||
url "https://example.com/download/foo-v1.2.0.dmg", | ||
verified: "example.com" | ||
end | ||
CASK | ||
end | ||
|
||
include_examples "does not report any offenses" | ||
end | ||
|
||
context "when url 'verified' value starts with a protocol" do | ||
let(:source) do | ||
<<~CASK | ||
cask "foo" do | ||
url "https://example.com/download/foo-v1.2.0.dmg", | ||
verified: "https://example.com" | ||
end | ||
CASK | ||
end | ||
|
||
let(:expected_offenses) do | ||
[{ | ||
message: "Verified URL parameter value should not start with https:// or http://.", | ||
severity: :convention, | ||
line: 3, | ||
column: 14, | ||
source: "\"https://example.com\"", | ||
}] | ||
end | ||
|
||
let(:correct_source) do | ||
<<~CASK | ||
cask "foo" do | ||
url "https://example.com/download/foo-v1.2.0.dmg", | ||
verified: "example.com" | ||
end | ||
CASK | ||
end | ||
|
||
include_examples "reports offenses" | ||
include_examples "autocorrects source" | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorbet doesn't run successfully. Need to read up on https://sorbet.org/docs/requires-ancestor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading https://blog.appsignal.com/2021/01/13/using-mixins-and-modules-in-your-ruby-on-rails-application.html for a general overview of mixins, and https://sorbet.org/docs/requires-ancestor, I do not understand what it wants me to do. As the person who introduced this
requires_ancestor
thing, @dduugg can you shed any light (either here or in Slack)? Have I taken a wrong approach here somewhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code means that any module that includes
RuboCop::Cop::Cask::OnUrlStanza
must subclassRuboCop::Cop::Cask::UrlLegacyCommaSeparators
. This makes the methods defined in the latter available in the former. (In this specific case,OnUrlStanza
makes use ofon_url_stanza
, the lone method defined inUrlLegacyCommaSeparators
.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, so if we now have
RuboCop::Cop::Cask::Url
andRuboCop::Cop::Cask::UrlLegacyCommaSeparators
cops that have theon_url_stanza
method , which one should inherit from which? They're separate cops.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured it out!