I am trying to invoke a rake task in in my rspec.
require "rake"
rake = Rake::Application.new
Rake.application = rake
rake.init
rake.load_rakefile
rake['rake my:task'].invoke
But i am getting error
Failure/Error: rake['rake db:migrate'].invoke
RuntimeError:
Don't know how to build task 'rake db:migrate'
Does anyone have a idea how we can invoke rake task in rspec code.
Any help would be highly appreciated.
Small namespacing issue, the task is db:migrate not rake db:migrate like the command line usage.
So changing it to this should help:
rake['db:migrate'].invoke
A simpler solution for Rails with Rspec :
In your spec_helper (or rails_helper for newer versions of rspec-rails) :
require "rake"
Rails.application.load_tasks
Then when you want to invoke your task you can do the following :
Rake::Task['my:task'].invoke
To pass in the arguments in square brackets to invoke:
rake sim:manual_review_referral_program[3,4]
becomes:
rake['sim:manual_review_referral_program'].invoke(3,4)
If your args are in an array, you can do the following:
args = [3,4]
rake['sim:manual_review_referral_program'].invoke(*args)
More info at this StackOverflow question: How to run Rake tasks from within Rake tasks?.
Related
I'm trying to tests my puppet modules, but I get an error message that it can't find class.
I've written a couple of internally used Puppet modules; now that I have my head around puppet, I want to write tests. Since I ran into issues, I decided to essentially start over with a new clean module to figure out what I need to do.
$puppet module generate wet-splunk
<<snip interactive questions>>
$cd splunk
$rake spec
<path snip>ruby -I/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-support-3.4.1/lib:/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/lib /Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration,types\}/\*\*/\*_spec.rb --color
F
Failures:
1) splunk with defaults for all parameters should contain Class[splunk]
Failure/Error: it { should contain_class('splunk') }
Puppet::PreformattedError:
Evaluation Error: Error while evaluating a Function Call, Could not find class ::splunk for host.example.com at line 1:1 on node host.example.com
# ./spec/classes/init_spec.rb:5:in `block (3 levels) in <top (required)>'
Finished in 0.21712 seconds (files took 3.14 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/classes/init_spec.rb:5 # splunk with defaults for all parameters should contain Class[splunk]
/Users/wet/.rbenv/versions/1.9.3-p0/bin/ruby -I/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-support-3.4.1/lib:/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/lib /Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration,types\}/\*\*/\*_spec.rb --color failed
Checking the files though I think they contain what they should:
manifests/init.pp
# <snip documentation boilterplate>
class splunk {
}
spec/classes/init_spec.rb
require 'spec_helper'
describe 'splunk' do
context 'with defaults for all parameters' do
it { should contain_class('splunk') }
end
end
The Could not find class ::splunk is the part that has me confused.
The way I read the spec file is it looking inside init for class{'splunk': ... } That could be me misreading it as it seems odd they would start with a broken spec file, but just in case I tried testing for package instead.
manifests/init.pp
class splunk {
package{'splunk':
ensure => 'present',
}
}
spec/classes/init_spec.rb
require 'spec_helper'
describe 'splunk' do
context 'with defaults for all parameters' do
it { should contain_package('splunk') }
end
end
I still get a similar error message:
$ rake spec
/Users/wet/.rbenv/versions/1.9.3-p0/bin/ruby -I/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-support-3.4.1/lib:/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/lib /Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration,types\}/\*\*/\*_spec.rb --color
F
Failures:
1) splunk with defaults for all parameters should contain Package[splunk]
Failure/Error: it { should contain_package('splunk') }
Puppet::PreformattedError:
Evaluation Error: Error while evaluating a Function Call, Could not find class ::splunk for host.example.com at line 1:1 on node host.example.com
# ./spec/classes/init_spec.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.21168 seconds (files took 3.17 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/classes/init_spec.rb:6 # splunk with defaults for all parameters should contain Package[splunk]
Sorry to ask such a basic question; I really appreciate your help. I'd really love links to good tutorials on testing Puppet code; I'd love to use Vagrant to have some confidence my code is actually going to do what I want it to.
Update:
I believe I have followed #Peter_Souter's suggestions; I still get the error that it can't find the class. I'm posting the contents of the files referenced to try and validate I did the right thing and in case there are other errors.
$ cat .fixtures.yml
fixtures:
symlinks:
"splunk": "#{source_dir}"
$ cat spec_helper.rb
require 'puppetlabs_spec_helper/module_spec_helper'
$ cat Rakefile
require 'puppetlabs_spec_helper/rake_tasks'
require 'puppet-lint/tasks/puppet-lint'
PuppetLint.configuration.send('disable_80chars')
PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"]
desc "Validate manifests, templates, and ruby files"
task :validate do
Dir['manifests/**/*.pp'].each do |manifest|
sh "puppet parser validate --noop #{manifest}"
end
Dir['spec/**/*.rb','lib/**/*.rb'].each do |ruby_file|
sh "ruby -c #{ruby_file}" unless ruby_file =~ /spec\/fixtures/
end
Dir['templates/**/*.erb'].each do |template|
sh "erb -P -x -T '-' #{template} | ruby -c"
end
end
$ cat Gemfile
source 'https://rubygems.org'
puppetversion = ENV.key?('PUPPET_VERSION') ? "#{ENV['PUPPET_VERSION']}" : ['>= 3.3']
gem 'puppet', puppetversion
gem 'puppetlabs_spec_helper', '>= 0.8.2'
gem 'puppet-lint', '>= 1.0.0'
gem 'facter', '>= 1.7.0'
gem 'rspec-puppet', '~> 2.1', :require => false
gem 'rspec-core', '3.1.7', :require => false
The way that rspec-puppet simulates the catalog for testing, it makes a fixtures directory with the required modules in it. So right now it's running it on an empty directory so it can't find your module. We generally fix that with the puppetlabs-spec-helper which makes a simlink to your current repository in that fixtures directory.
I'd recommend adding the following into your files:
# .fixtures.yml
fixtures:
symlinks:
"splunk": "#{source_dir}"
# spec_helper.rb
require 'puppetlabs_spec_helper/module_spec_helper'
# Rakefile
require 'puppetlabs_spec_helper/rake_tasks'
# Gemfile
gem 'rspec-puppet', '~> 2.1', :require => false
gem 'rspec-core', '3.1.7', :require => false
Can you try that and tell me if it works for you?
If somebody stumble around this Thread as I do. The for me correct answer is given in:
Why do I get puppet-rspec 'class does not exist' when it does?
means you have to do:
puppet module generate foo-bar
and then to fix this issue
bundle exec rspec-puppet-init
It's not nice but better then handling with hidden dot files.
I've been cap deploying my app all throughout it development, and this last time I tried to deploy it, it didn't work. Here's what happened:
* executing `deploy:assets:precompile'
* executing "cd /var/www/oneteam/releases/20121006153136 && bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile"
servers: ["electricsasquatch.com"]
[electricsasquatch.com] executing command
** [out :: electricsasquatch.com] rake aborted!
** [out :: electricsasquatch.com] uninitialized constant OneTeam::Application::FactoryGirl
** [out :: electricsasquatch.com]
** [out :: electricsasquatch.com] (See full trace by running task with --trace)
It looks like it failed on the deploy:assets:precompile command. I don't get why that command would have tried to do anything with FactoryGirl, though. Any ideas?
It is not that something is wrong with deploy:assets:precompile task, but something is wrong with rake task that is using FactoryGirl. Even if you place a syntax error let us say in lib/tasks/first.rake and you execute task from lib/tasks/second.rake for instance rake second the rake will scream with rake aborted!. Even rake -T will not work. So there is rake task that is trying to use FactoryGirl but FactoryGirl is not included.
I had this in config/application.rb:
FactoryGirl.define do
sequence(:random_string) { |s| ('a'..'z').to_a.shuffle[0, 30].join }
end
I changed it to this:
if Rails.env != "production"
FactoryGirl.define do
sequence(:random_string) { |s| ('a'..'z').to_a.shuffle[0, 30].join }
end
end
The problem went away.
While trying to do an assets:precompile for a production website I've encountered this error for the first time.
rake assets:precompile undefined method directory? for nil:NilClass
I have successfully updated the website and done a assets:precompile many times before.
The full example:
# RAILS_ENV=production rake assets:precompile --trace
/usr/local/rvm/gems/ruby-1.9.2-p290#pm/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53: warning: already initialized constant WFKV_
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/usr/local/rvm/gems/ruby-1.9.2-p290#pm/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53: warning: already initialized constant WFKV_
rake aborted!
undefined method `directory?' for nil:NilClass
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
I'm looking forward to someone's insight in this one, I've been racking my brain and Googling answers for hours.
This is what happens when the file system can't find one of the assets it thinks its supposed to precompile. This happened to me when my VM could not find one of the files in my shared folder (Weird VMware issue where it appeared in the directory listing but the file did not actually exist)
I had created a symbolic link to a non-existent file by mistake in the assets/javascript directory. Removing the link fixed the problem. Note that I do have other links in the assets directory so links in general are not a problem.
In my case, I had this issue when deploying an application in Heroku.
And one of my files was symlinked to somewhere outside the GIT repository, so the file was not present int 'master'.
I then had to remove the symbolic link (rm .../file) then copy the original file (cp file1 .../file) and finally add it to the repository (git -a -m "Added a forgotten file")
So I'm converting my Admin mailers to use Resque and execute in the background.
When I ran:
rake resque:work QUEUE='*' --trace
I get this:
$ rake resque:work QUEUE='*' --trace (in /my/directory)
** Invoke resque:work (first_time)
** Invoke resque:preload (first_time)
** Execute resque:preload rake aborted! undefined method `paginates_per' for #<Class:0x000000045ba1b8>
which traces back to my Micropost model that default to 10 micropost per page using kaminari's dsl method paginates_per:
class Micropost < ActiveRecord::Base
.
.
.
paginates_per 10
How do I get rake to stop puking without moving the DSL method?
I have upgraded a Rails 2.3.5 app to Rails 3.0.6. In the process I have also upgraded the resque from 1.9.1 to 1.15.0 and resque_scheduler from 1.9.1 to 1.9.9. The following commands used to work fine with Rails 2.3.5
COUNT=1 QUEUE=scheduled_1_queue,another_queue,yet_another_queue,slow_queue,redis_cleanup_queue,immediate_queue RAILS_ENV=development JOBS_PER_FORK=500 rake resque:workers
rake resque:scheduler
But now after the upgrade when I run above commands I get following error:
rake aborted!
wrong number of arguments (0 for 1)
Tasks: TOP => resque:work => resque:setup => environment
The RakeFile is as following:
require File.expand_path('../config/application', __FILE__)
require 'rake'
require 'resque/tasks'
require 'resque_scheduler/tasks'
MyApp::Application.load_tasks
task "resque:setup" => :environment
If i comment the last line 'task "resque:setup" => :environment' in RakeFile the resque scheduler and worker commands run without any error, but the scheduler does not schedule any tasks at all and stays on this:
2011-06-16 09:33:45 Schedule empty! Set Resque.schedule
I have also tried with older version of resque(1.9.1) and resque_scheduler(1.9.1) with Rails 3, but to no avail. I have also tried to run above commands with --trace, but it does not provide any trace info about the "wrong number of arguments (0 for 1)" error. I suspect I might need to provide more info, but don't know exactly what, please let me know what else is needed to answer this question. Thanks a lot.