I have a ruby on rails api_app and and a test_app that exercises the api_app. While development mode I want to run, from the api_app, a rake task called match:reset that is in the test_app. I am trying to do this with a ruby file that is in the api_app
#!/usr/bin/env ruby
require 'tty-command'
cmd = TTY::Command.new
Dir.chdir('../test_app') do
cmd.run 'run rake match:reset'
end
When I do this I get the following error
Your Ruby version is 2.4.4, but your Gemfile specified 2.4.1 (Bundler::RubyVersionMismatch)
I have tried adding cmd.run 'rvm use 2.4.1' but this does not work. How do set the correct environment so this does work?
I could not get this working with a ruby command file. I think the reason for this is that the file is run with ruby from the api_ppp directory so you cannot change to another ruby. What I ended up doing is using a bash file:
#!/bin/bash
cd ../test_app
source $HOME/.rvm/scripts/rvm
rvm use 2.4.4#test_app_2.4.4
BUNDLE_GEMFILE=../test_lab/Gemfile bundle exec rake -f ../test_app/Rakefile match:reset
I am happy to consider an answer that uses a ruby command file, as long as the answer has been tested.
This solution is not tested, based on the documentation I have made changes to the script.
require 'tty-command'
require 'rake'
cmd = TTY::Command.new
cmd.run("rvm use 2.4.1")
Dir.chdir("../test_app") do
cmd.run(:rake, 'match:reset', env: {rails_env: :development})
end
Related
Any rails command doesn't work for me. I have several versions of ruby installed through rvm. I tried installing rails with all the versions, they do install successfully but with all of them I face the following error whenever I run any rails command in my project directory:
~ rails new blog
Traceback (most recent call last):
1: from bin/rails:3:in `<main>'
bin/rails:3:in `require_relative': cannot load such file -- /Users/Am33d/Documents/config/boot (LoadError)
I tried looking up for the error but didn't find any solutions.
What can be done to fix this? I am using macOS Mojave (10.14.6)
This error would indicate that you do not have a boot.rb file in your config directory for some reason. When running rails commands -- regardless of if you run them as bin/rails [command] or bundle exec rails [command], runs the bin/rails file. This file typically has a line require_relative '../config/boot. The boilerplate bin/rails file in a new Rails 6 app is:
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
To simply fix this you can create a blank file by running touch config/boot.rb from the root directory of your application and that would itself suppress the error. Having said that, you'd be better off creating a config/boot.rb file that contains useful information. The boilerplate config/boot.rb file is this
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
Without this file you are not necessarily appropriately loading the gems from your gemfile or caching operations with bootsnap.
When I ran into this problem, I also received the same error when trying to use rails -s.
If the same is happening for you, its because your version of ruby isn't compatible with your rails version.
After upgrading to the latest rails version the error stopped and everything worked well.
A little bit weird.
rails new blog should not need to find boot file, neither need bundler. Actually, you are trying to create a new project, so it is expected there is not Gemfile, boot, or bundler created for your project.
So I would advise you to find what rails command is being executed. Sometimes one rails executable created by bundler is taking precedence in the $PATH environment variable.
Ex: some incorrect bin/rails executable is on your $HOME directory.
i was trying to build an app using 'rails server' and this error was showing. You have to make sure that the rails version matches the ruby version and even if you do what an answer above said (create the config/boot.rb paste) doesnt work, than you have to change the version of rails to the stable. I'll let the link of this problem here, there's an issue closed in github for this problem https://github.com/rails/rails/pull/43951
to solve the problem you have to replace the gem rails line on the gemfile to this:
gem "rails", github: "rails/rails", branch: "7-0-stable"
Sorry about my english.
If I run a script from rails and that script requires a gem that is not found in Gemfile it does not work, however the script executed has nothing to do with Rails should behave the same as if you executed anything else like an ls for example
example: /tmp/fichero.rb
#! /usr/bin/env ruby
#encoding: utf-8
require "rubygems"
require 'choice'
require 'fileutils'
...
...
Gemfile
...
...
#gem 'choice'
...
...
controller:
stmt = "ruby /tmp/fichero.rb -p hola"
stdout, stderr, status = Open3.capture3(stmt)
or
stmt = "ruby /tmp/fichero.rb -p hola"
%x[#{stmt}]
both:
`require': cannot load such file -- choice (LoadError)
if I change Gemfile:
...
...
gem 'choice'
...
...
It works but I don't want this gem in my Gemfile
Thanks in advance
bundle exec - Shelling-out
Any Ruby code that opens a subshell (like system, backticks, or %x{}) will automatically use the current Bundler environment. If you need to shell out to a Ruby command that is not part of your current bundle, use the with_clean_env method with a block. Any subshells created inside the block will be given the environment present before Bundler was activated. For example, Homebrew commands run Ruby, but don't work inside a bundle:
Bundler.with_clean_env do
`brew install wget`
end
I am trying to run rspec for Ruby on Rails. I am running Rails 4.1.1. I have installed the gem, have established a spec folder with some tests. I have created a directory through $ rails g rspec:install
I tried to create a testing database through $ rake db:test:prepare but it throws this error message:
WARNING: db:test:prepare is deprecated. The Rails test helper now maintains your test
schema automatically, see the release notes for details.
So I ended up looking at this stack overflow post, and of the two options, the one that worked was:
rake db:schema:load RAILS_ENV=test
So, now I need to run rspec.
When I run $ rspec spec from the command line I get this error:
/Users/myname/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/
kernel_require.rb:55:in `require': cannot load such file -- rails_helper (LoadError)
How do I resolve this so that I can start running tests?
There is some problem with rspec V3. But in your case you are using V2.
change
require 'rails_helper'
to
require 'spec_helper'
Other description find here https://teamtreehouse.com/forum/problem-with-rspec
For V3 :
If someone using rspec V3 then similar error occurs when generator not run. So before trying anything run generator.
rails generate rspec:install
If you are getting a huge list of warning on your console. Then you need to remove --warnings from .rspec file.
I actually just had this error on rails 4 with rspec 3, but in my case I forgot to run the generator:
rails generate rspec:install
Also I had to remove warnings from .rspec, as stated by one of rpsec developers
For me, the problem was due to a different "rspec-support" version loaded and activated. I solved by prepending a "bundle exec":
bundle exec rspec
Always remember to run the
rspec or the bundle exec rspec from the root of your project.
Something like:
bundle exec rspec spec/features/file_test_spec.rb
For newer versions (3.5), if you need to run the generator, rails generate rspec:install doesn't work for me. I used:
rspec --init
Creates .rspec and spec/spec_helper.rb.
EDIT:
Just in case, I found out that I installed rspec gem for ruby, not the one for Rails, so rspec:install wasn't available. Should use https://github.com/rspec/rspec-rails.
Sometimes you need to run rspec command from Project's parent directory
Please install the following gem:
gem install 'rspec-rails'
And then run:
rails generate rspec:install
This will generate the spec folder and rails_helper.rb and spec_helper.rb. Your error should be taken care once this is done.
None of these suggestions worked for me on Rails 4.2.0 with rspec-rails 3.2.3.
Instead, I placed the following two lines at the top of my spec file:
require 'rails_helper'
require 'spec_helper'
Now I can execute rspec spec/features/my_spec.rb.
I had the same error but there was no reference to rails_helper anywhere and we weren't even using rails.
However, I eventually found that my ~/.rspec was requiring rails_helper (presumably from creating rails app in the past) which was causing the issue ...
$ cat ~/.rspec
--color
--format documentation
--require spec_helper
--require rails_helper
The fix is as simple as removing that line. Hope this helps someone else grappling with the same issue!
One possibility that worked for me is to make sure you're requiring spec_helper. RubyMine can default require rspec and this can sometimes lead to this error.
You may also have your file named incorrectly. Make sure it ends in _spec.rb. I had misspelled my file to end in _soec.rb and was scratching my head for a while...
I got the error you described when running a controller test. However, when I ran a model test, I got the true error. it ended up saying that the error was occuring because my database wasn't migrated. I fixed the error by running this command...
rake db:test:prepare
For someone. If you got that error when you trynna test a specific file. you might can like this
$ bundle exec rspec ./spec/features/user_login_spec.rb
$ bundle exec rspec ./spec/models/user_login_spec.rb
I have a ruby script I wrote which generates data and loads it to MongoDB. I am now trying to call this load script from seed.rb of my Rails app (so I can run via rake db:seed)
I attempted to call it from rails using this code:
system( "ruby data_load/db_load.rb -a data_load/doc1.json" )
When that code executes, I get the following error. (Note it runs fine from the command line):
data_load/db_load.rb:15:in `require': cannot load such file -- mongo (LoadError)
from data_load/db_load.rb:15:in `<main>'
The top of db_load.rb looks like this:
# includes gems from gemfile
require 'rubygems'
require 'bundler/setup'
Bundler.setup
require 'mongo'
require_relative 'load_scripts/cmd_options'
require_relative 'load_scripts/build_index'
....
include Mongo
The script has it's own gemfile in the data_load directory.
My guess is ruby is running the script using the bundle for the rails application instead of the shell script.
Any suggestions on how I can execute this script?
I believe the problem is where Bundler is looking for the Gemfile. Since your script is being run in the parent directly it is finding the Gemfile for the main app.
Set the BUNDLE_GEMFILE before calling your script:
system "BUNDLE_GEMFILE=data_load/Gemfile ruby data_load/db_load.rb -a data_load/doc1.json"
I'm sorry but I think you can't do it, unless you run the script as a different process (like a shell command), doing it is easy:
`shell_command params`
Just use the correct path and params.
Otherwise, consider that a gemfile is "more or less" at its basic level, a bunch of require (or load) statements, so loading a different gemfile would overwrite the original one, creating a lot of issue with rails after that.
The subprocess command is a good idea, however you can only pass string as params, not in-memory objects.
Rails's script/server is just a few lines:
#!/usr/bin/env ruby
require File.expand_path('../../config/boot', __FILE__)
require 'commands/server'
I wonder where the server file is? I tried a
find . -name 'server.*'
find . -name 'server'
but can't find it
You have to look for the files in your related gem repo. Your Ruby is located at
which ruby
Your Rails gem is
bundle show rails
You can go to the dir with
cd `bundle show rails`/lib/commands
Then, open server.rb
thanks. in the case of a Mac, I found that they are on
/Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/commands/server.rb
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb
/Users/peter/.gem/ruby/1.8/gems/rails-2.3.5/lib/commands/server.rb
the following are there but doesn't have my Rails 2.3.5 version:
/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/commands/server.rb
/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/commands/server.rb
Update: Now the question is: which of the above 3 is the one? How can you tell easily?
In linux they are more likely to be under
/usr/lib/ruby/gems/ruby_version/gems/rails_version/lib/commands
ruby_version and rails_version depends on which versions you are using. The directory structure will also depend on whether or not you are using rvm, but hopefully you can find your way once you get to the /usr/lib/ruby/ directory