Merge pull request #10365 from SeekingMeaning/git-repo-shared-examples

git_repository_spec: group together shared examples
This commit is contained in:
Seeker 2021-01-20 16:32:25 -08:00 committed by GitHub
commit 4b5b1f61a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,34 @@
require "utils/git_repository"
describe Utils do
shared_examples "git_repository helper function" do |method_name|
context "when directory is not a Git repository" do
it "returns nil if `safe` parameter is `false`" do
expect(described_class.public_send(method_name, TEST_TMPDIR, safe: false)).to eq(nil)
end
it "raises an error if `safe` parameter is `true`" do
expect { described_class.public_send(method_name, TEST_TMPDIR, safe: true) }
.to raise_error("Not a Git repository: #{TEST_TMPDIR}")
end
end
context "when Git is unavailable" do
before do
allow(Utils::Git).to receive(:available?).and_return(false)
end
it "returns nil if `safe` parameter is `false`" do
expect(described_class.public_send(method_name, HOMEBREW_CACHE, safe: false)).to eq(nil)
end
it "raises an error if `safe` parameter is `true`" do
expect { described_class.public_send(method_name, HOMEBREW_CACHE, safe: true) }
.to raise_error("Git is unavailable")
end
end
end
before do
HOMEBREW_CACHE.cd do
system "git", "init"
@ -29,31 +57,7 @@ describe Utils do
end
end
context "when directory is not a Git repository" do
it "returns nil if `safe` parameter is `false`" do
expect(described_class.git_head(TEST_TMPDIR, safe: false)).to eq(nil)
end
it "raises an error if `safe` parameter is `true`" do
expect { described_class.git_head(TEST_TMPDIR, safe: true) }
.to raise_error("Not a Git repository: #{TEST_TMPDIR}")
end
end
context "when Git is unavailable" do
before do
allow(Utils::Git).to receive(:available?).and_return(false)
end
it "returns nil if `safe` parameter is `false`" do
expect(described_class.git_head(HOMEBREW_CACHE, safe: false)).to eq(nil)
end
it "raises an error if `safe` parameter is `true`" do
expect { described_class.git_head(HOMEBREW_CACHE, safe: true) }
.to raise_error("Git is unavailable")
end
end
include_examples "git_repository helper function", :git_head
end
describe ".git_short_head" do
@ -69,30 +73,6 @@ describe Utils do
end
end
context "when directory is not a Git repository" do
it "returns nil if `safe` parameter is `false`" do
expect(described_class.git_short_head(TEST_TMPDIR, safe: false)).to eq(nil)
end
it "raises an error if `safe` parameter is `true`" do
expect { described_class.git_short_head(TEST_TMPDIR, safe: true) }
.to raise_error("Not a Git repository: #{TEST_TMPDIR}")
end
end
context "when Git is unavailable" do
before do
allow(Utils::Git).to receive(:available?).and_return(false)
end
it "returns nil if `safe` parameter is `false`" do
expect(described_class.git_short_head(HOMEBREW_CACHE, safe: false)).to eq(nil)
end
it "raises an error if `safe` parameter is `true`" do
expect { described_class.git_short_head(HOMEBREW_CACHE, safe: true) }
.to raise_error("Git is unavailable")
end
end
include_examples "git_repository helper function", :git_short_head
end
end