| 
									
										
										
										
											2016-09-26 19:55:50 +02:00
										 |  |  | require "bundler" | 
					
						
							|  |  |  | require "testing_env" | 
					
						
							|  |  |  | require "fileutils" | 
					
						
							|  |  |  | require "pathname" | 
					
						
							|  |  |  | require "formula" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 00:03:40 +02:00
										 |  |  | class IntegrationCommandTestCase < Homebrew::TestCase | 
					
						
							| 
									
										
										
										
											2016-09-26 19:55:50 +02:00
										 |  |  |   def setup | 
					
						
							|  |  |  |     @cmd_id_index = 0 # Assign unique IDs to invocations of `cmd_output`. | 
					
						
							|  |  |  |     (HOMEBREW_PREFIX/"bin").mkpath | 
					
						
							|  |  |  |     FileUtils.touch HOMEBREW_PREFIX/"bin/brew" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def teardown | 
					
						
							|  |  |  |     coretap = CoreTap.new | 
					
						
							|  |  |  |     paths_to_delete = [ | 
					
						
							|  |  |  |       HOMEBREW_LINKED_KEGS, | 
					
						
							|  |  |  |       HOMEBREW_PINNED_KEGS, | 
					
						
							|  |  |  |       HOMEBREW_CELLAR.children, | 
					
						
							|  |  |  |       HOMEBREW_CACHE.children, | 
					
						
							|  |  |  |       HOMEBREW_LOCK_DIR.children, | 
					
						
							|  |  |  |       HOMEBREW_LOGS.children, | 
					
						
							|  |  |  |       HOMEBREW_TEMP.children, | 
					
						
							|  |  |  |       HOMEBREW_PREFIX/"bin", | 
					
						
							|  |  |  |       HOMEBREW_PREFIX/"share", | 
					
						
							|  |  |  |       HOMEBREW_PREFIX/"opt", | 
					
						
							| 
									
										
										
										
											2016-10-07 17:09:24 +08:00
										 |  |  |       HOMEBREW_PREFIX/"Caskroom", | 
					
						
							| 
									
										
										
										
											2016-09-26 19:55:50 +02:00
										 |  |  |       HOMEBREW_LIBRARY/"Taps/caskroom", | 
					
						
							|  |  |  |       HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-bundle", | 
					
						
							|  |  |  |       HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-foo", | 
					
						
							|  |  |  |       HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-services", | 
					
						
							|  |  |  |       HOMEBREW_LIBRARY/"Taps/homebrew/homebrew-shallow", | 
					
						
							|  |  |  |       HOMEBREW_REPOSITORY/".git", | 
					
						
							|  |  |  |       coretap.path/".git", | 
					
						
							|  |  |  |       coretap.alias_dir, | 
					
						
							|  |  |  |       coretap.formula_dir.children, | 
					
						
							|  |  |  |       coretap.path/"formula_renames.json", | 
					
						
							|  |  |  |     ].flatten | 
					
						
							|  |  |  |     FileUtils.rm_rf paths_to_delete | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def needs_test_cmd_taps | 
					
						
							|  |  |  |     return if ENV["HOMEBREW_TEST_OFFICIAL_CMD_TAPS"] | 
					
						
							|  |  |  |     skip "HOMEBREW_TEST_OFFICIAL_CMD_TAPS is not set" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def needs_macos | 
					
						
							|  |  |  |     skip "Not on MacOS" unless OS.mac? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def cmd_id_from_args(args) | 
					
						
							|  |  |  |     args_pretty = args.join(" ").gsub(TEST_TMPDIR, "@TMPDIR@") | 
					
						
							|  |  |  |     test_pretty = "#{self.class.name}\##{name}.#{@cmd_id_index += 1}" | 
					
						
							|  |  |  |     "[#{test_pretty}] brew #{args_pretty}" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def cmd_output(*args) | 
					
						
							|  |  |  |     # 1.8-compatible way of writing def cmd_output(*args, **env) | 
					
						
							|  |  |  |     env = args.last.is_a?(Hash) ? args.pop : {} | 
					
						
							|  |  |  |     cmd_args = %W[
 | 
					
						
							|  |  |  |       -W0 | 
					
						
							|  |  |  |       -I#{HOMEBREW_LIBRARY_PATH}/test/lib | 
					
						
							|  |  |  |       -rconfig | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     if ENV["HOMEBREW_TESTS_COVERAGE"] | 
					
						
							|  |  |  |       # This is needed only because we currently use a patched version of | 
					
						
							|  |  |  |       # simplecov, and gems installed through git are not available without | 
					
						
							|  |  |  |       # requiring bundler/setup first. See also the comment in test/Gemfile. | 
					
						
							|  |  |  |       # Remove this line when we'll switch back to a stable simplecov release. | 
					
						
							|  |  |  |       cmd_args << "-rbundler/setup" | 
					
						
							|  |  |  |       cmd_args << "-rsimplecov" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     cmd_args << "-rintegration_mocks" | 
					
						
							|  |  |  |     cmd_args << (HOMEBREW_LIBRARY_PATH/"brew.rb").resolved_path.to_s | 
					
						
							|  |  |  |     cmd_args += args | 
					
						
							|  |  |  |     Bundler.with_original_env do | 
					
						
							|  |  |  |       ENV["HOMEBREW_BREW_FILE"] = HOMEBREW_PREFIX/"bin/brew" | 
					
						
							|  |  |  |       ENV["HOMEBREW_INTEGRATION_TEST"] = cmd_id_from_args(args) | 
					
						
							|  |  |  |       ENV["HOMEBREW_TEST_TMPDIR"] = TEST_TMPDIR | 
					
						
							| 
									
										
										
										
											2016-09-27 13:02:07 +01:00
										 |  |  |       env.each_pair do |k, v| | 
					
						
							|  |  |  |         ENV[k] = v | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-09-26 19:55:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       read, write = IO.pipe | 
					
						
							|  |  |  |       begin | 
					
						
							|  |  |  |         pid = fork do | 
					
						
							|  |  |  |           read.close | 
					
						
							|  |  |  |           $stdout.reopen(write) | 
					
						
							|  |  |  |           $stderr.reopen(write) | 
					
						
							|  |  |  |           write.close | 
					
						
							|  |  |  |           exec RUBY_PATH, *cmd_args | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         write.close | 
					
						
							|  |  |  |         read.read.chomp | 
					
						
							|  |  |  |       ensure | 
					
						
							|  |  |  |         Process.wait(pid) | 
					
						
							|  |  |  |         read.close | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def cmd(*args) | 
					
						
							|  |  |  |     output = cmd_output(*args) | 
					
						
							|  |  |  |     status = $?.exitstatus | 
					
						
							| 
									
										
										
										
											2016-10-15 12:32:20 +01:00
										 |  |  |     puts "\n'brew #{args.join " "}' output: #{output}" if status.nonzero? | 
					
						
							| 
									
										
										
										
											2016-09-26 19:55:50 +02:00
										 |  |  |     assert_equal 0, status | 
					
						
							|  |  |  |     output | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def cmd_fail(*args) | 
					
						
							|  |  |  |     output = cmd_output(*args) | 
					
						
							|  |  |  |     status = $?.exitstatus | 
					
						
							| 
									
										
										
										
											2016-10-15 12:32:20 +01:00
										 |  |  |     $stderr.puts "\n'brew #{args.join " "}'" if status.zero? | 
					
						
							| 
									
										
										
										
											2016-09-26 19:55:50 +02:00
										 |  |  |     refute_equal 0, status | 
					
						
							|  |  |  |     output | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def setup_test_formula(name, content = nil) | 
					
						
							|  |  |  |     formula_path = CoreTap.new.formula_dir/"#{name}.rb" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case name | 
					
						
							|  |  |  |     when /^testball/ | 
					
						
							|  |  |  |       content = <<-EOS.undent
 | 
					
						
							|  |  |  |         desc "Some test" | 
					
						
							|  |  |  |         homepage "https://example.com/#{name}" | 
					
						
							| 
									
										
										
										
											2016-09-27 00:03:40 +02:00
										 |  |  |         url "file://#{File.expand_path("../..", __FILE__)}/tarballs/testball-0.1.tbz" | 
					
						
							| 
									
										
										
										
											2016-09-26 19:55:50 +02:00
										 |  |  |         sha256 "#{TESTBALL_SHA256}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         option "with-foo", "Build with foo" | 
					
						
							|  |  |  |         #{content} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def install | 
					
						
							|  |  |  |           (prefix/"foo"/"test").write("test") if build.with? "foo" | 
					
						
							|  |  |  |           prefix.install Dir["*"] | 
					
						
							|  |  |  |           (buildpath/"test.c").write \ | 
					
						
							|  |  |  |             "#include <stdio.h>\\nint main(){return printf(\\"test\\");}" | 
					
						
							|  |  |  |           bin.mkpath | 
					
						
							|  |  |  |           system ENV.cc, "test.c", "-o", bin/"test" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # something here | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |     when "foo" | 
					
						
							|  |  |  |       content = <<-EOS.undent
 | 
					
						
							|  |  |  |         url "https://example.com/#{name}-1.0" | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |     when "bar" | 
					
						
							|  |  |  |       content = <<-EOS.undent
 | 
					
						
							|  |  |  |         url "https://example.com/#{name}-1.0" | 
					
						
							|  |  |  |         depends_on "foo" | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     formula_path.write <<-EOS.undent
 | 
					
						
							|  |  |  |       class #{Formulary.class_s(name)} < Formula | 
					
						
							|  |  |  |         #{content} | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     EOS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     formula_path | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def setup_remote_tap(name) | 
					
						
							|  |  |  |     tap = Tap.fetch name | 
					
						
							|  |  |  |     tap.install(full_clone: false, quiet: true) unless tap.installed? | 
					
						
							|  |  |  |     tap | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def install_and_rename_coretap_formula(old_name, new_name) | 
					
						
							|  |  |  |     core_tap = CoreTap.new | 
					
						
							|  |  |  |     core_tap.path.cd do | 
					
						
							|  |  |  |       shutup do | 
					
						
							|  |  |  |         system "git", "init" | 
					
						
							|  |  |  |         system "git", "add", "--all" | 
					
						
							|  |  |  |         system "git", "commit", "-m", | 
					
						
							|  |  |  |           "#{old_name.capitalize} has not yet been renamed" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cmd("install", old_name) | 
					
						
							|  |  |  |     (core_tap.path/"Formula/#{old_name}.rb").unlink | 
					
						
							|  |  |  |     formula_renames = core_tap.path/"formula_renames.json" | 
					
						
							|  |  |  |     formula_renames.write Utils::JSON.dump(old_name => new_name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     core_tap.path.cd do | 
					
						
							|  |  |  |       shutup do | 
					
						
							|  |  |  |         system "git", "add", "--all" | 
					
						
							|  |  |  |         system "git", "commit", "-m", | 
					
						
							|  |  |  |           "#{old_name.capitalize} has been renamed to #{new_name.capitalize}" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def testball | 
					
						
							| 
									
										
										
										
											2016-09-27 00:03:40 +02:00
										 |  |  |     "#{File.expand_path("../..", __FILE__)}/testball.rb" | 
					
						
							| 
									
										
										
										
											2016-09-26 19:55:50 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | end |