Rake -T does not identify tasks - ruby-on-rails

I am trying to setup a Rake Task (without rails), and thus created a demo Hello World Task. However, upon running rake -T or rake --tasks nothing is returned, as if the task hasn't been identified. What am I doing wrong here?
Also When I run rake hello_world I get the following error:
➜ ~/GitHub/Cerner/test git:(master) ✗ rake hello_world
rake aborted!
Don't know how to build task 'hello_world' (See the list of available tasks with `rake --tasks`)
/usr/share/rvm/gems/ruby-2.6.6/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/home/hsbagga28/gems/bin/ruby_executable_hooks:22:in `eval'
/home/hsbagga28/gems/bin/ruby_executable_hooks:22:in `<main>'
(See full trace by running task with --trace)
[Update] The rakefile:
# frozen_string_literal: true
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
$LOAD_PATH.push File.expand_path('lib', __dir__)

Related

Rake doesn't know how to build task 'db:migrate'

I've created an app using Ruby on Rails and Spree.
After changing my SQlite database to PostgreSQL I needed to migrate my database.
But when I try to run rake db:migrate, rake db:migrate RAILS_ENV=development, bin/rake db:migrate RAILS_ENV=development I get the same error.
rake aborted!
Don't know how to build task 'db:migrate:up' (see --tasks)
/usr/local/rvm/gems/ruby-2.3.0/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
When I run --trace I get the following response
** Invoke default (first_time)
** Invoke spec (first_time)
** Execute spec
/usr/local/rvm/rubies/ruby-2.3.0/bin/ruby -I/usr/local/rvm/gems/ruby-2.3.0/gems/rspec-core-3.7.0/lib:/usr/local/rvm/gems/ruby-2.3.0/gems/rspec-support-3.7.0/lib /usr/local/rvm/gems/ruby-2.3.0/gems/rspec-core-3.7.0/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
Could not load dummy application. Please ensure you have run `bundle exec rake test_app`
** Execute default
Does any of you know what the cause of the problem is and what I should do?
Rakefile
require 'rubygems'
require 'rake'
require 'rake/testtask'
require 'rspec/core/rake_task'
require 'spree/testing_support/common_rake'
RSpec::Core::RakeTask.new
task default: :spec
desc "Generates a dummy app for testing"
task :test_app do
ENV['LIB_NAME'] = 'spree/frontend'
Rake::Task['common:test_app'].invoke
end
I added,
require File.expand_path('../config/application', __FILE__)
and
YouApp::Application.load_tasks
to my rake file.
Credits go to #nattfodd

Test tasks missing from rake: rake aborted! Don't know how to build task 'test:units'

