I am using ruby 2.2.1 and rails 4.2.1.
I am using following gems.
roo (2.0.1)
roo-xls (1.0.0)
rubyzip (1.1.7)
I have written a rake task as follows.
check_roo.rake
require 'rubygems'
require 'roo-xls'
require 'roo'
desc "desc"
task :check_roo => :environment do |task, args|
xlsx = Roo::Excelx.new("#{Rails.root}/v15_Data.xlsx")
end
Output for bundle exec rake check_roo:
NameError: uninitialized constant Roo::Excelx::Zip
/home/abhimanyu/.rvm/gems/ruby-2.2.1#newerp/gems/roo-2.0.1/lib/roo/excelx.rb:412:in `process_zipfile'
/home/abhimanyu/.rvm/gems/ruby-2.2.1#newerp/gems/roo-2.0.1/lib/roo/excelx.rb:100:in `initialize'
/home/abhimanyu/Documents/projects/abc/lib/tasks/check_roo.rake:9:in `new'
/home/abhimanyu/Documents/projects/abc/lib/tasks/check_roo.rake:9:in `block in <top (required)>'
But when I run the following command in rails console it works fine.
xlsx = Roo::Excelx.new("#{Rails.root}/v15_Data.xlsx")
Output:
<#Roo::Excelx:320135634 #tmpdirs #tmpdir #filename #comments_files #rels_files #sheet_files #workbook #sheet_names #sheets #styles #shared_strings #sheets_by_name #options #cell #cell_type #cells_read #first_row #last_row #first_column #last_column #header_line>
Not able to understand why the same command works in console but not inside a rake file.
Any help would be appreciated. Thanks in advance.
Related
On Rails 6 (6.1.4.1) we had a RakeFile that would run a subset of tests. For example:
# lib/tasks/carrier.rake
namespace :test do
task carriers: "test:prepare" do
$: << "test"
test_files = FileList["test/models/carrier_test.rb",
"test/controllers/admin/carriers/**/*_test.rb",
"test/system/admin/carriers/**/*_test.rb"]
Rails::TestUnit::Runner.run(test_files)
end
end
This would execute just fine when called:
rails test:carriers
However, somewhere along the way, something changed and we began seeing errors when trying to run our RakeFile test tasks. (I haven't tracked down exactly what changed and when it changed -- perhaps it was part of the Rails 7 release.) Here's the error we began seeing:
rails aborted!
NameError: uninitialized constant Shoulda
Shoulda::Matchers.configure do |config|
^^^^^^^
/path/test/test_helper.rb:15:in `<main>'
/path/test/models/carrier_test.rb:1:in `<main>'
/path/lib/tasks/carriers.rake:11:in `block (2 levels) in <main>'
Tasks: TOP => test:carriers
(See full trace by running task with --trace)
The error appeared with no changes to our tests or environment configuration. (Running a full rake worked just fine.)
When reviewing the source code for Rails::TestUnit::Runner, I came across rake_run. Simply replacing Rails::TestUnit::Runner.run with Rails::TestUnit::Runner.rake_run addressed the issue (no other changes required):
# lib/tasks/carrier.rake
namespace :test do
task carriers: "test:prepare" do
$: << "test"
test_files = FileList["test/models/carrier_test.rb",
"test/controllers/admin/carriers/**/*_test.rb",
"test/system/admin/carriers/**/*_test.rb"]
Rails::TestUnit::Runner.rake_run(test_files)
end
end
When I try to run rake db:migrate I am getting following error:
DEPRECATION WARNING: The factory_girl gem is deprecated. Please upgrade to factory_bot. See https://github.com/thoughtbot/factory_bot/blob/v4.9.0/UPGRADE_FROM_FACTORY_GIRL.md for further instructions. (called from <top (required)> at /var/www/html/sophy_web/config/application.rb:14)
/root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/actionpack-4.2.8/lib/action_dispatch/http/mime_type.rb:163: warning: already initialized constant Mime::PDF
/root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/actionpack-4.2.8/lib/action_dispatch/http/mime_type.rb:163: warning: previous definition of PDF was here
rake aborted!
NoMethodError: undefined method `>' for nil:NilClass
/var/www/html/application/config/initializers/rollbar.rb:2:in `<top (required)>'
/var/www/html/application/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
I am using ruby version 2.5.1 and rails 4.2.8.
Code of my rollbar.rb:
require 'rollbar/rails'
Rollbar.configure do |config|
config.access_token = '9234902384023843203fsd23'
# Here we'll disable in 'test':
if Rails.env.test?
config.enabled = false
end
end
Code of my environment.rb
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
I want to use ruby-progressbar in Rails 3.2.11 for my console scripts. I added gem 'ruby-progressbar' to Gemfile and ran bundle install.
It shows uninitialized constant ProgressBar when I try to use it.
If I do require 'ruby-progressbar' it shows the following error:
LoadError: cannot load such file -- ruby-progressbar
from /Users/evgenyneu/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
gem :
https://rubygems.org/gems/ruby-progressbar
You need write some as:
require 'progressbar'
pbar = ProgressBar.new('data processing', some_long_data_array.length)
some_long_data_array.each do |data|
data.process
pbar.inc
end
pbar.finish
I'm trying to create an xml runner to make a result report of Rails unit testing. Here is a code I have:
require 'test/unit'
require 'test/unit/ui/console/testrunner'
class FastFailRunner < Test::Unit::UI::Console::TestRunner
def add_fault(fault)
#faults << fault
nl
output("%3d) %s" % [#faults.length, fault.long_display])
output("--")
#already_outputted = true
end
def finished(elapsed_time)
nl
output("Finished in #{elapsed_time} seconds.")
nl
output(#result)
end
end
Test::Unit::AutoRunner::RUNNERS[:fastfail] = proc do |r|
FastFailRunner
end
When I run it as TESTOPTS="/home/alex/RubymineProjects/app2/test/unit/runner.rb --runner=xml" rake test
... I get an error (pretty weird error)
/home/alex/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/ext/module.rb:36:in `const_missing': uninitialized constant Test::Unit::UI::XML (NameError)
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/runner/xml.rb:5:in `block in <module:Unit>'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `[]'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:58:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit.rb:501:in `block in <top (required)>'
gem install minitest
/home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/runner/xml.rb:5:in `block in <module:Unit>': uninitialized constant Test::Unit::UI::XML (NameError)
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `[]'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:58:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit.rb:501:in `block in <top (required)>'
Errors running test:units! #<RuntimeError: Command failed with status (1): [/home/alex/.rvm/rubies/ruby-1.9.3-p194/bin...]>
Errors running test:functionals! #<RuntimeError: Command failed with status (1): [/home/alex/.rvm/rubies/ruby-1.9.3-p194/bin...]>
I tried to require test/unit/ui/xml/testrunner.rb but no luck.
Any thoughts?
If you are in Ruby 1.8.7 you can say:
require 'test/unit/ui/console/testrunner'
p Test::Unit::UI::Console::TestRunner # => no problem
(There is no such file as test/unit/ui/xml/testrunner.rb so I'm not sure what you were up to there.)
Look in the docs in test/unit.rb, there's actually sample code showing you how to do this require: http://www.ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit.html.
However, the problem is that you are in Ruby 1.9.3. There is no /test/unit in Ruby 1.9.3! Well, there is, but it's just a compatibility layer for basic tests; there is certainly no test/unit/ui/console/testrunner, and no module/class Test::Unit::UI::Console::TestRunner.
Instead, there's minitest. You can read the docs on minitest to see how to make a test runner. http://docs.seattlerb.org/minitest/
One thing to consider is that Test::Unit was included in the default Ruby 1.8.7 installation. If you want to use it in with a later version of Ruby then go ahead and install it as a gem.
sudo gem install test-unit
http://test-unit.rubyforge.org/
I've been using cucumber in my app, which is a rails 2.2.2 app running in ruby 1.8.6 (upgrading isn't an option right now). Cucumber's been fine, now i'm trying to use Capybara. I've installed the capybara (1.1.1) gem and put the line require 'capybara/rails' in my features/support/env.rb file.
Now, when i run cucumber, i get this error:
Using the default profile...
uninitialized constant Rack::Builder (NameError)
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:445:in `load_missing_constant'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:77:in `const_missing'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/capybara-1.1.1/lib/capybara/rails.rb:4
/home/max/.rvm/rubies/ruby-1.8.6-p420/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
/home/max/.rvm/rubies/ruby-1.8.6-p420/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:155:in `require'
/home/max/work/charanga/elearn_container/elearn/features/support/env.rb:10
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/cucumber-1.1.1/bin/../lib/cucumber/rb_support/rb_language.rb:143:in `load'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/cucumber-1.1.1/bin/../lib/cucumber/rb_support/rb_language.rb:143:in `load_code_file'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/cucumber-1.1.1/bin/../lib/cucumber/runtime/support_code.rb:171:in `load_file'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/cucumber-1.1.1/bin/../lib/cucumber/runtime/support_code.rb:83:in `load_files!'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/cucumber-1.1.1/bin/../lib/cucumber/runtime/support_code.rb:82:in `each'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/cucumber-1.1.1/bin/../lib/cucumber/runtime/support_code.rb:82:in `load_files!'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/cucumber-1.1.1/bin/../lib/cucumber/runtime.rb:137:in `load_step_definitions'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/cucumber-1.1.1/bin/../lib/cucumber/runtime.rb:39:in `run!'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/cucumber-1.1.1/bin/../lib/cucumber/cli/main.rb:43:in `execute!'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/cucumber-1.1.1/bin/../lib/cucumber/cli/main.rb:20:in `execute'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/cucumber-1.1.1/bin/cucumber:14
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/bin/cucumber:19:in `load'
/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/bin/cucumber:19
Here's the erroring file:
#/home/max/.rvm/gems/ruby-1.8.6-p420#elearning-resource/gems/capybara-1.1.1/lib/capybara/rails.rb
require 'capybara'
require 'capybara/dsl'
Capybara.app = Rack::Builder.new do
map "/" do
if Rails.version.to_f >= 3.0
run Rails.application
else # Rails 2
use Rails::Rack::Static
run ActionController::Dispatcher.new
end
end
end.to_app
Capybara.asset_root = Rails.root.join('public')
Capybara.save_and_open_page_path = Rails.root.join('tmp/capybara')
So, line 4 is creating the error with the Rack::Builder.new line. But, why? Any ideas? I have the Rack gem installed already.
thanks, max
Try adding require 'rack/builder' before the require 'capybara/rails'in features/support/env.rb. Rails versions lower than 2.3 don't use Rack internally and thus Rack::Builder will not be loaded like capybara seems to be assuming.
Did you use the cucumber-rails gem?
https://github.com/jnicklas/capybara says, either use the gem or, if your're not using rails, put both these lines in:
require 'capybara/cucumber'
Capybara.app = MyRackApp
try this out
gem install rack
require 'rack' # in features/support/env.rb before every require
Rack::Builder is defined in rack library.