Convert Tap test to spec.

This commit is contained in:
Markus Reiter 2017-02-27 23:07:22 +01:00
parent 14ae87d5c9
commit 3dcbb84fb5
2 changed files with 346 additions and 319 deletions

View File

@ -0,0 +1,346 @@
RSpec::Matchers.alias_matcher :have_formula_file, :be_formula_file
RSpec::Matchers.alias_matcher :have_custom_remote, :be_custom_remote
describe Tap do
include FileUtils
subject { Tap.new("Homebrew", "foo") }
let(:path) { Tap::TAP_DIRECTORY/"homebrew/homebrew-foo" }
let(:formula_file) { path/"Formula/foo.rb" }
let(:alias_file) { path/"Aliases/bar" }
let(:cmd_file) { path/"cmd/brew-tap-cmd.rb" }
let(:manpage_file) { path/"manpages/brew-tap-cmd.1" }
let(:bash_completion_file) { path/"completions/bash/brew-tap-cmd" }
let(:zsh_completion_file) { path/"completions/zsh/_brew-tap-cmd" }
let(:fish_completion_file) { path/"completions/fish/brew-tap-cmd.fish" }
before(:each) do
path.mkpath
end
def setup_tap_files
formula_file.write <<-EOS.undent
class Foo < Formula
url "https://example.com/foo-1.0.tar.gz"
end
EOS
alias_file.parent.mkpath
ln_s formula_file, alias_file
(path/"formula_renames.json").write <<-EOS.undent
{ "oldname": "foo" }
EOS
(path/"tap_migrations.json").write <<-EOS.undent
{ "removed-formula": "homebrew/foo" }
EOS
[
cmd_file,
manpage_file,
bash_completion_file,
zsh_completion_file,
fish_completion_file,
].each do |f|
f.parent.mkpath
touch f
end
chmod 0755, cmd_file
end
def setup_git_repo
path.cd do
shutup do
system "git", "init"
system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo"
system "git", "add", "--all"
system "git", "commit", "-m", "init"
end
end
end
specify "::fetch" do
begin
expect(described_class.fetch("Homebrew", "homebrew")).to be_kind_of(CoreTap)
tap = described_class.fetch("Homebrew", "foo")
expect(tap).to be_kind_of(Tap)
expect(tap.name).to eq("homebrew/foo")
expect {
described_class.fetch("foo")
}.to raise_error(/Invalid tap name/)
expect {
described_class.fetch("homebrew/homebrew/bar")
}.to raise_error(/Invalid tap name/)
expect {
described_class.fetch("homebrew", "homebrew/baz")
}.to raise_error(/Invalid tap name/)
ensure
described_class.clear_cache
end
end
specify "#names" do
expect(described_class.names.sort).to eq(["homebrew/core", "homebrew/foo"])
end
specify "attributes" do
expect(subject.user).to eq("Homebrew")
expect(subject.repo).to eq("foo")
expect(subject.name).to eq("homebrew/foo")
expect(subject.path).to eq(path)
expect(subject).to be_installed
expect(subject).to be_official
expect(subject).not_to be_a_core_tap
end
specify "#issues_url" do
begin
t = described_class.new("someone", "foo")
path = Tap::TAP_DIRECTORY/"someone/homebrew-foo"
path.mkpath
cd path do
shutup { system "git", "init" }
system "git", "remote", "add", "origin",
"https://github.com/someone/homebrew-foo"
end
expect(t.issues_url).to eq("https://github.com/someone/homebrew-foo/issues")
expect(subject.issues_url).to eq("https://github.com/Homebrew/homebrew-foo/issues")
(Tap::TAP_DIRECTORY/"someone/homebrew-no-git").mkpath
expect(described_class.new("someone", "no-git").issues_url).to be nil
ensure
path.parent.rmtree
end
end
specify "files" do
setup_tap_files
expect(subject.formula_files).to eq([formula_file])
expect(subject.formula_names).to eq(["homebrew/foo/foo"])
expect(subject.alias_files).to eq([alias_file])
expect(subject.aliases).to eq(["homebrew/foo/bar"])
expect(subject.alias_table).to eq("homebrew/foo/bar" => "homebrew/foo/foo")
expect(subject.alias_reverse_table).to eq("homebrew/foo/foo" => ["homebrew/foo/bar"])
expect(subject.formula_renames).to eq("oldname" => "foo")
expect(subject.tap_migrations).to eq("removed-formula" => "homebrew/foo")
expect(subject.command_files).to eq([cmd_file])
expect(subject.to_hash).to be_kind_of(Hash)
expect(subject).to have_formula_file(formula_file)
expect(subject).to have_formula_file("Formula/foo.rb")
expect(subject).not_to have_formula_file("bar.rb")
expect(subject).not_to have_formula_file("Formula/baz.sh")
end
describe "#remote" do
it "returns the remote URL" do
setup_git_repo
expect(subject.remote).to eq("https://github.com/Homebrew/homebrew-foo")
expect { described_class.new("Homebrew", "bar").remote }.to raise_error(TapUnavailableError)
expect(subject).not_to have_custom_remote
services_tap = described_class.new("Homebrew", "services")
services_tap.path.mkpath
services_tap.path.cd do
shutup do
system "git", "init"
system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-services"
end
end
expect(services_tap).not_to be_private
end
it "returns nil if the Tap is not a Git repo" do
expect(subject.remote).to be nil
end
it "returns nil if Git is not available" do
setup_git_repo
allow(Utils).to receive(:git_available?).and_return(false)
expect(subject.remote).to be nil
end
end
specify "Git variant" do
touch path/"README"
setup_git_repo
expect(subject.git_head).to eq("0453e16c8e3fac73104da50927a86221ca0740c2")
expect(subject.git_short_head).to eq("0453")
expect(subject.git_last_commit).to match(/\A\d+ .+ ago\Z/)
expect(subject.git_last_commit_date).to eq("2017-01-22")
end
specify "#private?" do
skip "HOMEBREW_GITHUB_API_TOKEN is required" unless GitHub.api_credentials
expect(subject).to be_private
end
describe "#install" do
it "raises an error when the Tap is already tapped" do
setup_git_repo
already_tapped_tap = described_class.new("Homebrew", "foo")
expect(already_tapped_tap).to be_installed
expect { already_tapped_tap.install }.to raise_error(TapAlreadyTappedError)
end
it "raises an error when the Tap is already tapped with the right remote" do
setup_git_repo
already_tapped_tap = described_class.new("Homebrew", "foo")
expect(already_tapped_tap).to be_installed
right_remote = subject.remote
expect { already_tapped_tap.install clone_target: right_remote }.to raise_error(TapAlreadyTappedError)
end
it "raises an error when the remote doesn't match" do
setup_git_repo
already_tapped_tap = described_class.new("Homebrew", "foo")
touch subject.path/".git/shallow"
expect(already_tapped_tap).to be_installed
wrong_remote = "#{subject.remote}-oops"
expect { already_tapped_tap.install clone_target: wrong_remote, full_clone: true }.to raise_error(TapRemoteMismatchError)
end
it "raises an error when the Tap is already unshallow" do
setup_git_repo
already_tapped_tap = described_class.new("Homebrew", "foo")
expect { already_tapped_tap.install full_clone: true }.to raise_error(TapAlreadyUnshallowError)
end
specify "Git error" do
tap = described_class.new("user", "repo")
expect {
shutup { tap.install clone_target: "file:///not/existed/remote/url" }
}.to raise_error(ErrorDuringExecution)
expect(tap).not_to be_installed
expect(Tap::TAP_DIRECTORY/"user").not_to exist
end
end
describe "#uninstall" do
it "raises an error if the Tap is not available" do
tap = described_class.new("Homebrew", "bar")
expect { tap.uninstall }.to raise_error(TapUnavailableError)
end
end
specify "#install and #uninstall" do
begin
setup_tap_files
setup_git_repo
tap = Tap.new("Homebrew", "bar")
shutup do
tap.install clone_target: subject.path/".git"
end
expect(tap).to be_installed
expect(HOMEBREW_PREFIX/"share/man/man1/brew-tap-cmd.1").to be_a_file
expect(HOMEBREW_PREFIX/"etc/bash_completion.d/brew-tap-cmd").to be_a_file
expect(HOMEBREW_PREFIX/"share/zsh/site-functions/_brew-tap-cmd").to be_a_file
expect(HOMEBREW_PREFIX/"share/fish/vendor_completions.d/brew-tap-cmd.fish").to be_a_file
shutup do
tap.uninstall
end
expect(tap).not_to be_installed
expect(HOMEBREW_PREFIX/"share/man/man1/brew-tap-cmd.1").not_to exist
expect(HOMEBREW_PREFIX/"share/man/man1").not_to exist
expect(HOMEBREW_PREFIX/"etc/bash_completion.d/brew-tap-cmd").not_to exist
expect(HOMEBREW_PREFIX/"share/zsh/site-functions/_brew-tap-cmd").not_to exist
expect(HOMEBREW_PREFIX/"share/fish/vendor_completions.d/brew-tap-cmd.fish").not_to exist
ensure
(HOMEBREW_PREFIX/"etc").rmtree if (HOMEBREW_PREFIX/"etc").exist?
(HOMEBREW_PREFIX/"share").rmtree if (HOMEBREW_PREFIX/"share").exist?
end
end
specify "#link_completions_and_manpages" do
begin
setup_tap_files
setup_git_repo
tap = Tap.new("Homebrew", "baz")
shutup { tap.install clone_target: subject.path/".git" }
(HOMEBREW_PREFIX/"share/man/man1/brew-tap-cmd.1").delete
(HOMEBREW_PREFIX/"etc/bash_completion.d/brew-tap-cmd").delete
(HOMEBREW_PREFIX/"share/zsh/site-functions/_brew-tap-cmd").delete
(HOMEBREW_PREFIX/"share/fish/vendor_completions.d/brew-tap-cmd.fish").delete
shutup { tap.link_completions_and_manpages }
expect(HOMEBREW_PREFIX/"share/man/man1/brew-tap-cmd.1").to be_a_file
expect(HOMEBREW_PREFIX/"etc/bash_completion.d/brew-tap-cmd").to be_a_file
expect(HOMEBREW_PREFIX/"share/zsh/site-functions/_brew-tap-cmd").to be_a_file
expect(HOMEBREW_PREFIX/"share/fish/vendor_completions.d/brew-tap-cmd.fish").to be_a_file
shutup { tap.uninstall }
ensure
(HOMEBREW_PREFIX/"etc").rmtree if (HOMEBREW_PREFIX/"etc").exist?
(HOMEBREW_PREFIX/"share").rmtree if (HOMEBREW_PREFIX/"share").exist?
end
end
specify "#pin and #unpin" do
expect(subject).not_to be_pinned
expect { subject.unpin }.to raise_error(TapPinStatusError)
subject.pin
expect(subject).to be_pinned
expect { subject.pin }.to raise_error(TapPinStatusError)
subject.unpin
expect(subject).not_to be_pinned
end
specify "#config" do
setup_git_repo
expect(subject.config["foo"]).to be nil
subject.config["foo"] = "bar"
expect(subject.config["foo"]).to eq("bar")
subject.config["foo"] = nil
expect(subject.config["foo"]).to be nil
end
end
describe CoreTap do
include FileUtils
specify "attributes" do
expect(subject.user).to eq("Homebrew")
expect(subject.repo).to eq("core")
expect(subject.name).to eq("homebrew/core")
expect(subject.command_files).to eq([])
expect(subject).to be_installed
expect(subject).not_to be_pinned
expect(subject).to be_official
expect(subject).to be_a_core_tap
end
specify "forbidden operations" do
expect { subject.uninstall }.to raise_error(RuntimeError)
expect { subject.pin }.to raise_error(RuntimeError)
expect { subject.unpin }.to raise_error(RuntimeError)
end
specify "files" do
formula_file = subject.formula_dir/"foo.rb"
formula_file.write <<-EOS.undent
class Foo < Formula
url "https://example.com/foo-1.0.tar.gz"
end
EOS
alias_file = subject.alias_dir/"bar"
alias_file.parent.mkpath
ln_s formula_file, alias_file
expect(subject.formula_files).to eq([formula_file])
expect(subject.formula_names).to eq(["foo"])
expect(subject.alias_files).to eq([alias_file])
expect(subject.aliases).to eq(["bar"])
expect(subject.alias_table).to eq("bar" => "foo")
expect(subject.alias_reverse_table).to eq("foo" => ["bar"])
end
end

