diff options
| author | Mike McQuaid | 2017-02-26 20:48:36 +0000 |
|---|---|---|
| committer | Mike McQuaid | 2017-02-26 20:48:36 +0000 |
| commit | c2a460ec6d857ba33c89174d8d93fcaa403c3717 (patch) | |
| tree | 089045fec4c46fb74e3b48868e2b335497df7b84 /Library/Homebrew/test/utils | |
| parent | d3ae1cc264dc9eb9b602dd6aa21c4282dc049c79 (diff) | |
| parent | ff93e1624b214c9b48731174a9135789fc3695a8 (diff) | |
| download | brew-c2a460ec6d857ba33c89174d8d93fcaa403c3717.tar.bz2 | |
Merge branch 'master' into filter_all_env_vars_932
Diffstat (limited to 'Library/Homebrew/test/utils')
| -rw-r--r-- | Library/Homebrew/test/utils/bottles/bintray_spec.rb | 18 | ||||
| -rw-r--r-- | Library/Homebrew/test/utils/bottles/bottles_spec.rb | 80 | ||||
| -rw-r--r-- | Library/Homebrew/test/utils/bottles/collector_spec.rb | 45 | ||||
| -rw-r--r-- | Library/Homebrew/test/utils/popen_spec.rb | 28 | ||||
| -rw-r--r-- | Library/Homebrew/test/utils/shell_spec.rb | 93 | ||||
| -rw-r--r-- | Library/Homebrew/test/utils/tty_spec.rb | 67 | ||||
| -rw-r--r-- | Library/Homebrew/test/utils/tty_test.rb | 46 |
7 files changed, 331 insertions, 46 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/bottles/bottles_spec.rb b/Library/Homebrew/test/utils/bottles/bottles_spec.rb new file mode 100644 index 000000000..8b54b0b34 --- /dev/null +++ b/Library/Homebrew/test/utils/bottles/bottles_spec.rb @@ -0,0 +1,80 @@ +require "utils/bottles" + +describe Utils::Bottles do + describe "#tag", :needs_macos do + it "returns :tiger_foo on Tiger PowerPC" do + allow(MacOS).to receive(:version).and_return(MacOS::Version.new("10.4")) + allow(Hardware::CPU).to receive(:type).and_return(:ppc) + allow(Hardware::CPU).to receive(:family).and_return(:foo) + allow(MacOS).to receive(:prefer_64_bit?).and_return(false) + expect(described_class.tag).to eq(:tiger_foo) + end + + it "returns :tiger on Tiger Intel" do + allow(MacOS).to receive(:version).and_return(MacOS::Version.new("10.4")) + allow(Hardware::CPU).to receive(:type).and_return(:intel) + allow(MacOS).to receive(:prefer_64_bit?).and_return(false) + expect(described_class.tag).to eq(:tiger) + end + + it "returns :tiger_g5_64 on Tiger PowerPC 64-bit" do + allow(MacOS).to receive(:version).and_return(MacOS::Version.new("10.4")) + allow(Hardware::CPU).to receive(:type).and_return(:ppc) + allow(Hardware::CPU).to receive(:family).and_return(:g5) + allow(MacOS).to receive(:prefer_64_bit?).and_return(true) + expect(described_class.tag).to eq(:tiger_g5_64) + end + + # Note that this will probably never be used + it "returns :tiger_64 on Tiger Intel 64-bit" do + allow(MacOS).to receive(:version).and_return(MacOS::Version.new("10.4")) + allow(Hardware::CPU).to receive(:type).and_return(:intel) + allow(MacOS).to receive(:prefer_64_bit?).and_return(true) + expect(described_class.tag).to eq(:tiger_64) + end + + it "returns :leopard on Leopard Intel" do + allow(MacOS).to receive(:version).and_return(MacOS::Version.new("10.5")) + allow(Hardware::CPU).to receive(:type).and_return(:intel) + allow(MacOS).to receive(:prefer_64_bit?).and_return(false) + expect(described_class.tag).to eq(:leopard) + end + + it "returns :leopard_g5_64 on Leopard PowerPC 64-bit" do + allow(MacOS).to receive(:version).and_return(MacOS::Version.new("10.5")) + allow(Hardware::CPU).to receive(:type).and_return(:ppc) + allow(Hardware::CPU).to receive(:family).and_return(:g5) + allow(MacOS).to receive(:prefer_64_bit?).and_return(true) + expect(described_class.tag).to eq(:leopard_g5_64) + end + + it "returns :leopard_64 on Leopard Intel 64-bit" do + allow(MacOS).to receive(:version).and_return(MacOS::Version.new("10.5")) + allow(Hardware::CPU).to receive(:type).and_return(:intel) + allow(MacOS).to receive(:prefer_64_bit?).and_return(true) + expect(described_class.tag).to eq(:leopard_64) + end + + it "returns :snow_leopard_32 on Snow Leopard 32-bit" do + allow(MacOS).to receive(:version).and_return(MacOS::Version.new("10.6")) + allow(Hardware::CPU).to receive(:is_64_bit?).and_return(false) + expect(described_class.tag).to eq(:snow_leopard_32) + end + + it "returns :snow_leopard on Snow Leopard 64-bit" do + allow(MacOS).to receive(:version).and_return(MacOS::Version.new("10.6")) + allow(Hardware::CPU).to receive(:is_64_bit?).and_return(true) + expect(described_class.tag).to eq(:snow_leopard) + end + + it "returns :lion on Lion" do + allow(MacOS).to receive(:version).and_return(MacOS::Version.new("10.7")) + expect(described_class.tag).to eq(:lion) + end + + it "returns :mountain_lion on Mountain Lion" do + allow(MacOS).to receive(:version).and_return(MacOS::Version.new("10.8")) + expect(described_class.tag).to eq(:mountain_lion) + end + end +end diff --git a/Library/Homebrew/test/utils/bottles/collector_spec.rb b/Library/Homebrew/test/utils/bottles/collector_spec.rb new file mode 100644 index 000000000..08484e545 --- /dev/null +++ b/Library/Homebrew/test/utils/bottles/collector_spec.rb @@ -0,0 +1,45 @@ +require "utils/bottles" + +describe Utils::Bottles::Collector do + describe "#fetch_checksum_for" do + it "returns passed tags" do + subject[:lion] = "foo" + subject[:mountain_lion] = "bar" + expect(subject.fetch_checksum_for(:mountain_lion)).to eq(["bar", :mountain_lion]) + end + + it "returns nil if empty" do + expect(subject.fetch_checksum_for(:foo)).to be nil + end + + it "returns nil when there is no match" do + subject[:lion] = "foo" + expect(subject.fetch_checksum_for(:foo)).to be nil + end + + it "returns nil when there is no match and later tag is present" do + subject[:lion_or_later] = "foo" + expect(subject.fetch_checksum_for(:foo)).to be nil + end + + it "prefers exact matches" do + subject[:lion_or_later] = "foo" + subject[:mountain_lion] = "bar" + expect(subject.fetch_checksum_for(:mountain_lion)).to eq(["bar", :mountain_lion]) + end + + it "finds '_or_later' tags", :needs_macos do + subject[:lion_or_later] = "foo" + expect(subject.fetch_checksum_for(:mountain_lion)).to eq(["foo", :lion_or_later]) + expect(subject.fetch_checksum_for(:snow_leopard)).to be nil + end + + it "finds '_altivec' tags", :needs_macos do + subject[:tiger_altivec] = "foo" + expect(subject.fetch_checksum_for(:tiger_g4)).to eq(["foo", :tiger_altivec]) + expect(subject.fetch_checksum_for(:tiger_g4e)).to eq(["foo", :tiger_altivec]) + expect(subject.fetch_checksum_for(:tiger_g5)).to eq(["foo", :tiger_altivec]) + expect(subject.fetch_checksum_for(:tiger_g3)).to be nil + 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/shell_spec.rb b/Library/Homebrew/test/utils/shell_spec.rb new file mode 100644 index 000000000..c44bd8253 --- /dev/null +++ b/Library/Homebrew/test/utils/shell_spec.rb @@ -0,0 +1,93 @@ +require "utils/shell" + +describe Utils::Shell do + describe "::shell_profile" do + it "returns ~/.bash_profile by default" do + ENV["SHELL"] = "/bin/another_shell" + expect(subject.shell_profile).to eq("~/.bash_profile") + end + + it "returns ~/.bash_profile for Sh" do + ENV["SHELL"] = "/bin/another_shell" + expect(subject.shell_profile).to eq("~/.bash_profile") + end + + it "returns ~/.bash_profile for Bash" do + ENV["SHELL"] = "/bin/bash" + expect(subject.shell_profile).to eq("~/.bash_profile") + end + + it "returns ~/.zshrc for Zsh" do + ENV["SHELL"] = "/bin/zsh" + expect(subject.shell_profile).to eq("~/.zshrc") + end + + it "returns ~/.kshrc for Ksh" do + ENV["SHELL"] = "/bin/ksh" + expect(subject.shell_profile).to eq("~/.kshrc") + end + end + + describe "::path_to_shell" do + it "supports a raw command name" do + expect(subject.path_to_shell("bash")).to eq(:bash) + end + + it "supports full paths" do + expect(subject.path_to_shell("/bin/bash")).to eq(:bash) + end + + it "supports versions" do + expect(subject.path_to_shell("zsh-5.2")).to eq(:zsh) + end + + it "strips newlines" do + expect(subject.path_to_shell("zsh-5.2\n")).to eq(:zsh) + end + + it "returns nil when input is invalid" do + expect(subject.path_to_shell("")).to be nil + expect(subject.path_to_shell("@@@@@@")).to be nil + expect(subject.path_to_shell("invalid_shell-4.2")).to be nil + end + end + + specify "::sh_quote" do + expect(subject.sh_quote("")).to eq("''") + expect(subject.sh_quote("\\")).to eq("\\\\") + expect(subject.sh_quote("\n")).to eq("'\n'") + expect(subject.sh_quote("$")).to eq("\\$") + expect(subject.sh_quote("word")).to eq("word") + end + + specify "::csh_quote" do + expect(subject.csh_quote("")).to eq("''") + expect(subject.csh_quote("\\")).to eq("\\\\") + # note this test is different than for sh + expect(subject.csh_quote("\n")).to eq("'\\\n'") + expect(subject.csh_quote("$")).to eq("\\$") + expect(subject.csh_quote("word")).to eq("word") + end + + describe "::prepend_path_in_shell_profile" do + let(:path) { "/my/path" } + + it "supports Tcsh" do + ENV["SHELL"] = "/bin/tcsh" + expect(subject.prepend_path_in_shell_profile(path)) + .to start_with("echo 'setenv PATH #{path}:$") + end + + it "supports Bash" do + ENV["SHELL"] = "/bin/bash" + expect(subject.prepend_path_in_shell_profile(path)) + .to start_with("echo 'export PATH=\"#{path}:$") + end + + it "supports Fish" do + ENV["SHELL"] = "/usr/local/bin/fish" + expect(subject.prepend_path_in_shell_profile(path)) + .to start_with("echo 'set -g fish_user_paths \"#{path}\" $fish_user_paths' >>") + end + end +end diff --git a/Library/Homebrew/test/utils/tty_spec.rb b/Library/Homebrew/test/utils/tty_spec.rb new file mode 100644 index 000000000..3ba89b6fd --- /dev/null +++ b/Library/Homebrew/test/utils/tty_spec.rb @@ -0,0 +1,67 @@ +require "utils" + +describe Tty do + describe "::strip_ansi" do + it "removes ANSI escape codes from a string" do + expect(subject.strip_ansi("\033\[36;7mhello\033\[0m")).to eq("hello") + end + end + + describe "::width" do + it "returns an Integer" do + expect(subject.width).to be_kind_of(Integer) + end + + it "cannot be negative" do + expect(subject.width).to be >= 0 + end + end + + describe "::truncate" do + it "truncates the text to the terminal width, minus 4, to account for '==> '" do + allow(subject).to receive(:width).and_return(15) + + expect(subject.truncate("foobar something very long")).to eq("foobar some") + expect(subject.truncate("truncate")).to eq("truncate") + end + + it "doesn't truncate the text if the terminal is unsupported, i.e. the width is 0" do + allow(subject).to receive(:width).and_return(0) + expect(subject.truncate("foobar something very long")).to eq("foobar something very long") + end + end + + context "when $stdout is not a TTY" do + before(:each) do + allow($stdout).to receive(:tty?).and_return(false) + end + + it "returns an empty string for all colors" do + expect(subject.to_s).to eq("") + expect(subject.red.to_s).to eq("") + expect(subject.green.to_s).to eq("") + expect(subject.yellow.to_s).to eq("") + expect(subject.blue.to_s).to eq("") + expect(subject.magenta.to_s).to eq("") + expect(subject.cyan.to_s).to eq("") + expect(subject.default.to_s).to eq("") + end + end + + context "when $stdout is a TTY" do + before(:each) do + allow($stdout).to receive(:tty?).and_return(true) + end + + it "returns an empty string for all colors" do + expect(subject.to_s).to eq("") + expect(subject.red.to_s).to eq("\033[31m") + expect(subject.green.to_s).to eq("\033[32m") + expect(subject.yellow.to_s).to eq("\033[33m") + expect(subject.blue.to_s).to eq("\033[34m") + expect(subject.magenta.to_s).to eq("\033[35m") + expect(subject.cyan.to_s).to eq("\033[36m") + expect(subject.default.to_s).to eq("\033[39m") + end + end +end diff --git a/Library/Homebrew/test/utils/tty_test.rb b/Library/Homebrew/test/utils/tty_test.rb deleted file mode 100644 index 09f092cbd..000000000 --- a/Library/Homebrew/test/utils/tty_test.rb +++ /dev/null @@ -1,46 +0,0 @@ -require "testing_env" -require "utils" - -class TtyTests < Homebrew::TestCase - def test_strip_ansi - assert_equal "hello", Tty.strip_ansi("\033\[36;7mhello\033\[0m") - end - - def test_width - assert_kind_of Integer, Tty.width - end - - def test_truncate - 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") - end - - def test_no_tty_formatting - $stdout.stubs(:tty?).returns false - assert_equal "", Tty.to_s - assert_equal "", Tty.red.to_s - assert_equal "", Tty.green.to_s - assert_equal "", Tty.yellow.to_s - assert_equal "", Tty.blue.to_s - assert_equal "", Tty.magenta.to_s - assert_equal "", Tty.cyan.to_s - assert_equal "", Tty.default.to_s - end - - def test_formatting - $stdout.stubs(:tty?).returns(true) - assert_equal "", Tty.to_s - assert_equal "\033[31m", Tty.red.to_s - assert_equal "\033[32m", Tty.green.to_s - assert_equal "\033[33m", Tty.yellow.to_s - assert_equal "\033[34m", Tty.blue.to_s - assert_equal "\033[35m", Tty.magenta.to_s - assert_equal "\033[36m", Tty.cyan.to_s - assert_equal "\033[39m", Tty.default.to_s - end -end |
