Added tests for last_revision_commit_of_file

This commit is contained in:
mansimarkaur 2017-07-28 00:37:32 +05:30
parent 551e5dd945
commit d91036e891
2 changed files with 51 additions and 6 deletions

View File

@ -0,0 +1,47 @@
require "utils/git"
describe Git do
before(:all) do
git = HOMEBREW_SHIMS_PATH/"scm/git"
file = "lib/blah.rb"
repo = Pathname.new("repo")
FileUtils.mkpath("repo/lib")
`#{git} init`
FileUtils.touch("repo/#{file}")
File.open(repo.to_s+"/"+file, "w") { |f| f.write("blah") }
`#{git} add repo/#{file}`
`#{git} commit -m"File added"`
@hash1 = `git rev-parse HEAD`
File.open(repo.to_s+"/"+file, "w") { |f| f.write("brew") }
`#{git} add repo/#{file}`
`#{git} commit -m"written to File"`
@hash2 = `git rev-parse HEAD`
end
let(:file) { "lib/blah.rb" }
let(:repo) { Pathname.new("repo") }
# after(:all) do
# FileUtils.rm_rf("repo")
# end
describe "#last_revision_commit_of_file" do
it "sets args as --skip=1 when before_commit is nil" do
expect(described_class.last_revision_commit_of_file(repo, file)).to eq(@hash1[0..6])
end
it "sets args as --skip=1 when before_commit is nil" do
expect(described_class.last_revision_commit_of_file(repo, file, before_commit: "0..3")).to eq(@hash2[0..6])
end
end
describe "#last_revision_of_file" do
it "returns last revision of file" do
expect(described_class.last_revision_of_file(repo, repo.to_s+"/"+file)).to eq("blah")
end
it "returns last revision of file based on before_commit" do
expect(described_class.last_revision_of_file(repo, repo.to_s+"/"+file, before_commit: "0..3")).to eq("brew")
end
end
end

View File

@ -4,7 +4,7 @@ module Git
module_function
def last_revision_commit_of_file(repo, file, before_commit: nil)
args = [before_commit.nil? ? "--skip=1" : before_commit.split("..").first]
args = ["--skip=#{before_commit.nil? ? 1 : before_commit.split("..").first}"]
out, = Open3.capture3(
HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo,
@ -16,11 +16,10 @@ module Git
def last_revision_of_file(repo, file, before_commit: nil)
relative_file = Pathname(file).relative_path_from(repo)
commit_hash = last_revision_commit_of_file(repo, file, before_commit: before_commit)
commit_hash = last_revision_commit_of_file(repo, relative_file, before_commit: before_commit)
out, = Open3.capture3(
HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo,
"show", "#{commit_hash}:#{relative_file}"
"show", "#{commit_hash}:#{file}"
)
out
end
@ -28,8 +27,7 @@ end
module Utils
def self.git_available?
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"
end
def self.git_path