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

Add -Werror flag to turn Ruby warnings into errors #995

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ def self.run args = []
Minitest.seed = options[:seed]
srand Minitest.seed

if options[:error_on_warning]
require "minitest/error_on_warning"
end

reporter = CompositeReporter.new
reporter << SummaryReporter.new(options[:io], options)
reporter << ProgressReporter.new(options[:io], options) unless options[:quiet]
Expand Down Expand Up @@ -253,6 +257,19 @@ def self.process_args args = [] # :nodoc:
options[:skip] = s.chars.to_a
end

opts.on "-Werror", String, "Turn Ruby warnings into errors" do |s|
case s
when "error"
options[:error_on_warning] = true
$VERBOSE = true
::Warning[:deprecated] if ::Warning.respond_to?(:[]=) # Ruby 2.7+
Copy link
Collaborator

Choose a reason for hiding this comment

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

did you mean?

Suggested change
::Warning[:deprecated] if ::Warning.respond_to?(:[]=) # Ruby 2.7+
::Warning[:deprecated] = true if ::Warning.respond_to?(:[]=) # Ruby 2.7+

Copy link
Author

Choose a reason for hiding this comment

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

Yes.

else
if ::Warning.respond_to?(:[]=) # Ruby 2.7+
::Warning[s.to_sym] = true
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm unfamiliar enough with Warning that I don't understand this side of the top branch... I can see that you're setting that category to true, but I don't see it doing anything? Clue me?

Copy link
Author

Choose a reason for hiding this comment

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

Warnings that have a category (:deprecated, :performance, :experimental), are only emitted if Warning[category_name] = true.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Part of my confusion was that minitest/test_task always runs -w by default so Warning[:deprecated] was always true.

end
end

unless extensions.empty?
opts.separator ""
opts.separator "Known extensions: #{extensions.join(", ")}"
Expand Down Expand Up @@ -995,6 +1012,15 @@ def result_label # :nodoc:
end
end

##
# Assertion raised on warning when running in -Werror mode.

class UnexpectedWarning < Assertion
def result_label # :nodoc:
"Warning"
end
end

##
# Assertion wrapping an unexpected error that was raised during a run.

Expand Down
11 changes: 11 additions & 0 deletions lib/minitest/error_on_warning.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Minitest

module ErrorOnWarning
def warn(message, category: nil)
message = "[#{category}] #{message}" if category
raise UnexpectedWarning, message
end
end

::Warning.singleton_class.prepend(ErrorOnWarning)
end