Skip to content

Instantly share code, notes, and snippets.

@djangofan
Created June 14, 2015 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djangofan/1fbd6abc49662c2edd4f to your computer and use it in GitHub Desktop.
Save djangofan/1fbd6abc49662c2edd4f to your computer and use it in GitHub Desktop.
Rakefile example for passing parameters for optparse
require 'rake'
require 'rake/testtask'
# uses '--' args format because ruby 'optparse' lib wants it that way
# runs myapp tests with args client, env, and application
namespace :myapp do |args|
desc "Example test task."
Rake::TestTask.new do |t|
t.name = "runTestsReal"
t.test_files = FileList['test/**/*_test.rb'].exclude(
'test/integration/**/*_test.rb'
)
t.verbose = true
end
desc "Runs all tests."
task :runTests, [:client, :env, :app] do |t, args|
puts "Args: #{args}"
end
desc "Runs example."
task :example do
Rake.application.invoke_task("myapp:runTests[--client=EXAMPLE, --env=Staging, --app=myapp]")
end
desc "Runs example alternate."
task :exampleAlt do
Rake::Task["myapp:runTests"].invoke('--client=EXAMPLE', '--env=Staging', '--app=myappalt')
end
desc "Runs tests with default values."
task :defaults, :client, :env, :app do |t, args|
args.with_defaults(:default_client => '--client=EXAMPLE', :default_env => '--env=Staging', :default_app => '--app=myapp')
puts "Args: #{args}"
end
end
desc "Prints out envs for fun, usage : 'client=EXAMPLE env=Staging app=myapp rake printEnvs'."
task :printEnvs do |t, args|
puts "client is #{ENV['client']}"
puts "env is #{ENV['env']}"
puts "app is #{ENV['app']}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment