Why can't Padrino find my rake tasks? - ruby

I just upgraded Padrino from version 0.10.5 to 0.12.1 and now none of my Rake tasks work anymore.
A simple case I just added:
# PROJECT_ROOT/lib/tasks/example_task.rake
task :example_task do
puts "Is this working?"
end
Running rake example_task gives:
rake aborted!
Don't know how to build task 'example_task'
Running padrino rake example_task gives:
=> Executing Rake example_task ...
/Users/george/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/rake/task_manager.rb:49:in `[]': Don't know how to build task 'example_task' (RuntimeError)
Padrino's docs say that Padrino should automatically detect any *.rake files in lib/tasks, and these worked before I upgraded Padrino. What's the problem?
My Gemfile:
source 'https://rubygems.org'
ruby '2.1.0'
gem 'rake'
gem 'unicorn'
gem 'rack_hoptoad', require: 'rack/hoptoad'
gem 'haml'
gem 'bson_ext'
gem 'mongo'
gem 'mongoid', '~> 3.1.6'
gem 'httparty'
group :development, :test do
gem 'debugger'
gem 'fabrication', '~> 1.3.1'
gem 'zippy'
gem 'rubyzip', '~> 0.9.9'
end
group :test do
gem 'mocha', '~> 0.10.4'
gem 'rspec', '~> 2.8.0'
gem 'capybara', '~> 1.1.2'
gem 'launchy'
gem 'cucumber', '~> 1.1.8'
end
gem 'tilt', '~> 1.4.1'
gem 'padrino', '~> 0.12.1'
gem 'padrino-cookies', '~> 0.1.2'
According to bundle show rake, I'm using rake version 10.3.1.
What am I doing wrong?

Well, that was a waste of a bounty.
I opened an issue on Github and got an answer there. I needed to make the following changes to the top of config/boot.rb... (this is taken from the output of git diff, so note that lines which start with - were removed and lines which start with + were added.)
-begin
- # Require the preresolved locked set of gems.
- require File.expand_path('../../.bundle/environment', __FILE__)
-rescue LoadError
- # Fallback on doing the resolve at runtime.
- require 'rubygems'
- require 'bundler'
- Bundler.setup
-end
-
+require 'rubygems' unless defined?(Gem)
+require 'bundler/setup'
And add these lines to my Rakefile:
+require 'bundler/setup'
+require 'padrino-core/cli/rake'
+
+PadrinoTasks.use(:database)
+PadrinoTasks.use(:mongoid)
+PadrinoTasks.init
Now my rake tasks work as normal. Yay!

Use this pattern and it will work with new version of Padrino without any core changes:
# ./lib/tasks/seed_fonts.rake
namespace :fonts do
desc "Seen the fonts into database table db.fonts"
task :seed_fonts => :environment do
# Do your thing ...
end
end

Related

FrozenError - can't modify frozen Array - Error when trying to run brand new Padrino project [duplicate]

When I start server with padrino start, this error happens:
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/padrino-core-0.15.1/lib/padrino-core/application.rb:32:in `concat': can't modify frozen Array:
[/\\/sinatra(\\/(base|main|show_exceptions))?\\.rb$/,
/lib\\/tilt.*\\.rb$/, /^\\(.*\\)$/,
/rubygems\\/(custom|core_ext\\/kernel)_require\\.rb$/,
/active_support/, /bundler(\\/(?:runtime|inline))?\\.rb/,
/<internal:/] (FrozenError)
And my gemfile is...
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Padrino supports Ruby version 2.2.2 and later
ruby '2.7.0'
# Distribute your app as a gem
# gemspec
# Server requirements
# gem 'thin' # or mongrel
# gem 'trinidad', :platform => 'jruby'
# Optional JSON codec (faster performance)
# gem 'oj'
# Project requirements
gem 'rake'
gem 'bcrypt'
gem 'activesupport'
# Component requirements
gem 'erubi', '~> 1.6'
gem 'activerecord', '>= 3.1', :require => 'active_record'
gem 'sqlite3'
# Test requirements
# Padrino Stable Gem
gem 'padrino', '0.15.1'
The error is being raised here when padrino tries to modify the CALLERS_TO_IGNORE array.
The root cause it is a change in sinatra 3, the CALLERS_TO_IGNORE array was not frozen on version 2.2.2.
Given that last version of padrino was released on April 26, 2021, I would not expect it to be solved soon.
My advice is to pin the version of sinatra in your gemfile gem 'sinatra', '2.2.2'.

