aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test
diff options
context:
space:
mode:
authorMarkus Reiter2017-02-16 21:17:46 +0100
committerMarkus Reiter2017-02-19 18:41:53 +0100
commit05a98f24c4d21b3a3c7fcb4045e74619a4628f4c (patch)
tree4b48a58a7d392fc963681745c445a9cf2dc49e8f /Library/Homebrew/test
parent9f31d41fb263718bff98e194df411b8aaea72aba (diff)
downloadbrew-05a98f24c4d21b3a3c7fcb4045e74619a4628f4c.tar.bz2
Convert Utils test to spec.
Diffstat (limited to 'Library/Homebrew/test')
-rw-r--r--Library/Homebrew/test/utils/bottles/bintray_spec.rb18
-rw-r--r--Library/Homebrew/test/utils/popen_spec.rb28
-rw-r--r--Library/Homebrew/test/utils_spec.rb283
-rw-r--r--Library/Homebrew/test/utils_test.rb252
4 files changed, 329 insertions, 252 deletions
diff --git a/Library/Homebrew/test/utils/bottles/bintray_spec.rb b/Library/Homebrew/test/utils/bottles/bintray_spec.rb
new file mode 100644
index 000000000..a7dfc00ea
--- /dev/null
+++ b/Library/Homebrew/test/utils/bottles/bintray_spec.rb
@@ -0,0 +1,18 @@
+require "utils/bottles"
+
+describe Utils::Bottles::Bintray do
+ describe "::package" do
+ it "converts a Formula name to a package name" do
+ expect(described_class.package("openssl@1.1")).to eq("openssl:1.1")
+ expect(described_class.package("gtk+")).to eq("gtkx")
+ expect(described_class.package("llvm")).to eq("llvm")
+ end
+ end
+
+ describe "::repository" do
+ it "returns the repository for a given Tap" do
+ expect(described_class.repository(Tap.new("homebrew", "bintray-test")))
+ .to eq("bottles-bintray-test")
+ end
+ end
+end
diff --git a/Library/Homebrew/test/utils/popen_spec.rb b/Library/Homebrew/test/utils/popen_spec.rb
new file mode 100644
index 000000000..e3704a876
--- /dev/null
+++ b/Library/Homebrew/test/utils/popen_spec.rb
@@ -0,0 +1,28 @@
+require "utils/popen"
+
+describe Utils do
+ describe "::popen_read" do
+ it "reads the standard output of a given command" do
+ expect(subject.popen_read("sh", "-c", "echo success").chomp).to eq("success")
+ expect($?).to be_a_success
+ end
+
+ it "can be given a block to manually read from the pipe" do
+ expect(
+ subject.popen_read("sh", "-c", "echo success") do |pipe|
+ pipe.read.chomp
+ end,
+ ).to eq("success")
+ expect($?).to be_a_success
+ end
+ end
+
+ describe "::popen_write" do
+ it "with supports writing to a command's standard input" do
+ subject.popen_write("grep", "-q", "success") do |pipe|
+ pipe.write("success\n")
+ end
+ expect($?).to be_a_success
+ end
+ end
+end
diff --git a/Library/Homebrew/test/utils_spec.rb b/Library/Homebrew/test/utils_spec.rb
new file mode 100644
index 000000000..de6b287d2
--- /dev/null
+++ b/Library/Homebrew/test/utils_spec.rb
@@ -0,0 +1,283 @@
+require "utils"
+
+RSpec::Matchers.alias_matcher :have_failed, :be_failed
+
+describe "globally-scoped helper methods" do
+ let(:dir) { @dir = Pathname.new(Dir.mktmpdir) }
+
+ after(:each) { dir.rmtree unless @dir.nil? }
+
+ def esc(code)
+ /(\e\[\d+m)*\e\[#{code}m/
+ end
+
+ describe "#ofail" do
+ it "sets Homebrew.failed to true" do
+ begin
+ shutup do
+ ofail "foo"
+ end
+
+ expect(Homebrew).to have_failed
+ ensure
+ Homebrew.failed = false
+ end
+ end
+ end
+
+ describe "#odie" do
+ it "exits with 1" do
+ expect(self).to receive(:exit).and_return(1)
+ expect {
+ odie "foo"
+ }.to output("Error: foo\n").to_stderr
+ end
+ end
+
+ describe "#pretty_installed" do
+ subject { pretty_installed("foo") }
+
+ context "when $stdout is a TTY" do
+ before(:each) { allow($stdout).to receive(:tty?).and_return(true) }
+
+ context "with HOMEBREW_NO_EMOJI unset" do
+ before(:each) { ENV.delete("HOMEBREW_NO_EMOJI") }
+
+ it "returns a string with a colored checkmark" do
+ expect(subject)
+ .to match(/#{esc 1}foo #{esc 32}✔#{esc 0}/)
+ end
+ end
+
+ context "with HOMEBREW_NO_EMOJI set" do
+ before(:each) { ENV["HOMEBREW_NO_EMOJI"] = "1" }
+
+ it "returns a string with colored info" do
+ expect(subject)
+ .to match(/#{esc 1}foo \(installed\)#{esc 0}/)
+ end
+ end
+ end
+
+ context "when $stdout is not a TTY" do
+ before(:each) { allow($stdout).to receive(:tty?).and_return(false) }
+
+ it "returns plain text" do
+ expect(subject).to eq("foo")
+ end
+ end
+ end
+
+ describe "#pretty_uninstalled" do
+ subject { pretty_uninstalled("foo") }
+
+ context "when $stdout is a TTY" do
+ before(:each) { allow($stdout).to receive(:tty?).and_return(true) }
+
+ context "with HOMEBREW_NO_EMOJI unset" do
+ before(:each) { ENV.delete("HOMEBREW_NO_EMOJI") }
+
+ it "returns a string with a colored checkmark" do
+ expect(subject)
+ .to match(/#{esc 1}foo #{esc 31}✘#{esc 0}/)
+ end
+ end
+
+ context "with HOMEBREW_NO_EMOJI set" do
+ before(:each) { ENV["HOMEBREW_NO_EMOJI"] = "1" }
+
+ it "returns a string with colored info" do
+ expect(subject)
+ .to match(/#{esc 1}foo \(uninstalled\)#{esc 0}/)
+ end
+ end
+ end
+
+ context "when $stdout is not a TTY" do
+ before(:each) { allow($stdout).to receive(:tty?).and_return(false) }
+
+ it "returns plain text" do
+ expect(subject).to eq("foo")
+ end
+ end
+ end
+
+ describe "#interactive_shell" do
+ let(:shell) { dir/"myshell" }
+
+ it "starts an interactive shell session" do
+ IO.write shell, <<-EOS.undent
+ #!/bin/sh
+ echo called > "#{dir}/called"
+ EOS
+
+ FileUtils.chmod 0755, shell
+
+ ENV["SHELL"] = shell
+
+ expect { interactive_shell }.not_to raise_error
+ expect(dir/"called").to exist
+ end
+ end
+
+ describe "#with_custom_locale" do
+ it "temporarily overrides the system locale" do
+ ENV["LC_ALL"] = "en_US.UTF-8"
+
+ with_custom_locale("C") do
+ expect(ENV["LC_ALL"]).to eq("C")
+ end
+
+ expect(ENV["LC_ALL"]).to eq("en_US.UTF-8")
+ end
+ end
+
+ describe "#run_as_not_developer" do
+ it "temporarily unsets HOMEBREW_DEVELOPER" do
+ ENV["HOMEBREW_DEVELOPER"] = "foo"
+
+ run_as_not_developer do
+ expect(ENV["HOMEBREW_DEVELOPER"]).to be nil
+ end
+
+ expect(ENV["HOMEBREW_DEVELOPER"]).to eq("foo")
+ end
+ end
+
+ describe "#which" do
+ let(:cmd) { dir/"foo" }
+
+ before(:each) { FileUtils.touch cmd }
+
+ it "returns the first executable that is found" do
+ cmd.chmod 0744
+ expect(which(File.basename(cmd), File.dirname(cmd))).to eq(cmd)
+ end
+
+ it "skips non-executables" do
+ expect(which(File.basename(cmd), File.dirname(cmd))).to be nil
+ end
+
+ it "skips malformed path and doesn't fail" do
+ # 'which' should not fail if a path is malformed
+ # see https://github.com/Homebrew/legacy-homebrew/issues/32789 for an example
+ cmd.chmod 0744
+
+ # ~~ will fail because ~foo resolves to foo's home and there is no '~' user
+ path = ["~~", File.dirname(cmd)].join(File::PATH_SEPARATOR)
+ expect(which(File.basename(cmd), path)).to eq(cmd)
+ end
+ end
+
+ describe "#which_all" do
+ let(:cmd1) { dir/"foo" }
+ let(:cmd2) { dir/"bar/foo" }
+ let(:cmd3) { dir/"bar/baz/foo" }
+
+ before(:each) do
+ (dir/"bar/baz").mkpath
+
+ FileUtils.touch cmd2
+
+ [cmd1, cmd3].each do |cmd|
+ FileUtils.touch cmd
+ cmd.chmod 0744
+ end
+ end
+
+ it "returns an array of all executables that are found" do
+ path = [
+ "#{dir}/bar/baz",
+ "#{dir}/baz:#{dir}",
+ "~baduserpath",
+ ].join(File::PATH_SEPARATOR)
+ expect(which_all("foo", path)).to eq([cmd3, cmd1])
+ end
+ end
+
+ specify "#which_editor" do
+ ENV["HOMEBREW_EDITOR"] = "vemate"
+ expect(which_editor).to eq("vemate")
+ end
+
+ specify "#gzip" do
+ Dir.mktmpdir do |path|
+ path = Pathname.new(path)
+ somefile = path/"somefile"
+ FileUtils.touch somefile
+ expect(gzip(somefile)[0].to_s).to eq("#{somefile}.gz")
+ expect(Pathname.new("#{somefile}.gz")).to exist
+ end
+ end
+
+ specify "#capture_stderr" do
+ err = capture_stderr do
+ $stderr.print "test"
+ end
+
+ expect(err).to eq("test")
+ end
+
+ describe "#pretty_duration" do
+ it "converts seconds to a human-readable string" do
+ expect(pretty_duration(1)).to eq("1 second")
+ expect(pretty_duration(2.5)).to eq("2 seconds")
+ expect(pretty_duration(42)).to eq("42 seconds")
+ expect(pretty_duration(240)).to eq("4 minutes")
+ expect(pretty_duration(252.45)).to eq("4 minutes 12 seconds")
+ end
+ end
+
+ specify "#plural" do
+ expect(plural(1)).to eq("")
+ expect(plural(0)).to eq("s")
+ expect(plural(42)).to eq("s")
+ expect(plural(42, "")).to eq("")
+ end
+
+ specify "#disk_usage_readable" do
+ expect(disk_usage_readable(1)).to eq("1B")
+ expect(disk_usage_readable(1000)).to eq("1000B")
+ expect(disk_usage_readable(1024)).to eq("1K")
+ expect(disk_usage_readable(1025)).to eq("1K")
+ expect(disk_usage_readable(4_404_020)).to eq("4.2M")
+ expect(disk_usage_readable(4_509_715_660)).to eq("4.2G")
+ end
+
+ describe "#number_readable" do
+ it "returns a string with thousands separators" do
+ expect(number_readable(1)).to eq("1")
+ expect(number_readable(1_000)).to eq("1,000")
+ expect(number_readable(1_000_000)).to eq("1,000,000")
+ end
+ end
+
+ specify "#truncate_text_to_approximate_size" do
+ 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)
+ expect(s.length).to eq(n)
+ expect(s).to match(/^x+#{Regexp.escape(glue)}x+$/)
+
+ s = truncate_text_to_approximate_size(long_s, n, front_weight: 0.0)
+ expect(s).to eq(glue + ("x" * (n - glue.length)))
+
+ s = truncate_text_to_approximate_size(long_s, n, front_weight: 1.0)
+ expect(s).to eq(("x" * (n - glue.length)) + glue)
+ end
+
+ describe "#odeprecated" do
+ it "raises a MethodDeprecatedError" do
+ ENV.delete("HOMEBREW_DEVELOPER")
+ expect {
+ odeprecated(
+ "method", "replacement",
+ caller: ["#{HOMEBREW_LIBRARY}/Taps/homebrew/homebrew-core/"],
+ disable: true
+ )
+ }.to raise_error(MethodDeprecatedError, %r{method.*replacement.*homebrew/homebrew-core.*homebrew/core}m)
+ end
+ end
+end
diff --git a/Library/Homebrew/test/utils_test.rb b/Library/Homebrew/test/utils_test.rb
deleted file mode 100644
index 1f2fb7b55..000000000
--- a/Library/Homebrew/test/utils_test.rb
+++ /dev/null
@@ -1,252 +0,0 @@
-require "testing_env"
-require "utils"
-require "tempfile"
-require "utils/shell"
-
-class UtilTests < Homebrew::TestCase
- def setup
- super
- @dir = Pathname.new(mktmpdir)
- end
-
- def esc(code)
- /(\e\[\d+m)*\e\[#{code}m/
- 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 true
- ENV.delete("HOMEBREW_NO_EMOJI")
- tty_with_emoji_output = /\A#{esc 1}foo #{esc 32}✔#{esc 0}\Z/
- assert_match tty_with_emoji_output, pretty_installed("foo")
-
- ENV["HOMEBREW_NO_EMOJI"] = "1"
- tty_no_emoji_output = /\A#{esc 1}foo \(installed\)#{esc 0}\Z/
- assert_match tty_no_emoji_output, pretty_installed("foo")
-
- $stdout.stubs(:tty?).returns false
- assert_equal "foo", pretty_installed("foo")
- end
-
- def test_pretty_uninstalled
- $stdout.stubs(:tty?).returns true
- ENV.delete("HOMEBREW_NO_EMOJI")
- tty_with_emoji_output = /\A#{esc 1}foo #{esc 31}✘#{esc 0}\Z/
- assert_match tty_with_emoji_output, pretty_uninstalled("foo")
-
- ENV["HOMEBREW_NO_EMOJI"] = "1"
- tty_no_emoji_output = /\A#{esc 1}foo \(uninstalled\)#{esc 0}\Z/
- assert_match tty_no_emoji_output, pretty_uninstalled("foo")
-
- $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
-
- 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
-
- 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"]
- end
-
- def test_put_columns_empty
- out, err = capture_io do
- puts Formatter.columns([])
- end
-
- assert_equal out, "\n"
- assert_equal err, ""
- end
-
- 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
- # see https://github.com/Homebrew/legacy-homebrew/issues/32789 for an example
- 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
-
- 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
-
- 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_capture_stderr
- assert_equal "test\n", capture_stderr { $stderr.puts "test" }
- end
-
- def test_shell_profile
- ENV["SHELL"] = "/bin/sh"
- assert_equal "~/.bash_profile", Utils::Shell.shell_profile
- ENV["SHELL"] = "/bin/bash"
- assert_equal "~/.bash_profile", Utils::Shell.shell_profile
- ENV["SHELL"] = "/bin/another_shell"
- assert_equal "~/.bash_profile", Utils::Shell.shell_profile
- ENV["SHELL"] = "/bin/zsh"
- assert_equal "~/.zshrc", Utils::Shell.shell_profile
- ENV["SHELL"] = "/bin/ksh"
- assert_equal "~/.kshrc", Utils::Shell.shell_profile
- end
-
- def test_popen_read
- out = Utils.popen_read("sh", "-c", "echo success").chomp
- assert_equal "success", out
- assert_predicate $?, :success?
- end
-
- def test_popen_read_with_block
- out = Utils.popen_read("sh", "-c", "echo success") do |pipe|
- pipe.read.chomp
- end
- assert_equal "success", out
- assert_predicate $?, :success?
- end
-
- def test_popen_write_with_block
- Utils.popen_write("grep", "-q", "success") do |pipe|
- pipe.write("success\n")
- end
- assert_predicate $?, :success?
- end
-
- def test_pretty_duration
- assert_equal "1 second", pretty_duration(1)
- assert_equal "2 seconds", pretty_duration(2.5)
- assert_equal "42 seconds", pretty_duration(42)
- assert_equal "4 minutes", pretty_duration(240)
- assert_equal "4 minutes 12 seconds", pretty_duration(252.45)
- end
-
- def test_plural
- assert_equal "", plural(1)
- assert_equal "s", plural(0)
- assert_equal "s", plural(42)
- assert_equal "", plural(42, "")
- end
-
- 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
-
- 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)
- s = truncate_text_to_approximate_size(long_s, n, front_weight: 0.0)
- assert_equal glue + ("x" * (n - glue.length)), s
- s = truncate_text_to_approximate_size(long_s, n, front_weight: 1.0)
- assert_equal(("x" * (n - glue.length)) + glue, s)
- end
-
- def test_odeprecated
- ENV.delete("HOMEBREW_DEVELOPER")
- e = assert_raises(MethodDeprecatedError) do
- odeprecated("method", "replacement",
- caller: ["#{HOMEBREW_LIBRARY}/Taps/homebrew/homebrew-core/"],
- disable: true)
- 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
-
- 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
-end