I am using a third party gem that gives me the ability to execute some tests using the rake command:
rake jasminerice:run
Is it possible to include the task as part of default rake task? (i.e. when I run rake then rake jasminerice:run is added to the rest of my tests). This is required for CI integration.
Any help would be greatly appreciated!
In Rake, tasks can have dependencies. Generally, you can do this:
task :hello do
puts "hello"
end
task :default => :hello
For your specific problem, you should be able to do this:
task :default => "jasminerice:run"
Related
I am using Ruby on Rails and Heroku.
I would like something like rake db:migrate, where it will only run the parts that have not been run before.
I can set a rake task that can run other rake task, but is there in way to only run those that did not run before?
What are any alternative, if I want rake tasks to be run automatically?
first of, running a rake task twice, ie rake db:migrate will neither duplicate nor overwrite your database columns if they already exist.
For example capistrano, used to deploy your rails app to basically anywhere, will rerun tasks like db:migrateor assets:precompile every time you deploy your app. So no need to worry about that really.
To run tasks automatically you will need something like a cron job. Whenever is a great ruby gem that allows you to install a job like this with the beloved ruby syntax.
After installing the gem and running wheneverize . in the root of your rails app, you may edit the scheduler.rb and add something like:
every :sunday, :at => '12pm' do
rake my:awesome:task
end
The Github page as well as the default scheduler.rb hold many useful examples of how these jobs are built.
with whenever --update-crontab you can write your rubyesque cronjobs into the crontab, which will run your tasks periodically at the time you want them to.
** edit
multiple tasks in one:
task :setup => [:a, :b, :c]
task :a do
%x(bash command)
end
task :b do
rake db:migrate
end
task :c do
rake whatever
end
I'm trying to setup my rails project so that all the verification required by a contributor is in one command, currently we have been running:
rake test
But now we also want to use rubocop for static analysis:
rubocop -R -a
I want this to be executable in one simple rake task. It would be nice to override 'rake test' to run rubocop then the standard rake test stuff for a rails project, as then no-one will have to remember to change the command. But if I have to create a separate rake task, that's probably fine too.
I've seen the rubocop rake integration here, at the bottom, but I'm not sure how to bundle that with 'rake test' into one task... Any thoughts?
I prefer to set my default task to run rubocop then the tests. Either way, it is a good idea to have those tasks separate rather than have one task do two things.
require 'rubocop/rake_task'
task :default => [:rubocop, :test]
desc 'Run tests'
task(:test) do
# run your specs here
end
desc 'Run rubocop'
task :rubocop do
RuboCop::RakeTask.new
end
Your tasks:
> rake -T
rake rubocop # Run rubocop
rake test # Run tests
This is the .rake file that I ended up with in the end.
desc 'Run tests and rubocop'
task :validate do
Rake::Task['rubocop'].invoke
Rake::Task['test'].invoke
end
task :rubocop do
require 'rubocop'
cli = Rubocop::CLI.new
cli.run(%w(--rails --auto-correct))
end
You could easily define your own rake task which first invokes Rails' test rake task and then the code snippet you mentioned for rubocop.
For example, in a .rake file you could have something like that:
require 'rubocop/rake_task'
desc 'Run tests and rubocop'
task :my_test do
Rake::Task['test'].invoke
RuboCop::RakeTask.new
end
If you feel the need to customize the call to Rubocop and that involves more code, you could create another custom task, say :rubocop, which you then invoke from :my_test as well.
Finally, an alternative to creating your own rake task and sticking with rake test would be to modify your test_helper to invoke whatever you need invoked after testing is completed.
It appears to have changed from:
Rubocop::RakeTask.new
to:
RuboCop::RakeTask.new
See ma? I know how to CamelCase!
I used following .rake file to run the test and rubocop tasks at once:
task default: %w[rubocop test]
RuboCop::RakeTask.new(:rubocop) do |task|
task.patterns = ['**/*.rb']
task.fail_on_error = false
task.options = ["--auto-correct-all"]
end
task :test do
ruby 'test/program_test.rb'
end
The first line allows both tasks to be run by calling rake.
Command line arguments can also be added in the options array.
Are the scripts from config/initializers executed when I run rake task?
It does if your rake task depends on :environment.
i.e, you declare your task like so:
task :my_task => :environment do
...
end
Mostly yes. rake loads a complete rails environment including initializers, when your task depends on :environment.
Eventually I would like to get to setting it up as a Rake task and do a cron job, but for right now...all I want to do is take my ruby script that used to work as a standalone script and have it work within my Rails app.
I renamed the file to be .rake instead of .rb and tried doing rake my_script at the command-line, but that gave me this error message:
rake aborted!
Don't know how to build task 'my_script'
(See full trace by running task with --trace)
How do I run this script within my Rails environment?
This is the first time I am doing something like this, so any assistance would be greatly appreciated.
Thanks.
I think what you're looking for is rails runner. I know in Rails 2.3.x you'd do
ruby script/runner <your file>
In Rails 3 it might be slightly different.
http://guides.rubyonrails.org/command_line.html#rails-runner
The primary difference between a runner and a rake task is : runner would boot rails while rake task doesn't (you can tell it to do so).
Since rake can do both (boot/no boot), there's no concept of runner in rails-3 anymore.
So, create a rake task: whatever_name.rake
Example:
desc "This task does awesome stuff"
task :do_awesome_stuff do
awesome_method
end
def awesome_method
#put your ruby code here
end
Now from your command prompt, type rake do_awesome_stuff to execute this rake task.
To make it boot Rails, change task definition to this:
task :do_awesome_stuff => :environment do
I would like to run db:migrate VERSION=0 and then db:migrate inside of my own rake task. I am confused about how to do this. Do I need a special require statement? My rake task will reside in the lib/tasks directory of a Rails app. Thanks.
Is your task just dependent on having a clean db? If that's the case then you can do:
task :my_task => [:environment, 'db:reset']
EDIT: Rake::Task[] won't accept parameters, you have to set it in ENV. In addition, you have to reenable the task to run it multiple times.
ENV['VERSION']= '0'
Rake::Task['db:migrate'].invoke
Rake::Task['db:migrate'].reenable
ENV.delete 'VERSION'
Rake::Task["db:migrate"].invoke
NOTE: Rake::Task.reenable requires Rake 0.8.2 or higher.
Check out rake db:reset as that will accomplish what you are trying to do.
To see what all of your rake tasks do, run rake -T