rspec error (LoadError) - ruby

I am running the rspec command
bundle exec rspec spec/requests
resulting in the error:
ms$ bundle exec rspec spec/requests/ms/.rvm/gems/ruby-1.9.3-p286/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load': cannot load such file -- /Users/ms/Dropbox/Ruby/Code/<appname>/spec/requests/spec/requests (LoadError)
for simplicity i replaced the name of my app with <appname>. I can see that the path below <appame> is duplicated spec/request/spec/requests
I don't know what I did (nothing?) to make this start automatically this wrong path.
When i test the models it's like this:
cannot load such file -- /Users/i814509/Dropbox/Ruby/Code/<appname>/spec/requests/spec/models (LoadError)
again automatically looking for the spec/requests directory before the one that I'm trying to specificy

You specified spec/requests but it seems to already be the default:
just run bundle exec rspec
Also, if you configured your environment, or don't use rvm/pik, you can just run rspec

Related

Ruby: run rake task to execute ruby scripts

I have a bunch of Ruby scripts and I'd like to start them with a Rake task.
A simplified version to illustrate my issue:
export_stats.rake:
desc 'Export statistics'
task :export_stats do
puts "executing: export_stats.rb #{START_MONTH} #{END_MONTH} #{OUTPUT} #{ENVIRONMENT}"
ruby "export_stats.rb #{START_MONTH} #{END_MONTH} #{OUTPUT} #{ENVIRONMENT}"
end
rake aborted! elk-stack/export_stats.rake Don't know how to build task
'export_stats'
the export_stats.rb file is in same directory with export_stats.rake
the rake gem is installed and if I run
rake export_stats
I get an error:
rake aborted!
Don't know how to build task 'export_stats'
What am I missing?
If I understand you correctly you have a folder with some ruby scripts and you are trying to run a rake task that is located in the same folder. I assume you are not using any application framework like Rails (because you did tag the question only with "Ruby").
Do you have a Rakefile in same directory? If so does it contain a statement to load the specific files to run?
# Rakefile
#!/usr/bin/env rake
load 'export_stats.rake'
You have a typo.
The code says export_stats, and the error says exports_stats.
There's an extra s.
Read the error message carefully! ;)

