Permalink
Browse files

create_ref specs pass with good coverage

  • Loading branch information...
1 parent a932ef2 commit ff9740096d1364db9dc41bf572cf1acb204e8b98 @zilkey committed Sep 4, 2010
View
@@ -1,12 +1,13 @@
module AutoTagger
- class StageCannotBeBlankError < StandardError;
- end
def self.version
File.read(File.expand_path(File.join(__FILE__, "/../../../VERSION")))
end
class Base
+ class StageCannotBeBlankError < StandardError;
+ end
+
attr_reader :options
def initialize(options)
@@ -22,18 +23,20 @@ def repo
def last_ref_from_previous_stage
return unless previous_stage
- matcher = /refs\/#{Regexp.escape(configuration.ref_path)}\/(#{Regexp.escape(previous_stage)})\/.*/
+ ref_path = Regexp.escape(configuration.ref_path)
+ stage = Regexp.escape(previous_stage)
+ matcher = /refs\/#{ref_path}\/(#{stage})\/.*/
repo.refs.all.select do |ref|
(ref.name =~ matcher) ? ref : nil
end.last
end
def create_ref(commit = nil)
- commit ||= repo.latest_commit_sha
ensure_stage
- repo.refs.fetch("refs/#{configuration.ref_path}/*", configuration.remote) if configuration.fetch_refs?
- new_tag = repo.refs.create(commit, ref_name)
- repo.refs.push("refs/#{configuration.ref_path}/*", configuration.remote) if configuration.push_refs?
+ pattern = "refs/#{configuration.ref_path}/*"
+ repo.refs.fetch(pattern, configuration.remote) if configuration.fetch_refs?
+ new_tag = repo.refs.create(commit || repo.latest_commit_sha, ref_name)
+ repo.refs.push(pattern, configuration.remote) if configuration.push_refs?
new_tag
end
@@ -30,7 +30,7 @@ def settings
end
def stages
- stages = settings[:stages]
+ stages = settings[:stages] || []
stages = stages.to_s.split(",").map { |stage| stage.strip } if stages.is_a?(String)
stages.reject { |stage| stage.to_s == "" }
end
@@ -44,22 +44,15 @@
end
end
-# commit ||= repo.latest_commit_sha
-# ensure_stage
-# repo.refs.fetch("refs/#{configuration.ref_path}/*", configuration.remote) if configuration.fetch_refs?
-# new_tag = repo.refs.create(commit, ref_name)
-# repo.refs.push("refs/#{configuration.ref_path}/*", configuration.remote) if configuration.push_refs?
-# new_tag
describe "#create_ref" do
- it "creates a ref" do
+ it "creates a ref with the given sha and returns the ref" do
base = AutoTagger::Base.new :stages => ["ci", "demo", "production"], :stage => "demo"
base.stub(:timestamp).and_return("20081010")
- base.repo.should_receive(:latest_commit_sha).and_return("abc123")
base.repo.stub(:exec) { true }
base.repo.should_receive(:exec).with("update-ref refs/tags/demo/20081010 abc123")
- ref = base.create_ref
+ ref = base.create_ref "abc123"
ref.name.should == "refs/tags/demo/20081010"
ref.sha.should == "abc123"
end
@@ -76,6 +69,49 @@
ref.name.should == "refs/tags/demo/20081010"
ref.sha.should == "abc123"
end
+
+ it "respects the passed in date separator" do
+ time = Time.now.utc
+ timestamp = time.strftime("%Y-%m-%d-%H-%M-%S")
+ base = AutoTagger::Base.new :stages => ["ci"], :stage => "ci", :date_separator => "-"
+ base.repo.stub(:exec) { true }
+ base.repo.should_receive(:exec).with("update-ref refs/tags/ci/#{timestamp} abc123")
+ base.create_ref "abc123"
+ end
+
+ it "raises an error if the stage is not set" do
+ proc do
+ AutoTagger::Base.new({}).create_ref
+ end.should raise_error(AutoTagger::Base::StageCannotBeBlankError)
+ end
+
+ it "fetches tags before creating tags" do
+ base = AutoTagger::Base.new :stages => ["ci"], :stage => "ci"
+ base.repo.stub(:exec) { true }
+ base.repo.should_receive(:exec).with("fetch origin refs/tags/*:refs/tags/*")
+ base.create_ref "abc123"
+ end
+
+ it "does not fetch tags before creating tags if fetch tags is false" do
+ base = AutoTagger::Base.new :stages => ["ci"], :stage => "ci", :fetch_tags => false
+ base.repo.stub(:exec) { true }
+ base.repo.should_receive(:exec).with("push origin refs/tags/*:refs/tags/*")
+ base.create_ref "abc123"
+ end
+
+ it "pushes tags before creating tags" do
+ base = AutoTagger::Base.new :stages => ["ci"], :stage => "ci"
+ base.repo.stub(:exec) { true }
+ base.repo.should_receive(:exec).with("push origin refs/tags/*:refs/tags/*")
+ base.create_ref "abc123"
+ end
+
+ it "does not push tags before creating tags if push tags is false" do
+ base = AutoTagger::Base.new :stages => ["ci"], :stage => "ci", :push_tags => false
+ base.repo.stub(:exec) { true }
+ base.repo.should_receive(:exec).with("push origin refs/tags/*:refs/tags/*")
+ base.create_ref "abc123"
+ end
end
# describe "#cleanup" do
@@ -103,6 +103,11 @@
config = AutoTagger::Configuration.new :stages => ["demo", "production"]
config.stage.should == "production"
end
+
+ it "returns nil if stage and stages are not passed in" do
+ config = AutoTagger::Configuration.new
+ config.stage.should be_nil
+ end
end
describe "#date_separator" do

0 comments on commit ff97400

Please sign in to comment.