Permalink
Browse files

Added only_when_success option

  • Loading branch information...
1 parent a442b94 commit 68725992d02919d8c363e7ed6a682a4fa945f45e @iain committed Jul 22, 2012
View
@@ -9,6 +9,7 @@ Among its features are:
* A convenient DSL to determine how and when to run scripts
* Determine which scripts in parallel with each other
* Manage the exit status of your scripts
+* Conditionally run certain commands
* A variaty of output formatters, including one that exports the output of the
scripts via websockets!
* Specify groups of tasks
@@ -159,8 +160,21 @@ run "stop xvfb" do
end
```
-And finally, to have a command run only if other commands have failed, mark it
-with `only_when_failed!`:
+### Conditionally run certain commands
+
+There are two types of commands that can be run conditionally.
+
+To run a command only when all previous commands were successful, mark it with
+`only_when_success!`:
+
+``` ruby
+run "mail me if everything went ok" do
+ only_when_success!
+end
+```
+
+To have a command run only if other commands have failed, mark it with
+`only_when_failed!`:
``` ruby
run "mail me if build failed" do
@@ -0,0 +1,78 @@
+Feature: Conditional Commands
+
+ There are two types of commands that can be run conditionally, depending on
+ the output of the other commands. These are marked with `only_when_success!`
+ and `only_when_failed!`.
+
+ Usually you want to put the commands at the end of your script, because it
+ measures the success or failure by the commands that have executed until that
+ point.
+
+ Tip: You can combine these commands with `important!` to stop the script half
+ way, without making every command important.
+
+ Scenario: Only when success when no other command failed
+ Given the configuration:
+ """
+ run "true"
+ run "echo only when success has run" do
+ only_when_success!
+ end
+ """
+ When I run scripted
+ Then it should pass
+ And the output should contain "only when success has run"
+
+ Scenario: Only when success with a failing command
+ Given the configuration:
+ """
+ run "false"
+ run "echo only when success has run" do
+ only_when_success!
+ end
+ """
+ When I run scripted
+ Then it should fail
+ And the output should not contain "only when success has run"
+
+ Scenario: Only when failed with a failing command
+ Given the configuration:
+ """
+ run "false"
+ run "echo only when failed has run" do
+ only_when_failed!
+ end
+ """
+ When I run scripted
+ Then it should fail
+ And the output should contain "only when failed has run"
+
+ Scenario: Only when failed when nothing failed
+ Given the configuration:
+ """
+ run "true"
+ run "echo only when failed has run" do
+ only_when_failed!
+ end
+ """
+ When I run scripted
+ Then it should pass
+ And the output should not contain "only when failed has run"
+
+ Scenario: Stopping a script half way
+ Given the configuration:
+ """
+ run "true"
+ run "false"
+
+ run "make sure all the previous tasks have succeeded" do
+ `false`
+ only_when_failed!
+ important!
+ end
+
+ # the rest
+ run "echo this should not be run"
+ """
+ When I run scripted
+ Then the output should not contain "this should not be run"
@@ -42,17 +42,6 @@ Feature: Controlling Exit Status
end
```
- If the run did fail, you might want to do some extra work, like sending an
- email. You can mark those with `only_when_failed!`.
-
- ``` ruby
- run "email" do
- `mail -s Failure you@example.org`
- only_when_failed!
- end
- ```
-
-
Scenario: Having failing commands
Given the configuration:
"""
@@ -98,27 +87,3 @@ Feature: Controlling Exit Status
"""
When I run scripted
Then it should pass
-
- Scenario: Only when failed with a failing command
- Given the configuration:
- """
- run "false"
- run "echo only when failed has run" do
- only_when_failed!
- end
- """
- When I run scripted
- Then it should fail
- And the output should contain "only when failed has run"
-
- Scenario: Only when failed when nothing failed
- Given the configuration:
- """
- run "true"
- run "echo only when failed has run" do
- only_when_failed!
- end
- """
- When I run scripted
- Then it should pass
- And the output should not contain "only when failed has run"
View
@@ -78,5 +78,13 @@ def only_when_failed!
@only_when_failed = true
end
+ def only_when_success!
+ @only_when_success = true
+ end
+
+ def only_when_success?
+ !!@only_when_success
+ end
+
end
end
@@ -96,6 +96,10 @@ def name
command.name
end
+ def only_when_success?
+ command.only_when_success?
+ end
+
end
end
end
@@ -87,7 +87,7 @@ def should_execute?(command)
if halted?
command.forced? or command.only_when_failed?
elsif failed?
- true
+ not command.only_when_success?
else
not command.only_when_failed?
end
@@ -64,6 +64,12 @@
expect(command).to be_only_when_failed
end
+ it "can be only when success" do
+ expect(command).not_to be_only_when_success
+ command.only_when_success!
+ expect(command).to be_only_when_success
+ end
+
it "can be parallel" do
command = Scripted::Command.new("true", :parallel_id => 10)
expect(command.parallel_id).to eq 10
@@ -145,6 +145,18 @@ def run_command(options = {})
expect(run_commands).to be_failed
end
+ it "executes only_when_success-commands when no other command failed" do
+ command1 = run_command(:failed? => true)
+ command2 = run_command(:only_when_success? => true)
+
+ run_commands.run [ command1, command2 ]
+
+ expect(command1).to be_executed
+ expect(command2).not_to be_executed
+
+ expect(run_commands).to be_failed
+ end
+
end
end

0 comments on commit 6872599

Please sign in to comment.