Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ben-biddington/670548 to your computer and use it in GitHub Desktop.
Save ben-biddington/670548 to your computer and use it in GitHub Desktop.
Rake tasks may take their argument values from ENV if they're not supplied
require 'rake'
require 'spec'
describe 'About rake tasks, arguments and ENV' do
before(:each) do
ENV.delete('any_environment_variable')
Rake::Task.clear
end
it 'Argument may be satisfied from the ENV array if there is none explicitly supplied when invoked' do
the_argument_supplied = nil
task :an_example_with_an_argument, :any_environment_variable do |_, args|
the_argument_supplied = args.any_environment_variable
end
ENV['any_environment_variable'] = 'Chubby Rain and Phil Murphy\'s bike seat'
Rake::Task[:an_example_with_an_argument].execute
the_argument_supplied.should === 'Chubby Rain and Phil Murphy\'s bike seat'
end
it 'Argument is nil when there is no matching environment variable and no value supplied when invoked' do
the_argument_supplied = nil
task :an_example_with_an_argument, :any_environment_variable do |_, args|
the_argument_supplied = args.any_environment_variable
end
Rake::Task[:an_example_with_an_argument].execute
the_argument_supplied.should be_nil
end
it 'Argument may be supplied as follows' do
the_argument_supplied = nil
task :an_example_with_an_argument, :any_environment_variable do |_, args|
the_argument_supplied = args.any_environment_variable
end
Rake::Task[:an_example_with_an_argument].invoke('xxx')
the_argument_supplied.should === 'xxx'
end
it 'When satisfied from ENV, the key may be uppercase' do
the_argument_supplied = nil
task :an_example_with_an_argument, :any_environment_variable do |_, args|
the_argument_supplied = args.any_environment_variable
end
ENV['ANY_ENVIRONMENT_VARIABLE'] = 'Chubby Rain and Phil Murphy\'s bike seat'
Rake::Task[:an_example_with_an_argument].execute
the_argument_supplied.should === 'Chubby Rain and Phil Murphy\'s bike seat'
end
it 'Argument overrides anything in ENV' do
the_argument_supplied = nil
task :an_example_with_an_argument, :any_environment_variable do |_, args|
the_argument_supplied = args.any_environment_variable
end
ENV['any_environment_variable'] = 'Chubby Rain and Phil Murphy\'s bike seat'
Rake::Task[:an_example_with_an_argument].invoke("Another kebab on a weekday")
the_argument_supplied.should === 'Another kebab on a weekday'
end
end
@ben-biddington
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment