Merge pull request #10296 from SeekingMeaning/utils-named-subject

test/utils/*: use `described_class` or named `subject`
This commit is contained in:
Seeker 2021-01-12 13:38:55 -08:00 committed by GitHub
commit 99a7e2192c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 107 additions and 141 deletions

View File

@ -1,28 +1,26 @@
# This configuration was generated by # This configuration was generated by
# `rubocop --auto-gen-config --exclude-limit 100` # `rubocop --auto-gen-config --exclude-limit 100`
# on 2019-03-28 10:03:26 +0000 using RuboCop version 0.66.0. # on 2021-01-11 20:21:22 UTC using RuboCop version 1.8.0.
# The point is for the user to remove these configuration records # The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base. # one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new # Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again. # versions of RuboCop, may require this file to be generated again.
# Offense count: 1 # Offense count: 2
# Configuration parameters: Max. # Configuration parameters: Max.
RSpec/ExampleLength: RSpec/ExampleLength:
Exclude: Exclude:
- 'rubocops/patches_spec.rb' - 'rubocops/patches_spec.rb'
- 'dev-cmd/bottle_spec.rb'
# Offense count: 6 # Offense count: 7
# Configuration parameters: AssignmentOnly. # Configuration parameters: AssignmentOnly.
RSpec/InstanceVariable: RSpec/InstanceVariable:
Exclude: Exclude:
- 'download_strategies_spec.rb' - 'download_strategies_spec.rb'
- 'support/helper/spec/shared_context/integration_test.rb' - 'support/helper/spec/shared_context/integration_test.rb'
- 'utils/git_spec.rb' - 'utils/git_spec.rb'
- 'version_spec.rb'
# Offense count: 76 # Offense count: 73
RSpec/MultipleDescribes: RSpec/MultipleDescribes:
Exclude: Exclude:
- 'ENV_spec.rb' - 'ENV_spec.rb'
@ -34,7 +32,6 @@ RSpec/MultipleDescribes:
- 'cmd/--repository_spec.rb' - 'cmd/--repository_spec.rb'
- 'cmd/--version_spec.rb' - 'cmd/--version_spec.rb'
- 'cmd/analytics_spec.rb' - 'cmd/analytics_spec.rb'
- 'cmd/autoremove_spec.rb'
- 'cmd/cleanup_spec.rb' - 'cmd/cleanup_spec.rb'
- 'cmd/commands_spec.rb' - 'cmd/commands_spec.rb'
- 'cmd/config_spec.rb' - 'cmd/config_spec.rb'
@ -57,7 +54,6 @@ RSpec/MultipleDescribes:
- 'cmd/readall_spec.rb' - 'cmd/readall_spec.rb'
- 'cmd/reinstall_spec.rb' - 'cmd/reinstall_spec.rb'
- 'cmd/search_spec.rb' - 'cmd/search_spec.rb'
- 'cmd/switch_spec.rb'
- 'cmd/tap-info_spec.rb' - 'cmd/tap-info_spec.rb'
- 'cmd/tap_spec.rb' - 'cmd/tap_spec.rb'
- 'cmd/uninstall_spec.rb' - 'cmd/uninstall_spec.rb'
@ -78,17 +74,14 @@ RSpec/MultipleDescribes:
- 'dev-cmd/irb_spec.rb' - 'dev-cmd/irb_spec.rb'
- 'dev-cmd/linkage_spec.rb' - 'dev-cmd/linkage_spec.rb'
- 'dev-cmd/livecheck_spec.rb' - 'dev-cmd/livecheck_spec.rb'
- 'dev-cmd/pull_spec.rb'
- 'dev-cmd/ruby_spec.rb' - 'dev-cmd/ruby_spec.rb'
- 'dev-cmd/sh_spec.rb' - 'dev-cmd/sh_spec.rb'
- 'dev-cmd/style_spec.rb'
- 'dev-cmd/tap-new_spec.rb' - 'dev-cmd/tap-new_spec.rb'
- 'dev-cmd/test_spec.rb' - 'dev-cmd/test_spec.rb'
- 'dev-cmd/unpack_spec.rb' - 'dev-cmd/unpack_spec.rb'
- 'download_strategies_spec.rb' - 'download_strategies_spec.rb'
- 'exceptions_spec.rb' - 'exceptions_spec.rb'
- 'formula_support_spec.rb' - 'formula_support_spec.rb'
- 'inreplace_spec.rb'
- 'language/python_spec.rb' - 'language/python_spec.rb'
- 'options_spec.rb' - 'options_spec.rb'
- 'os/mac/mach_spec.rb' - 'os/mac/mach_spec.rb'
@ -96,21 +89,18 @@ RSpec/MultipleDescribes:
- 'rubocops/checksum_spec.rb' - 'rubocops/checksum_spec.rb'
- 'rubocops/class_spec.rb' - 'rubocops/class_spec.rb'
- 'rubocops/deprecate_disable_spec.rb' - 'rubocops/deprecate_disable_spec.rb'
- 'rubocops/formula_desc_spec.rb'
- 'rubocops/lines_spec.rb' - 'rubocops/lines_spec.rb'
- 'rubocops/livecheck_spec.rb' - 'rubocops/livecheck_spec.rb'
- 'rubocops/text_spec.rb' - 'rubocops/text_spec.rb'
- 'rubocops/urls_spec.rb' - 'rubocops/urls_spec.rb'
- 'software_spec_spec.rb' - 'software_spec_spec.rb'
- 'tap_spec.rb' - 'tap_spec.rb'
- 'utils/git_spec.rb'
- 'version_spec.rb' - 'version_spec.rb'
# Offense count: 816 # Offense count: 665
# Configuration parameters: IgnoreSharedExamples. # Configuration parameters: IgnoreSharedExamples.
RSpec/NamedSubject: RSpec/NamedSubject:
Exclude: Exclude:
- 'ARGV_spec.rb'
- 'ENV_spec.rb' - 'ENV_spec.rb'
- 'build_environment_spec.rb' - 'build_environment_spec.rb'
- 'build_options_spec.rb' - 'build_options_spec.rb'
@ -118,7 +108,6 @@ RSpec/NamedSubject:
- 'cask/audit_spec.rb' - 'cask/audit_spec.rb'
- 'cask/cmd/style_spec.rb' - 'cask/cmd/style_spec.rb'
- 'cask/cmd_spec.rb' - 'cask/cmd_spec.rb'
- 'cask/depends_on_spec.rb'
- 'cask/dsl/appcast_spec.rb' - 'cask/dsl/appcast_spec.rb'
- 'caveats_spec.rb' - 'caveats_spec.rb'
- 'cleaner_spec.rb' - 'cleaner_spec.rb'
@ -132,13 +121,10 @@ RSpec/NamedSubject:
- 'descriptions_spec.rb' - 'descriptions_spec.rb'
- 'diagnostic_checks_spec.rb' - 'diagnostic_checks_spec.rb'
- 'download_strategies_spec.rb' - 'download_strategies_spec.rb'
- 'emoji_spec.rb'
- 'error_during_execution_spec.rb' - 'error_during_execution_spec.rb'
- 'exceptions_spec.rb' - 'exceptions_spec.rb'
- 'formatter_spec.rb' - 'formatter_spec.rb'
- 'formula_pin_spec.rb' - 'formula_pin_spec.rb'
- 'inreplace_spec.rb'
- 'java_requirement_spec.rb'
- 'language/node_spec.rb' - 'language/node_spec.rb'
- 'language/python_spec.rb' - 'language/python_spec.rb'
- 'linkage_cache_store_spec.rb' - 'linkage_cache_store_spec.rb'
@ -150,9 +136,7 @@ RSpec/NamedSubject:
- 'os/linux/diagnostic_spec.rb' - 'os/linux/diagnostic_spec.rb'
- 'os/mac/dependency_collector_spec.rb' - 'os/mac/dependency_collector_spec.rb'
- 'os/mac/diagnostic_spec.rb' - 'os/mac/diagnostic_spec.rb'
- 'os/mac/java_requirement_spec.rb'
- 'os/mac/keg_spec.rb' - 'os/mac/keg_spec.rb'
- 'os/mac/version_spec.rb'
- 'os/mac_spec.rb' - 'os/mac_spec.rb'
- 'patch_spec.rb' - 'patch_spec.rb'
- 'requirement_spec.rb' - 'requirement_spec.rb'
@ -161,36 +145,18 @@ RSpec/NamedSubject:
- 'sandbox_spec.rb' - 'sandbox_spec.rb'
- 'searchable_spec.rb' - 'searchable_spec.rb'
- 'software_spec_spec.rb' - 'software_spec_spec.rb'
- 'string_spec.rb'
- 'system_command_result_spec.rb' - 'system_command_result_spec.rb'
- 'tab_spec.rb' - 'tab_spec.rb'
- 'tap_spec.rb' - 'tap_spec.rb'
- 'utils/bottles/collector_spec.rb'
- 'utils/github_spec.rb'
- 'utils/popen_spec.rb'
- 'utils/shell_spec.rb'
- 'utils/tty_spec.rb'
- 'utils_spec.rb' - 'utils_spec.rb'
- 'version_spec.rb' - 'version_spec.rb'
- 'x11_requirement_spec.rb'
# Offense count: 24 # Offense count: 64
RSpec/SubjectStub:
Exclude:
- 'cache_store_spec.rb'
- 'download_strategies_spec.rb'
- 'formula_installer_spec.rb'
- 'java_requirement_spec.rb'
- 'language/python_spec.rb'
- 'os/mac/java_requirement_spec.rb'
# Offense count: 66
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames. # Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
RSpec/VerifiedDoubles: RSpec/VerifiedDoubles:
Exclude: Exclude:
- 'cache_store_spec.rb' - 'cache_store_spec.rb'
- 'cask/artifact/pkg_spec.rb' - 'cask/artifact/pkg_spec.rb'
- 'cask/cmd/style_spec.rb'
- 'cask/cmd_spec.rb' - 'cask/cmd_spec.rb'
- 'cask/installer_spec.rb' - 'cask/installer_spec.rb'
- 'cask/pkg_spec.rb' - 'cask/pkg_spec.rb'
@ -205,9 +171,7 @@ RSpec/VerifiedDoubles:
- 'formula_spec.rb' - 'formula_spec.rb'
- 'language/python_spec.rb' - 'language/python_spec.rb'
- 'linkage_cache_store_spec.rb' - 'linkage_cache_store_spec.rb'
- 'livecheck/livecheck_spec.rb'
- 'resource_spec.rb' - 'resource_spec.rb'
- 'software_spec_spec.rb' - 'software_spec_spec.rb'
- 'support/helper/formula.rb'
- 'utils/analytics_spec.rb' - 'utils/analytics_spec.rb'
- 'version_spec.rb' - 'version_spec.rb'

View File

@ -4,43 +4,45 @@
require "utils/bottles" require "utils/bottles"
describe Utils::Bottles::Collector do describe Utils::Bottles::Collector do
subject(:collector) { described_class.new }
describe "#fetch_checksum_for" do describe "#fetch_checksum_for" do
it "returns passed tags" do it "returns passed tags" do
subject[:mojave] = "foo" collector[:mojave] = "foo"
subject[:catalina] = "bar" collector[:catalina] = "bar"
expect(subject.fetch_checksum_for(:catalina)).to eq(["bar", :catalina]) expect(collector.fetch_checksum_for(:catalina)).to eq(["bar", :catalina])
end end
it "returns nil if empty" do it "returns nil if empty" do
expect(subject.fetch_checksum_for(:foo)).to be nil expect(collector.fetch_checksum_for(:foo)).to be nil
end end
it "returns nil when there is no match" do it "returns nil when there is no match" do
subject[:catalina] = "foo" collector[:catalina] = "foo"
expect(subject.fetch_checksum_for(:foo)).to be nil expect(collector.fetch_checksum_for(:foo)).to be nil
end end
it "uses older tags when needed", :needs_macos do it "uses older tags when needed", :needs_macos do
subject[:mojave] = "foo" collector[:mojave] = "foo"
expect(subject.send(:find_matching_tag, :mojave)).to eq(:mojave) expect(collector.send(:find_matching_tag, :mojave)).to eq(:mojave)
expect(subject.send(:find_matching_tag, :catalina)).to eq(:mojave) expect(collector.send(:find_matching_tag, :catalina)).to eq(:mojave)
end end
it "does not use older tags when requested not to", :needs_macos do it "does not use older tags when requested not to", :needs_macos do
allow(Homebrew::EnvConfig).to receive(:developer?).and_return(true) allow(Homebrew::EnvConfig).to receive(:developer?).and_return(true)
allow(Homebrew::EnvConfig).to receive(:skip_or_later_bottles?).and_return(true) allow(Homebrew::EnvConfig).to receive(:skip_or_later_bottles?).and_return(true)
allow(OS::Mac).to receive(:prerelease?).and_return(true) allow(OS::Mac).to receive(:prerelease?).and_return(true)
subject[:mojave] = "foo" collector[:mojave] = "foo"
expect(subject.send(:find_matching_tag, :mojave)).to eq(:mojave) expect(collector.send(:find_matching_tag, :mojave)).to eq(:mojave)
expect(subject.send(:find_matching_tag, :catalina)).to be_nil expect(collector.send(:find_matching_tag, :catalina)).to be_nil
end end
it "ignores HOMEBREW_SKIP_OR_LATER_BOTTLES on release versions", :needs_macos do it "ignores HOMEBREW_SKIP_OR_LATER_BOTTLES on release versions", :needs_macos do
allow(Homebrew::EnvConfig).to receive(:skip_or_later_bottles?).and_return(true) allow(Homebrew::EnvConfig).to receive(:skip_or_later_bottles?).and_return(true)
allow(OS::Mac).to receive(:prerelease?).and_return(false) allow(OS::Mac).to receive(:prerelease?).and_return(false)
subject[:mojave] = "foo" collector[:mojave] = "foo"
expect(subject.send(:find_matching_tag, :mojave)).to eq(:mojave) expect(collector.send(:find_matching_tag, :mojave)).to eq(:mojave)
expect(subject.send(:find_matching_tag, :catalina)).to eq(:mojave) expect(collector.send(:find_matching_tag, :catalina)).to eq(:mojave)
end end
end end
end end

View File

@ -6,7 +6,7 @@ require "utils/github"
describe GitHub do describe GitHub do
describe "::search_code", :needs_network do describe "::search_code", :needs_network do
it "queries GitHub code with the passed parameters" do it "queries GitHub code with the passed parameters" do
results = subject.search_code(repo: "Homebrew/brew", path: "/", results = described_class.search_code(repo: "Homebrew/brew", path: "/",
filename: "readme", language: "markdown") filename: "readme", language: "markdown")
expect(results.count).to eq(1) expect(results.count).to eq(1)
@ -17,24 +17,24 @@ describe GitHub do
describe "::query_string" do describe "::query_string" do
it "builds a query with the given hash parameters formatted as key:value" do it "builds a query with the given hash parameters formatted as key:value" do
query = subject.query_string(user: "Homebrew", repo: "brew") query = described_class.query_string(user: "Homebrew", repo: "brew")
expect(query).to eq("q=user%3AHomebrew+repo%3Abrew&per_page=100") expect(query).to eq("q=user%3AHomebrew+repo%3Abrew&per_page=100")
end end
it "adds a variable number of top-level string parameters to the query when provided" do it "adds a variable number of top-level string parameters to the query when provided" do
query = subject.query_string("value1", "value2", user: "Homebrew") query = described_class.query_string("value1", "value2", user: "Homebrew")
expect(query).to eq("q=value1+value2+user%3AHomebrew&per_page=100") expect(query).to eq("q=value1+value2+user%3AHomebrew&per_page=100")
end end
it "turns array values into multiple key:value parameters" do it "turns array values into multiple key:value parameters" do
query = subject.query_string(user: ["Homebrew", "caskroom"]) query = described_class.query_string(user: ["Homebrew", "caskroom"])
expect(query).to eq("q=user%3AHomebrew+user%3Acaskroom&per_page=100") expect(query).to eq("q=user%3AHomebrew+user%3Acaskroom&per_page=100")
end end
end end
describe "::search_issues", :needs_network do describe "::search_issues", :needs_network do
it "queries GitHub issues with the passed parameters" do it "queries GitHub issues with the passed parameters" do
results = subject.search_issues("brew search", results = described_class.search_issues("brew search",
repo: "Homebrew/legacy-homebrew", repo: "Homebrew/legacy-homebrew",
author: "MikeMcQuaid", author: "MikeMcQuaid",
is: "closed") is: "closed")
@ -45,7 +45,7 @@ describe GitHub do
describe "::approved_reviews", :needs_network do describe "::approved_reviews", :needs_network do
it "can get reviews for a pull request" do it "can get reviews for a pull request" do
reviews = subject.approved_reviews("Homebrew", "homebrew-core", 1, commit: "deadbeef") reviews = described_class.approved_reviews("Homebrew", "homebrew-core", 1, commit: "deadbeef")
expect(reviews).to eq([]) expect(reviews).to eq([])
end end
end end
@ -53,7 +53,7 @@ describe GitHub do
describe "::sponsors_by_tier", :needs_network do describe "::sponsors_by_tier", :needs_network do
it "errors on an unauthenticated token" do it "errors on an unauthenticated token" do
expect { expect {
subject.sponsors_by_tier("Homebrew") described_class.sponsors_by_tier("Homebrew")
}.to raise_error(/INSUFFICIENT_SCOPES|FORBIDDEN|token needs the 'admin:org' scope/) }.to raise_error(/INSUFFICIENT_SCOPES|FORBIDDEN|token needs the 'admin:org' scope/)
end end
end end
@ -61,23 +61,23 @@ describe GitHub do
describe "::get_artifact_url", :needs_network do describe "::get_artifact_url", :needs_network do
it "fails to find a nonexistant workflow" do it "fails to find a nonexistant workflow" do
expect { expect {
subject.get_artifact_url( described_class.get_artifact_url(
subject.get_workflow_run("Homebrew", "homebrew-core", 1), described_class.get_workflow_run("Homebrew", "homebrew-core", 1),
) )
}.to raise_error(/No matching workflow run found/) }.to raise_error(/No matching workflow run found/)
end end
it "fails to find artifacts that don't exist" do it "fails to find artifacts that don't exist" do
expect { expect {
subject.get_artifact_url( described_class.get_artifact_url(
subject.get_workflow_run("Homebrew", "homebrew-core", 51971, artifact_name: "false_bottles"), described_class.get_workflow_run("Homebrew", "homebrew-core", 51971, artifact_name: "false_bottles"),
) )
}.to raise_error(/No artifact .+ was found/) }.to raise_error(/No artifact .+ was found/)
end end
it "gets an artifact link" do it "gets an artifact link" do
url = subject.get_artifact_url( url = described_class.get_artifact_url(
subject.get_workflow_run("Homebrew", "homebrew-core", 51971, artifact_name: "bottles"), described_class.get_workflow_run("Homebrew", "homebrew-core", 51971, artifact_name: "bottles"),
) )
expect(url).to eq("https://api.github.com/repos/Homebrew/homebrew-core/actions/artifacts/3557392/zip") expect(url).to eq("https://api.github.com/repos/Homebrew/homebrew-core/actions/artifacts/3557392/zip")
end end
@ -87,11 +87,11 @@ describe GitHub do
hashes = %w[188606a4a9587365d930b02c98ad6857b1d00150 25a71fe1ea1558415d6496d23834dc70778ddee5] hashes = %w[188606a4a9587365d930b02c98ad6857b1d00150 25a71fe1ea1558415d6496d23834dc70778ddee5]
it "gets commit hashes for a pull request" do it "gets commit hashes for a pull request" do
expect(subject.pull_request_commits("Homebrew", "legacy-homebrew", 50678)).to eq(hashes) expect(described_class.pull_request_commits("Homebrew", "legacy-homebrew", 50678)).to eq(hashes)
end end
it "gets commit hashes for a paginated pull request API response" do it "gets commit hashes for a paginated pull request API response" do
expect(subject.pull_request_commits("Homebrew", "legacy-homebrew", 50678, per_page: 1)).to eq(hashes) expect(described_class.pull_request_commits("Homebrew", "legacy-homebrew", 50678, per_page: 1)).to eq(hashes)
end end
end end
end end

View File

@ -6,13 +6,13 @@ require "utils/popen"
describe Utils do describe Utils do
describe "::popen_read" do describe "::popen_read" do
it "reads the standard output of a given command" do it "reads the standard output of a given command" do
expect(subject.popen_read("sh", "-c", "echo success").chomp).to eq("success") expect(described_class.popen_read("sh", "-c", "echo success").chomp).to eq("success")
expect($CHILD_STATUS).to be_a_success expect($CHILD_STATUS).to be_a_success
end end
it "can be given a block to manually read from the pipe" do it "can be given a block to manually read from the pipe" do
expect( expect(
subject.popen_read("sh", "-c", "echo success") do |pipe| described_class.popen_read("sh", "-c", "echo success") do |pipe|
pipe.read.chomp pipe.read.chomp
end, end,
).to eq("success") ).to eq("success")
@ -20,7 +20,7 @@ describe Utils do
end end
it "fails when the command does not exist" do it "fails when the command does not exist" do
expect(subject.popen_read("./nonexistent", err: :out)) expect(described_class.popen_read("./nonexistent", err: :out))
.to eq("brew: command not found: ./nonexistent\n") .to eq("brew: command not found: ./nonexistent\n")
expect($CHILD_STATUS).to be_a_failure expect($CHILD_STATUS).to be_a_failure
end end
@ -32,14 +32,14 @@ describe Utils do
before { foo.write "Foo\n" } before { foo.write "Foo\n" }
it "supports writing to a command's standard input" do it "supports writing to a command's standard input" do
subject.popen_write("grep", "-q", "success") do |pipe| described_class.popen_write("grep", "-q", "success") do |pipe|
pipe.write "success\n" pipe.write "success\n"
end end
expect($CHILD_STATUS).to be_a_success expect($CHILD_STATUS).to be_a_success
end end
it "returns the command's standard output before writing" do it "returns the command's standard output before writing" do
child_stdout = subject.popen_write("cat", foo, "-") do |pipe| child_stdout = described_class.popen_write("cat", foo, "-") do |pipe|
pipe.write "Bar\n" pipe.write "Bar\n"
end end
expect($CHILD_STATUS).to be_a_success expect($CHILD_STATUS).to be_a_success
@ -50,7 +50,7 @@ describe Utils do
end end
it "returns the command's standard output after writing" do it "returns the command's standard output after writing" do
child_stdout = subject.popen_write("cat", "-", foo) do |pipe| child_stdout = described_class.popen_write("cat", "-", foo) do |pipe|
pipe.write "Bar\n" pipe.write "Bar\n"
end end
expect($CHILD_STATUS).to be_a_success expect($CHILD_STATUS).to be_a_success
@ -61,7 +61,7 @@ describe Utils do
end end
it "supports interleaved writing between two reads" do it "supports interleaved writing between two reads" do
child_stdout = subject.popen_write("cat", foo, "-", foo) do |pipe| child_stdout = described_class.popen_write("cat", foo, "-", foo) do |pipe|
pipe.write "Bar\n" pipe.write "Bar\n"
end end
expect($CHILD_STATUS).to be_a_success expect($CHILD_STATUS).to be_a_success

View File

@ -7,76 +7,76 @@ describe Utils::Shell do
describe "::profile" do describe "::profile" do
it "returns ~/.profile by default" do it "returns ~/.profile by default" do
ENV["SHELL"] = "/bin/another_shell" ENV["SHELL"] = "/bin/another_shell"
expect(subject.profile).to eq("~/.profile") expect(described_class.profile).to eq("~/.profile")
end end
it "returns ~/.profile for sh" do it "returns ~/.profile for sh" do
ENV["SHELL"] = "/bin/sh" ENV["SHELL"] = "/bin/sh"
expect(subject.profile).to eq("~/.profile") expect(described_class.profile).to eq("~/.profile")
end end
it "returns ~/.profile for Bash" do it "returns ~/.profile for Bash" do
ENV["SHELL"] = "/bin/bash" ENV["SHELL"] = "/bin/bash"
expect(subject.profile).to eq("~/.profile") expect(described_class.profile).to eq("~/.profile")
end end
it "returns /tmp/.zshrc for Zsh if ZDOTDIR is /tmp" do it "returns /tmp/.zshrc for Zsh if ZDOTDIR is /tmp" do
ENV["SHELL"] = "/bin/zsh" ENV["SHELL"] = "/bin/zsh"
ENV["ZDOTDIR"] = "/tmp" ENV["ZDOTDIR"] = "/tmp"
expect(subject.profile).to eq("/tmp/.zshrc") expect(described_class.profile).to eq("/tmp/.zshrc")
end end
it "returns ~/.zshrc for Zsh" do it "returns ~/.zshrc for Zsh" do
ENV["SHELL"] = "/bin/zsh" ENV["SHELL"] = "/bin/zsh"
ENV["ZDOTDIR"] = nil ENV["ZDOTDIR"] = nil
expect(subject.profile).to eq("~/.zshrc") expect(described_class.profile).to eq("~/.zshrc")
end end
it "returns ~/.kshrc for Ksh" do it "returns ~/.kshrc for Ksh" do
ENV["SHELL"] = "/bin/ksh" ENV["SHELL"] = "/bin/ksh"
expect(subject.profile).to eq("~/.kshrc") expect(described_class.profile).to eq("~/.kshrc")
end end
end end
describe "::from_path" do describe "::from_path" do
it "supports a raw command name" do it "supports a raw command name" do
expect(subject.from_path("bash")).to eq(:bash) expect(described_class.from_path("bash")).to eq(:bash)
end end
it "supports full paths" do it "supports full paths" do
expect(subject.from_path("/bin/bash")).to eq(:bash) expect(described_class.from_path("/bin/bash")).to eq(:bash)
end end
it "supports versions" do it "supports versions" do
expect(subject.from_path("zsh-5.2")).to eq(:zsh) expect(described_class.from_path("zsh-5.2")).to eq(:zsh)
end end
it "strips newlines" do it "strips newlines" do
expect(subject.from_path("zsh-5.2\n")).to eq(:zsh) expect(described_class.from_path("zsh-5.2\n")).to eq(:zsh)
end end
it "returns nil when input is invalid" do it "returns nil when input is invalid" do
expect(subject.from_path("")).to be nil expect(described_class.from_path("")).to be nil
expect(subject.from_path("@@@@@@")).to be nil expect(described_class.from_path("@@@@@@")).to be nil
expect(subject.from_path("invalid_shell-4.2")).to be nil expect(described_class.from_path("invalid_shell-4.2")).to be nil
end end
end end
specify "::sh_quote" do specify "::sh_quote" do
expect(subject.send(:sh_quote, "")).to eq("''") expect(described_class.send(:sh_quote, "")).to eq("''")
expect(subject.send(:sh_quote, "\\")).to eq("\\\\") expect(described_class.send(:sh_quote, "\\")).to eq("\\\\")
expect(subject.send(:sh_quote, "\n")).to eq("'\n'") expect(described_class.send(:sh_quote, "\n")).to eq("'\n'")
expect(subject.send(:sh_quote, "$")).to eq("\\$") expect(described_class.send(:sh_quote, "$")).to eq("\\$")
expect(subject.send(:sh_quote, "word")).to eq("word") expect(described_class.send(:sh_quote, "word")).to eq("word")
end end
specify "::csh_quote" do specify "::csh_quote" do
expect(subject.send(:csh_quote, "")).to eq("''") expect(described_class.send(:csh_quote, "")).to eq("''")
expect(subject.send(:csh_quote, "\\")).to eq("\\\\") expect(described_class.send(:csh_quote, "\\")).to eq("\\\\")
# NOTE: this test is different than for sh # NOTE: this test is different than for sh
expect(subject.send(:csh_quote, "\n")).to eq("'\\\n'") expect(described_class.send(:csh_quote, "\n")).to eq("'\\\n'")
expect(subject.send(:csh_quote, "$")).to eq("\\$") expect(described_class.send(:csh_quote, "$")).to eq("\\$")
expect(subject.send(:csh_quote, "word")).to eq("word") expect(described_class.send(:csh_quote, "word")).to eq("word")
end end
describe "::prepend_path_in_profile" do describe "::prepend_path_in_profile" do
@ -84,20 +84,20 @@ describe Utils::Shell do
it "supports Tcsh" do it "supports Tcsh" do
ENV["SHELL"] = "/bin/tcsh" ENV["SHELL"] = "/bin/tcsh"
expect(subject.prepend_path_in_profile(path)) expect(described_class.prepend_path_in_profile(path))
.to eq("echo 'setenv PATH #{path}:$PATH' >> #{shell_profile}") .to eq("echo 'setenv PATH #{path}:$PATH' >> #{shell_profile}")
end end
it "supports Bash" do it "supports Bash" do
ENV["SHELL"] = "/bin/bash" ENV["SHELL"] = "/bin/bash"
expect(subject.prepend_path_in_profile(path)) expect(described_class.prepend_path_in_profile(path))
.to eq("echo 'export PATH=\"#{path}:$PATH\"' >> #{shell_profile}") .to eq("echo 'export PATH=\"#{path}:$PATH\"' >> #{shell_profile}")
end end
it "supports Fish" do it "supports Fish" do
ENV["SHELL"] = "/usr/local/bin/fish" ENV["SHELL"] = "/usr/local/bin/fish"
ENV["fish_user_paths"] = "/some/path" ENV["fish_user_paths"] = "/some/path"
expect(subject.prepend_path_in_profile(path)) expect(described_class.prepend_path_in_profile(path))
.to eq("echo 'set -g fish_user_paths \"#{path}\" $fish_user_paths' >> #{shell_profile}") .to eq("echo 'set -g fish_user_paths \"#{path}\" $fish_user_paths' >> #{shell_profile}")
end end
end end

View File

@ -4,31 +4,31 @@
describe Tty do describe Tty do
describe "::strip_ansi" do describe "::strip_ansi" do
it "removes ANSI escape codes from a string" do it "removes ANSI escape codes from a string" do
expect(subject.strip_ansi("\033\[36;7mhello\033\[0m")).to eq("hello") expect(described_class.strip_ansi("\033\[36;7mhello\033\[0m")).to eq("hello")
end end
end end
describe "::width" do describe "::width" do
it "returns an Integer" do it "returns an Integer" do
expect(subject.width).to be_kind_of(Integer) expect(described_class.width).to be_kind_of(Integer)
end end
it "cannot be negative" do it "cannot be negative" do
expect(subject.width).to be >= 0 expect(described_class.width).to be >= 0
end end
end end
describe "::truncate" do describe "::truncate" do
it "truncates the text to the terminal width, minus 4, to account for '==> '" do it "truncates the text to the terminal width, minus 4, to account for '==> '" do
allow(subject).to receive(:width).and_return(15) allow(described_class).to receive(:width).and_return(15)
expect(subject.truncate("foobar something very long")).to eq("foobar some") expect(described_class.truncate("foobar something very long")).to eq("foobar some")
expect(subject.truncate("truncate")).to eq("truncate") expect(described_class.truncate("truncate")).to eq("truncate")
end end
it "doesn't truncate the text if the terminal is unsupported, i.e. the width is 0" do 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) allow(described_class).to receive(:width).and_return(0)
expect(subject.truncate("foobar something very long")).to eq("foobar something very long") expect(described_class.truncate("foobar something very long")).to eq("foobar something very long")
end end
end end
@ -38,14 +38,14 @@ describe Tty do
end end
it "returns an empty string for all colors" do it "returns an empty string for all colors" do
expect(subject.to_s).to eq("") expect(described_class.to_s).to eq("")
expect(subject.red.to_s).to eq("") expect(described_class.red.to_s).to eq("")
expect(subject.green.to_s).to eq("") expect(described_class.green.to_s).to eq("")
expect(subject.yellow.to_s).to eq("") expect(described_class.yellow.to_s).to eq("")
expect(subject.blue.to_s).to eq("") expect(described_class.blue.to_s).to eq("")
expect(subject.magenta.to_s).to eq("") expect(described_class.magenta.to_s).to eq("")
expect(subject.cyan.to_s).to eq("") expect(described_class.cyan.to_s).to eq("")
expect(subject.default.to_s).to eq("") expect(described_class.default.to_s).to eq("")
end end
end end
@ -55,26 +55,26 @@ describe Tty do
end end
it "returns ANSI escape codes for colors" do it "returns ANSI escape codes for colors" do
expect(subject.to_s).to eq("") expect(described_class.to_s).to eq("")
expect(subject.red.to_s).to eq("\033[31m") expect(described_class.red.to_s).to eq("\033[31m")
expect(subject.green.to_s).to eq("\033[32m") expect(described_class.green.to_s).to eq("\033[32m")
expect(subject.yellow.to_s).to eq("\033[33m") expect(described_class.yellow.to_s).to eq("\033[33m")
expect(subject.blue.to_s).to eq("\033[34m") expect(described_class.blue.to_s).to eq("\033[34m")
expect(subject.magenta.to_s).to eq("\033[35m") expect(described_class.magenta.to_s).to eq("\033[35m")
expect(subject.cyan.to_s).to eq("\033[36m") expect(described_class.cyan.to_s).to eq("\033[36m")
expect(subject.default.to_s).to eq("\033[39m") expect(described_class.default.to_s).to eq("\033[39m")
end end
it "returns an empty string for all colors when HOMEBREW_NO_COLOR is set" do it "returns an empty string for all colors when HOMEBREW_NO_COLOR is set" do
ENV["HOMEBREW_NO_COLOR"] = "1" ENV["HOMEBREW_NO_COLOR"] = "1"
expect(subject.to_s).to eq("") expect(described_class.to_s).to eq("")
expect(subject.red.to_s).to eq("") expect(described_class.red.to_s).to eq("")
expect(subject.green.to_s).to eq("") expect(described_class.green.to_s).to eq("")
expect(subject.yellow.to_s).to eq("") expect(described_class.yellow.to_s).to eq("")
expect(subject.blue.to_s).to eq("") expect(described_class.blue.to_s).to eq("")
expect(subject.magenta.to_s).to eq("") expect(described_class.magenta.to_s).to eq("")
expect(subject.cyan.to_s).to eq("") expect(described_class.cyan.to_s).to eq("")
expect(subject.default.to_s).to eq("") expect(described_class.default.to_s).to eq("")
end end
end end
end end