From 5c185eaa4355810ccc6617cb078ee67d4fa0777c Mon Sep 17 00:00:00 2001
From: Michael Ledin <m.ledin@appheads.ru>
Date: Sun, 19 Feb 2017 07:57:41 +0300
Subject: [PATCH] Expand glob patterns.

---
 .../cask/lib/hbc/artifact/uninstall_base.rb   | 23 ++++++++++++-------
 .../cask/spec/cask/artifact/uninstall_spec.rb | 22 ++++++++++++++----
 .../cask/spec/cask/artifact/zap_spec.rb       | 22 ++++++++++++++----
 .../fixtures/cask/Casks/with-installable.rb   |  5 ++--
 .../cask/Casks/with-uninstall-delete.rb       |  5 ++--
 .../cask/Casks/with-uninstall-trash.rb        |  5 ++--
 .../fixtures/cask/Casks/with-zap-delete.rb    |  5 ++--
 .../fixtures/cask/Casks/with-zap-trash.rb     |  5 ++--
 8 files changed, 66 insertions(+), 26 deletions(-)

diff --git a/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb b/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb
index cc47e05ad3..75d210931f 100644
--- a/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb
+++ b/Library/Homebrew/cask/lib/hbc/artifact/uninstall_base.rb
@@ -34,6 +34,10 @@ module Hbc
         end
       end
 
+      def self.expand_glob(path_strings)
+        path_strings.flat_map(&Pathname.method(:glob))
+      end
+
       def self.remove_relative_path_strings(action, path_strings)
         relative = path_strings.map do |path_string|
           path_string if %r{/\.\.(?:/|\Z)}.match(path_string) || !%r{\A/}.match(path_string)
@@ -54,6 +58,13 @@ module Hbc
         path_strings - undeletable
       end
 
+      def self.prepare_path_strings(action, path_strings, expand_tilde)
+        path_strings = expand_path_strings(path_strings) if expand_tilde
+        path_strings = remove_relative_path_strings(action, path_strings)
+        path_strings = expand_glob(path_strings)
+        remove_undeletable_path_strings(action, path_strings)
+      end
+
       def dispatch_uninstall_directives(expand_tilde: true)
         directives_set = @cask.artifacts[stanza]
         ohai "Running #{stanza} process for #{@cask}; your password may be necessary"
@@ -225,9 +236,7 @@ module Hbc
       def uninstall_delete(directives, expand_tilde = true)
         Array(directives[:delete]).concat(Array(directives[:trash])).flatten.each_slice(PATH_ARG_SLICE_SIZE) do |path_slice|
           ohai "Removing files: #{path_slice.utf8_inspect}"
-          path_slice = self.class.expand_path_strings(path_slice) if expand_tilde
-          path_slice = self.class.remove_relative_path_strings(:delete, path_slice)
-          path_slice = self.class.remove_undeletable_path_strings(:delete, path_slice)
+          path_slice = self.class.prepare_path_strings(:delete, path_slice, expand_tilde)
           @command.run!("/bin/rm", args: path_slice.unshift("-rf", "--"), sudo: true)
         end
       end
@@ -238,11 +247,9 @@ module Hbc
         uninstall_delete(directives, expand_tilde)
       end
 
-      def uninstall_rmdir(directives, expand_tilde = true)
-        Array(directives[:rmdir]).flatten.each do |directory|
-          directory = self.class.expand_path_strings([directory]).first if expand_tilde
-          directory = self.class.remove_relative_path_strings(:rmdir, [directory]).first
-          directory = self.class.remove_undeletable_path_strings(:rmdir, [directory]).first
+      def uninstall_rmdir(directories, expand_tilde = true)
+        action = :rmdir
+        self.class.prepare_path_strings(action, Array(directories[action]).flatten, expand_tilde).each do |directory|
           next if directory.to_s.empty?
           ohai "Removing directory if empty: #{directory.to_s.utf8_inspect}"
           directory = Pathname.new(directory)
diff --git a/Library/Homebrew/cask/spec/cask/artifact/uninstall_spec.rb b/Library/Homebrew/cask/spec/cask/artifact/uninstall_spec.rb
index a38ed903ee..05af462dc5 100644
--- a/Library/Homebrew/cask/spec/cask/artifact/uninstall_spec.rb
+++ b/Library/Homebrew/cask/spec/cask/artifact/uninstall_spec.rb
@@ -7,7 +7,17 @@ describe Hbc::Artifact::Uninstall do
     Hbc::Artifact::Uninstall.new(cask, command: Hbc::FakeSystemCommand)
   }
 
