[rubygems/rubygems] Add tests for GitProxy#checkout

This commit adds tests to capture the current behavior of `#checkout`.
They are not exhaustive, but they cover cases cloning and fetching the
repository with different ref types. This will make it easier to change
the caching behavior in a subsequent commit.

https://github.com/rubygems/rubygems/commit/f637a412a6
This commit is contained in:
Thomas Marshall 2025-06-02 13:10:33 +01:00 committed by Hiroshi SHIBATA
parent f1fe26a334
commit 6839eadd53
No known key found for this signature in database
GPG Key ID: F9CF13417264FAC2

View File

@ -252,4 +252,75 @@ RSpec.describe Bundler::Source::Git::GitProxy do
end
end
end
describe "#checkout" do
context "when the repository isn't cloned" do
before do
allow(path).to receive(:exist?).and_return(false)
end
it "clones the repository" do
allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
expect(git_proxy).to receive(:capture).with([*base_clone_args, "--", uri, path.to_s], nil).and_return(["", "", clone_result])
subject.checkout
end
end
context "when the repository is cloned" do
before do
allow(path).to receive(:exist?).and_return(true)
end
context "with a locked revision" do
let(:revision) { Digest::SHA1.hexdigest("ruby") }
context "when the revision exists locally" do
it "uses the cached revision" do
allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
expect(git_proxy).to receive(:git).with("cat-file", "-e", revision, dir: path).and_return(true)
subject.checkout
end
end
context "when the revision doesn't exist locally" do
it "fetches the specific revision" do
allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
expect(git_proxy).to receive(:git).with("cat-file", "-e", revision, dir: path).and_raise(Bundler::GitError)
expect(git_proxy).to receive(:capture).with(["fetch", "--force", "--quiet", "--no-tags", "--depth", "1", "--", uri, "#{revision}:refs/#{revision}-sha"], path).and_return(["", "", clone_result])
subject.checkout
end
end
end
context "with no explicit ref" do
it "fetches the HEAD revision" do
parsed_revision = Digest::SHA1.hexdigest("ruby")
allow(git_proxy).to receive(:git_local).with("rev-parse", "--abbrev-ref", "HEAD", dir: path).and_return(parsed_revision)
allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
expect(git_proxy).to receive(:capture).with(["fetch", "--force", "--quiet", "--no-tags", "--depth", "1", "--", uri, "refs/heads/#{parsed_revision}:refs/heads/#{parsed_revision}"], path).and_return(["", "", clone_result])
subject.checkout
end
end
context "with a commit ref" do
let(:ref) { Digest::SHA1.hexdigest("ruby") }
it "fetches the specific revision" do
allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
expect(git_proxy).to receive(:capture).with(["fetch", "--force", "--quiet", "--no-tags", "--depth", "1", "--", uri, "#{ref}:refs/#{ref}-sha"], path).and_return(["", "", clone_result])
subject.checkout
end
end
context "with a non-commit ref" do
let(:ref) { "HEAD" }
it "fetches all revisions" do
allow(git_proxy).to receive(:git_local).with("--version").and_return("git version 2.14.0")
expect(git_proxy).to receive(:capture).with(["fetch", "--force", "--quiet", "--no-tags", "--", uri, "refs/*:refs/*"], path).and_return(["", "", clone_result])
subject.checkout
end
end
end
end
end