Rspec suite fails running on AWS Ubuntu with ECONNREFUSED - but each test passes individually

We are trying to migrate our rails build from a local network server to an AWS EC2 instance running Ubuntu. The test suite works fine on all the developers machines (combination of mac and ubuntu) and the old build server (linux) but refuses to pass on the amazon instance.
The symptom is:
An error occurred in an after hook
Errno::ECONNREFUSED: Connection refused - connect(2)
occurred at /home/ubuntu/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/net/http.rb:878:in 'initialize'
which is repeated a bunch of times. There is no error logged before this one.
However this issue only occurs when running multiple tests together (eg, bundle exec rspec) - when I run each test individually they all pass. Locally none of the dev team have seen this issue before.
Our tests use Selenium and Firefox which should run headless on ubuntu using Xvfb. We also use WebMock for some tests.
I have researched this issue for a day or two now and tried everything I can think of, to no avail. I have checked the versions of firefox and all the gems installed are the same on aws as locally.
My gemfile and spec_helper are below. I have investigated the firefox and xvfb installation, but because each test is able to pass individually I feel the system is generally ok ... but clearly something is not happy.
Version details
ruby 2.0.0p481
rails 3.2.16
rspec 2.14.8
firefox 29
ubuntu 14.04
Gemfile
source 'https://rubygems.org'
#Base gems
gem 'rails', '~> 3.2.16'
gem 'sass-rails'
gem 'bcrypt-ruby', '~> 3.0.0'
gem 'faker', '~> 1.2.0'
gem 'jquery-rails', '~> 3.0.0'
#gem 'jquery-rails', '~> 2.1.4'
gem 'jquery-migrate-rails'
# threading gem
gem 'sucker_punch', '~> 1.0'
gem 'mixpanel-ruby'
# Mongo gems
gem 'mongoid', '~> 3.1.6'
gem 'bson_ext', '~> 1.9.2'
gem 'origin', '~> 1.1.0'
gem 'mongoid-enum' , '~> 0.1.1'
#filemaker gem
#gem 'ginjo-rfm', '~> 2.1.7'
# data gird gem
gem 'datagrid', '1.0.5'
# New Relic and Engine Yard Gems
gem 'ey_config', '~> 0.0.6'
gem 'newrelic_rpm'
#Authorsiation
gem 'cancan', '~> 1.6.10'
#paging
gem 'kaminari', '~> 0.15'
#client side validation
gem 'jquery-validation-rails'
#file upload
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
gem 'rmagick', '~> 2.13.2'
gem 'fog' , '~> 1.19.0'
gem 'unf', '~> 0.1.3'
gem 'remotipart', '~> 1.2'
gem 'listen', '2.7.6'
# reports for jenkins - see if having it here allows engineyard to deploy
gem 'ci_reporter', '1.9.1'
#NOTE YOU MUST INSTALL IMG MAGIC IF bundle install fails
#http://www.imagemagick.org/script/binary-releases.php#unix
#sudo apt-get install libmagickwand-dev imagemagick
gem 'timecop', '~> 0.7.1'
#file upload
gem 'jquery-fileupload-rails', '~> 0.4.1'
gem 'jquery-cookie-rails', '~> 1.3.1'
#configuring Rails
gem 'figaro'
#History tracking
gem 'mongoid-history'
gem 'nav_lynx'
#modal window requirements
gem 'jquery-ui-rails'
gem 'jquery-modal-rails'
gem 'itree'
gem 'lumberjack'
gem 'newrelic_moped'
group :development, :test do
gem 'rspec-rails', '~> 2.14.0'
gem 'guard-rspec', '~> 4.2.0'
gem 'guard-spork', '~> 1.5.1'
gem 'childprocess', '0.3.9'
gem 'spork', '~> 1.0.0rc4'
gem 'spork-rails', '~> 4.0.0'
gem 'bullet'
end
group :development, :staging do
gem 'rack-mini-profiler'
end
#Used to annotate model files generated
group :development do
gem 'annotate', '~> 2.6.0'
gem 'letter_opener', '~> 1.1.2'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'coffee-rails', '~> 3.2.2'
gem 'uglifier', '~> 2.3.2'
end
group :test do
gem 'capybara', '~> 2.2.1'
gem 'selenium-webdriver', '2.39.0'
gem 'database_cleaner', '~> 1.2.0'
#needs sudo apt-get install xvfb
gem 'headless'
#static security analysis tool
gem 'brakeman', :require => false
#generating mock objects in tests
gem 'factory_girl_rails', '~> 4.3.0'
gem 'webmock', '~> 1.16.0'
#for windows
gem 'rb-fchange' if /win32/ =~ RUBY_PLATFORM
gem 'rb-notifu' if /win32/ =~ RUBY_PLATFORM
gem 'win32console' if /win32/ =~ RUBY_PLATFORM
#for linux
gem 'rb-inotify', '~> 0.9.2' if /linux/ =~ RUBY_PLATFORM
gem 'libnotify', '~> 0.8.2' if /linux/ =~ RUBY_PLATFORM
#for macosx
gem 'growl', '~> 1.0.3' if /darwin/ =~ RUBY_PLATFORM
gem 'rb-fsevent', '~> 0.9.3' if /darwin/ =~ RUBY_PLATFORM
# for rcov in Jenkins
gem 'simplecov'
gem 'simplecov-rcov'
#additional matches for rspect
gem 'shoulda-matchers', '~> 2.5.0'
#open browser on demand to debug tests
gem 'launchy'
#http rest client
gem 'rest-client', '~> 1.6.7'
gem 'show_me_the_cookies'
end
spec_helper.rb
require 'rubygems'
require 'spork'
require 'webmock/rspec'
require 'sucker_punch'
require 'sucker_punch/testing/inline'
Spork.prefork do
if ENV["RUBYMINE_HOME"]
$:.unshift(File.expand_path("rb/testing/patch/common", ENV["RUBYMINE_HOME"]))
$:.unshift(File.expand_path("rb/testing/patch/bdd", ENV["RUBYMINE_HOME"]))
end
# setup simple cov for Jenkins
require 'simplecov'
require 'simplecov-rcov'
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
SimpleCov.start 'rails'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.include ShowMeTheCookies
# Include Factory Girl syntax to simplify calls to factories
config.include FactoryGirl::Syntax::Methods
#Don't run any tests that contain :skip_test => true
config.filter_run_excluding :skip_test => true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include Capybara::DSL
config.before(:suite) do
Headless.new.start
end
config.before(:all) do
#manually clear database
YearDates.destroy_all
CoursePlan.destroy_all
Topic.destroy_all
FactoryGirl.create(:stub_year_dates)
end
config.after(:all) do
Timecop.return
end
#Mongoid database cleaner
config.before(:suite) do
DatabaseCleaner[:mongoid, {:connection => :unit_test}].clean_with(:truncation)
DatabaseCleaner[:mongoid, {:connection => :unit_test}].strategy = :truncation
end
config.before(:each) do
new_time = Time.local(2014, 1, 20) #week two of the year
Timecop.travel(new_time)
Timecop.baseline = new_time
DatabaseCleaner[:mongoid, {:connection => :unit_test}].start
FactoryGirl.create(:stub_year_dates) #recreate this on each test as it is needed all the time, but edited sometimes
#need to ensure these exist before the other factories are called
FactoryGirl.create(:english_subject)
FactoryGirl.create(:chemistry_subject)
FactoryGirl.create(:maths_ext_2_subject)
FactoryGirl.create(:maths_ext_1_tutorials_only_subject)
end
config.after(:each) do
Timecop.return_to_baseline
DatabaseCleaner[:mongoid, {:connection => :unit_test}].clean
end
Capybara.default_wait_time = 5 #wait longer for ajax requests
end
end
Spork.each_run do
# This code will be run each time you run your specs.
FactoryGirl.reload
end
WebMock.allow_net_connect!
I would greatly appreciate any assistance!
update
I have determined that the results are inconsistent between test runs. At first I thought it was the same each time, but the number of tests failing varies each run. I thought I had fixed the problem when all the tests passed except one, but the next run it was back to over a hundred failures.
update 2
Upon closer inspection (oops) I have discovered there were some tests failing with a different error the the majority. Instead of ECONNREFUSED, they are reporting 'End Of File Reached'. When I disabled these tests (5 in total) the remaining tests pass reliably. I suspect that xvfb is encountering some javascript or ajax related error, which is then preventing any of the following tests from passing. We are now working on diagnosing what is happening in these tests that is causing the failure.
File this one under 'not sure'. We ended up running up a new AWS server and setting it up again from scratch, and this time it worked fine. Some where suspect an issue with the javascript driver, possibly permissions related, but it is not worth our time investigating.

