cap-util 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +20 -0
- data/Gemfile +6 -0
- data/LICENSE +22 -0
- data/README.md +92 -0
- data/Rakefile +8 -0
- data/cap-util.gemspec +23 -0
- data/lib/cap-util.rb +29 -0
- data/lib/cap-util/fake_cap.rb +35 -0
- data/lib/cap-util/halt.rb +23 -0
- data/lib/cap-util/rake_task.rb +30 -0
- data/lib/cap-util/run.rb +60 -0
- data/lib/cap-util/say.rb +41 -0
- data/lib/cap-util/time.rb +30 -0
- data/lib/cap-util/timer.rb +32 -0
- data/lib/cap-util/unset_var.rb +10 -0
- data/lib/cap-util/version.rb +3 -0
- data/test/cap_util_tests.rb +69 -0
- data/test/fake_cap_tests.rb +30 -0
- data/test/helper.rb +21 -0
- data/test/irb.rb +9 -0
- data/test/rake_task_tests.rb +57 -0
- data/test/timer_tests.rb +49 -0
- data/test/unset_var_tests.rb +44 -0
- metadata +136 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 <TODO: copyright holders>
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# CapUtil
|
2
|
+
|
3
|
+
A set of utilities for writing cap tasks. Use these to help extract business logic from your tasks and test them.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'cap-util'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install cap-util
|
18
|
+
|
19
|
+
## The Mixin
|
20
|
+
|
21
|
+
The main `CapUtil` mixin can be used to make any class a cap utility. All the cap util requires is that your class define a `cap` method that returns an instance of a cap invocations.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# in some_great_util.rb
|
25
|
+
require 'cap-util'
|
26
|
+
|
27
|
+
class SomeGreatUtil
|
28
|
+
include CapUtil
|
29
|
+
|
30
|
+
def initialize(cap)
|
31
|
+
@cap = cap
|
32
|
+
end
|
33
|
+
|
34
|
+
def do_something
|
35
|
+
run "echo something"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# in Capfile
|
40
|
+
require 'some_great_util.rb'
|
41
|
+
|
42
|
+
desc "some great util"
|
43
|
+
task :some_great_util do
|
44
|
+
SomeGreatUtil.new(self).do_something
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
The goal here is to move all cap task business logic into neat little classes that can be unit tested. In addition, the mixin provides a bunch of helpers for running commands, halting tasks, outputting info, and timing tasks.
|
49
|
+
|
50
|
+
## FakeCap helper
|
51
|
+
|
52
|
+
The `FakeCap` helper class is handy to use when testing your cap utils. CapUtil uses it in its own test suite. It fakes a common subset of cap functions so that they can be safely tested against. Extend it to suit your own test suite's needs.
|
53
|
+
|
54
|
+
## UnsetVar helper
|
55
|
+
|
56
|
+
The `UnsetVar` helper class is handy for defining cap vars that need to have a value set in some other context. Think of it as the equivalent of raising a `NotImplementedError` in a method. If the variable used without being overridden first, the deploy is halted with a message.
|
57
|
+
|
58
|
+
To use:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
# in your Capfile...
|
62
|
+
|
63
|
+
# halt with the default msg ":application var not set."
|
64
|
+
set :application, CapUtil::UnsetVar.new(:application)
|
65
|
+
|
66
|
+
# halt with the custom msg ":stage var not set (no stage task used)."
|
67
|
+
set :stage, CapUtil::UnsetVar(:stage, "no stage task used")
|
68
|
+
```
|
69
|
+
|
70
|
+
## RakeTask util
|
71
|
+
|
72
|
+
This util is handy for running a rake task remotely using a cap task. It constructs a command that cd's to the rakefile root dir and runs the specified task. That constructed command that command can then be run using cap.
|
73
|
+
|
74
|
+
By default, it expects the rakefile to be in the `:current_path` and uses bundler to run `rake`. These defaults can be overriden by passing options to the constructor.
|
75
|
+
|
76
|
+
To use, do something like:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
# in your Capfile...
|
80
|
+
|
81
|
+
task :some_rake_task do
|
82
|
+
CapUtil::RakeTask.new(self, "a:task:to:run").run
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
## Contributing
|
87
|
+
|
88
|
+
1. Fork it
|
89
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
90
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
91
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
92
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/cap-util.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/cap-util/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = "cap-util"
|
6
|
+
gem.version = CapUtil::VERSION
|
7
|
+
gem.description = %q{A set of utilities for writing cap tasks.}
|
8
|
+
gem.summary = %q{A set of utilities for writing cap tasks.}
|
9
|
+
|
10
|
+
gem.authors = ["Kelly Redding"]
|
11
|
+
gem.email = ["kelly@kellyredding.com"]
|
12
|
+
gem.homepage = "http://github.com/redding/cap-util"
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split("\n")
|
15
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
|
19
|
+
gem.add_development_dependency("assert")
|
20
|
+
gem.add_dependency("capistrano")
|
21
|
+
gem.add_dependency("scmd", ["~>1.1"])
|
22
|
+
|
23
|
+
end
|
data/lib/cap-util.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'cap-util/say'
|
2
|
+
require 'cap-util/run'
|
3
|
+
require 'cap-util/halt'
|
4
|
+
require 'cap-util/time'
|
5
|
+
|
6
|
+
require 'cap-util/unset_var'
|
7
|
+
|
8
|
+
module CapUtil
|
9
|
+
|
10
|
+
def self.included(receiver)
|
11
|
+
receiver.send(:attr_accessor, :cap)
|
12
|
+
|
13
|
+
receiver.send(:include, CapUtil::Say)
|
14
|
+
receiver.send(:include, CapUtil::Halt)
|
15
|
+
receiver.send(:include, CapUtil::Run)
|
16
|
+
receiver.send(:include, CapUtil::Time)
|
17
|
+
end
|
18
|
+
|
19
|
+
def get(*args, &block)
|
20
|
+
cap.get(*args, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def hostname
|
24
|
+
val = ""
|
25
|
+
run("hostname") {|ch, stream, out| val = out.strip}
|
26
|
+
val
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module CapUtil
|
4
|
+
class FakeCap
|
5
|
+
|
6
|
+
attr_reader :roles, :cmds_run
|
7
|
+
|
8
|
+
def initialize(*args)
|
9
|
+
@struct = OpenStruct.new
|
10
|
+
@roles = []
|
11
|
+
@cmds_run = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def method_missing(method, *args, &block)
|
15
|
+
@struct.send(method, *args, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def respond_to?(method)
|
19
|
+
@struct.respond_to?(method) ? true : super
|
20
|
+
end
|
21
|
+
|
22
|
+
def run(*args)
|
23
|
+
@cmds_run << args
|
24
|
+
end
|
25
|
+
|
26
|
+
def fetch(var_name)
|
27
|
+
self.send("fetch_#{var_name}")
|
28
|
+
end
|
29
|
+
|
30
|
+
def role(name, hostname, opts)
|
31
|
+
@roles << [name, hostname, opts]
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module CapUtil
|
2
|
+
|
3
|
+
class DeployHalted < RuntimeError
|
4
|
+
def backtrace; []; end
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.halt(msg='deploy halted')
|
8
|
+
raise CapUtil::DeployHalted, color(msg, :bold, :yellow)
|
9
|
+
end
|
10
|
+
|
11
|
+
module Halt
|
12
|
+
|
13
|
+
def self.included(receiver)
|
14
|
+
receiver.send(:extend, HaltMethods)
|
15
|
+
receiver.send(:include, HaltMethods)
|
16
|
+
end
|
17
|
+
|
18
|
+
module HaltMethods
|
19
|
+
def halt(*args); CapUtil.halt(*args); end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'cap-util'
|
2
|
+
|
3
|
+
module CapUtil
|
4
|
+
|
5
|
+
# This defines a base utiltiy for building tasks that run rake tasks. Pass
|
6
|
+
# in the rake task name and this will make sure it is run with the appropriate
|
7
|
+
# settings and environment
|
8
|
+
|
9
|
+
class RakeTask
|
10
|
+
include CapUtil
|
11
|
+
|
12
|
+
attr_reader :cmd
|
13
|
+
|
14
|
+
def initialize(cap, task, opts=nil)
|
15
|
+
@cap = cap
|
16
|
+
|
17
|
+
opts ||= {}
|
18
|
+
opts[:root] ||= :current_path
|
19
|
+
opts[:rake] ||= "bundle exec rake"
|
20
|
+
opts[:env] ||= ""
|
21
|
+
rakefile_root = cap.send(opts[:root])
|
22
|
+
|
23
|
+
@cmd = "cd #{rakefile_root} && #{opts[:env]} #{opts[:rake]} #{task}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def run; super(@cmd); end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/lib/cap-util/run.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'scmd'
|
2
|
+
|
3
|
+
module CapUtil
|
4
|
+
|
5
|
+
def self.run_locally(cmd_str)
|
6
|
+
cmd = Scmd.new(cmd_str)
|
7
|
+
|
8
|
+
log "running `#{cmd}'"
|
9
|
+
cmd.run
|
10
|
+
log_error(cmd.stderr) if !cmd.success?
|
11
|
+
|
12
|
+
cmd
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.run_locally_with_stdin(cmd_str, input)
|
16
|
+
cmd = Scmd.new(cmd_str)
|
17
|
+
|
18
|
+
log "running `#{cmd}'"
|
19
|
+
cmd.run(input)
|
20
|
+
log_error(cmd.stderr) if !cmd.success?
|
21
|
+
|
22
|
+
cmd
|
23
|
+
end
|
24
|
+
|
25
|
+
module Run
|
26
|
+
|
27
|
+
def self.included(receiver)
|
28
|
+
receiver.send(:extend, RunLocalMethods)
|
29
|
+
receiver.send(:include, RunLocalMethods)
|
30
|
+
receiver.send(:include, InstanceMethods)
|
31
|
+
end
|
32
|
+
|
33
|
+
module RunLocalMethods
|
34
|
+
def run_locally(*args); CapUtil.run_locally(*args); end
|
35
|
+
def run_locally_with_stdin(*args); CapUtil.run_locally_with_stdin(*args); end
|
36
|
+
end
|
37
|
+
|
38
|
+
module InstanceMethods
|
39
|
+
|
40
|
+
def run(*args, &block)
|
41
|
+
cap.run(*args, &block)
|
42
|
+
end
|
43
|
+
|
44
|
+
def run_with_stdin(cmd_str, input, opts={})
|
45
|
+
run(cmd_str, opts.merge(:pty => true)) {|ch, stream, out| ch.send_data(input + "\n")}
|
46
|
+
end
|
47
|
+
|
48
|
+
def run_as(user, cmd_str, opts={}, &block)
|
49
|
+
as_cmd_str = "su #{user} -lc '#{cmd_str.gsub("'", "\\'")}'"
|
50
|
+
run(as_cmd_str, opts, &block)
|
51
|
+
end
|
52
|
+
|
53
|
+
def run_as_with_stdin(user, cmd_str, input, opts={})
|
54
|
+
run_as(user, cmd_str, opts) {|ch, stream, out| ch.send_data(input + "\n")}
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
data/lib/cap-util/say.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'capistrano/cli'
|
2
|
+
|
3
|
+
module CapUtil
|
4
|
+
|
5
|
+
def self.color(*args)
|
6
|
+
Capistrano::CLI.ui.color(*args)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.say(msg, *args)
|
10
|
+
Capistrano::CLI.ui.say(" #{msg}", *args)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.log(msg, *args)
|
14
|
+
say("* #{msg}", *args)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.log_error(msg, *args)
|
18
|
+
say(" #{color "[ERROR]", :bold, :red} #{msg}", *args)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.log_warning(msg, *args)
|
22
|
+
say(" #{color "[WARN]", :bold, :yellow} #{msg}", *args)
|
23
|
+
end
|
24
|
+
|
25
|
+
module Say
|
26
|
+
|
27
|
+
def self.included(receiver)
|
28
|
+
receiver.send(:extend, SayMethods)
|
29
|
+
receiver.send(:include, SayMethods)
|
30
|
+
end
|
31
|
+
|
32
|
+
module SayMethods
|
33
|
+
def color(*args); CapUtil.color(*args); end
|
34
|
+
def say(*args); CapUtil.say(*args); end
|
35
|
+
def log(*args); CapUtil.log(*args); end
|
36
|
+
def log_error(*args); CapUtil.log_error(*args); end
|
37
|
+
def log_warning(*args); CapUtil.log_warning(*args); end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'cap-util/timer'
|
2
|
+
|
3
|
+
module CapUtil
|
4
|
+
|
5
|
+
def self.time(timer_set, name, &block)
|
6
|
+
timer_set[name] = CapUtil::Timer.new(name)
|
7
|
+
if !block.nil?
|
8
|
+
begin
|
9
|
+
timer_set[name].start
|
10
|
+
block.call
|
11
|
+
ensure
|
12
|
+
timer_set[name].end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
timer_set[name]
|
16
|
+
end
|
17
|
+
|
18
|
+
module Time
|
19
|
+
|
20
|
+
def self.included(receiver)
|
21
|
+
receiver.send(:extend, TimeMethods)
|
22
|
+
receiver.send(:include, TimeMethods)
|
23
|
+
end
|
24
|
+
|
25
|
+
module TimeMethods
|
26
|
+
def time(*args, &block); CapUtil.time(*args, &block); end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module CapUtil
|
2
|
+
class Timer
|
3
|
+
|
4
|
+
def self.pretty_time(elapsed)
|
5
|
+
"#{elapsed / 60}:#{(elapsed % 60).to_s.rjust(2, '0')}"
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :name, :start_time, :end_time, :elapsed_time
|
9
|
+
|
10
|
+
def initialize(name, quiet=nil)
|
11
|
+
@name, @start_time, @end_time, @elapsed_time = name, 0, 0, 0
|
12
|
+
@quiet = !!(quiet == :quiet)
|
13
|
+
end
|
14
|
+
|
15
|
+
def start(time=nil)
|
16
|
+
say " Starting #{CapUtil.color @name, :cyan}." if !@quiet
|
17
|
+
@start_time = (time || Time.now)
|
18
|
+
end
|
19
|
+
|
20
|
+
def end(time=nil)
|
21
|
+
@end_time = (time || Time.now)
|
22
|
+
@elapsed_time = @end_time - @start_time
|
23
|
+
if !@quiet
|
24
|
+
elapsed = self.class.pretty_time(@elapsed_time.to_i)
|
25
|
+
say " #{CapUtil.color @name, :bold, :yellow} completed in"\
|
26
|
+
" #{CapUtil.color elapsed, :underline, :yellow}."
|
27
|
+
end
|
28
|
+
@end_time
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'assert'
|
2
|
+
|
3
|
+
module CapUtil
|
4
|
+
|
5
|
+
class CapUtilTests < Assert::Context
|
6
|
+
desc "the CapUtil module"
|
7
|
+
setup do
|
8
|
+
@mod = CapUtil
|
9
|
+
end
|
10
|
+
subject { @mod }
|
11
|
+
|
12
|
+
should have_imeth :halt, :time
|
13
|
+
should have_imeths :color, :say, :log, :log_error, :log_warning
|
14
|
+
should have_imeths :run_locally, :run_locally_with_stdin
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
class HaltTests < CapUtilTests
|
19
|
+
desc "`halt` util methods"
|
20
|
+
|
21
|
+
should "raise a `DeployHalted` custom exception" do
|
22
|
+
assert_raises(CapUtil::DeployHalted) { subject.halt }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class CapUtilMixinTests < Assert::Context
|
27
|
+
desc "the CapUtil mixin"
|
28
|
+
setup do
|
29
|
+
@cap_util = TestHelpers::AnCapUtil.new(FakeCap.new)
|
30
|
+
end
|
31
|
+
subject { @cap_util }
|
32
|
+
|
33
|
+
should have_accessor :cap
|
34
|
+
|
35
|
+
should have_cmeth :halt
|
36
|
+
should have_imeth :halt
|
37
|
+
|
38
|
+
should have_cmeths :color, :say, :log, :log_error, :log_warning
|
39
|
+
should have_imeths :color, :say, :log, :log_error, :log_warning
|
40
|
+
|
41
|
+
should have_cmeths :run_locally, :run_locally_with_stdin
|
42
|
+
should have_imeths :run_locally, :run_locally_with_stdin
|
43
|
+
should have_imeths :run, :run_with_stdin, :run_as, :run_as_with_stdin
|
44
|
+
|
45
|
+
should have_cmeth :time
|
46
|
+
should have_imeth :time
|
47
|
+
|
48
|
+
should have_imeths :get, :hostname
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
class DeployHaltedTests < Assert::Context
|
53
|
+
desc "the CapUtility DeployHalted custome exception"
|
54
|
+
setup do
|
55
|
+
@err = CapUtil::DeployHalted.new
|
56
|
+
end
|
57
|
+
subject { @err }
|
58
|
+
|
59
|
+
should "be a `RuntimeError`" do
|
60
|
+
assert_kind_of RuntimeError, subject
|
61
|
+
end
|
62
|
+
|
63
|
+
should "have no backtrace" do
|
64
|
+
assert_empty subject.backtrace
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'assert'
|
2
|
+
|
3
|
+
require 'cap-util/fake_cap'
|
4
|
+
|
5
|
+
module CapUtil
|
6
|
+
|
7
|
+
class FakeCapTests < Assert::Context
|
8
|
+
desc "the fake cap helper"
|
9
|
+
setup do
|
10
|
+
@fc = FakeCap.new
|
11
|
+
end
|
12
|
+
subject { @fc }
|
13
|
+
|
14
|
+
should have_imeths :method_missing, :respond_to?
|
15
|
+
should have_imeths :run, :fetch, :role
|
16
|
+
should have_readers :roles, :cmds_run
|
17
|
+
|
18
|
+
should "store off args to run cmd calls" do
|
19
|
+
assert_empty subject.cmds_run
|
20
|
+
|
21
|
+
subject.run(:a, 1)
|
22
|
+
assert_equal [:a, 1], subject.cmds_run.last
|
23
|
+
|
24
|
+
subject.run
|
25
|
+
assert_equal [], subject.cmds_run.last
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# this file is automatically required in when you require 'assert' in your tests
|
2
|
+
# put test helpers here
|
3
|
+
|
4
|
+
# add root dir to the load path
|
5
|
+
$LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
|
6
|
+
|
7
|
+
require 'cap-util'
|
8
|
+
require 'cap-util/fake_cap'
|
9
|
+
|
10
|
+
module TestHelpers
|
11
|
+
|
12
|
+
class AnCapUtil
|
13
|
+
include CapUtil
|
14
|
+
|
15
|
+
def initialize(cap)
|
16
|
+
@cap = cap
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/test/irb.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'assert'
|
2
|
+
|
3
|
+
require 'cap-util/rake_task'
|
4
|
+
|
5
|
+
module CapUtil
|
6
|
+
|
7
|
+
class RakeTaskTests < Assert::Context
|
8
|
+
desc "the rake task util"
|
9
|
+
setup do
|
10
|
+
@fake_cap = FakeCap.new
|
11
|
+
@fake_cap.fetch_rake = "bundle exec rake"
|
12
|
+
@fake_cap.current_path = "/a/current/path"
|
13
|
+
@fake_cap.release_path = "/dat/release/path"
|
14
|
+
@rake_task_util = RakeTask.new(@fake_cap, 'a:task:to:run')
|
15
|
+
end
|
16
|
+
subject { @rake_task_util }
|
17
|
+
|
18
|
+
should have_imeths :run
|
19
|
+
|
20
|
+
should "build a rake task to run" do
|
21
|
+
assert_match 'rake ', subject.cmd
|
22
|
+
assert_match ' a:task:to:run', subject.cmd
|
23
|
+
end
|
24
|
+
|
25
|
+
should "run the task with bundler by default" do
|
26
|
+
assert_match 'bundle exec rake', subject.cmd
|
27
|
+
end
|
28
|
+
|
29
|
+
should "run the task with a custom rake if given" do
|
30
|
+
task = RakeTask.new(@fake_cap, '', :rake => '/path/to/rake')
|
31
|
+
assert_match '/path/to/rake', task.cmd
|
32
|
+
end
|
33
|
+
|
34
|
+
should "use cap's current path by default" do
|
35
|
+
assert_match "cd #{@fake_cap.current_path} &&", subject.cmd
|
36
|
+
end
|
37
|
+
|
38
|
+
should "use a custom cap path if given" do
|
39
|
+
task = RakeTask.new(@fake_cap, '', :root => :release_path)
|
40
|
+
assert_match "cd #{@fake_cap.release_path} &&", task.cmd
|
41
|
+
end
|
42
|
+
|
43
|
+
should "use a custom env var string if given" do
|
44
|
+
task = RakeTask.new(@fake_cap, '', :env => "FOO=bar")
|
45
|
+
assert_match "FOO=bar bundle", task.cmd
|
46
|
+
end
|
47
|
+
|
48
|
+
should "run its rake cmd" do
|
49
|
+
exp_cmd = "cd /a/current/path && bundle exec rake a:task:to:run"
|
50
|
+
subject.run
|
51
|
+
|
52
|
+
assert_equal [exp_cmd], @fake_cap.cmds_run.last
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/test/timer_tests.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'assert'
|
2
|
+
|
3
|
+
require 'cap-util/timer'
|
4
|
+
|
5
|
+
module CapUtil
|
6
|
+
|
7
|
+
class TimerTests < Assert::Context
|
8
|
+
desc "the Timer helper class"
|
9
|
+
setup do
|
10
|
+
@timer_util = Timer.new('a timer', :quiet)
|
11
|
+
end
|
12
|
+
subject { @timer_util }
|
13
|
+
|
14
|
+
should have_readers :name, :start_time, :end_time, :elapsed_time
|
15
|
+
should have_imeths :start, :end
|
16
|
+
should have_cmeth :pretty_time
|
17
|
+
|
18
|
+
should "know its name" do
|
19
|
+
assert_equal 'a timer', subject.name
|
20
|
+
end
|
21
|
+
|
22
|
+
should "default its start, end, and elapsed times" do
|
23
|
+
assert_equal 0, subject.start_time
|
24
|
+
assert_equal 0, subject.end_time
|
25
|
+
assert_equal 0, subject.elapsed_time
|
26
|
+
end
|
27
|
+
|
28
|
+
should "record its start time on `start`" do
|
29
|
+
exp_start_time = ::Time.now
|
30
|
+
subject.start(exp_start_time)
|
31
|
+
|
32
|
+
assert_equal exp_start_time, subject.start_time
|
33
|
+
end
|
34
|
+
|
35
|
+
should "record its end and elapsed time on `end`" do
|
36
|
+
exp_start_time = ::Time.now
|
37
|
+
subject.start(exp_start_time)
|
38
|
+
sleep 1
|
39
|
+
exp_end_time = ::Time.now
|
40
|
+
subject.end(exp_end_time)
|
41
|
+
|
42
|
+
assert_equal exp_end_time, subject.end_time
|
43
|
+
assert_equal (exp_end_time - exp_start_time), subject.elapsed_time
|
44
|
+
assert_equal "0:01", Timer.pretty_time(subject.elapsed_time.to_i)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'assert'
|
2
|
+
|
3
|
+
require 'cap-util/unset_var'
|
4
|
+
|
5
|
+
module CapUtil
|
6
|
+
|
7
|
+
class UnsetVarTests < Assert::Context
|
8
|
+
desc "the unset var helper"
|
9
|
+
setup do
|
10
|
+
@var_name = 'test'
|
11
|
+
@unset = UnsetVar.new(@var_name)
|
12
|
+
end
|
13
|
+
subject { @unset }
|
14
|
+
|
15
|
+
should "be a proc" do
|
16
|
+
assert_kind_of ::Proc, subject
|
17
|
+
end
|
18
|
+
|
19
|
+
should "raise `DeployHalted` when called" do
|
20
|
+
assert_raises(CapUtil::DeployHalted) { subject.call }
|
21
|
+
end
|
22
|
+
|
23
|
+
should "raise with a default msg based on the variable name" do
|
24
|
+
exp_msg = ":#{@var_name} var not set."
|
25
|
+
begin
|
26
|
+
subject.call
|
27
|
+
rescue CapUtil::DeployHalted => err
|
28
|
+
assert_match exp_msg, err.message
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
should "accept a custom msg to raise with" do
|
33
|
+
exp_msg = ":#{@var_name} var not set (a custom err msg)."
|
34
|
+
unset = CapUtil::UnsetVar.new(@var_name, exp_msg)
|
35
|
+
begin
|
36
|
+
unset.call
|
37
|
+
rescue CapUtil::DeployHalted => err
|
38
|
+
assert_match exp_msg, err.message
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cap-util
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Kelly Redding
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-10-16 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: assert
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
requirement: *id001
|
33
|
+
prerelease: false
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: capistrano
|
36
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 3
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
version: "0"
|
45
|
+
type: :runtime
|
46
|
+
requirement: *id002
|
47
|
+
prerelease: false
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: scmd
|
50
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 13
|
56
|
+
segments:
|
57
|
+
- 1
|
58
|
+
- 1
|
59
|
+
version: "1.1"
|
60
|
+
type: :runtime
|
61
|
+
requirement: *id003
|
62
|
+
prerelease: false
|
63
|
+
description: A set of utilities for writing cap tasks.
|
64
|
+
email:
|
65
|
+
- kelly@kellyredding.com
|
66
|
+
executables: []
|
67
|
+
|
68
|
+
extensions: []
|
69
|
+
|
70
|
+
extra_rdoc_files: []
|
71
|
+
|
72
|
+
files:
|
73
|
+
- .gitignore
|
74
|
+
- Gemfile
|
75
|
+
- LICENSE
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- cap-util.gemspec
|
79
|
+
- lib/cap-util.rb
|
80
|
+
- lib/cap-util/fake_cap.rb
|
81
|
+
- lib/cap-util/halt.rb
|
82
|
+
- lib/cap-util/rake_task.rb
|
83
|
+
- lib/cap-util/run.rb
|
84
|
+
- lib/cap-util/say.rb
|
85
|
+
- lib/cap-util/time.rb
|
86
|
+
- lib/cap-util/timer.rb
|
87
|
+
- lib/cap-util/unset_var.rb
|
88
|
+
- lib/cap-util/version.rb
|
89
|
+
- test/cap_util_tests.rb
|
90
|
+
- test/fake_cap_tests.rb
|
91
|
+
- test/helper.rb
|
92
|
+
- test/irb.rb
|
93
|
+
- test/rake_task_tests.rb
|
94
|
+
- test/timer_tests.rb
|
95
|
+
- test/unset_var_tests.rb
|
96
|
+
homepage: http://github.com/redding/cap-util
|
97
|
+
licenses: []
|
98
|
+
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 3
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
hash: 3
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
version: "0"
|
122
|
+
requirements: []
|
123
|
+
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 1.8.24
|
126
|
+
signing_key:
|
127
|
+
specification_version: 3
|
128
|
+
summary: A set of utilities for writing cap tasks.
|
129
|
+
test_files:
|
130
|
+
- test/cap_util_tests.rb
|
131
|
+
- test/fake_cap_tests.rb
|
132
|
+
- test/helper.rb
|
133
|
+
- test/irb.rb
|
134
|
+
- test/rake_task_tests.rb
|
135
|
+
- test/timer_tests.rb
|
136
|
+
- test/unset_var_tests.rb
|