Rails 3.1 rake test:profile deprecation warning and empty output files - ruby-on-rails

When runnign rake test:profile I get:
DEPRECATION WARNING: read_csv_fixture_files is deprecated and will be
removed from Rails 3.2. (called from block in autorun at
/Users/hade/.rvm/rubies/ruby-1.9.2-p290-perf/lib/ruby/1.9.1/minitest/unit.rb:508)
Unit.rb around line 508:
def self.autorun
at_exit {
next if $! # don't run if there was an exception
# the order here is important. The at_exit handler must be
# installed before anyone else gets a chance to install their
# own, that way we can be assured that our exit will be last
# to run (at_exit stacks).
exit_code = nil
at_exit { exit false if exit_code && exit_code != 0 }
exit_code = MiniTest::Unit.new.run(ARGV) <------ LINE 508
} unless ##installed_at_exit
##installed_at_exit = true
end
Is MiniTest calling this read_csv_fixture_files method? Is this normal behaviour with my setup? How can I remove this?
My environment:
Rails 3.1.0
Ruby 1.9.2-p290 with GC-patch
My tests seem to run ok, but my output files are useless. In my output files I only have this line:
ActiveSupport::Testing::Performance::Profiler#run
But when I run rails profiler 'User.all' I get nice console output and output files with some content.
Any help would be really appreciated. I have tried different Ruby versions with GC-patch, but no help there.

Related

Rails 6 Tasks with arguments

// This is a question for Ruby on Rails
I have a Rails Tasks that should take arguments from command line.
Looks like this :
task update: :environment do
if (ARGV[1] == "DEBUG")
DEBUG = true
else
DEBUG = false
end
Now I can run the command line :
rails call:update DEBUG
and it works !
But after the task was finished I got also the message :
rails aborted!
Don't know how to build task 'DEBUG' (See the list of available tasks with `rails --tasks`)
I looking already around the community, but all what I found was quite old and seems not to be compatible with Rails 6 ! So thats the reason why I asking here.
I try with the code you shared above I got the same error of rake abort.
its Comming because they way you calling the rake task with the argument it assume the DEBUG is another task so it try to find and give this error.
Here is proper way of doing it
I did some modification in the code to get rid of the error use the following code it will work perfectly
task :update, [:value] => [:environment] do |t, args|
if (args[:value] == "DEBUG")
DEBUG = true
p "Value is True"
else
DEBUG = false
p "Value is False"
end
end
it works perfectly
You can also put this task in the namespace and then call it from there as well
namespace :debug_task do
# task code here what I mentioned above
end
and then call it to form the terminal like this
rake debug_task:update["DEBUG"]

Informational text for rails runners

I have a script like
OptionParser.new do |opts|
opt.on("-h","--help","help") do
puts opts
end
end.parse!
But whenever I call rails runner my_script.rb --help it shows me help for the rails runner and not my script. Is there a way that I can prevent rails runner from swallowing up this option?
I am afraid you cannot do this with runner - runner first searches for its own options and in case of --help or -h it prints the help and exits before even checking whether your script exists or not:
# Railites: lib/rails/commands/runner
opts.on("-h", "--help",
"Show this help message.") { $stdout.puts opts; exit }
You can however get around this by simply not using runner at all and writing pure ruby script:
#!/usr/bin/env ruby
require 'optparse'
environment = 'development'
OptionParser.new do |opts|
opt.on("-e", "--environment") do |v|
environment = v
end
opt.on("-h","--help","help") do
puts opts
end
end.parse!
# RAILS_ENV is used by environment.rb file to load correct configuration
ENV['RAILS_ENV'] = environment
# Load your rails application
require_relative "../config/environment.rb"
puts User.count # Your code here

rake aborted! Don't know how to build task 'sandbox'