rails 3.1, why my coffee scripts not working?

I am trying to test coffee script in my rails 3.1 project, but it doesn't get executed, or generated as javascript in the application.js
I have a controller Page, here is the pages.js.coffee:
test = (input) ->
alert input
test 'hello'
but that alert never get executed at http://127.0.0.1:3000/pages,
in fact, the application.js is just empty!
here is the GemFile in case it would help:
source 'http://rubygems.org'
gem 'rails', '3.1.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
gem 'devise', '1.4.8'
gem 'will_paginate', '~> 3.0.2'
#gem 'nested_form', :git => 'git://github.com/fxposter/nested_form.git'
gem "simple_form"
gem 'kaminari'
gem 'foreigner'
gem "cocoon"
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', " ~> 3.1.0"
gem 'coffee-rails', "~> 3.1.0"
gem 'uglifier'
end
gem "jquery-rails"
gem 'coffee-script'
gem 'haml'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
group :test do
# Pretty printed test output
gem 'turn', :require => false
end
EDIT
I have added those gems also and installed them, but, the coffee script is not executed
gem 'therubyracer'
gem 'coffee-script-source'
gem 'json'
gem 'execjs'
First: Having "coffee-rails" in your :assets group should be enough. Older versions had "coffee-script" in the :assets group.
Second: Where are you keeping your *.js.coffee files? You need to make sure they are in an "assets" folder some place. Start with app/assets/javascripts. If they aren't in your assets folder, they won't make it into the asset pipeline.
If you are doing that already, consider starting a new project and use the default configuration. It should work without any trouble. Compare the working project against the non-working project.
If all else fails, is it possible for you to share your project with us on GitHub?
Please check your config/application.rb, you should find a section like this:
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
#Bundler.require(:default, :assets, Rails.env)
end
I suspect you are not precompiling assets on deploy, even though you have it set to ignore the assets gems.

ZenTest not working with RSpec 2.7 and Rails 3.1.0

I've basically followed this RSpec guide on my simple Rails app, but when I run autotest I get
$ autotest
(Not running features. To run features in autotest, set AUTOFEATURE=true.)
loading autotest/rails_rspec2
bundle exec /Users/darth/.rvm/rubies/ruby-1.9.2-p290/bin/ruby -S /Users/darth/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.7.0/bin/rspec --tty '/Users/darth/projects/checkin/spec/models/player_spec.rb'
/Users/darth/.rvm/rubies/ruby-1.9.2-p290/bin/ruby: No such file or directory -- /Users/darth/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.7.0/bin/rspec (LoadError)
When I look at the gem directory, it looks like there's exe folder instead of bin
$ ls ~/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.7.0/
README.md exe features lib spec
$ ls ~/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.7.0/exe/
autospec rspec
And this is my Gemfile
group :test, :development do
gem 'rspec-rails'
end
group :test do
# Pretty printed test output
gem 'turn', :require => false
gem 'rspec'
gem 'cucumber-rails'
gem 'database_cleaner'
gem 'webrat'
gem 'capybara'
gem 'ZenTest', '~> 4.4.2'
gem 'autotest-rails', '~> 4.1.0'
end
See https://github.com/rspec/rspec-core/issues/478

Why 'bundle install' fails in Rails generator?

I'm trying to create custom generator for Rails 3.1. And I wrote this:
module SomeGem
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
desc "This adds devise"
def install
gem "devise"
run "bundle install"
end
end
end
end
But when I run this generator (rails g somegem:install, in the fresh-generated rails app) I've get this error:
gemfile devise
run bundle install
Could not find gem 'devise (>= 0)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
My generator adds Devise into Gemfile properly, but it fails when run 'bundle install' command from generator. When I run 'bundle install' from console it installs all gems without any errors.
Why is this happening?
Here is my Gemfile after I run 'rails g somegem:install' (I removed comments from listing):
source 'http://rubygems.org'
source 'http://gemcutter.org'
source "http://gems.github.com"
gem 'rails', '3.1.0'
gem 'mysql2'
group :assets do
gem 'sass-rails', " ~> 3.1.0"
gem 'coffee-rails', "~> 3.1.0"
gem 'uglifier'
end
gem 'jquery-rails'
group :test do
# Pretty printed test output
gem 'turn', :require => false
end
gem 'therubyracer'
gem "devise"
As pointed here, this is a bug in Bundler.
To make it work:
module SomeGem
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
desc "This adds devise"
def install
gem "devise"
Bundler.with_clean_env do
run "bundle install"
end
end
end
end
end

Resources