I created a Rails 4 app recently and for some reason my rake tasks don't include test. Here's some console action to demonstrate the problem:
> rake test:units
rake aborted!
Don't know how to build task 'test:units'
/Users/clozach/.rvm/gems/ruby-2.1.3#hoverfly/bin/ruby_executable_hooks:15:in `eval'
/Users/clozach/.rvm/gems/ruby-2.1.3#hoverfly/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
Fail. :(
> rake test
>
No output. :(
> rake -T --all | grep test
rake db:test:clone #
rake db:test:clone_schema #
rake db:test:clone_structure #
rake db:test:deprecated #
rake db:test:load #
rake db:test:load_schema #
rake db:test:load_structure #
rake db:test:prepare #
rake db:test:purge #
rake log:clear # Truncates all *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)
rake tmp/cache/assets/test #
No test tasks at all! :'(
Any idea why this is the case, and what I can do to fix it?
Take a look into config/application.rb whether the testing framework is actually required there. At the very top, there should be
require "rails/all"
Depending on your setup, you might have each individual component required separately. Make sure that test_unit is among them.
require "rails/test_unit/railtie"

heroku run rake db:migrate ... rake aborted! No Rakefile found

When I run heroku run rake db:migrate, I receive the following error:
$ heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.8507
rake aborted!
No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
/usr/local/lib/ruby/1.9.1/rake.rb:2367:in `raw_load_rakefile'
/usr/local/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile'
/usr/local/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/usr/local/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile'
/usr/local/lib/ruby/1.9.1/rake.rb:1991:in `run'
/usr/local/bin/rake:31:in `<main>'
Other users have posted identical errors, but in their case they actually didn't have a Rakefile and it worked for them once they created one, or they were in the wrong directory. My app does have a Rakefile, in the correct directory and with all the proper text in it, and I am in my app's root directory.
This is what my Rakefile looks like:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
Rails.application.load_tasks
This is what "heroku run cat Rakefile" does:
$ heroku run cat Rakefile
Running `cat Rakefile` attached to terminal... up, run.9132
cat: Rakefile: No such file or directory
Whenever you are struggling with Heroku you should check your logs:
heroku logs
Scan through for any errors that you can google and/or get hints to what is going wrong.
If you are using git you can quickly search for errors with this:
git grep "whatever you want to search for"
You will then know where to find the file so you might try to verify that the rakefile actually exists with:
git grep rakefile
Sometimes it is easier to just start fresh and reset your database and reload your current schema with:
rake db:reset db:migrate all
This will destroy your db and then create it and then migrate your current schema:
rake db:drop db:create db:migrate
If you want to reset the heroku db
To drop the database, if you are using SHARED_DATABASE_URL:
heroku pg:reset DATABASE
To recreate the database with nothing in it:
heroku run rake db:migrate
To populate the database with your seed data:
heroku run rake db:seed

Test Unit error when running rake tasks

I upgrade my app from rails2 to rails3. Now every time I run a task I get this error:
➜ cimm git:(master) ✗ ruby -v
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin13.0.0]
➜ cimm git:(master) ✗ rails -v
Rails 3.0.20
➜ cimm git:(master) ✗ bundle exec rake sitemaps:servicos --trace
** Invoke sitemaps:servicos (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute sitemaps:servicos
/Users/netto/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: sitemaps:servicos (ArgumentError)
from /Users/netto/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
from /Users/netto/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
from /Users/netto/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
from /Users/netto/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
from /Users/netto/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
from /Users/netto/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
from /Users/netto/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/test/unit.rb:21:in `run'
from /Users/netto/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
from /Users/netto/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
from /Users/netto/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'
I don't know why is loading something about Test Unit if I'm using Rspec. I tried change rake version, changing the require 'rails/all' but none fixed this issue.
But my tasks are loaded, so seems this is kind of a warning. But I'd to know how to fix this.
Update 1
These errors only appear when my task interact with Model/Controllers.
My Rakefile:
require File.expand_path('../config/application', __FILE__)
require 'rake'
CIMM::Application.load_tasks
Check your Rakefile. it probably loading TestUnit
You should show the custom task if you want more help.
➜ cimm git:(master) ✗ rake -T
rake about # List versions of all Rails frameworks and the environment
rake airbrake:deploy # Notify Airbrake of a new deploy.
rake airbrake:heroku:add_deploy_notification # Install Heroku deploy notifications addon
rake airbrake:test # Verify your gem installation by sending a test exception to the airbrake service / Verify your gem instal...
rake db:create # Create the database from config/database.yml for the current Rails.env (use db:create:all to create all d...
rake db:drop # Drops the database for the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load # Load fixtures into the current environment's database.
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false).
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n).
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db...
rake db:structure:dump # Dump the database structure to an SQL file
rake db:version # Retrieves the current schema version number
rake doc:app # Generate docs for the app -- also available doc:rails, doc:guides, doc:plugins (options: TEMPLATE=/rdoc-t...
rake log:clear # Truncates all *.log files in log/ to zero bytes
rake middleware # Prints out your Rack middleware stack
rake notes # Enumerate all annotations (use notes:optimize, :fixme, :todo for focus)
rake notes:custom # Enumerate a custom annotation, specify with ANNOTATION=CUSTOM
rake rails:template # Applies the template supplied by LOCATION=(/path/to/template) or URL
rake rails:update # Update both configs and public/javascripts from Rails (or use just update:javascripts or update:configs)
rake rails:upgrade:backup # Backs up your likely modified files so you can run the Rails 3 generator on your app with little risk
rake rails:upgrade:check # Runs a battery of checks on your Rails 2.x app and generates a report on required upgrades for Rails 3
rake rails:upgrade:configuration # Extracts your configuration code so you can create a new config/application.rb
rake rails:upgrade:gems # Generates a Gemfile for your Rails 3 app out of your config.gem directives
rake rails:upgrade:routes # Create a new, upgraded route file from your current routes.rb
rake routes # Print out all defined routes in match order, with names.
rake secret # Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie se...
rake sitemaps:servicos # Cria sitemap para servicos
rake spec # Run all specs in spec directory (excluding plugin specs)
rake spec:controllers # Run the code examples in spec/controllers
rake stats # Report code statistics (KLOCs, etc) from the application
rake time:zones:all # Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter,...
rake tire:import # Import data from your model using paginate: rake environment tire:import CLASS='MyModel'.
rake tire:index:drop # Delete indices passed in the INDEX environment variable; separate multiple indices by comma.
rake tmp:clear # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sock...
rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids

How do I get a custom Rake task to run in Sinatra?

*I want to get a custom Rake task to run in my Sinatra app but I keep getting rake aborted!
Don't know how to build task 'greet'.
Here's the custom Rake task (greet.rake) for testing purpose:
task :greet do
puts "Hello!"
end
I've put the greet.rake in ./lib/tasks (Rails). I'm guessing Rake can't find the correct directory for the file.
How do I get a custom Rake task to run in Sinatra?
I'm using Ruby 2.0.0 and Sinatra 1.4.4.
UPDATE
The Rakefile now looks like this:
require "./app"
require "sinatra/activerecord/rake"
require "./lib/tasks"
When using:
rake greet
I get:
rake aborted!
cannot load such file -- ./lib/tasks
/Users/*/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport- 4.0.1/lib/active_support/dependencies.rb:229:in `block in require'
/Users/*/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport- 4.0.1/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/*/.rvm/gems/ruby-2.0.0-p247#global/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:229:in `require'
/Users/*/Dropbox/Development/Sinatra/sinatra-mp-experiment/Rakefile:3:in `<top (required)>'
(See full trace by running task with --trace)
Create a Rakefile at your Sinatra app's top directory, require the file that contains this task you want to use and you should be good to go.
Edit:
A simple solution is changing your Rakefile to:
require "./app"
require "sinatra/activerecord/rake"
Dir.glob('lib/tasks/*.rake').each { |r| load r}
Now any .rake file under lib/tasks will be loaded.

Resources