From 8472d6a40eab2e10a33202d3d9a05c20cc2dfc73 Mon Sep 17 00:00:00 2001
From: Mike McQuaid <mike@mikemcquaid.com>
Date: Thu, 30 Mar 2017 19:39:26 +0100
Subject: [PATCH] formula: check installed_alias_path exists.

Otherwise return `nil`.

Fixes https://github.com/Homebrew/brew/issues/2417
---
 Library/Homebrew/formula.rb           |  6 ++++--
 Library/Homebrew/test/formula_spec.rb | 28 +++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb
index 4436192063..00af3e19e4 100644
--- a/Library/Homebrew/formula.rb
+++ b/Library/Homebrew/formula.rb
@@ -252,12 +252,14 @@ class Formula
 
   public
 
-  # The alias path that was used to install this formula, if present.
+  # The alias path that was used to install this formula, if it exists.
   # Can differ from alias_path, which is the alias used to find the formula,
   # and is specified to this instance.
   def installed_alias_path
     path = build.source["path"] if build.is_a?(Tab)
-    path if path =~ %r{#{HOMEBREW_TAP_DIR_REGEX}/Aliases}
+    return unless path =~ %r{#{HOMEBREW_TAP_DIR_REGEX}/Aliases}
+    return unless File.symlink?(path)
+    path
   end
 
   def installed_alias_name
diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb
index 1e064912fa..2309c36fb1 100644
--- a/Library/Homebrew/test/formula_spec.rb
+++ b/Library/Homebrew/test/formula_spec.rb
@@ -129,6 +129,8 @@ describe Formula do
 
     alias_name = "bar"
     alias_path = "#{CoreTap.instance.alias_dir}/#{alias_name}"
+    CoreTap.instance.alias_dir.mkpath
+    FileUtils.ln_sf f.path, alias_path
 
     f.build = Tab.new(source: { "path" => alias_path })
 
@@ -160,6 +162,8 @@ describe Formula do
     alias_name = "bar"
     full_alias_name = "#{tap.user}/#{tap.repo}/#{alias_name}"
     alias_path = "#{tap.alias_dir}/#{alias_name}"
+    tap.alias_dir.mkpath
+    FileUtils.ln_sf f.path, alias_path
 
     f.build = Tab.new(source: { "path" => alias_path })
 
@@ -168,6 +172,8 @@ describe Formula do
     expect(f.full_installed_alias_name).to eq(full_alias_name)
     expect(f.installed_specified_name).to eq(alias_name)
     expect(f.full_installed_specified_name).to eq(full_alias_name)
+
+    FileUtils.rm_rf HOMEBREW_LIBRARY/"Taps/user"
   end
 
   specify "#prefix" do
@@ -402,6 +408,8 @@ describe Formula do
         url "foo-1.0"
       end
       f.build = Tab.new(source: { "path" => source_path.to_s })
+      CoreTap.instance.alias_dir.mkpath
+      FileUtils.ln_sf f.path, source_path
 
       expect(f.alias_path).to eq(alias_path)
       expect(f.installed_alias_path).to eq(source_path.to_s)
@@ -443,6 +451,9 @@ describe Formula do
 
       allow(described_class).to receive(:installed).and_return(formulae)
 
+      CoreTap.instance.alias_dir.mkpath
+      FileUtils.ln_sf formula_with_alias.path, alias_path
+
       expect(described_class.installed_with_alias_path(alias_path))
         .to eq([formula_with_alias])
     end
@@ -940,6 +951,9 @@ describe Formula do
       tab.source["path"] = alias_path
       stub_formula_loader(f, alias_path)
 
+      CoreTap.instance.alias_dir.mkpath
+      FileUtils.ln_sf f.path, alias_path
+
       expect(f.current_installed_alias_target).to eq(f)
       expect(f.latest_formula).to eq(f)
       expect(f).not_to have_changed_installed_alias_target
@@ -952,6 +966,9 @@ describe Formula do
       tab.source["path"] = alias_path
       stub_formula_loader(new_formula, alias_path)
 
+      CoreTap.instance.alias_dir.mkpath
+      FileUtils.ln_sf new_formula.path, alias_path
+
       expect(f.current_installed_alias_target).to eq(new_formula)
       expect(f.latest_formula).to eq(new_formula)
       expect(f).to have_changed_installed_alias_target
@@ -964,6 +981,9 @@ describe Formula do
       tab.source["path"] = alias_path
       stub_formula_loader(new_formula, alias_path)
 
+      CoreTap.instance.alias_dir.mkpath
+      FileUtils.ln_sf new_formula.path, alias_path
+
       expect(new_formula.current_installed_alias_target).to eq(new_formula)
       expect(new_formula.latest_formula).to eq(new_formula)
       expect(new_formula).not_to have_changed_installed_alias_target
@@ -1050,6 +1070,10 @@ describe Formula do
       f.follow_installed_alias = true
       f.build = setup_tab_for_prefix(same_prefix, path: alias_path)
       stub_formula_loader(new_formula, alias_path)
+
+      CoreTap.instance.alias_dir.mkpath
+      FileUtils.ln_sf new_formula.path, alias_path
+
       expect(f.outdated_kegs).not_to be_empty
     end
 
@@ -1088,6 +1112,10 @@ describe Formula do
       tab = setup_tab_for_prefix(old_alias_target_prefix, path: alias_path)
       old_formula.build = tab
       allow(described_class).to receive(:installed).and_return([old_formula])
+
+      CoreTap.instance.alias_dir.mkpath
+      FileUtils.ln_sf f.path, alias_path
+
       expect(f.outdated_kegs).not_to be_empty
     end
 
-- 
GitLab