| 
									
										
										
										
											2010-02-12 14:56:01 -08:00
										 |  |  | abort if ARGV.include? "--skip-update" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require 'testing_env' | 
					
						
							| 
									
										
										
										
											2011-06-12 15:46:08 +02:00
										 |  |  | HOMEBREW_CELLAR.mkpath | 
					
						
							| 
									
										
										
										
											2010-02-12 14:56:01 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | require 'extend/ARGV' # needs to be after test/unit to avoid conflict with OptionsParser | 
					
						
							|  |  |  | ARGV.extend(HomebrewArgvExtension) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require 'formula' | 
					
						
							|  |  |  | require 'utils' | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | require 'cmd/update' | 
					
						
							| 
									
										
										
										
											2010-02-12 14:56:01 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class RefreshBrewMock < RefreshBrew | 
					
						
							| 
									
										
										
										
											2011-06-12 17:07:59 +02:00
										 |  |  |   def git_repo? | 
					
						
							|  |  |  |     @git_repo | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |   attr_writer :git_repo | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def in_prefix_expect(cmd, output = '') | 
					
						
							|  |  |  |     @outputs  ||= Hash.new { |h,k| h[k] = [] } | 
					
						
							|  |  |  |     @expected ||= [] | 
					
						
							|  |  |  |     @expected << cmd | 
					
						
							|  |  |  |     @outputs[cmd] << output | 
					
						
							| 
									
										
										
										
											2010-02-12 14:56:01 -08:00
										 |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   def `(cmd) | 
					
						
							| 
									
										
										
										
											2011-06-12 17:07:59 +02:00
										 |  |  |     if Dir.pwd == HOMEBREW_PREFIX.to_s and @expected.include?(cmd) and !@outputs[cmd].empty? | 
					
						
							|  |  |  |       @called ||= [] | 
					
						
							|  |  |  |       @called << cmd | 
					
						
							|  |  |  |       @outputs[cmd].shift | 
					
						
							| 
									
										
										
										
											2010-02-12 14:56:01 -08:00
										 |  |  |     else | 
					
						
							|  |  |  |       raise "#{inspect} Unexpectedly called backticks in pwd `#{HOMEBREW_PREFIX}' and command `#{cmd}'" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-05-08 15:35:06 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |   alias safe_system `
 | 
					
						
							| 
									
										
										
										
											2010-02-12 14:56:01 -08:00
										 |  |  |    | 
					
						
							|  |  |  |   def expectations_met? | 
					
						
							| 
									
										
										
										
											2011-06-12 17:07:59 +02:00
										 |  |  |     @expected == @called | 
					
						
							| 
									
										
										
										
											2010-02-12 14:56:01 -08:00
										 |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   def inspect | 
					
						
							|  |  |  |     "#<#{self.class.name} #{object_id}>" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-29 10:41:03 -08:00
										 |  |  | class UpdaterTests < Test::Unit::TestCase | 
					
						
							|  |  |  |   OUTSIDE_PREFIX = '/tmp' | 
					
						
							|  |  |  |   def outside_prefix | 
					
						
							|  |  |  |     Dir.chdir(OUTSIDE_PREFIX) { yield } | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   def fixture(name) | 
					
						
							|  |  |  |     self.class.fixture_data[name] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   def self.fixture_data | 
					
						
							|  |  |  |     unless @fixture_data | 
					
						
							|  |  |  |       require 'yaml' | 
					
						
							|  |  |  |       @fixture_data = YAML.load_file(Pathname.new(ABS__FILE__).parent.realpath + 'fixtures/updater_fixture.yaml') | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     @fixture_data | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-12 17:07:59 +02:00
										 |  |  |   def test_init_homebrew | 
					
						
							|  |  |  |     outside_prefix do | 
					
						
							|  |  |  |       updater = RefreshBrewMock.new | 
					
						
							|  |  |  |       updater.git_repo = false | 
					
						
							|  |  |  |       updater.in_prefix_expect("git init") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git pull #{RefreshBrewMock::REPOSITORY_URL} master") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git rev-parse HEAD", "1234abcd") | 
					
						
							|  |  |  |        | 
					
						
							|  |  |  |       assert_equal false, updater.update_from_masterbrew! | 
					
						
							|  |  |  |       assert updater.expectations_met? | 
					
						
							|  |  |  |       assert updater.updated_formulae.empty? | 
					
						
							|  |  |  |       assert updater.added_formulae.empty? | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-29 10:41:03 -08:00
										 |  |  |   def test_update_homebrew_without_any_changes | 
					
						
							|  |  |  |     outside_prefix do | 
					
						
							|  |  |  |       updater = RefreshBrewMock.new | 
					
						
							| 
									
										
										
										
											2011-06-12 17:07:59 +02:00
										 |  |  |       updater.git_repo = true | 
					
						
							|  |  |  |       updater.in_prefix_expect("git checkout -q master") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git rev-parse HEAD", "1234abcd") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git pull #{RefreshBrewMock::REPOSITORY_URL} master") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git rev-parse HEAD", "3456cdef") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git diff-tree -r --name-status -z 1234abcd 3456cdef", "") | 
					
						
							| 
									
										
										
										
											2010-01-29 10:41:03 -08:00
										 |  |  |        | 
					
						
							|  |  |  |       assert_equal false, updater.update_from_masterbrew! | 
					
						
							|  |  |  |       assert updater.expectations_met? | 
					
						
							|  |  |  |       assert updater.updated_formulae.empty? | 
					
						
							|  |  |  |       assert updater.added_formulae.empty? | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   def test_update_homebrew_without_formulae_changes | 
					
						
							|  |  |  |     outside_prefix do | 
					
						
							|  |  |  |       updater = RefreshBrewMock.new | 
					
						
							| 
									
										
										
										
											2011-06-12 17:07:59 +02:00
										 |  |  |       updater.git_repo = true | 
					
						
							|  |  |  |       diff_output = fixture('update_git_diff_output_without_formulae_changes') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       updater.in_prefix_expect("git checkout -q master") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git rev-parse HEAD", "1234abcd") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git pull #{RefreshBrewMock::REPOSITORY_URL} master") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git rev-parse HEAD", "3456cdef") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git diff-tree -r --name-status -z 1234abcd 3456cdef", diff_output.gsub(/\s+/, "\0")) | 
					
						
							| 
									
										
										
										
											2010-01-29 10:41:03 -08:00
										 |  |  |        | 
					
						
							|  |  |  |       assert_equal true, updater.update_from_masterbrew! | 
					
						
							|  |  |  |       assert !updater.pending_formulae_changes? | 
					
						
							|  |  |  |       assert updater.updated_formulae.empty? | 
					
						
							|  |  |  |       assert updater.added_formulae.empty? | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   def test_update_homebrew_with_formulae_changes | 
					
						
							|  |  |  |     outside_prefix do | 
					
						
							|  |  |  |       updater = RefreshBrewMock.new | 
					
						
							| 
									
										
										
										
											2011-06-12 17:07:59 +02:00
										 |  |  |       updater.git_repo = true | 
					
						
							|  |  |  |       diff_output = fixture('update_git_diff_output_with_formulae_changes') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       updater.in_prefix_expect("git checkout -q master") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git rev-parse HEAD", "1234abcd") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git pull #{RefreshBrewMock::REPOSITORY_URL} master") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git rev-parse HEAD", "3456cdef") | 
					
						
							|  |  |  |       updater.in_prefix_expect("git diff-tree -r --name-status -z 1234abcd 3456cdef", diff_output.gsub(/\s+/, "\0")) | 
					
						
							| 
									
										
										
										
											2010-01-29 10:41:03 -08:00
										 |  |  |        | 
					
						
							|  |  |  |       assert_equal true, updater.update_from_masterbrew! | 
					
						
							|  |  |  |       assert updater.pending_formulae_changes? | 
					
						
							|  |  |  |       assert_equal %w{ xar yajl }, updater.updated_formulae | 
					
						
							|  |  |  |       assert_equal %w{ antiword bash-completion ddrescue dict lua }, updater.added_formulae | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |