| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "testing_env" | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  | require "utils" | 
					
						
							| 
									
										
										
										
											2015-08-14 23:23:52 +02:00
										 |  |  | require "tempfile" | 
					
						
							| 
									
										
										
										
											2016-08-10 23:19:09 -07:00
										 |  |  | require "utils/shell" | 
					
						
							| 
									
										
										
										
											2010-03-08 22:10:12 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  | class TtyTests < Homebrew::TestCase | 
					
						
							|  |  |  |   def test_strip_ansi | 
					
						
							|  |  |  |     assert_equal "hello", | 
					
						
							|  |  |  |       Tty.strip_ansi("\033\[36;7mhello\033\[0m") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_truncate | 
					
						
							| 
									
										
										
										
											2016-05-03 15:56:47 +01:00
										 |  |  |     Tty.stubs(:width).returns 15
 | 
					
						
							|  |  |  |     assert_equal "foobar some", Tty.truncate("foobar something very long") | 
					
						
							|  |  |  |     assert_equal "truncate", Tty.truncate("truncate") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # When the terminal is unsupported, we report 0 width | 
					
						
							|  |  |  |     Tty.stubs(:width).returns 0
 | 
					
						
							|  |  |  |     assert_equal "foobar something very long", Tty.truncate("foobar something very long") | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_no_tty_formatting | 
					
						
							|  |  |  |     $stdout.stubs(:tty?).returns false | 
					
						
							|  |  |  |     assert_nil Tty.blue | 
					
						
							|  |  |  |     assert_nil Tty.white | 
					
						
							|  |  |  |     assert_nil Tty.red | 
					
						
							|  |  |  |     assert_nil Tty.green | 
					
						
							|  |  |  |     assert_nil Tty.gray | 
					
						
							|  |  |  |     assert_nil Tty.yellow | 
					
						
							|  |  |  |     assert_nil Tty.reset | 
					
						
							|  |  |  |     assert_nil Tty.em | 
					
						
							|  |  |  |     assert_nil Tty.highlight | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 20:32:51 -05:00
										 |  |  | class UtilTests < Homebrew::TestCase | 
					
						
							| 
									
										
										
										
											2015-08-14 23:23:52 +02:00
										 |  |  |   def setup | 
					
						
							|  |  |  |     @dir = Pathname.new(mktmpdir) | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  |     @env = ENV.to_hash | 
					
						
							| 
									
										
										
										
											2015-08-14 23:23:52 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def teardown | 
					
						
							|  |  |  |     @dir.rmtree | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  |     ENV.replace @env | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_ofail | 
					
						
							|  |  |  |     shutup { ofail "foo" } | 
					
						
							|  |  |  |     assert Homebrew.failed | 
					
						
							|  |  |  |   ensure | 
					
						
							|  |  |  |     Homebrew.failed = false | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_odie | 
					
						
							|  |  |  |     expects(:exit).returns 1
 | 
					
						
							|  |  |  |     shutup { odie "foo" } | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_pretty_installed | 
					
						
							|  |  |  |     $stdout.stubs(:tty?).returns false | 
					
						
							|  |  |  |     assert_equal "foo", pretty_installed("foo") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_pretty_uninstalled | 
					
						
							|  |  |  |     $stdout.stubs(:tty?).returns false | 
					
						
							|  |  |  |     assert_equal "foo", pretty_uninstalled("foo") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_interactive_shell | 
					
						
							|  |  |  |     mktmpdir do |path| | 
					
						
							|  |  |  |       shell = "#{path}/myshell" | 
					
						
							|  |  |  |       File.open(shell, "w") do |file| | 
					
						
							|  |  |  |         file.write "#!/bin/sh\necho called > #{path}/called\n" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       FileUtils.chmod 0755, shell | 
					
						
							|  |  |  |       ENV["SHELL"] = shell | 
					
						
							|  |  |  |       assert_nothing_raised { interactive_shell } | 
					
						
							|  |  |  |       assert File.exist? "#{path}/called" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-01 04:21:08 +01:00
										 |  |  |   def test_with_custom_locale | 
					
						
							|  |  |  |     ENV["LC_ALL"] = "en_US.UTF-8" | 
					
						
							|  |  |  |     with_custom_locale("C") do | 
					
						
							|  |  |  |       assert_equal "C", ENV["LC_ALL"] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     assert_equal "en_US.UTF-8", ENV["LC_ALL"] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  |   def test_run_as_not_developer | 
					
						
							|  |  |  |     ENV["HOMEBREW_DEVELOPER"] = "foo" | 
					
						
							|  |  |  |     run_as_not_developer do | 
					
						
							|  |  |  |       assert_nil ENV["HOMEBREW_DEVELOPER"] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     assert_equal "foo", ENV["HOMEBREW_DEVELOPER"] | 
					
						
							| 
									
										
										
										
											2015-08-14 23:23:52 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-08 22:10:12 -08:00
										 |  |  |   def test_put_columns_empty | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  |     # Issue #217 put columns with no results fails. | 
					
						
							| 
									
										
										
										
											2014-06-12 22:05:45 -05:00
										 |  |  |     assert_silent { puts_columns [] } | 
					
						
							| 
									
										
										
										
											2010-03-08 22:10:12 -08:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 23:23:52 +02:00
										 |  |  |   def test_which | 
					
						
							|  |  |  |     cmd = @dir/"foo" | 
					
						
							|  |  |  |     FileUtils.touch cmd | 
					
						
							|  |  |  |     cmd.chmod 0744
 | 
					
						
							|  |  |  |     assert_equal Pathname.new(cmd), | 
					
						
							|  |  |  |       which(File.basename(cmd), File.dirname(cmd)) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_which_skip_non_executables | 
					
						
							|  |  |  |     cmd = @dir/"foo" | 
					
						
							|  |  |  |     FileUtils.touch cmd | 
					
						
							|  |  |  |     assert_nil which(File.basename(cmd), File.dirname(cmd)) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_which_skip_malformed_path | 
					
						
							|  |  |  |     # 'which' should not fail if a path is malformed | 
					
						
							| 
									
										
										
										
											2016-05-01 14:55:45 +02:00
										 |  |  |     # see https://github.com/Homebrew/legacy-homebrew/issues/32789 for an example | 
					
						
							| 
									
										
										
										
											2015-08-14 23:23:52 +02:00
										 |  |  |     cmd = @dir/"foo" | 
					
						
							|  |  |  |     FileUtils.touch cmd | 
					
						
							|  |  |  |     cmd.chmod 0744
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # ~~ will fail because ~foo resolves to foo's home and there is no '~' user | 
					
						
							|  |  |  |     # here | 
					
						
							|  |  |  |     assert_equal Pathname.new(cmd), | 
					
						
							|  |  |  |       which(File.basename(cmd), "~~#{File::PATH_SEPARATOR}#{File.dirname(cmd)}") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-02 23:16:08 +08:00
										 |  |  |   def test_which_all | 
					
						
							|  |  |  |     (@dir/"bar/baz").mkpath | 
					
						
							|  |  |  |     cmd1 = @dir/"foo" | 
					
						
							|  |  |  |     cmd2 = @dir/"bar/foo" | 
					
						
							|  |  |  |     cmd3 = @dir/"bar/baz/foo" | 
					
						
							|  |  |  |     FileUtils.touch cmd2 | 
					
						
							|  |  |  |     [cmd1, cmd3].each do |cmd| | 
					
						
							|  |  |  |       FileUtils.touch cmd | 
					
						
							|  |  |  |       cmd.chmod 0744
 | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     assert_equal [cmd3, cmd1], | 
					
						
							|  |  |  |       which_all("foo", "#{@dir}/bar/baz:#{@dir}/baz:#{@dir}:~baduserpath") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  |   def test_which_editor | 
					
						
							|  |  |  |     ENV["HOMEBREW_EDITOR"] = "vemate" | 
					
						
							|  |  |  |     assert_equal "vemate", which_editor | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_gzip | 
					
						
							|  |  |  |     mktmpdir do |path| | 
					
						
							|  |  |  |       somefile = "#{path}/somefile" | 
					
						
							|  |  |  |       FileUtils.touch somefile | 
					
						
							|  |  |  |       assert_equal "#{somefile}.gz", | 
					
						
							|  |  |  |         gzip(somefile)[0].to_s | 
					
						
							|  |  |  |       assert File.exist?("#{somefile}.gz") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_shell_profile | 
					
						
							|  |  |  |     ENV["SHELL"] = "/bin/sh" | 
					
						
							| 
									
										
										
										
											2016-08-10 23:19:09 -07:00
										 |  |  |     assert_equal "~/.bash_profile", Utils::Shell.shell_profile | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  |     ENV["SHELL"] = "/bin/bash" | 
					
						
							| 
									
										
										
										
											2016-08-10 23:19:09 -07:00
										 |  |  |     assert_equal "~/.bash_profile", Utils::Shell.shell_profile | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  |     ENV["SHELL"] = "/bin/another_shell" | 
					
						
							| 
									
										
										
										
											2016-08-10 23:19:09 -07:00
										 |  |  |     assert_equal "~/.bash_profile", Utils::Shell.shell_profile | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  |     ENV["SHELL"] = "/bin/zsh" | 
					
						
							| 
									
										
										
										
											2016-08-10 23:19:09 -07:00
										 |  |  |     assert_equal "~/.zshrc", Utils::Shell.shell_profile | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  |     ENV["SHELL"] = "/bin/ksh" | 
					
						
							| 
									
										
										
										
											2016-08-10 23:19:09 -07:00
										 |  |  |     assert_equal "~/.kshrc", Utils::Shell.shell_profile | 
					
						
							| 
									
										
										
										
											2016-01-08 18:27:03 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |   def test_popen_read | 
					
						
							| 
									
										
										
										
											2016-07-27 21:50:43 -06:00
										 |  |  |     out = Utils.popen_read("sh", "-c", "echo success").chomp | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |     assert_equal "success", out | 
					
						
							|  |  |  |     assert_predicate $?, :success? | 
					
						
							| 
									
										
										
										
											2016-01-04 15:15:56 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_popen_read_with_block | 
					
						
							| 
									
										
										
										
											2016-07-27 21:50:43 -06:00
										 |  |  |     out = Utils.popen_read("sh", "-c", "echo success") do |pipe| | 
					
						
							| 
									
										
										
										
											2016-01-04 15:15:56 +01:00
										 |  |  |       pipe.read.chomp | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     assert_equal "success", out | 
					
						
							|  |  |  |     assert_predicate $?, :success? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_popen_write_with_block | 
					
						
							| 
									
										
										
										
											2016-07-27 21:50:43 -06:00
										 |  |  |     Utils.popen_write("grep", "-q", "success") do |pipe| | 
					
						
							| 
									
										
										
										
											2016-01-04 15:15:56 +01:00
										 |  |  |       pipe.write("success\n") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     assert_predicate $?, :success? | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2015-12-14 15:01:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-14 14:57:17 +01:00
										 |  |  |   def test_pretty_duration | 
					
						
							| 
									
										
										
										
											2015-10-07 22:48:42 +02:00
										 |  |  |     assert_equal "1 second", pretty_duration(1) | 
					
						
							| 
									
										
										
										
											2015-12-14 14:57:17 +01:00
										 |  |  |     assert_equal "2 seconds", pretty_duration(2.5) | 
					
						
							|  |  |  |     assert_equal "42 seconds", pretty_duration(42) | 
					
						
							| 
									
										
										
										
											2015-10-07 22:48:42 +02:00
										 |  |  |     assert_equal "4 minutes", pretty_duration(240) | 
					
						
							|  |  |  |     assert_equal "4 minutes 12 seconds", pretty_duration(252.45) | 
					
						
							| 
									
										
										
										
											2015-12-14 14:57:17 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-14 15:01:31 +01:00
										 |  |  |   def test_plural | 
					
						
							|  |  |  |     assert_equal "", plural(1) | 
					
						
							|  |  |  |     assert_equal "s", plural(0) | 
					
						
							|  |  |  |     assert_equal "s", plural(42) | 
					
						
							|  |  |  |     assert_equal "", plural(42, "") | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2015-12-30 17:05:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def test_disk_usage_readable | 
					
						
							|  |  |  |     assert_equal "1B", disk_usage_readable(1) | 
					
						
							|  |  |  |     assert_equal "1000B", disk_usage_readable(1000) | 
					
						
							|  |  |  |     assert_equal "1K", disk_usage_readable(1024) | 
					
						
							|  |  |  |     assert_equal "1K", disk_usage_readable(1025) | 
					
						
							|  |  |  |     assert_equal "4.2M", disk_usage_readable(4_404_020) | 
					
						
							|  |  |  |     assert_equal "4.2G", disk_usage_readable(4_509_715_660) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_number_readable | 
					
						
							|  |  |  |     assert_equal "1", number_readable(1) | 
					
						
							|  |  |  |     assert_equal "1,000", number_readable(1_000) | 
					
						
							|  |  |  |     assert_equal "1,000,000", number_readable(1_000_000) | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-07-05 08:45:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def test_truncate_text_to_approximate_size | 
					
						
							|  |  |  |     glue = "\n[...snip...]\n" # hard-coded copy from truncate_text_to_approximate_size | 
					
						
							|  |  |  |     n = 20
 | 
					
						
							|  |  |  |     long_s = "x" * 40
 | 
					
						
							|  |  |  |     s = truncate_text_to_approximate_size(long_s, n) | 
					
						
							|  |  |  |     assert_equal n, s.length | 
					
						
							|  |  |  |     assert_match(/^x+#{Regexp.escape(glue)}x+$/, s) | 
					
						
							| 
									
										
										
										
											2016-09-17 15:32:44 +01:00
										 |  |  |     s = truncate_text_to_approximate_size(long_s, n, front_weight: 0.0) | 
					
						
							| 
									
										
										
										
											2016-07-05 08:45:17 -04:00
										 |  |  |     assert_equal glue + ("x" * (n - glue.length)), s | 
					
						
							| 
									
										
										
										
											2016-09-17 15:32:44 +01:00
										 |  |  |     s = truncate_text_to_approximate_size(long_s, n, front_weight: 1.0) | 
					
						
							| 
									
										
										
										
											2016-07-05 08:45:17 -04:00
										 |  |  |     assert_equal(("x" * (n - glue.length)) + glue, s) | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-07-31 19:12:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def test_odeprecated | 
					
						
							|  |  |  |     ARGV.stubs(:homebrew_developer?).returns false | 
					
						
							|  |  |  |     e = assert_raises(FormulaMethodDeprecatedError) do | 
					
						
							|  |  |  |       odeprecated("method", "replacement", | 
					
						
							| 
									
										
										
										
											2016-09-17 15:32:44 +01:00
										 |  |  |         caller: ["#{HOMEBREW_LIBRARY}/Taps/homebrew/homebrew-core/"], | 
					
						
							|  |  |  |         die: true) | 
					
						
							| 
									
										
										
										
											2016-07-31 19:12:04 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |     assert_match "method", e.message | 
					
						
							|  |  |  |     assert_match "replacement", e.message | 
					
						
							|  |  |  |     assert_match "homebrew/homebrew-core", e.message | 
					
						
							|  |  |  |     assert_match "homebrew/core", e.message | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-08-29 19:47:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def test_bottles_bintray | 
					
						
							|  |  |  |     assert_equal "openssl:1.1", Utils::Bottles::Bintray.package("openssl@1.1") | 
					
						
							|  |  |  |     assert_equal "gtkx", Utils::Bottles::Bintray.package("gtk+") | 
					
						
							|  |  |  |     assert_equal "llvm", Utils::Bottles::Bintray.package("llvm") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     tap = Tap.new("homebrew", "bintray-test") | 
					
						
							|  |  |  |     assert_equal "bottles-bintray-test", Utils::Bottles::Bintray.repository(tap) | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-03-08 22:10:12 -08:00
										 |  |  | end |