Implementing dry-run in ruby script
This could help:
def perform(*commands)
commands.each { |x| DRY_RUN ? puts(x) : eval(x)}
end
It results in:
DRY_RUN = true
perform("puts 'Hello'")
puts 'Hello'
=> ["puts 'Hello'"]
DRY_RUN = false
perform("puts 'Hello'")
Hello
=> ["puts 'Hello'"]
Implementing dry-run in ruby
On the else sentence, where you have:
command.each { |x| x }
Replace that with either system(x)
if you are running a system command, or eval(x)
if you are trying to run ruby code, like:
DRY_RUN = true
def perform(*args)
command = args
if DRY_RUN
command.each{ |x| puts x }
else
command.each { |x| system(x) }
end
end
or
DRY_RUN = true
def perform(*args)
command = args
if DRY_RUN
command.each{ |x| puts x }
else
command.each { |x| eval(x) }
end
end
Error when run cucumber with dry run option
The problem is that dry-run (-d) does not load your support/env.rb file. If you do cucumber -h
to see the help, it says:
-d, --dry-run Invokes formatters without executing the steps. This also omits the loading of your support/env.rb file if it exists.
Since you require watir-webdriver in the env.rb and env.rb does not get loaded, then your hooks file will not know what Watir is.
One solution would be to add require 'watir-webdriver'
(or require 'watir'
) to your hooks.rb file.
An alternative solution is to move the browser creation and at_exit hooks into the env.rb file. This way, when using the dry run option, you will not see the browser open and close.
Can I dry run/sandbox sql commands?
In Postgres, you can do much with transactions that are rolled back at the end:
BEGIN;
UPDATE foo ...:
INSERT bar ...;
SELECT baz FROM ...;
CREATE TABLE abc...; -- even works for DDL statements
DROP TABLE def...;
ALTER TABLE ghi ...:
ROLLBACK; -- !
More in the manual: BEGIN
ROLLBACK
Be aware that some things cannot be rolled back, though. For instances sequences don't roll back. Or some special commands like dblink calls.
And some commands cannot be run in a transaction with others. Like CREATE DATABASE
or VACUUM
.
Also, there may be side effects with concurrent load, like deadlocks. Unlikely, though. You can set the transaction isolation level to your requirements to rule out any side effects (at some cost to performance).
I would not do this with sensible data. The risk of committing by accident is too great. And letting users execute arbitrary code is a risk that hardly containable. But for a training environment, that should be good enough.
Back it up with a template database. If something should go wrong, that's the fastest way to restore a basic state. Example (look at the last chapter):
Truncating all tables in a Postgres database
This can also be used as brute force alternative: to provide a pristine new database for each trainee.
Rake: How to accept command-line arguments which include a hypen? (when using the key=value syntax)
Those variables are managed by your shell. When rake
does ENV['verbose']
he's just asking the shell, "hey, do you have any value called 'verbose'? If yes, give it to me".
Shells are generally not able to handle hypens in their variable names; like in most programming languages, the hypen is reserved for the substraction operation.
The simplest fix would be replacing the hyphen by an underscore:
rake deploy verbose=true environment=production dry_run=true
EDIT:
Another option would be using something more suited for command line management. thor should allow you to define a command like so:
deploy --verbose --environment=production --dry-run
EDIT2:
It turns out that the values are passed verbatim to rake, not handled by the shell as I thought - see first comment on this answer for details. The solutions proposed on this answer work nevertheless.
ruby dry run No such file or directory /platform-tools/adb (Errno::ENOENT)
The problem you're receiving is dryrun not being able to recognize an adb path. The adb path needs to be set to ANDROID_HOME
, for dryrun
to work.
Steps to Fix this:
echo export "ANDROID_HOME=/Users/yourName/Library/Android/sdk" >> ~/.bash_profile
source ~/.bash_profile
echo $ANDROID_HOME
Test
dryrun git@github.com:cesarferreira/android-helloworld.git
How do I make a Ruby script using Trollop for command line parsing?
A: Here is a self-contained example.
The business with if __FILE__ == $0
isn't specific to Trollop; that just means "run the following code if this file is executed as a script". This technique allows you use the file as a library module, separating the business logic from the command line parsing.
#!/usr/bin/env ruby
#
# This is a near-minimal ruby script to demonstrate trollop, a simple
# and elegant command-line parsing package. To run, copy this file to
# 'trollop_demo.rb' and make it executable by
# $ chmod +x trollop_demo.rb
# Then run it via:
# $ ./trollop_demo.rb <your args here>
# For more info on Trollop, see http://trollop.rubyforge.org/
require 'trollop'
# A trivial program...
def main_method(filename, options)
puts("filename = #{filename}, options = #{options}")
end
# Execute this part when the file is run as a script
if __FILE__ == $0
opts = Trollop::options do
version "#{$0} Version 3.14159 (C) 2041 Spacely Sprockets, Inc."
banner <<-EOS
#{$0} is a command-line program to demonstrate trollop
Usage:
#{$0} [options] <filename>
where [options] are zero or more of:
EOS
opt :verbose, "Print extra information."
opt :dry_run, "Don't actually do anything.", :short => "-n"
end
filename = ARGV.shift
if (filename.nil?) || (!File.exist?(filename))
Trollop::die "filename must be given and name an existing file"
else
main_method(filename, opts)
end
end
With just that bit of code, you can now try all these things:
$ ./trollop_demo.rb
$ ./trollop_demo.rb a_file_that_doesn't_exist
$ ./trollop_demo.rb --version
$ ./trollop_demo.rb --help
$ ./trollop_demo.rb trollop_demo.rb
$ ./trollop_demo.rb --dry-run trollop_demo.rb
$ ./trollop_demo.rb --no-dry-run trollop_demo.rb
$ ./trollop_demo.rb --verbose trollop_demo.rb
$ ./trollop_demo.rb -v trollop_demo.rb
For more information, see http://trollop.rubyforge.org/
Related Topics
How to Sort a Ruby Array by Two Conditions
Rails Migration Complains About Undefined Method 'Attachment' Using Paperclip
Assets Precompiling Error with Jquery UI Plugin
Ruby: How to Remove Items from Array a If It's Not in Array B
Rails Cors Issue with Ajax API Endpoint Request
Ruby Net-Ssh Calling Bash Script with Interactive Prompts
Why Are Database Entries Being Automatically Created When I Visit the "New" Page
How to Seed the Production Database Using the Capistrano Gem
How to Verify Pacts Against an API That Requires an Auth Token
How to Make an Infowindow Automatically Display as Open with Google-Maps-For-Rails
Can't Run a Ruby Hello World Application in Aptana
How to Make Private Activities
Mongomapper Many to Many Issue with Array
How to Share Image and Description Using Social_Share_Button in Rails