Skip to content

Commit

Permalink
close #7 Validates actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Adams committed Jul 17, 2012
1 parent 9abacca commit cdf80b9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
15 changes: 11 additions & 4 deletions lib/trooper/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def call(configuration)
@config = configuration
@call_count += 1

if continue_call?
if everything_ok? && continue_call?
reset_commands!

build_commands
Expand All @@ -70,16 +70,23 @@ def prerequisite_call(configuration)
"touch #{prerequisite_list}; if grep -vz #{self.name} #{prerequisite_list}; then #{original_commands.join(' && ')}; else echo 'Already Done'; fi"
end

# Public: Validates the action object. (NOT WORKING)
# Public: Validates the action object.
#
# Examples
#
# @action.ok? # => true
#
# Returns true.
# Returns true or raise an InvalidActionError exception.
def ok?
true
begin
build_commands
reset_commands!
true
rescue Exception => e
raise InvalidActionError, "Action missing config variables - #{e.message}"
end
end
alias :everything_ok? :ok?

# Public: What type of action this is.
#
Expand Down
15 changes: 10 additions & 5 deletions spec/trooper/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

before do
@action = Trooper::Action.new :my_action, 'description' do
run 'touch test.txt'
run "touch #{my_var}.txt"
end

@action2 = Trooper::Action.new :my_action, 'description', :local => true do |a|
Expand Down Expand Up @@ -36,24 +36,29 @@
end

it "should be able to call the action after init" do
@action.call(:my_var => 'my_var').should == ['touch test.txt']
@action.call(:my_var => 'my_var').should == ['touch my_var.txt']

@action2.commands.should == []
end

it "should validate that an action has all the variables it needs" do
@action.call(:my_var => 'my_var').should == ['touch my_var.txt']
lambda { @action.call({}) }.should raise_error(Trooper::InvalidActionError)
end

it "should be able to pass a block" do
@action.call(:my_var => 'my_var')
@action.commands.should == ['touch test.txt']
@action.commands.should == ['touch my_var.txt']

@action2.call(:my_var => 'my_var')
@action2.commands.should == ['touch test.txt']
end

it "should not duplicate the commands if called again" do
@action.call(:my_var => 'my_var')
@action.commands.should == ['touch test.txt']
@action.commands.should == ['touch my_var.txt']
@action.call(:my_var => 'my_var')
@action.commands.should == ['touch test.txt']
@action.commands.should == ['touch my_var.txt']
end

it "should add strings to the commands array" do
Expand Down

0 comments on commit cdf80b9

Please sign in to comment.