Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only load rake rasks in a certain environment?

Right now, I'm using bundler to manage my gems. Bundler loads different gems for different environments.

I have some rake tasks that use testing gems (rspec), but these cause problems in production environments where that gem isn't loaded.

So what I'd like to be able to do is to only have the rake task (and the require 'rspec/core/rake_task' line associated with it) load in the test environment.

I can't quite figure out the best way to do this.

I currently have:

require "bundler"
require 'rspec/core/rake_task'

desc "Task for running Rspec tests"
RSpec::Rake::SpecTask.new(:spec)
like image 904
GlyphGryph Avatar asked Jan 18 '12 18:01

GlyphGryph


People also ask

What is environment rake task?

Including => :environment will tell Rake to load full the application environment, giving the relevant task access to things like classes, helpers, etc. Without the :environment , you won't have access to any of those extras.

Where are Rake tasks defined?

In any Rails application you can see which rake tasks are available - either by running rake -AT (or rake --all --tasks) to see all tasks, or rake -T (or rake --tasks ) to see all tasks with descriptions.

What is use of rake task?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.


1 Answers

How about:

require "bundler"

unless Rails.env.production?
  require 'rspec/core/rake_task'

  desc "Task for running Rspec tests"
  RSpec::Rake::SpecTask.new(:spec)
end

Not the prettiest solution, but it will work.

like image 184
Mario Uher Avatar answered Sep 21 '22 16:09

Mario Uher