1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
require "fileutils"
require "pathname"
require "securerandom"
require "librarian/posix"
require "librarian/source/git/repository"
require "librarian/mock/environment"
require "support/project_path_macro"
describe Librarian::Source::Git::Repository do
include Support::ProjectPathMacro
let(:env) { Librarian::Mock::Environment.new }
let(:tmp_path) { project_path + "tmp/spec/functional/source/git/repository" }
after { tmp_path.rmtree if tmp_path && tmp_path.exist? }
let(:git_source_path) { tmp_path + SecureRandom.hex(16) }
let(:branch) { "the-branch" }
let(:tag) { "the-tag" }
let(:atag) { "the-atag" }
def cmd!(command)
Librarian::Posix.run! command
end
def git!(command)
cmd!([described_class.bin] + command)
end
before do
git_source_path.mkpath
Dir.chdir(git_source_path) do
git! %W[init]
git! %W[config user.name Simba]
git! %W[config user.email simba@savannah-pride.gov]
# master
FileUtils.touch "butter.txt"
git! %W[add butter.txt]
git! %W[commit -m #{"Initial Commit"} --no-gpg-sign]
# branch
git! %W[checkout -b #{branch} --quiet]
FileUtils.touch "jam.txt"
git! %W[add jam.txt]
git! %W[commit -m #{"Branch Commit"} --no-gpg-sign]
git! %W[checkout master --quiet]
# tag/atag
git! %W[checkout -b deletable --quiet]
FileUtils.touch "jelly.txt"
git! %W[add jelly.txt]
git! %W[commit -m #{"Tag Commit"} --no-gpg-sign]
git! %W[tag #{tag}]
git! %W[tag -am #{"Annotated Tag Commit"} #{atag}]
git! %W[checkout master --quiet]
git! %W[branch -D deletable]
end
end
describe ".bin" do
specify { expect(described_class.bin).to_not be_empty }
end
describe ".git_version" do
specify { expect(described_class.git_version).to match( /^\d+(\.\d+)+$/ ) }
end
context "the original" do
subject { described_class.new(env, git_source_path) }
it "should recognize it" do
expect(subject).to be_git
end
it "should not list any remotes for it" do
expect(subject.remote_names).to be_empty
end
it "should not list any remote branches for it" do
expect(subject.remote_branch_names).to be_empty
end
it "should have divergent shas for master, branch, tag, and atag" do
revs = %W[ master #{branch} #{tag} #{atag} ]
rev_parse = proc{|rev| git!(%W[rev-parse #{rev} --quiet]).strip}
shas = Dir.chdir(git_source_path){revs.map(&rev_parse)}
expect(shas.map(&:class).uniq).to eq [String]
expect(shas.map(&:size).uniq).to eq [40]
expect(shas.uniq).to eq shas
end
end
context "a clone" do
let(:git_clone_path) { tmp_path + SecureRandom.hex(16) }
subject { described_class.clone!(env, git_clone_path, git_source_path) }
let(:master_sha) { subject.hash_from("origin", "master") }
let(:branch_sha) { subject.hash_from("origin", branch) }
let(:tag_sha) { subject.hash_from("origin", tag) }
let(:atag_sha) { subject.hash_from("origin", atag) }
it "should recognize it" do
expect(subject).to be_git
end
it "should have a single remote for it" do
expect(subject.remote_names.size).to eq 1
end
it "should have a remote with the expected name" do
expect(subject.remote_names.first).to eq "origin"
end
it "should have the remote branch" do
expect(subject.remote_branch_names["origin"]).to include branch
end
it "should be checked out on the master" do
expect(subject).to be_checked_out(master_sha)
end
context "checking for commits" do
it "has the master commit" do
expect(subject).to have_commit(master_sha)
end
it "has the branch commit" do
expect(subject).to have_commit(branch_sha)
end
it "has the tag commit" do
expect(subject).to have_commit(tag_sha)
end
it "has the atag commit" do
expect(subject).to have_commit(atag_sha)
end
it "does not have a made-up commit" do
expect(subject).to_not have_commit(SecureRandom.hex(20))
end
it "does not have a tree commit" do
master_tree_sha = Dir.chdir(git_source_path) do
git!(%W[log -1 --no-color --format=tformat:%T master]).strip
end
expect(master_tree_sha).to match(/\A[0-9a-f]{40}\z/) # sanity
expect(subject).to_not have_commit(master_tree_sha)
end
end
context "checking out the branch" do
before do
subject.checkout! branch
end
it "should be checked out on the branch" do
expect(subject).to be_checked_out(branch_sha)
end
it "should not be checked out on the master" do
expect(subject).to_not be_checked_out(master_sha)
end
end
context "checking out the tag" do
before do
subject.checkout! tag
end
it "should be checked out on the tag" do
expect(subject).to be_checked_out(tag_sha)
end
it "should not be checked out on the master" do
expect(subject).to_not be_checked_out(master_sha)
end
end
context "checking out the annotated tag" do
before do
subject.checkout! atag
end
it "should be checked out on the annotated tag" do
expect(subject).to be_checked_out(atag_sha)
end
it "should not be checked out on the master" do
expect(subject).to_not be_checked_out(master_sha)
end
end
end
end
|