+  let(:absolute_path) { Pathname.new("#{TEST_TMPDIR}/absolute_path") }
+  let(:path_with_tilde) { Pathname.new("#{TEST_TMPDIR}/path_with_tilde") }
+  let(:glob_path1) { Pathname.new("#{TEST_TMPDIR}/glob_path1") }
+  let(:glob_path2) { Pathname.new("#{TEST_TMPDIR}/glob_path2") }
+
   before(:each) do
+    FileUtils.touch(absolute_path)
+    FileUtils.touch(path_with_tilde)
+    FileUtils.touch(glob_path1)
+    FileUtils.touch(glob_path2)
+    ENV["HOME"] = TEST_TMPDIR
     shutup do
       InstallHelper.install_without_artifacts(cask)
     end
@@ -233,8 +243,10 @@ describe Hbc::Artifact::Uninstall do
       it "can uninstall" do
         Hbc::FakeSystemCommand.expects_command(
           sudo(%w[/bin/rm -rf --],
-               Pathname.new("/permissible/absolute/path"),
-               Pathname.new("~/permissible/path/with/tilde").expand_path),
+               absolute_path,
+               path_with_tilde,
+               glob_path1,
+               glob_path2),
         )
 
         subject
@@ -247,8 +259,10 @@ describe Hbc::Artifact::Uninstall do
       it "can uninstall" do
         Hbc::FakeSystemCommand.expects_command(
           sudo(%w[/bin/rm -rf --],
-               Pathname.new("/permissible/absolute/path"),
-               Pathname.new("~/permissible/path/with/tilde").expand_path),
+               absolute_path,
+               path_with_tilde,
+               glob_path1,
+               glob_path2),
         )
 
         subject
diff --git a/Library/Homebrew/cask/spec/cask/artifact/zap_spec.rb b/Library/Homebrew/cask/spec/cask/artifact/zap_spec.rb
index 50e132bfa7..b87b6ce1c2 100644
--- a/Library/Homebrew/cask/spec/cask/artifact/zap_spec.rb
+++ b/Library/Homebrew/cask/spec/cask/artifact/zap_spec.rb
@@ -8,7 +8,17 @@ describe Hbc::Artifact::Zap do
     Hbc::Artifact::Zap.new(cask, command: Hbc::FakeSystemCommand)
   }
 
+  let(:absolute_path) { Pathname.new("#{TEST_TMPDIR}/absolute_path") }
+  let(:path_with_tilde) { Pathname.new("#{TEST_TMPDIR}/path_with_tilde") }
+  let(:glob_path1) { Pathname.new("#{TEST_TMPDIR}/glob_path1") }
+  let(:glob_path2) { Pathname.new("#{TEST_TMPDIR}/glob_path2") }
+
   before(:each) do
+    FileUtils.touch(absolute_path)
+    FileUtils.touch(path_with_tilde)
+    FileUtils.touch(glob_path1)
+    FileUtils.touch(glob_path2)
+    ENV["HOME"] = TEST_TMPDIR
     shutup do
       InstallHelper.install_without_artifacts(cask)
     end
@@ -234,8 +244,10 @@ describe Hbc::Artifact::Zap do
       it "can zap" do
         Hbc::FakeSystemCommand.expects_command(
           sudo(%w[/bin/rm -rf --],
-               Pathname.new("/permissible/absolute/path"),
-               Pathname.new("~/permissible/path/with/tilde").expand_path),
+               absolute_path,
+               path_with_tilde,
+               glob_path1,
+               glob_path2),
         )
 
         subject
@@ -248,8 +260,10 @@ describe Hbc::Artifact::Zap do
       it "can zap" do
         Hbc::FakeSystemCommand.expects_command(
           sudo(%w[/bin/rm -rf --],
-               Pathname.new("/permissible/absolute/path"),
-               Pathname.new("~/permissible/path/with/tilde").expand_path),
+               absolute_path,
+               path_with_tilde,
+               glob_path1,
+               glob_path2),
         )
 
         subject
