Permalink
Browse files

Adds tests for tampon helpers

  • Loading branch information...
1 parent d6a448d commit 4cf09930c37e4561a5b86d6a546d557da4462a0e @esmarkowski committed Aug 18, 2011
View
10 Gemfile
@@ -2,12 +2,16 @@ source "http://rubygems.org"
# Add dependencies required to use your gem here.
# Example:
# gem "activesupport", ">= 2.3.5"
-
+gem 'capistrano'
+gem 'rake', '0.8.7'
+gem 'stringex'
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
- gem "shoulda", ">= 0"
- gem "yard", "~> 0.6.0"
+ gem "shoulda", "~> 3.0.0.beta2"
+ gem 'active_support'
+ gem 'i18n'
+ gem 'minitest-rg'
gem "cucumber", ">= 0"
gem "bundler", "~> 1.0.0"
gem "jeweler", "~> 1.6.4"
View
2 Rakefile
@@ -45,5 +45,3 @@ Cucumber::Rake::Task.new(:features)
task :default => :test
-require 'yard'
-YARD::Rake::YardocTask.new
View
11 features/tampon.feature
@@ -1,9 +1,6 @@
-Feature: something something
- In order to something something
- A user something something
- something something something
+Feature: Manage release tags
- Scenario: something something
- Given inspiration
- When I create a sweet new gem
+ Scenario: Attemping to deploy a production release
+ Given I want to deploy to production
+ When I type
Then everyone should see how awesome I am
View
59 lib/capistrano/helpers/tampon_helper.rb
@@ -0,0 +1,59 @@
+module Capistrano
+ module Helpers
+ module TamponHelper
+
+ def who
+ (`git config user.name` || `whoami`).chomp
+ end
+
+ def tags
+ `git tag`.split("\n").compact
+ end
+
+ def non_release_tags
+ tags - releases
+ end
+
+ def current_branch
+ branches.select{|b| b =~ /^\*\s/}.first.gsub(/^\*\s/,"")
+ end
+
+ def branches
+ `git branch --no-color`.split("\n")
+ end
+
+ def version_tag_prefix
+ `git config gitflow.prefix.versiontag`.split("\n").first
+ end
+
+ def releases
+ tags.select{|t| t =~ /^#{version_tag_prefix}(\d+)/}
+ end
+
+ def latest_release
+ releases.sort{|x,y| x.split(version_tag_prefix).last <=> y.split(version_tag_prefix).last}.last
+ end
+
+ def available_tags
+ Capistrano::CLI.ui.say "Available Tags:"
+ Capistrano::CLI.ui.say "#{non_release_tags.join("\n")}"
+ end
+
+ def available_releases
+ Capistrano::CLI.ui.say "\nAvailable Releases:"
+ Capistrano::CLI.ui.say "#{releases.join("\n")}"
+ end
+
+ def deploy_from
+ if stage == :production
+ Capistrano::CLI.ui.ask "\nRelease to deploy: [#{current_branch}] ".color(:yellow).bright.to_url
+ elsif stage == :staging
+ Capistrano::CLI.ui.ask "\nTag or release to deploy (make sure to push the tag/release first): [#{current_branch}] ".color(:yellow).bright.to_url
+ end
+ end
+
+
+
+ end
+ end
+end
View
169 lib/capistrano/tampon.rb
@@ -0,0 +1,169 @@
+require 'capistrano'
+require 'capistrano/helpers/tampon_helper'
+require 'stringex'
+
+module Capistrano
+ class Tampon
+ include Capistrano::Helpers::TamponHelper
+
+ def self.load_into(capistrano_configuration)
+ capistrano_configuration.load do
+ before "deploy:update_code", "gitflow:calculate_tag"
+ before "gitflow:calculate_tag", "gitflow:verify_up_to_date"
+
+ namespace :tampon do
+
+ def next_tag
+ hwhen = Date.today.to_s
+ what = Capistrano::CLI.ui.ask("What does this release introduce? (this will be normalized and used in the tag for this release) ").to_url
+ "#{hwhen}-#{who}-#{what}"
+ end
+
+ def using_git?
+ fetch(:scm, :git).to_sym == :git
+ end
+
+ task :verify_up_to_date do
+ if using_git?
+ set :local_branch, `git branch --no-color 2> /dev/null | sed -e '/^[^*]/d'`.gsub(/\* /, '').chomp
+ set :local_sha, `git log --pretty=format:%H HEAD -1`.chomp
+ set :origin_sha, `git log --pretty=format:%H #{local_branch} -1`
+ unless local_sha == origin_sha
+ abort """
+Your #{local_branch} branch is not up to date with origin/#{local_branch}.
+Please make sure you have pulled and pushed all code before deploying:
+
+ git pull origin #{local_branch}
+ # run tests, etc
+ git push origin #{local_branch}
+
+ """
+ end
+ end
+ end
+
+ desc "Calculate the tag to deploy"
+ task :calculate_tag do
+ if using_git?
+ # make sure we have any other deployment tags that have been pushed by others so our auto-increment code doesn't create conflicting tags
+ `git fetch`
+
+ if respond_to?("tag_#{stage}")
+ send "tag_#{stage}"
+
+ system "git push --tags origin #{local_branch}"
+ if $? != 0
+ abort "git push failed"
+ end
+ end
+ end
+ end
+
+ desc "Show log between most recent staging tag (or given tag=XXX) and last production release."
+ task :commit_log do
+ from_tag = if stage == :production
+ last_production_tag
+ elsif stage == :staging
+ last_staging_tag
+ else
+ abort "Unsupported stage #{stage}"
+ end
+
+ # no idea how to properly test for an optional cap argument a la '-s tag=x'
+ to_tag = capistrano_configuration[:tag]
+ to_tag ||= begin
+ puts "Calculating 'end' tag for :commit_log for '#{stage}'"
+ to_tag = if stage == :production
+ last_staging_tag
+ elsif stage == :staging
+ 'master'
+ else
+ abort "Unsupported stage #{stage}"
+ end
+ end
+
+
+ command = if `git config remote.origin.url` =~ /git@github.com:(.*)\/(.*).git/
+ "open https://github.com/#{$1}/#{$2}/compare/#{from_tag}...#{to_tag || 'master'}"
+ else
+ log_subcommand = if ENV['git_log_command'] && ENV['git_log_command'].strip != ''
+ ENV['git_log_command']
+ else
+ 'log'
+ end
+ "git #{log_subcommand} #{fromTag}..#{toTag}"
+ end
+ puts command
+ system command
+ end
+
+ desc "Mark the current code as a staging/qa release"
+ task :tag_staging do
+ current_sha = `git log --pretty=format:%H HEAD -1`
+ last_staging_tag_sha = if last_staging_tag
+ `git log --pretty=format:%H #{last_staging_tag} -1`
+ end
+
+ if last_staging_tag_sha == current_sha
+ puts "Not re-tagging staging because latest tag (#{last_staging_tag}) already points to HEAD"
+ new_staging_tag = last_staging_tag
+ else
+ new_staging_tag = next_staging_tag
+ puts "Tagging current branch for deployment to staging as '#{new_staging_tag}'"
+ system "git tag -a -m 'tagging current code for deployment to staging' #{new_staging_tag}"
+ end
+
+ set :branch, new_staging_tag
+ end
+
+ desc "Push the approved tag to production. Pass in tag to deploy with '-s tag=staging-YYYY-MM-DD-X-feature'."
+ task :tag_production do
+ promote_to_production_tag = capistrano_configuration[:tag] || last_staging_tag
+
+ unless promote_to_production_tag && promote_to_production_tag =~ /staging-.*/
+ abort "Couldn't find a staging tag to deploy; use '-s tag=staging-YYYY-MM-DD.X'"
+ end
+ unless last_tag_matching(promote_to_production_tag)
+ abort "Staging tag #{promote_to_production_tag} does not exist."
+ end
+
+ promote_to_production_tag =~ /^staging-(.*)$/
+ new_production_tag = "production-#{$1}"
+
+ if new_production_tag == last_production_tag
+ puts "Not re-tagging #{last_production_tag} because it already exists"
+ really_deploy = Capistrano::CLI.ui.ask("Do you really want to deploy #{last_production_tag}? [y/N]").to_url
+
+ exit(1) unless really_deploy =~ /^[Yy]$/
+ else
+ puts "Preparing to promote staging tag '#{promote_to_production_tag}' to '#{new_production_tag}'"
+ unless capistrano_configuration[:tag]
+ really_deploy = Capistrano::CLI.ui.ask("Do you really want to deploy #{new_production_tag}? [y/N]").to_url
+
+ exit(1) unless really_deploy =~ /^[Yy]$/
+ end
+ puts "Promoting staging tag #{promote_to_production_tag} to production as '#{new_production_tag}'"
+ system "git tag -a -m 'tagging current code for deployment to production' #{new_production_tag} #{promote_to_production_tag}"
+ end
+
+ set :branch, new_production_tag
+ end
+ end
+
+ namespace :deploy do
+ namespace :pending do
+ task :compare do
+ gitflow.commit_log
+ end
+ end
+ end
+
+ end
+
+ end
+ end
+end
+
+if Capistrano::Configuration.instance
+ Capistrano::Tampon.load_into(Capistrano::Configuration.instance)
+end
View
0 lib/tampon.rb
No changes.
View
4 test/helper.rb
@@ -9,10 +9,12 @@
end
require 'test/unit'
require 'shoulda'
+require 'minitest-rg'
+require 'active_support/core_ext'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
-require 'tampon'
+require 'capistrano/tampon'
class Test::Unit::TestCase
end
View
5 test/test_tampon.rb
@@ -1,7 +1,6 @@
require 'helper'
class TestTampon < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
- end
+
+
end
View
98 test/test_tampon_helpers.rb
@@ -0,0 +1,98 @@
+require 'helper'
+
+class TestTamponHelpers < Test::Unit::TestCase
+ context "when intialized" do
+ setup do
+ class HelperMethods
+ include Capistrano::Helpers::TamponHelper
+ end
+
+ @test_class = HelperMethods.new
+
+ end
+ context "tags" do
+
+
+ should "respond_to who" do
+ assert_respond_to @test_class, :who
+ end
+
+ should "not be blank" do
+ puts "\ncurrent user: #{@test_class.who}"
+ assert_equal false, @test_class.who.blank?
+ end
+
+ should 'tags should be an Array' do
+ puts "\nall tags: #{@test_class.tags}"
+ assert @test_class.tags.kind_of?(Array)
+ end
+
+ end
+
+ context "then non-release tags" do
+
+ should "respond_to non_release_tags" do
+ assert_respond_to @test_class, :non_release_tags
+ end
+
+ should "return an Array" do
+ assert @test_class.non_release_tags.kind_of?(Array)
+ end
+
+ end
+
+ context "branches" do
+
+ should "respond_to :branches" do
+ assert_respond_to @test_class, :branches
+ end
+
+ should 'not be blank' do
+ assert_equal false, @test_class.branches.empty?
+ end
+
+ should "respond to :current_branch" do
+ assert_respond_to @test_class, :current_branch
+ end
+
+ should "show the current branch" do
+ puts "\ncurrent branch: #{@test_class.current_branch}"
+ assert_equal "master", @test_class.current_branch
+ end
+
+ end
+
+ context "version tags" do
+
+ should "respond_to version_tag_prefix" do
+ assert_respond_to @test_class, :version_tag_prefix
+ end
+
+ should "be 'v'" do
+ assert_equal "v", @test_class.version_tag_prefix
+ end
+
+ end
+
+ context "releases" do
+
+ should "respond to releases" do
+ assert_respond_to @test_class, :releases
+ end
+
+ should "only begin with the version tag prefix" do
+
+ end
+
+ should 'display all available releases' do
+
+ end
+
+ should 'display latest release' do
+ assert_equal 'v1.4.2', @test_class.latest_release
+ end
+
+ end
+
+ end
+end

0 comments on commit 4cf0993

Please sign in to comment.