From 6404f82d0941211d5a8b5431e506c6de5368e9da Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Wed, 12 Jun 2013 15:09:16 -0400 Subject: [PATCH] Verify appdir after reading command line opts Helps prevent default appdir from being created regardless of whether the user configured it or not. Closes #496 --- lib/cask.rb | 5 +++-- lib/cask/cli.rb | 2 +- test/cask/cli_test.rb | 32 ++++++++++++++++++++++++++++++++ test/support/fake_appdir.rb | 2 +- test/test_helper.rb | 1 + 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/cask.rb b/lib/cask.rb index 10acecf82..04bd93aac 100644 --- a/lib/cask.rb +++ b/lib/cask.rb @@ -47,8 +47,9 @@ class Cask ohai "We need to make Caskroom for the first time at #{caskroom}" ohai "We'll set permissions properly so we won't need sudo in the future" current_user = ENV['USER'] - system "sudo mkdir -p #{caskroom}" - system "sudo chown -R #{current_user}:staff #{caskroom.parent}" + sudo = 'sudo' unless caskroom.parent.writable? + system "#{sudo} mkdir -p #{caskroom}" + system "#{sudo} chown -R #{current_user}:staff #{caskroom.parent}" end appdir.mkpath unless appdir.exist? end diff --git a/lib/cask/cli.rb b/lib/cask/cli.rb index 7252d16cf..d9fc896ca 100644 --- a/lib/cask/cli.rb +++ b/lib/cask/cli.rb @@ -15,9 +15,9 @@ class Cask::CLI end def self.process(arguments) - Cask.init command, *rest = *arguments rest = process_options(rest) + Cask.init lookup_command(command).run(*rest) end diff --git a/test/cask/cli_test.rb b/test/cask/cli_test.rb index 23eaf0cf9..494bac501 100644 --- a/test/cask/cli_test.rb +++ b/test/cask/cli_test.rb @@ -12,4 +12,36 @@ describe Cask::CLI do phinze-cask/adium ]) end + + describe "process" do + it "creates the appdir if it does not exist" do + Cask.appdir.rmdir + shutup { + Cask::CLI.process('list') + } + Cask.appdir.directory?.must_equal true + end + + it "respects the env variable when choosing what appdir to create, not touching the default appdir" do + default_applications_dir = Cask.appdir + default_applications_dir.rmdir + custom_applications_dir = Pathname(Dir.mktmpdir('custom_app_dir')) + custom_applications_dir.rmdir + + default_applications_dir.directory?.must_equal false + custom_applications_dir.directory?.must_equal false + + begin + ENV['HOMEBREW_CASK_OPTS'] = "--appdir=#{custom_applications_dir}" + shutup { + Cask::CLI.process('list') + } + ensure + ENV.delete 'HOMEBREW_CASK_OPTS' + end + + default_applications_dir.directory?.must_equal false + custom_applications_dir.directory?.must_equal true + end + end end diff --git a/test/support/fake_appdir.rb b/test/support/fake_appdir.rb index c1b510926..7f4c476fd 100644 --- a/test/support/fake_appdir.rb +++ b/test/support/fake_appdir.rb @@ -9,7 +9,7 @@ module FakeAppdirHooks def after_teardown super - @canned_appdir.rmtree + @canned_appdir.rmtree if @canned_appdir.exist? end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 026d081cc..ce43b7b1c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -73,6 +73,7 @@ require 'support/fake_fetcher' require 'support/fake_appdir' require 'support/fake_system_command' require 'support/cleanup' +require 'tmpdir' # pretend like we installed the cask tap project_root = Pathname.new(File.expand_path("#{File.dirname(__FILE__)}/../"))