diff --git a/Library/Homebrew/test/support/fixtures/cask/Casks/with-installable.rb b/Library/Homebrew/test/support/fixtures/cask/Casks/with-installable.rb
index b0ad0c6269..706b85f6b2 100644
--- a/Library/Homebrew/test/support/fixtures/cask/Casks/with-installable.rb
+++ b/Library/Homebrew/test/support/fixtures/cask/Casks/with-installable.rb
@@ -11,8 +11,9 @@ cask 'with-installable' do
             quit:       'my.fancy.package.app',
             login_item: 'Fancy',
             delete:     [
-                          '/permissible/absolute/path',
-                          '~/permissible/path/with/tilde',
+                          "#{TEST_TMPDIR}/absolute_path",
+                          '~/path_with_tilde',
+                          "#{TEST_TMPDIR}/glob_path*",
                           'impermissible/relative/path',
                           '/another/impermissible/../relative/path',
                         ],
diff --git a/Library/Homebrew/test/support/fixtures/cask/Casks/with-uninstall-delete.rb b/Library/Homebrew/test/support/fixtures/cask/Casks/with-uninstall-delete.rb
index cba51f18f9..c7d2e47674 100644
--- a/Library/Homebrew/test/support/fixtures/cask/Casks/with-uninstall-delete.rb
+++ b/Library/Homebrew/test/support/fixtures/cask/Casks/with-uninstall-delete.rb
@@ -8,8 +8,9 @@ cask 'with-uninstall-delete' do
   pkg 'Fancy.pkg'
 
   uninstall delete: [
-                      '/permissible/absolute/path',
-                      '~/permissible/path/with/tilde',
+                      "#{TEST_TMPDIR}/absolute_path",
+                      '~/path_with_tilde',
+                      "#{TEST_TMPDIR}/glob_path*",
                       'impermissible/relative/path',
                       '/another/impermissible/../relative/path',
                     ]
diff --git a/Library/Homebrew/test/support/fixtures/cask/Casks/with-uninstall-trash.rb b/Library/Homebrew/test/support/fixtures/cask/Casks/with-uninstall-trash.rb
index 171780bb78..b085b3e321 100644
--- a/Library/Homebrew/test/support/fixtures/cask/Casks/with-uninstall-trash.rb
+++ b/Library/Homebrew/test/support/fixtures/cask/Casks/with-uninstall-trash.rb
@@ -8,8 +8,9 @@ cask 'with-uninstall-trash' do
   pkg 'Fancy.pkg'
 
   uninstall trash: [
-                     '/permissible/absolute/path',
-                     '~/permissible/path/with/tilde',
+                     "#{TEST_TMPDIR}/absolute_path",
+                     '~/path_with_tilde',
+                     "#{TEST_TMPDIR}/glob_path*",
                      'impermissible/relative/path',
                      '/another/impermissible/../relative/path',
                    ]
diff --git a/Library/Homebrew/test/support/fixtures/cask/Casks/with-zap-delete.rb b/Library/Homebrew/test/support/fixtures/cask/Casks/with-zap-delete.rb
index a0ab83fc2f..d81a387f9b 100644
--- a/Library/Homebrew/test/support/fixtures/cask/Casks/with-zap-delete.rb
+++ b/Library/Homebrew/test/support/fixtures/cask/Casks/with-zap-delete.rb
@@ -8,8 +8,9 @@ cask 'with-zap-delete' do
   pkg 'Fancy.pkg'
 
   zap delete: [
-                '/permissible/absolute/path',
-                '~/permissible/path/with/tilde',
+                "#{TEST_TMPDIR}/absolute_path",
+                '~/path_with_tilde',
+                "#{TEST_TMPDIR}/glob_path*",
                 'impermissible/relative/path',
                 '/another/impermissible/../relative/path',
               ]
diff --git a/Library/Homebrew/test/support/fixtures/cask/Casks/with-zap-trash.rb b/Library/Homebrew/test/support/fixtures/cask/Casks/with-zap-trash.rb
index 8f4e91d67f..ea2bc2a018 100644
--- a/Library/Homebrew/test/support/fixtures/cask/Casks/with-zap-trash.rb
+++ b/Library/Homebrew/test/support/fixtures/cask/Casks/with-zap-trash.rb
@@ -8,8 +8,9 @@ cask 'with-zap-trash' do
   pkg 'Fancy.pkg'
 
   zap trash: [
-               '/permissible/absolute/path',
-               '~/permissible/path/with/tilde',
+               "#{TEST_TMPDIR}/absolute_path",
+               '~/path_with_tilde',
+               "#{TEST_TMPDIR}/glob_path*",
                'impermissible/relative/path',
                '/another/impermissible/../relative/path',
              ]
-- 
GitLab