{"id":16948585,"url":"https://github.com/technicalpickles/capistrano-spec","last_synced_at":"2026-03-17T19:23:23.096Z","repository":{"id":985696,"uuid":"790170","full_name":"technicalpickles/capistrano-spec","owner":"technicalpickles","description":"Helpers and matchers for testing capistrano","archived":false,"fork":false,"pushed_at":"2014-05-11T17:28:22.000Z","size":374,"stargazers_count":85,"open_issues_count":2,"forks_count":15,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-06T16:46:20.995Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/technicalpickles.png","metadata":{"files":{"readme":"README.rdoc","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-07-22T03:45:31.000Z","updated_at":"2022-06-27T18:25:41.000Z","dependencies_parsed_at":"2022-08-16T11:45:11.057Z","dependency_job_id":null,"html_url":"https://github.com/technicalpickles/capistrano-spec","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/technicalpickles%2Fcapistrano-spec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/technicalpickles%2Fcapistrano-spec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/technicalpickles%2Fcapistrano-spec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/technicalpickles%2Fcapistrano-spec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/technicalpickles","download_url":"https://codeload.github.com/technicalpickles/capistrano-spec/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248112214,"owners_count":21049617,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T21:51:29.724Z","updated_at":"2026-03-17T19:23:23.061Z","avatar_url":"https://github.com/technicalpickles.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"= capistrano-spec\n\n{\u003cimg src=\"https://travis-ci.org/technicalpickles/capistrano-spec.png?branch=master\" alt=\"Build Status\" /\u003e}[https://travis-ci.org/technicalpickles/capistrano-spec]\n\nCapistrano... 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.\n\n== Install\n\nYou know the drill:\n\n  gem install capistrano-spec\n\nAnd require it in your +spec/spec_helper.rb+:\n\n  require 'capistrano-spec'\n\n== Designing your capistrano extension\n\nIn the wild, you'll mostly commonly come across two patterns:\n\n* files living under recipes/* that are autoloaded\n* files living under lib that are required from config/deploy.rb\n\nIn these files, you can start using the capistrano top-level methods, like +namespace+ or +task+, like:\n\n  # in recipes/speak.rb or lib/speak.rb\n  task :speak do\n    set :message, 'oh hai'\n    puts message\n  end\n\nCapistrano 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.\n\nSome 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:\n\n  # in recipes/speak.rb or lib/speak.rb\n  Capistrano::Configuration.instance(true).load do\n    task :speak do\n      set :message, 'oh hai'\n      puts message\n    end\n  end\n\nThere's a problem though: it's not particular testable. You can't take some +Capistrano::Configuration+ and easily bring your task into it.\n\nSo, here's what I recommend instead: create a method for taking a configuration, and adding your goodies to it.\n\n  require 'capistrano'\n  module Capistrano\n    module Speak\n      def self.load_into(configuration)\n        configuration.load do\n          task :speak do\n            set :message, 'oh hai'\n            puts message\n          end\n        end\n      end\n    end\n  end\n\n  # may as well load it if we have it\n  if Capistrano::Configuration.instance\n    Capistrano::Speak.load_into(Capistrano::Configuration.instance)\n  end\n\nNow, we're going to be able to test this. Behold!\n\n== Testing\n\nAlright, we can start testing by making Capistrano::Configuration and load Capistrano::Speak into it.\n\n  describe Capistrano::Speak, \"loaded into a configuration\" do\n    before do\n      @configuration = Capistrano::Configuration.new\n      Capistrano::Speak.load_into(@configuration)\n    end\n\n  end\n\n\nNow you have access to a configuration, so you can start poking around the +@configuration+ object as you see fit.\n\nNow, remember, if you +set+ values, you can access them using +fetch+:\n\n  before do\n    @configuration.set :foo, 'bar'\n  end\n\n  it \"should define foo\" do\n    @configuration.fetch(:foo).should == 'bar'\n  end\n\nYou can also find and execute tasks, so you can verify if you successfully set a value:\n\n\n  describe 'speak task' do\n    before do\n      @configuration.find_and_execute_task('speak')\n    end\n\n    it \"should define message\" do\n      @configuration.fetch(:message).should == 'oh hai'\n    end\n  end\n\nOne 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.\n\n  before do\n    @configuration = Capistrano::Configuration.new\n    @configuration.extend(Capistrano::Spec::ConfigurationExtension)\n  end\n\n  it \"should run yes\" do\n    @configuration.run \"yes\"\n    @configuration.should have_run(\"yes\")\n  end\n\n  it \"should upload foo\" do\n    @configuration.upload 'foo', '/tmp/foo'\n    @configuration.should have_uploaded('foo').to('/tmp/foo')\n  end\n\n  it \"should have gotten\" do\n    @configuration.get '/tmp/bar', 'bar'\n    @configuration.should have_gotten('/tmp/bar').to('bar')\n  end\n\n  it \"should have put\" do\n    @configuration.put 'some: content', '/config.yml'\n    @configuration.should have_put('some: content').to('/config.yml')\n  end\n\nYou also test callbacks[http://rubydoc.info/github/capistrano/capistrano/master/Capistrano/Configuration/Callbacks] to see if your tasks are being called at the right time:\n\n  require 'capistrano'\n  module Capistrano\n    module Speak\n      def self.load_into(configuration)\n        configuration.load do\n          before \"deploy:finalize_update\", \"foo:bar\"\n          namespace :foo do\n            task :bar do\n              set :message, 'before finalize'\n              puts message\n            end\n          end\n        end\n      end\n    end\n  end\n\n  it \"performs foo:bar before deploy:finalize_update\" do\n    @configuration.should callback('foo:bar').before('deploy:finalize_update')\n  end\n\nYou can also stub requests if you need to access their output:\n\n  task :pwd do\n    set :pwd, capture('pwd')\n  end\n\n  it 'should capture working directory' do\n    @configuration.stub_command 'pwd', data: '/path/to/working/dir'\n    @configuration.fetch(:pwd).should == '/path/to/working/dir'\n  end\n\nAdditional options are +channel+ and +stream+ for testing custom +run+ blocks:\n\n  task :custom do\n    invoke_command 'pwd', :via =\u003e :sudo do |ch, stream, data|\n      # magical foo\n    end\n  end\n\nAs +sudo+ and +invoke_command+ use +run+ internal and +capture+ uses\n+invoke_command+ they are also stub-able by specifying the exact command.\n\n  task :sudo_pwd do\n    set :pwd, capture('pwd', :via =\u003e :sudo)\n  end\n\n  it 'should capture sudo working directory' do\n    @configuration.stub_command 'sudo -p 'sudo password: ' pwd', data: '/sudo/dir'\n    @configuration.fetch(:pwd).should == '/sudo/dir'\n  end\n\n== Real world examples\n\n* capistrano-mountaintop[https://github.com/technicalpickles/capistrano-mountaintop/blob/master/spec/capistrano-mountaintop_spec.rb]\n* moonshine[https://github.com/railsmachine/moonshine/blob/master/spec/moonshine/capistrano_integration_spec.rb]\n* rvm-capistrano[https://github.com/wayneeseguin/rvm-capistrano/tree/master/spec]\n* capistrano-unicorn[https://github.com/sosedoff/capistrano-unicorn/blob/master/spec/capistrano_integration_spec.rb]\n\n== Note on Patches/Pull Requests\n\n* Fork the project.\n* Make your feature addition or bug fix.\n* Add tests for it. This is important so I don't break it in a future version unintentionally.\n* 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)\n* Send me a pull request. Bonus points for topic branches.\n\n== Copyright\n\nCopyright (c) 2010 Joshua Nichols. See LICENSE for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftechnicalpickles%2Fcapistrano-spec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftechnicalpickles%2Fcapistrano-spec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftechnicalpickles%2Fcapistrano-spec/lists"}