Added clear git version cache method

This commit is contained in:
mansimarkaur 2017-08-25 17:19:34 +05:30
parent 372a422305
commit c695dffd37
2 changed files with 37 additions and 41 deletions

View File

@ -1,44 +1,39 @@
require "utils/git" require "utils/git"
describe Git do describe Git do
before(:all) do before(:each) do
git = HOMEBREW_SHIMS_PATH/"scm/git" git = HOMEBREW_SHIMS_PATH/"scm/git"
@file = "lib/blah.rb" @file = "blah.rb"
@repo = Pathname.new("repo")
(@repo/"lib").mkpath HOMEBREW_CACHE.cd do
system git, "init" system git, "init"
FileUtils.touch(@repo/@file)
File.open(@repo/@file, "w") { |f| f.write("blah") } File.open(@file, "w") { |f| f.write("blah") }
system git, "add", @repo/@file system git, "add", HOMEBREW_CACHE/@file
system git, "commit", "-m", "'File added'" system git, "commit", "-m", "'File added'"
@h1 = `git rev-parse HEAD` @h1 = `git rev-parse HEAD`
File.open(@repo/@file, "w") { |f| f.write("brew") } File.open(@file, "w") { |f| f.write("brew") }
system git, "add", @repo/@file system git, "add", HOMEBREW_CACHE/@file
system git, "commit", "-m", "'written to File'" system git, "commit", "-m", "'written to File'"
@h2 = `git rev-parse HEAD` @h2 = `git rev-parse HEAD`
end end
end
let(:hash1) { @h1[0..6] } let(:hash1) { @h1[0..6] }
let(:hash2) { @h2[0..6] } let(:hash2) { @h2[0..6] }
after(:all) do
FileUtils.rm_rf(@repo)
end
describe "#last_revision_commit_of_file" do describe "#last_revision_commit_of_file" do
it "gives last revision commit when before_commit is nil" do it "gives last revision commit when before_commit is nil" do
expect( expect(
described_class.last_revision_commit_of_file(@repo, @file), described_class.last_revision_commit_of_file(HOMEBREW_CACHE, @file),
).to eq(hash1) ).to eq(hash1)
end end
it "gives revision commit based on before_commit when it is not nil" do it "gives revision commit based on before_commit when it is not nil" do
expect( expect(
described_class.last_revision_commit_of_file(@repo, described_class.last_revision_commit_of_file(HOMEBREW_CACHE,
@file, @file,
before_commit: hash2), before_commit: hash2),
).to eq(hash2) ).to eq(hash2)
@ -48,14 +43,14 @@ describe Git do
describe "#last_revision_of_file" do describe "#last_revision_of_file" do
it "returns last revision of file" do it "returns last revision of file" do
expect( expect(
described_class.last_revision_of_file(@repo, described_class.last_revision_of_file(HOMEBREW_CACHE,
@repo/@file), HOMEBREW_CACHE/@file),
).to eq("blah") ).to eq("blah")
end end
it "returns last revision of file based on before_commit" do it "returns last revision of file based on before_commit" do
expect( expect(
described_class.last_revision_of_file(@repo, @repo/@file, described_class.last_revision_of_file(HOMEBREW_CACHE, HOMEBREW_CACHE/@file,
before_commit: "0..3"), before_commit: "0..3"),
).to eq("brew") ).to eq("brew")
end end
@ -64,9 +59,7 @@ end
describe Utils do describe Utils do
before(:each) do before(:each) do
if described_class.instance_variable_defined?(:@git) described_class.clear_git_version_cache
described_class.send(:remove_instance_variable, :@git)
end
end end
describe "::git_available?" do describe "::git_available?" do
@ -75,14 +68,14 @@ describe Utils do
end end
it "returns false if git --version command does not succeed" do it "returns false if git --version command does not succeed" do
stub_const("HOMEBREW_SHIMS_PATH", mktmpdir/"shim") stub_const("HOMEBREW_SHIMS_PATH", HOMEBREW_PREFIX/"bin/shim")
expect(described_class.git_available?).to be_falsey expect(described_class.git_available?).to be_falsey
end end
end end
describe "::git_path" do describe "::git_path" do
it "returns nil when git is not available" do it "returns nil when git is not available" do
stub_const("HOMEBREW_SHIMS_PATH", mktmpdir/"shim") stub_const("HOMEBREW_SHIMS_PATH", HOMEBREW_PREFIX/"bin/shim")
expect(described_class.git_path).to eq(nil) expect(described_class.git_path).to eq(nil)
end end
@ -93,7 +86,7 @@ describe Utils do
describe "::git_version" do describe "::git_version" do
it "returns nil when git is not available" do it "returns nil when git is not available" do
stub_const("HOMEBREW_SHIMS_PATH", mktmpdir/"shim") stub_const("HOMEBREW_SHIMS_PATH", HOMEBREW_PREFIX/"bin/shim")
expect(described_class.git_path).to eq(nil) expect(described_class.git_path).to eq(nil)
end end
@ -109,7 +102,7 @@ describe Utils do
context "when git is not already available" do context "when git is not already available" do
before do before do
stub_const("HOMEBREW_SHIMS_PATH", mktmpdir/"shim") stub_const("HOMEBREW_SHIMS_PATH", HOMEBREW_PREFIX/"bin/shim")
end end
it "can't install brewed git if homebrew/core is unavailable" do it "can't install brewed git if homebrew/core is unavailable" do
@ -118,7 +111,7 @@ describe Utils do
end end
it "raises error if can't install git" do it "raises error if can't install git" do
stub_const("HOMEBREW_BREW_FILE", mktmpdir/"brew") stub_const("HOMEBREW_BREW_FILE", HOMEBREW_PREFIX/"bin/brew")
expect { described_class.ensure_git_installed! }.to raise_error("Git is unavailable") expect { described_class.ensure_git_installed! }.to raise_error("Git is unavailable")
end end
@ -131,24 +124,23 @@ describe Utils do
describe "::git_remote_exists" do describe "::git_remote_exists" do
it "returns true when git is not available" do it "returns true when git is not available" do
stub_const("HOMEBREW_SHIMS_PATH", mktmpdir/"shim") stub_const("HOMEBREW_SHIMS_PATH", HOMEBREW_PREFIX/"bin/shim")
expect(described_class.git_remote_exists("blah")).to be_truthy expect(described_class.git_remote_exists("blah")).to be_truthy
end end
context "when git is available" do context "when git is available" do
it "returns true when git remote exists", :needs_network do it "returns true when git remote exists", :needs_network do
git = HOMEBREW_SHIMS_PATH/"scm/git" git = HOMEBREW_SHIMS_PATH/"scm/git"
repo = Pathname.new("hey") url = "http://github.com/Homebrew/homebrew.github.io"
repo = HOMEBREW_CACHE/"hey"
repo.mkpath repo.mkpath
system "cd", repo repo.cd do
system git, "init" system git, "init"
system git, "remote", "add", "origin", "http://github.com/Homebrew/homebrew.github.io" system git, "remote", "add", "origin", url
system "cd .." end
expect(described_class.git_remote_exists("http://github.com/Homebrew/homebrew.github.io")).to be_truthy expect(described_class.git_remote_exists(url)).to be_truthy
FileUtils.rm_rf(repo)
end end
it "returns false when git remote does not exist" do it "returns false when git remote does not exist" do

View File

@ -19,13 +19,17 @@ module Git
commit_hash = last_revision_commit_of_file(repo, relative_file, before_commit: before_commit) commit_hash = last_revision_commit_of_file(repo, relative_file, before_commit: before_commit)
out, = Open3.capture3( out, = Open3.capture3(
HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo, HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo,
"show", "#{commit_hash}:#{file}" "show", "#{commit_hash}:#{relative_file}"
) )
out out
end end
end end
module Utils module Utils
def self.clear_git_version_cache
remove_instance_variable(:@git) if instance_variable_defined?(:@git)
end
def self.git_available? def self.git_available?
return @git if instance_variable_defined?(:@git) return @git if instance_variable_defined?(:@git)
@git = quiet_system HOMEBREW_SHIMS_PATH/"scm/git", "--version" @git = quiet_system HOMEBREW_SHIMS_PATH/"scm/git", "--version"