Skip to content

Commit

Permalink
Initial implementation and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
technicalpickles committed Jul 22, 2010
1 parent f78c02d commit 8f1723f
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 7 deletions.
120 changes: 115 additions & 5 deletions README.rdoc
@@ -1,15 +1,125 @@
= capistrano-spec

Description goes here.
Capistrano... the final frontier of testing... well, maybe not final, but it is a frontier. I had set out to do some bug fixing and some BDDing on some of my capistrano code, but found it wasn't really obvious how to do so. As a result, I set out to write capistrano-spec and document how to test capistrano libraries.

== Designing your capistrano extension

In the wild, you'll mostly commonly come across two patterns:

* files living under recipes/* that are autoloaded
* files living under lib that are required from config/deploy.rb

In these files, you can start using the capistrano top-level methods, like `namespace` or `task`, like:

# in recipes/speak.rb or lib/speak.rb
task :speak do
set :message, 'oh hai'
puts message
end

Capistrano does some trickery to `require` and `load` so that if you `require` or `load`, the file is ran in the context of a Capistrano::Configuration, where all the `task` and `namespace` methods you know and love will.

Some consider this a little gross, because it'd be easy to accidentally require/load this without being in the context of a Capistrano::Configuration. The answer to this is to pull use `Capistrano::Configuration.instance` to make sure it's evaluted in that context:

# in recipes/speak.rb or lib/speak.rb
Capistrano::Configuration.instance(true).load do
task :speak do
set :message, 'oh hai'
puts message
end
end

There's a problem though: it's not particular testable. You can't take some `Capistrano::Configuration` and easily bring your task into it.

So, here's what I recommend instead: create a method for taking a configuration, and adding your goodies to it.

require 'capistrano'
module Capistrano
module Speak
def self.load_into(configuration)
configuration.load do
task :speak do
set :message, 'oh hai'
puts message
end
end
end
end
end

# may as well load it if we have it
if Capistrano::Configuration.instance
Capistrano::Speak.load_into(Capistrano::Configuration.instance)
end

Now, we're going to be able to test this. Behold!

== Testing

Alright, we can start testing by making Capistrano::Configuration and load Capistrano::Speak into it.

describe Capistrano::Speak, "loaded into a configuration" do
before do
@configuration = Capistrano::Configuration.new
Capistrano::Speak.load_into(@configuration)
end

end


Now you have access to a configuration, so you can start poking around the `@configuration` object as you see fit.

Now, remember, by `set`ting values, you can access them using `fetch`:

before do
@configuration.set :foo, 'bar'
end

it "should define foo" do
@configuration.fetch(:foo).should == 'bar'
end

You can also find and execute tasks, so you can verify if you successfully set a value:


describe 'speak task' do
before do
@configuration.find_and_execute_task('speak')
end

it "should define message" do
@configuration.fetch(:message).should == 'oh hai'
end
end

One thing you might be wondering now is... that's cool, but what about working with remote servers? I have just the trick for you: extensions to Capistrano::Configuration to track what files were up or downloaded and what commands were run. Now, this is no substitution for manually testing your capistrano recipe by running it on the server, but it is good for sanity checking.

before do
@configuration = Capistrano::Configuration.new
@configuration.extend(Capistrano::Spec::ConfigurationExtension)
end

it "should run yes" do
@configuration.run "yes"
@configuration.should have_run("yes")
end

it "should upload foo" do
@configuration.upload 'foo', '/tmp/foo'
@configuration.should have_uploaded('foo').to('/tmp/foo')
end

it "should have gotten" do
@configuration.get '/tmp/bar', 'bar'
@configuration.should have_gotten('/tmp/bar').to('bar')
end

== Note on Patches/Pull Requests

* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a
future version unintentionally.
* Commit, do not mess with rakefile, version, or history.
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
* Add tests for it. This is important so I don't break it in a future version unintentionally.
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
* Send me a pull request. Bonus points for topic branches.

== Copyright
Expand Down
6 changes: 4 additions & 2 deletions Rakefile
Expand Up @@ -5,8 +5,10 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "capistrano-spec"
gem.summary = %Q{TODO: one-line summary of your gem}
gem.description = %Q{TODO: longer description of your gem}
gem.version = '0.1.0'

gem.summary = %Q{Test your capistrano recipes}
gem.description = %Q{Helpers and matchers for capistrano}
gem.email = "josh@technicalpickles.com"
gem.homepage = "http://github.com/technicalpickles/capistrano-spec"
gem.authors = ["Joshua Nichols"]
Expand Down
53 changes: 53 additions & 0 deletions capistrano-spec.gemspec
@@ -0,0 +1,53 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{capistrano-spec}
s.version = "0.1.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Joshua Nichols"]
s.date = %q{2010-07-20}
s.description = %q{Helpers and matchers for capistrano}
s.email = %q{josh@technicalpickles.com}
s.extra_rdoc_files = [
"LICENSE",
"README.rdoc"
]
s.files = [
".document",
".gitignore",
"LICENSE",
"README.rdoc",
"Rakefile",
"lib/capistrano-spec.rb",
"spec/capistrano-spec_spec.rb",
"spec/spec.opts",
"spec/spec_helper.rb"
]
s.homepage = %q{http://github.com/technicalpickles/capistrano-spec}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.6}
s.summary = %q{Test your capistrano recipes}
s.test_files = [
"spec/capistrano-spec_spec.rb",
"spec/spec_helper.rb"
]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
else
s.add_dependency(%q<rspec>, [">= 1.2.9"])
end
else
s.add_dependency(%q<rspec>, [">= 1.2.9"])
end
end

1 change: 1 addition & 0 deletions lib/capistrano-spec.rb
@@ -0,0 +1 @@
require 'capistrano/spec'
167 changes: 167 additions & 0 deletions lib/capistrano/spec.rb
@@ -0,0 +1,167 @@
module Capistrano
module Spec
module ConfigurationExtension
def get(remote_path, path, options={}, &block)
gets[remote_path] = {:path => path, :options => options, :block => block}
end

def gets
@gets ||= {}
end

def run(cmd, options={}, &block)
runs[cmd] = {:options => options, :block => block}
end

def runs
@runs ||= {}
end

def upload(from, to, options={}, &block)
uploads[from] = {:to => to, :options => options, :block => block}
end

def uploads
@uploads ||= {}
end

end

module Helpers
def find_callback(configuration, on, task)
if task.kind_of?(String)
task = configuration.find_task(task)
end

callbacks = configuration.callbacks[on]

callbacks && callbacks.select do |task_callback|
task_callback.applies_to?(task) || task_callback.source == task.fully_qualified_name
end
end

end

module Matchers
extend ::Spec::Matchers::DSL

define :callback do |task_name|
extend Helpers

match do |configuration|
@task = configuration.find_task(task_name)
callbacks = find_callback(configuration, @on, @task)

if callbacks
@callback = callbacks.first

if @callback && @after_task_name
@after_task = configuration.find_task(@after_task_name)
@callback.applies_to?(@after_task)
elsif @callback && @before_task_name
@before_task = configuration.find_task(@before_task_name)
@callback.applies_to?(@before_task)
else
! @callback.nil?
end
else
false
end
end

def on(on)
@on = on
self
end

def before(before_task_name)
@on = :before
@before_task_name = before_task_name
self
end

def after(after_task_name)
@on = :after
@after_task_name = after_task_name
self
end

failure_message_for_should do |actual|
if @after_task_name
"expected configuration to callback #{task_name.inspect} #{@on} #{@after_task_name.inspect}, but did not"
elsif @before_task_name
"expected configuration to callback #{task_name.inspect} #{@on} #{@before_task_name.inspect}, but did not"
else
"expected configuration to callback #{task_name.inspect} on #{@on}, but did not"
end
end

end

define :have_gotten do |path|
match do |configuration|

get = configuration.gets[path]
if @to
get && get[:path] == @to
else
get
end
end

def to(to)
@to = to
self
end

failure_message_for_should do |actual|
if @to
"expected configuration to get #{path} to #{@to}, but did not"
else
"expected configuration to get #{path}, but did not"
end
end
end

define :have_uploaded do |path|
match do |configuration|
upload = configuration.uploads[path]
if @to
upload && upload[:to] == @to
else
upload
end
end

def to(to)
@to = to
self
end

failure_message_for_should do |actual|
if @to
"expected configuration to upload #{path} to #{@to}, but did not"
else
"expected configuration to upload #{path}, but did not"
end
end
end

define :have_run do |cmd|

match do |configuration|
run = configuration.runs[cmd]

run
end

failure_message_for_should do |actual|
"expected configuration to run #{cmd}, but did not"
end

end

end
end
end

0 comments on commit 8f1723f

Please sign in to comment.