aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew
diff options
context:
space:
mode:
authormansimarkaur2017-07-28 00:37:32 +0530
committermansimarkaur2017-08-27 01:23:32 +0530
commitd91036e8912b31e3eb145b1cb3964a321a587c4f (patch)
tree96e55ed4a4fac27d24af276b3945372f441e933b /Library/Homebrew
parent551e5dd94578062e0ab42bc56e0fdd106807839b (diff)
downloadbrew-d91036e8912b31e3eb145b1cb3964a321a587c4f.tar.bz2
Added tests for last_revision_commit_of_file
Diffstat (limited to 'Library/Homebrew')
-rw-r--r--Library/Homebrew/test/utils/git_spec.rb47
-rw-r--r--Library/Homebrew/utils/git.rb10
2 files changed, 51 insertions, 6 deletions
diff --git a/Library/Homebrew/test/utils/git_spec.rb b/Library/Homebrew/test/utils/git_spec.rb
new file mode 100644
index 000000000..d4eb97820
--- /dev/null
+++ b/Library/Homebrew/test/utils/git_spec.rb
@@ -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
diff --git a/Library/Homebrew/utils/git.rb b/Library/Homebrew/utils/git.rb
index 43d93b64e..3668b9d11 100644
--- a/Library/Homebrew/utils/git.rb
+++ b/Library/Homebrew/utils/git.rb
@@ -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