Rspec a file which requires others. `require': cannot load such file

Is it possible to run an rspec on a file which requires other files?
my .rb file has the following lines:
require "colorize"
require "./board_initializer"
require "./pieces"
and when running a rake I get the following error:
.rvm/rubies/ruby-2.2.5/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- ./board_initializer (LoadError)
Thanks!
The error means board_initializer.rb isn't in the current working directory for __FILE__. Some ways to resolve this include:
Providing a valid filename argument to require.
Using require_relative with a valid relative path.
Modifying the current LOAD_PATH.
There are certainly other ways to resolve this, but they all amount to ensuring that board_initializer.rb can be found by the interpreter when you load or require the file.

Ruby: 'bundle exec' throws error for all shell commands

In my project, I have a Gem that contains a shell script in its bin folder, let's call the script do_something.sh.
do_something.sh actually executes a ruby script using Jruby command, the script is called ruby_script.rb.
I am trying to call do_something.sh from my project using:
bundle exec do_something.sh
it keeps throwing errors for all shell commands in the script. I erased all the contents of the script and added only one line "echo 'Hello'" and it is still throwing the following error
NoMethodError: undefined method echo' for main:Object
/usr/local/rvm/gems/ruby-2.1.1#projectName/gems/gemName/bin/do_something.sh:10:in'
/usr/local/rvm/gems/ruby-2.1.1#projectName/bin/do_something.sh:23:in load'
/usr/local/rvm/gems/ruby-2.1.1#projectName/bin/do_something.sh:23:in'
Edit#1
All the files has execute permission.
I also tried to put ruby_script.rb in the bin directory where are files are executables (according to a rule in the gemspec file) and tried calling
bundle exec ruby_script.rb
I get the error "bundler: command not found: ruby_script.rb"
bundle exec ruby ruby_script.rb
I get the error "ruby: No such file or directory -- ruby_script.rb (LoadError)"
Why am I getting this error and how can I solve it? I want to be able to either run do_something.sh or ruby_script.rb. Right now, ruby_script.rb is not recognised and do_something.sh does not recognise the commands.
Ok, finally after two days, I was able to make it work.
bundle install installs my GEM and creates a ruby wrapper by default to sh files in the 'bin' directory. So, when I call my script using bundle exec the ruby wrapper gets called first and it calls my script using a ruby 'load' function. This load function expects a ruby file and that is why all my shell script in the file threw errors, because they were expected to be in ruby.
A workaround to this is installing my gem before 'bundle install' using the following command:
gem install --no-wrapper my-gem
Apparently this command disabled creating the ruby wrapper and I was finally able to call the script using bundle execute do_something.sh
Try specifying sh when you run bundle exec. The NoMethodError is from the script being executed in ruby and not sh.
bundle exec sh do_something.sh
In order to make it work. Setting_up_permissions_on_a_script
First: you have to change the mode for the file using chomd +x file_path
Second: Now you can call either by bundle exec file_path or ./file_path
N.B
1: Don't forget to add #!/bin/sh on top of the file.
2: And do check with deploy-a-shell-script-with-ruby-gem-and-install-in-bin-directory will definitely help you.
Hope this help you!
According to Rubygems Specification Reference, executables included in the gem
... must be executable Ruby files. Files that use bash or other interpreters will not work.
In order to to add an executable to a gem please make sure to read the Adding an executable section on Rubygems Guide.
Eventually you'll be able to run the executable like this:
bundle exec do_something
where bundle exec part is optional.

How do I run Rails/Rake from another directory?

Without cd-ing into the root directory of my Rails application, how can I execute a Rails or Rake command for that application.
I tried:
bundle exec rake my_tasks:do_stuff BUNDLE_GEMFILE=/PATH/TO/RAILS_APP/Gemfile
among other combinations, to no avail.
[Update]
The issue is actually two-fold, bundle doesn't know where the gemfile is and rake doesn't know what to run.
To use Bundler:
BUNDLE_GEMFILE=/PATH/TO/RAILS_APP/Gemfile bundle exec ...
Note that BUNDLE_GEMFILE has to go before 'bundle exec'.
To use Rake:
rake -f /PATH/TO/RAILS_APP/Rakefile my_task:do_stuff
To use Rails console:
????
Have yet to figure out how to enter the Rails console from another directory. Looking at the source, I think it may not be possible, because it eventually does File.join('script','rails') to kick off the rails process.
Without you showing the error message you are getting, I'm assuming it has less to do with Bundler than it does Rake. When the rake command is run, it searches for a Rakefile starting in the current directory and traversing up the tree until it finds one. You can override this behavior by explicitly specifying a Rakefile in the options to the rake command. This is done using the -f <RAKEFILE> option.
eg.
bundle exec rake -f /PATH/TO/RAILS_APP/Rakefile my_task:do_stuff
Bear in mind that your Rake tasks have to be "CWD agnostic". Most tasks and scripts are as they tend to get the project directory based on a path relative to a known file in the directory tree. You probably already understand that, but it's worth mentioning in case the tasks are expecting the current working directory to actually be the rails root. That would be a case where running them from outside the project could potentially be dangerous.

Difficulty installing RSpec on Windows

I'm trying to get started with RSpec. I already had Ruby 1.8.7 installed on my Windows 7 machine.
So I typed gem install rspec and that seemed to work. But if I type spec in the command line, the command is not found. My path currently includes the bin folder in my RUBY_HOME.
If I look into the C:\Users\Eric\.gem\specs\rubygems.org%80\quick\Marshal.4.8 directory, I do see four RSpec files such as rspec-core-2.5.0.gemspec. Nevertheless, the spec command fails even in this directory.
What needs to be done to install RSpec correctly? It would seem like a path issue, but I have been unable to find a directory where the spec command works, so I can't figure what to add to my path.
On Windows, try this bundle exec rspec spec
There's no spec command in RSpec 2. Try rake spec or rspec spec.

Resources