Warn developers when uninstalling a dependency

Suggested in #1084.

Made the existing warning output entirely to STDERR, because
previously the first line went to STDERR and subsequent ones went
to STDOUT.
This commit is contained in:
Alyssa Ross 2016-11-11 20:08:26 +00:00
parent 5294b99d6f
commit 3c310b2e3d
4 changed files with 81 additions and 17 deletions

View File

@ -28,10 +28,8 @@ module Homebrew
ARGV.kegs.group_by(&:rack) ARGV.kegs.group_by(&:rack)
end end
if should_check_for_dependents? handle_unsatisfied_dependents(kegs_by_rack)
all_kegs = kegs_by_rack.values.flatten(1) return if Homebrew.failed?
return if check_for_dependents all_kegs
end
kegs_by_rack.each do |rack, kegs| kegs_by_rack.each do |rack, kegs|
if ARGV.force? if ARGV.force?
@ -78,28 +76,47 @@ module Homebrew
end end
end end
def should_check_for_dependents? def handle_unsatisfied_dependents(kegs_by_rack)
# --ignore-dependencies, to be consistent with install return if ARGV.include?("--ignore-dependencies")
return false if ARGV.include?("--ignore-dependencies")
return false if ARGV.homebrew_developer? all_kegs = kegs_by_rack.values.flatten(1)
true check_for_dependents all_kegs
end end
def check_for_dependents(kegs) def check_for_dependents(kegs)
return false unless result = Keg.find_some_installed_dependents(kegs) return false unless result = Keg.find_some_installed_dependents(kegs)
requireds, dependents = result if ARGV.homebrew_developer?
dependents_output_for_developers(*result)
else
dependents_output_for_nondevelopers(*result)
end
true
end
def dependents_output_for_developers(requireds, dependents)
msg = requireds.join(", ")
msg << (requireds.count == 1 ? " is" : " are")
msg << " required by #{dependents.join(", ")}, which "
msg << (dependents.count == 1 ? "is" : "are")
msg << " currently installed."
msg << "\nYou can silence this warning with "
msg << "`brew uninstall --ignore-dependencies "
msg << "#{requireds.map(&:name).join(" ")}`."
opoo msg
end
def dependents_output_for_nondevelopers(requireds, dependents)
msg = "Refusing to uninstall #{requireds.join(", ")} because " msg = "Refusing to uninstall #{requireds.join(", ")} because "
msg << (requireds.count == 1 ? "it is" : "they are") msg << (requireds.count == 1 ? "it is" : "they are")
msg << " required by #{dependents.join(", ")}, which " msg << " required by #{dependents.join(", ")}, which "
msg << (dependents.count == 1 ? "is" : "are") msg << (dependents.count == 1 ? "is" : "are")
msg << " currently installed." msg << " currently installed."
msg << "\nYou can override this and force removal with "
msg << "`brew uninstall --ignore-dependencies "
msg << "#{requireds.map(&:name).join(" ")}`."
ofail msg ofail msg
print "You can override this and force removal with "
puts "`brew uninstall --ignore-dependencies #{requireds.map(&:name).join(" ")}`."
true
end end
def rm_pin(rack) def rm_pin(rack)

View File

@ -2,20 +2,55 @@ require "helper/integration_command_test_case"
require "cmd/uninstall" require "cmd/uninstall"
class UninstallTests < Homebrew::TestCase class UninstallTests < Homebrew::TestCase
def setup
@dependency = formula("dependency") { url "f-1" }
@dependent = formula("dependent") do
url "f-1"
depends_on "dependency"
end
[@dependency, @dependent].each { |f| f.installed_prefix.mkpath }
tab = Tab.empty
tab.tabfile = @dependent.installed_prefix/Tab::FILENAME
tab.runtime_dependencies = [
{ "full_name" => "dependency", "version" => "1" },
]
tab.write
stub_formula_loader @dependency
stub_formula_loader @dependent
end
def teardown
Homebrew.failed = false
[@dependency, @dependent].each { |f| f.rack.rmtree }
end
def handle_unsatisfied_dependents
capture_stderr do
opts = { @dependency.rack => [Keg.new(@dependency.installed_prefix)] }
Homebrew.handle_unsatisfied_dependents(opts)
end
end
def test_check_for_testball_f2s_when_developer def test_check_for_testball_f2s_when_developer
refute_predicate Homebrew, :should_check_for_dependents? assert_match "Warning", handle_unsatisfied_dependents
refute_predicate Homebrew, :failed?
end end
def test_check_for_dependents_when_not_developer def test_check_for_dependents_when_not_developer
run_as_not_developer do run_as_not_developer do
assert_predicate Homebrew, :should_check_for_dependents? assert_match "Error", handle_unsatisfied_dependents
assert_predicate Homebrew, :failed?
end end
end end
def test_check_for_dependents_when_ignore_dependencies def test_check_for_dependents_when_ignore_dependencies
ARGV << "--ignore-dependencies" ARGV << "--ignore-dependencies"
run_as_not_developer do run_as_not_developer do
refute_predicate Homebrew, :should_check_for_dependents? assert_empty handle_unsatisfied_dependents
refute_predicate Homebrew, :failed?
end end
ensure ensure
ARGV.delete("--ignore-dependencies") ARGV.delete("--ignore-dependencies")

View File

@ -174,6 +174,10 @@ class UtilTests < Homebrew::TestCase
end end
end end
def test_capture_stderr
assert_equal "test\n", capture_stderr { $stderr.puts "test" }
end
def test_shell_profile def test_shell_profile
ENV["SHELL"] = "/bin/sh" ENV["SHELL"] = "/bin/sh"
assert_equal "~/.bash_profile", Utils::Shell.shell_profile assert_equal "~/.bash_profile", Utils::Shell.shell_profile

View File

@ -376,6 +376,14 @@ ensure
trap("INT", std_trap) trap("INT", std_trap)
end end
def capture_stderr
old, $stderr = $stderr, StringIO.new
yield
$stderr.string
ensure
$stderr = old
end
def nostdout def nostdout
if ARGV.verbose? if ARGV.verbose?
yield yield