View File

@ -1,319 +0,0 @@
require "testing_env"
class TapTest < Homebrew::TestCase
include FileUtils
def setup
super
@path = Tap::TAP_DIRECTORY/"homebrew/homebrew-foo"
@path.mkpath
@tap = Tap.new("Homebrew", "foo")
end
def setup_tap_files
@formula_file = @path/"Formula/foo.rb"
@formula_file.write <<-EOS.undent
class Foo < Formula
url "https://example.com/foo-1.0.tar.gz"
end
EOS
@alias_file = @path/"Aliases/bar"
@alias_file.parent.mkpath
ln_s @formula_file, @alias_file
(@path/"formula_renames.json").write <<-EOS.undent
{ "oldname": "foo" }
EOS
(@path/"tap_migrations.json").write <<-EOS.undent
{ "removed-formula": "homebrew/foo" }
EOS
@cmd_file = @path/"cmd/brew-tap-cmd.rb"
@cmd_file.parent.mkpath
touch @cmd_file
chmod 0755, @cmd_file
@manpage_file = @path/"manpages/brew-tap-cmd.1"
@manpage_file.parent.mkpath
touch @manpage_file
@bash_completion_file = @path/"completions/bash/brew-tap-cmd"
@bash_completion_file.parent.mkpath
touch @bash_completion_file
@zsh_completion_file = @path/"completions/zsh/_brew-tap-cmd"
@zsh_completion_file.parent.mkpath
touch @zsh_completion_file
@fish_completion_file = @path/"completions/fish/brew-tap-cmd.fish"
@fish_completion_file.parent.mkpath
touch @fish_completion_file
end
def setup_git_repo
@path.cd do
shutup do
system "git", "init"
system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo"
system "git", "add", "--all"
system "git", "commit", "-m", "init"
end
end
end
def test_fetch
assert_kind_of CoreTap, Tap.fetch("Homebrew", "homebrew")
tap = Tap.fetch("Homebrew", "foo")
assert_kind_of Tap, tap
assert_equal "homebrew/foo", tap.name
assert_match "Invalid tap name",
assert_raises { Tap.fetch("foo") }.message
assert_match "Invalid tap name",
assert_raises { Tap.fetch("homebrew/homebrew/bar") }.message
assert_match "Invalid tap name",
assert_raises { Tap.fetch("homebrew", "homebrew/baz") }.message
ensure
Tap.clear_cache
end
def test_names
assert_equal ["homebrew/core", "homebrew/foo"], Tap.names.sort
end
def test_attributes
assert_equal "Homebrew", @tap.user
assert_equal "foo", @tap.repo
assert_equal "homebrew/foo", @tap.name
assert_equal @path, @tap.path
assert_predicate @tap, :installed?
assert_predicate @tap, :official?
refute_predicate @tap, :core_tap?
end
def test_issues_url
t = Tap.new("someone", "foo")
path = Tap::TAP_DIRECTORY/"someone/homebrew-foo"
path.mkpath
cd path do
shutup { system "git", "init" }
system "git", "remote", "add", "origin",
"https://github.com/someone/homebrew-foo"
end
assert_equal "https://github.com/someone/homebrew-foo/issues", t.issues_url
assert_equal "https://github.com/Homebrew/homebrew-foo/issues", @tap.issues_url
(Tap::TAP_DIRECTORY/"someone/homebrew-no-git").mkpath
assert_nil Tap.new("someone", "no-git").issues_url
ensure
path.parent.rmtree
end
def test_files
setup_tap_files
assert_equal [@formula_file], @tap.formula_files
assert_equal ["homebrew/foo/foo"], @tap.formula_names
assert_equal [@alias_file], @tap.alias_files
assert_equal ["homebrew/foo/bar"], @tap.aliases
assert_equal @tap.alias_table, "homebrew/foo/bar" => "homebrew/foo/foo"
assert_equal @tap.alias_reverse_table, "homebrew/foo/foo" => ["homebrew/foo/bar"]
assert_equal @tap.formula_renames, "oldname" => "foo"
assert_equal @tap.tap_migrations, "removed-formula" => "homebrew/foo"
assert_equal [@cmd_file], @tap.command_files
assert_kind_of Hash, @tap.to_hash
assert_equal true, @tap.formula_file?(@formula_file)
assert_equal true, @tap.formula_file?("Formula/foo.rb")
assert_equal false, @tap.formula_file?("bar.rb")
assert_equal false, @tap.formula_file?("Formula/baz.sh")
end
def test_remote
setup_git_repo
assert_equal "https://github.com/Homebrew/homebrew-foo", @tap.remote
assert_raises(TapUnavailableError) { Tap.new("Homebrew", "bar").remote }
refute_predicate @tap, :custom_remote?
services_tap = Tap.new("Homebrew", "services")
services_tap.path.mkpath
services_tap.path.cd do
shutup do
system "git", "init"
system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-services"
end
end
refute_predicate services_tap, :private?
end
def test_remote_not_git_repo
assert_nil @tap.remote
end
def test_remote_git_not_available
setup_git_repo
Utils.stubs(:git_available?).returns(false)
assert_nil @tap.remote
end
def test_git_variant
touch @path/"README"
setup_git_repo
assert_equal "0453e16c8e3fac73104da50927a86221ca0740c2", @tap.git_head
assert_equal "0453", @tap.git_short_head
assert_match(/\A\d+ .+ ago\Z/, @tap.git_last_commit)
assert_equal "2017-01-22", @tap.git_last_commit_date
end
def test_private_remote
skip "HOMEBREW_GITHUB_API_TOKEN is required" unless GitHub.api_credentials
assert_predicate @tap, :private?
end
def test_install_tap_already_tapped_error
setup_git_repo
already_tapped_tap = Tap.new("Homebrew", "foo")
assert_equal true, already_tapped_tap.installed?
assert_raises(TapAlreadyTappedError) { already_tapped_tap.install }
end
def test_install_tap_remote_match_already_tapped_error
setup_git_repo
already_tapped_tap = Tap.new("Homebrew", "foo")
assert_equal true, already_tapped_tap.installed?
right_remote = @tap.remote
assert_raises(TapAlreadyTappedError) { already_tapped_tap.install clone_target: right_remote }
end
def test_install_tap_remote_mismatch_error
setup_git_repo
already_tapped_tap = Tap.new("Homebrew", "foo")
touch @tap.path/".git/shallow"
assert_equal true, already_tapped_tap.installed?
wrong_remote = "#{@tap.remote}-oops"
assert_raises(TapRemoteMismatchError) { already_tapped_tap.install clone_target: wrong_remote, full_clone: true }
end
def test_install_tap_already_unshallow_error
setup_git_repo
already_tapped_tap = Tap.new("Homebrew", "foo")
assert_raises(TapAlreadyUnshallowError) { already_tapped_tap.install full_clone: true }
end
def test_uninstall_tap_unavailable_error
tap = Tap.new("Homebrew", "bar")
assert_raises(TapUnavailableError) { tap.uninstall }
end
def test_install_git_error
tap = Tap.new("user", "repo")
assert_raises(ErrorDuringExecution) do
shutup { tap.install clone_target: "file:///not/existed/remote/url" }
end
refute_predicate tap, :installed?
refute_predicate Tap::TAP_DIRECTORY/"user", :exist?
end
def test_install_and_uninstall
setup_tap_files
setup_git_repo
tap = Tap.new("Homebrew", "bar")
shutup { tap.install clone_target: @tap.path/".git" }
assert_predicate tap, :installed?
assert_predicate HOMEBREW_PREFIX/"share/man/man1/brew-tap-cmd.1", :file?
assert_predicate HOMEBREW_PREFIX/"etc/bash_completion.d/brew-tap-cmd", :file?
assert_predicate HOMEBREW_PREFIX/"share/zsh/site-functions/_brew-tap-cmd", :file?
assert_predicate HOMEBREW_PREFIX/"share/fish/vendor_completions.d/brew-tap-cmd.fish", :file?
shutup { tap.uninstall }
refute_predicate tap, :installed?
refute_predicate HOMEBREW_PREFIX/"share/man/man1/brew-tap-cmd.1", :exist?
refute_predicate HOMEBREW_PREFIX/"share/man/man1", :exist?
refute_predicate HOMEBREW_PREFIX/"etc/bash_completion.d/brew-tap-cmd", :exist?
refute_predicate HOMEBREW_PREFIX/"share/zsh/site-functions/_brew-tap-cmd", :exist?
refute_predicate HOMEBREW_PREFIX/"share/fish/vendor_completions.d/brew-tap-cmd.fish", :exist?
ensure
(HOMEBREW_PREFIX/"etc").rmtree if (HOMEBREW_PREFIX/"etc").exist?
(HOMEBREW_PREFIX/"share").rmtree if (HOMEBREW_PREFIX/"share").exist?
end
def test_link_completions_and_manpages
setup_tap_files
setup_git_repo
tap = Tap.new("Homebrew", "baz")
shutup { tap.install clone_target: @tap.path/".git" }
(HOMEBREW_PREFIX/"share/man/man1/brew-tap-cmd.1").delete
(HOMEBREW_PREFIX/"etc/bash_completion.d/brew-tap-cmd").delete
(HOMEBREW_PREFIX/"share/zsh/site-functions/_brew-tap-cmd").delete
(HOMEBREW_PREFIX/"share/fish/vendor_completions.d/brew-tap-cmd.fish").delete
shutup { tap.link_completions_and_manpages }
assert_predicate HOMEBREW_PREFIX/"share/man/man1/brew-tap-cmd.1", :file?
assert_predicate HOMEBREW_PREFIX/"etc/bash_completion.d/brew-tap-cmd", :file?
assert_predicate HOMEBREW_PREFIX/"share/zsh/site-functions/_brew-tap-cmd", :file?
assert_predicate HOMEBREW_PREFIX/"share/fish/vendor_completions.d/brew-tap-cmd.fish", :file?
shutup { tap.uninstall }
ensure
(HOMEBREW_PREFIX/"etc").rmtree if (HOMEBREW_PREFIX/"etc").exist?
(HOMEBREW_PREFIX/"share").rmtree if (HOMEBREW_PREFIX/"share").exist?
end
def test_pin_and_unpin
refute_predicate @tap, :pinned?
assert_raises(TapPinStatusError) { @tap.unpin }
@tap.pin
assert_predicate @tap, :pinned?
assert_raises(TapPinStatusError) { @tap.pin }
@tap.unpin
refute_predicate @tap, :pinned?
end
def test_config
setup_git_repo
assert_nil @tap.config["foo"]
@tap.config["foo"] = "bar"
assert_equal "bar", @tap.config["foo"]
@tap.config["foo"] = nil
assert_nil @tap.config["foo"]
end
end
class CoreTapTest < Homebrew::TestCase
include FileUtils
def setup
super
@repo = CoreTap.new
end
def test_attributes
assert_equal "Homebrew", @repo.user
assert_equal "core", @repo.repo
assert_equal "homebrew/core", @repo.name
assert_equal [], @repo.command_files
assert_predicate @repo, :installed?
refute_predicate @repo, :pinned?
assert_predicate @repo, :official?
assert_predicate @repo, :core_tap?
end
def test_forbidden_operations
assert_raises(RuntimeError) { @repo.uninstall }
assert_raises(RuntimeError) { @repo.pin }
assert_raises(RuntimeError) { @repo.unpin }
end
def test_files
@formula_file = @repo.formula_dir/"foo.rb"
@formula_file.write <<-EOS.undent
class Foo < Formula
url "https://example.com/foo-1.0.tar.gz"
end
EOS
@alias_file = @repo.alias_dir/"bar"
@alias_file.parent.mkpath
ln_s @formula_file, @alias_file
assert_equal [@formula_file], @repo.formula_files
assert_equal ["foo"], @repo.formula_names
assert_equal [@alias_file], @repo.alias_files
assert_equal ["bar"], @repo.aliases
assert_equal @repo.alias_table, "bar" => "foo"
assert_equal @repo.alias_reverse_table, "foo" => ["bar"]
end
end