I'm trying to add the sandbox to my rails spree application and have run into this error
(using windows 8/powershell with Rails 4.1.6). I'm going by this manual: https://github.com/spree/spree/issues/411
This link Use older version of Rake
seems to have a similar issue but I am not sure how to take the necessary steps to achieve it.
When I try:
C:\Ruby193\openUpShop> bundle exec rake sandbox
I get:
rake aborted!
Don't know how to build task 'sandbox'
I'm am new to rails and am still not sure how everything works so a throughout explanation
with step by step instructions would be greatly appreciated! Thanks.
you can use a file sandbox.rb
# use example: rake task:sub_task -- --sandbox
if ARGV.any? {|arg| arg == '--sandbox' }
puts "** << USING SANDBOX!! >> **"
# beginning
$sandbox = -> do
ActiveRecord::Base.connection.begin_transaction
end
# end
at_exit do
ActiveRecord::Base.connection.rollback_transaction
end
end
then only you need add at the beginning of your task.rake file
require_relative 'path_to_your/sandbox.rb'
..and add at the beggining of your task code
desc "description task"
task example_task: :environment do
$sandbox.call if $sandbox.present?
...

Ruby on Rails rescuing from 'Errno::ENOENT'

I need to rescue from 'Errno::ENOENT' in a Ruby on Rails 3.0.4 application. I currently have controller with the following code:
begin
`lame #{parameters}`
rescue Errno::ENOENT
logger.info "command 'lame' not found: ensure LAME is installed"
end
However, the log code is never called, but the logs show:
script/rails: No such file or directory - lame ...
If I create a ruby script with same snippet, the exception is rescued.
In Ruby 1.8, Errno::ENOENT is not raised by shell execution / back-ticks - the error you're seeing is standard error, printed by the shell. If you want to detect this, I'd recommend looking for an exit code of 127:
`lame #{parameters} 2>&1`
if $?.exitstatus == 127
logger.info "command 'lame' not found: ensure LAME is installed"
end
However, this will raise Errno::ENOENT in Ruby 1.9.
You might consider checking the output from which lame instead:
lame_installed = system("which lame >/dev/null")
# or even better
lame_available = !(lame_path = `which lame`.strip).empty? && File.executable?(lame_path)
Further reading:
http://www.faqs.org/docs/abs/HTML/exitcodes.html

How can I tell if Rails code is being run via rake or script/generate?

I've got a plugin that is a bit heavy-weight. (Bullet, configured with Growl notifications.) I'd like to not enable it if I'm just running a rake task or a generator, since it's not useful in those situations. Is there any way to tell if that's the case?
It's as simple as that:
if $rails_rake_task
puts 'Guess what, I`m running from Rake'
else
puts 'No; this is not a Rake task'
end
Rails 4+
Instead of $rails_rake_task, use:
File.basename($0) == 'rake'
I like NickMervin's answer better, because it does not depend on the internal implementation of Rake (e.g. on Rake's global variable).
This is even better - no regexp needed
File.split($0).last == 'rake'
File.split() is needed, because somebody could start rake with it's full path, e.g.:
/usr/local/bin/rake taskname
$0 holds the current ruby program being run, so this should work:
$0 =~ /rake$/
It appears that running rake will define a global variable $rakefile, but in my case it gets set to nil; so you're better off just checking if $rakefile has been defined... seeing as __FILE__ and $FILENAME don't get defined to anything special.
$ cat test.rb
puts(global_variables.include? "$rakefile")
puts __FILE__
puts $FILENAME
$ cat Rakefile
task :default do
load 'test.rb'
end
$ ruby test.rb
false
test.rb
-
$ rake
(in /tmp)
true
./test.rb
-
Not sure about script/generator, though.
The most stable option is to add $is_rake = true at the beginning of Rakefile and use it from your code.
Use of $0 or $PROGRAM_NAME sometimes will fail, for example when using spring and checking variables from config/initializers
You can disable the plugin using environment variable:
$ DISABLE_BULLET= 1 rake some:task
And then in your code:
unless ENV['DISABLE_BULLET']
end
We could ask this
Rake.application.top_level_tasks
In a rails application, this is an empty array, whereas in a Rake task, the array has the task name in it.
top_level_tasks probably isn't a public API, so it's subject to changes. But this is the only thing I have found.

Resources