capistrano-spec 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Joshua Nichols
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,137 @@
1
+ = capistrano-spec
2
+
3
+ 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.
4
+
5
+ == Install
6
+
7
+ You know the drill:
8
+
9
+ gem install capistrano-spec
10
+
11
+ And require it in your +spec/spec_helper.rb+:
12
+
13
+ require 'capistrano-spec'
14
+
15
+ == Designing your capistrano extension
16
+
17
+ In the wild, you'll mostly commonly come across two patterns:
18
+
19
+ * files living under recipes/* that are autoloaded
20
+ * files living under lib that are required from config/deploy.rb
21
+
22
+ In these files, you can start using the capistrano top-level methods, like +namespace+ or +task+, like:
23
+
24
+ # in recipes/speak.rb or lib/speak.rb
25
+ task :speak do
26
+ set :message, 'oh hai'
27
+ puts message
28
+ end
29
+
30
+ 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 be available.
31
+
32
+ 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:
33
+
34
+ # in recipes/speak.rb or lib/speak.rb
35
+ Capistrano::Configuration.instance(true).load do
36
+ task :speak do
37
+ set :message, 'oh hai'
38
+ puts message
39
+ end
40
+ end
41
+
42
+ There's a problem though: it's not particular testable. You can't take some +Capistrano::Configuration+ and easily bring your task into it.
43
+
44
+ So, here's what I recommend instead: create a method for taking a configuration, and adding your goodies to it.
45
+
46
+ require 'capistrano'
47
+ module Capistrano
48
+ module Speak
49
+ def self.load_into(configuration)
50
+ configuration.load do
51
+ task :speak do
52
+ set :message, 'oh hai'
53
+ puts message
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ # may as well load it if we have it
61
+ if Capistrano::Configuration.instance
62
+ Capistrano::Speak.load_into(Capistrano::Configuration.instance)
63
+ end
64
+
65
+ Now, we're going to be able to test this. Behold!
66
+
67
+ == Testing
68
+
69
+ Alright, we can start testing by making Capistrano::Configuration and load Capistrano::Speak into it.
70
+
71
+ describe Capistrano::Speak, "loaded into a configuration" do
72
+ before do
73
+ @configuration = Capistrano::Configuration.new
74
+ Capistrano::Speak.load_into(@configuration)
75
+ end
76
+
77
+ end
78
+
79
+
80
+ Now you have access to a configuration, so you can start poking around the +@configuration+ object as you see fit.
81
+
82
+ Now, remember, if you +set+ values, you can access them using +fetch+:
83
+
84
+ before do
85
+ @configuration.set :foo, 'bar'
86
+ end
87
+
88
+ it "should define foo" do
89
+ @configuration.fetch(:foo).should == 'bar'
90
+ end
91
+
92
+ You can also find and execute tasks, so you can verify if you successfully set a value:
93
+
94
+
95
+ describe 'speak task' do
96
+ before do
97
+ @configuration.find_and_execute_task('speak')
98
+ end
99
+
100
+ it "should define message" do
101
+ @configuration.fetch(:message).should == 'oh hai'
102
+ end
103
+ end
104
+
105
+ 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.
106
+
107
+ before do
108
+ @configuration = Capistrano::Configuration.new
109
+ @configuration.extend(Capistrano::Spec::ConfigurationExtension)
110
+ end
111
+
112
+ it "should run yes" do
113
+ @configuration.run "yes"
114
+ @configuration.should have_run("yes")
115
+ end
116
+
117
+ it "should upload foo" do
118
+ @configuration.upload 'foo', '/tmp/foo'
119
+ @configuration.should have_uploaded('foo').to('/tmp/foo')
120
+ end
121
+
122
+ it "should have gotten" do
123
+ @configuration.get '/tmp/bar', 'bar'
124
+ @configuration.should have_gotten('/tmp/bar').to('bar')
125
+ end
126
+
127
+ == Note on Patches/Pull Requests
128
+
129
+ * Fork the project.
130
+ * Make your feature addition or bug fix.
131
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
132
+ * 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)
133
+ * Send me a pull request. Bonus points for topic branches.
134
+
135
+ == Copyright
136
+
137
+ Copyright (c) 2010 Joshua Nichols. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "capistrano-spec"
8
+ gem.version = '0.1.0'
9
+
10
+ gem.summary = %Q{Test your capistrano recipes}
11
+ gem.description = %Q{Helpers and matchers for testing capistrano}
12
+ gem.email = "josh@technicalpickles.com"
13
+ gem.homepage = "http://github.com/technicalpickles/capistrano-spec"
14
+ gem.authors = ["Joshua Nichols"]
15
+ gem.add_development_dependency "rspec", ">= 1.2.9"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "capistrano-spec #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{capistrano-spec}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Joshua Nichols"]
12
+ s.date = %q{2010-07-21}
13
+ s.description = %q{Helpers and matchers for testing capistrano}
14
+ s.email = %q{josh@technicalpickles.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "capistrano-spec.gemspec",
26
+ "lib/capistrano-spec.rb",
27
+ "lib/capistrano/spec.rb",
28
+ "spec/capistrano-spec_spec.rb",
29
+ "spec/spec.opts",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/technicalpickles/capistrano-spec}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.6}
36
+ s.summary = %q{Test your capistrano recipes}
37
+ s.test_files = [
38
+ "spec/capistrano-spec_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
48
+ else
49
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ end
54
+ end
55
+
@@ -0,0 +1,167 @@
1
+ module Capistrano
2
+ module Spec
3
+ module ConfigurationExtension
4
+ def get(remote_path, path, options={}, &block)
5
+ gets[remote_path] = {:path => path, :options => options, :block => block}
6
+ end
7
+
8
+ def gets
9
+ @gets ||= {}
10
+ end
11
+
12
+ def run(cmd, options={}, &block)
13
+ runs[cmd] = {:options => options, :block => block}
14
+ end
15
+
16
+ def runs
17
+ @runs ||= {}
18
+ end
19
+
20
+ def upload(from, to, options={}, &block)
21
+ uploads[from] = {:to => to, :options => options, :block => block}
22
+ end
23
+
24
+ def uploads
25
+ @uploads ||= {}
26
+ end
27
+
28
+ end
29
+
30
+ module Helpers
31
+ def find_callback(configuration, on, task)
32
+ if task.kind_of?(String)
33
+ task = configuration.find_task(task)
34
+ end
35
+
36
+ callbacks = configuration.callbacks[on]
37
+
38
+ callbacks && callbacks.select do |task_callback|
39
+ task_callback.applies_to?(task) || task_callback.source == task.fully_qualified_name
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ module Matchers
46
+ extend ::Spec::Matchers::DSL
47
+
48
+ define :callback do |task_name|
49
+ extend Helpers
50
+
51
+ match do |configuration|
52
+ @task = configuration.find_task(task_name)
53
+ callbacks = find_callback(configuration, @on, @task)
54
+
55
+ if callbacks
56
+ @callback = callbacks.first
57
+
58
+ if @callback && @after_task_name
59
+ @after_task = configuration.find_task(@after_task_name)
60
+ @callback.applies_to?(@after_task)
61
+ elsif @callback && @before_task_name
62
+ @before_task = configuration.find_task(@before_task_name)
63
+ @callback.applies_to?(@before_task)
64
+ else
65
+ ! @callback.nil?
66
+ end
67
+ else
68
+ false
69
+ end
70
+ end
71
+
72
+ def on(on)
73
+ @on = on
74
+ self
75
+ end
76
+
77
+ def before(before_task_name)
78
+ @on = :before
79
+ @before_task_name = before_task_name
80
+ self
81
+ end
82
+
83
+ def after(after_task_name)
84
+ @on = :after
85
+ @after_task_name = after_task_name
86
+ self
87
+ end
88
+
89
+ failure_message_for_should do |actual|
90
+ if @after_task_name
91
+ "expected configuration to callback #{task_name.inspect} #{@on} #{@after_task_name.inspect}, but did not"
92
+ elsif @before_task_name
93
+ "expected configuration to callback #{task_name.inspect} #{@on} #{@before_task_name.inspect}, but did not"
94
+ else
95
+ "expected configuration to callback #{task_name.inspect} on #{@on}, but did not"
96
+ end
97
+ end
98
+
99
+ end
100
+
101
+ define :have_gotten do |path|
102
+ match do |configuration|
103
+
104
+ get = configuration.gets[path]
105
+ if @to
106
+ get && get[:path] == @to
107
+ else
108
+ get
109
+ end
110
+ end
111
+
112
+ def to(to)
113
+ @to = to
114
+ self
115
+ end
116
+
117
+ failure_message_for_should do |actual|
118
+ if @to
119
+ "expected configuration to get #{path} to #{@to}, but did not"
120
+ else
121
+ "expected configuration to get #{path}, but did not"
122
+ end
123
+ end
124
+ end
125
+
126
+ define :have_uploaded do |path|
127
+ match do |configuration|
128
+ upload = configuration.uploads[path]
129
+ if @to
130
+ upload && upload[:to] == @to
131
+ else
132
+ upload
133
+ end
134
+ end
135
+
136
+ def to(to)
137
+ @to = to
138
+ self
139
+ end
140
+
141
+ failure_message_for_should do |actual|
142
+ if @to
143
+ "expected configuration to upload #{path} to #{@to}, but did not"
144
+ else
145
+ "expected configuration to upload #{path}, but did not"
146
+ end
147
+ end
148
+ end
149
+
150
+ define :have_run do |cmd|
151
+
152
+ match do |configuration|
153
+ run = configuration.runs[cmd]
154
+
155
+ run
156
+ end
157
+
158
+ failure_message_for_should do |actual|
159
+ "expected configuration to run #{cmd}, but did not"
160
+ end
161
+
162
+ end
163
+
164
+ end
165
+ end
166
+ end
167
+
@@ -0,0 +1 @@
1
+ require 'capistrano/spec'
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "CapistranoSpec" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'capistrano-spec'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-spec
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Joshua Nichols
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-21 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Helpers and matchers for testing capistrano
35
+ email: josh@technicalpickles.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - capistrano-spec.gemspec
50
+ - lib/capistrano-spec.rb
51
+ - lib/capistrano/spec.rb
52
+ - spec/capistrano-spec_spec.rb
53
+ - spec/spec.opts
54
+ - spec/spec_helper.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/technicalpickles/capistrano-spec
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.6
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Test your capistrano recipes
85
+ test_files:
86
+ - spec/capistrano-spec_spec.rb
87
+ - spec/spec_helper.rb