Skip to content
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

audit: Detect multiline and interpolated strings in formula desc cop #2628

Merged
merged 1 commit into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions Library/Homebrew/rubocops/extend/formula_cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ def source_buffer(node)
node.source_range.source_buffer
end

# Returns the string representation if node is of type str
# Returns the string representation if node is of type str(plain) or dstr(interpolated)
def string_content(node)
node.str_content if node.type == :str
return node.str_content if node.type == :str
node.each_child_node(:str).map(&:str_content).join("") if node.type == :dstr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any other types of strings beyond these two?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is execute-string https://github.com/whitequark/parser/blob/master/doc/AST_FORMAT.md#execute-string
I'm not sure if we should consider that here. Can you please take a look and confirm that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh, I think we can ignore that, thanks!

end

# Returns printable component name
Expand Down
25 changes: 25 additions & 0 deletions Library/Homebrew/test/rubocops/formula_desc_cop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@ class Foo < Formula
end
end

it "When desc is multiline string" do
source = <<-EOS.undent
class Foo < Formula
url 'http://example.com/foo-1.0.tgz'
desc '#{"bar"*10}'\
'#{"foo"*21}'
end
EOS

msg = <<-EOS.undent
Description is too long. "name: desc" should be less than 80 characters.
Length is calculated as Foo + desc. (currently 98)
EOS
expected_offenses = [{ message: msg,
severity: :convention,
line: 3,
column: 2,
source: source }]

inspect_source(cop, source)
expected_offenses.zip(cop.offenses).each do |expected, actual|
expect_offense(expected, actual)
end
end

it "When wrong \"command-line\" usage in desc" do
source = <<-EOS.undent
class Foo < Formula
Expand Down