Merge pull request #1498 from alyssais/uninstall_developer_warning

Warn developers when uninstalling a dependency
This commit is contained in:
Mike McQuaid 2016-11-15 08:15:42 +00:00 committed by GitHub
commit 484e3e0769
4 changed files with 115 additions and 22 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,30 +76,77 @@ 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?
DeveloperDependentsMessage.new(*result).output
msg = "Refusing to uninstall #{requireds.join(", ")} because " else
msg << (requireds.count == 1 ? "it is" : "they are") NondeveloperDependentsMessage.new(*result).output
msg << " required by #{dependents.join(", ")}, which " end
msg << (dependents.count == 1 ? "is" : "are")
msg << " currently installed."
ofail msg
print "You can override this and force removal with "
puts "`brew uninstall --ignore-dependencies #{requireds.map(&:name).join(" ")}`."
true true
end end
class DependentsMessage
attr_reader :reqs, :deps
def initialize(requireds, dependents)
@reqs = requireds
@deps = dependents
end
protected
def are(items)
items.count == 1 ? "is" : "are"
end
def they(items)
items.count == 1 ? "it" : "they"
end
def list(items)
items.join(", ")
end
def sample_command
"brew uninstall --ignore-dependencies #{list reqs.map(&:name)}"
end
def are_required_by_deps
"#{are reqs} required by #{list deps}, which #{are deps} currently installed"
end
end
class DeveloperDependentsMessage < DependentsMessage
def output
opoo <<-EOS.undent
#{list reqs} #{are_required_by_deps}.
You can silence this warning with:
#{sample_command}
EOS
end
end
class NondeveloperDependentsMessage < DependentsMessage
def output
ofail <<-EOS.undent
Refusing to uninstall #{list reqs}
because #{they reqs} #{are_required_by_deps}.
You can override this and force removal with:
#{sample_command}
EOS
end
end
def rm_pin(rack) def rm_pin(rack)
Formulary.from_rack(rack).unpin Formulary.from_rack(rack).unpin
rescue rescue

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,15 @@ 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