I'm working on my 'gravaty gem (https://rubygems.org/gems/gravaty) and I have some configuration issues working on command line and RubyMine IDE at the same time.
I'm using JetBrains' RubyMine IDE and sometimes also command like through my Rakefile. If, from project main dir, I wrote rake test via command line, all tests work properly (they run and provide results, one test is not passing but that's another story...) while, if I run them from the IDE (with a Run/Debug configuration) I have this message:
1. B:/Programmi/gravaty/test/gravaty/locales/test_locales.rb:1
Fail to load: B:/Programmi/gravaty/test/gravaty/parsers/test_avatar.rb:1
Exception message: cannot load such file -- gravaty/test_parsable_duck_type
["B:/Programmi/gravaty/test/test_helper.rb:34:in `require'", "B:/Programmi/gravaty/test/test_helper.rb:34:in `<top (required)>'", "B:/Programmi/gravaty/test/gravaty/parsers/test_avatar.rb:15:in `require_relative'", "B:/Programmi/gravaty/test/gravaty/parsers/test_avatar.rb:15:in `<top (required)>'", "C:/Program Files (x86)/JetBrains/RubyMine 6.0.3/rb/testing/runner/tunit_or_minitest_in_folder_runner.rb:51:in `require'", "C:/Program Files (x86)/JetBrains/RubyMine 6.0.3/rb/testing/runner/tunit_or_minitest_in_folder_runner.rb:51:in `block in require_all_test_scripts'", "C:/Program Files (x86)/JetBrains/RubyMine 6.0.3/rb/testing/runner/tunit_or_minitest_in_folder_runner.rb:44:in `each'", "C:/Program Files (x86)/JetBrains/RubyMine 6.0.3/rb/testing/runner/tunit_or_minitest_in_folder_runner.rb:44:in `require_all_test_scripts'", "C:/Program Files (x86)/JetBrains/RubyMine 6.0.3/rb/testing/runner/tunit_or_minitest_in_folder_runner.rb:138:in `<top (required)>'", "-e:1:in `load'", "-e:1:in `<main>'"]
and so on (one for each test file) with all its stacktrace.
In order to easily configure the correct (?) require environment, I put the statements
lib_dir = File.dirname(__FILE__) '/../lib'
$:.unshift lib_dir unless $:.include?(lib_dir)
at the beginning of my test_helper.rb file and it seems working from the command line, but the IDE does not appreciate. Considering this statements in the test_helper.rb,
require 'minitest/autorun'
require 'minitest/spec'
lib_dir = File.dirname(__FILE__) + '/../lib'
$:.unshift lib_dir unless $:.include?(lib_dir)
require 'gravaty'
require 'gravaty/constants'
require 'gravaty/parsers/avatar'
require 'gravaty/parsers/callback'
require 'gravaty/parsers/default'
require 'gravaty/parsers/force'
require 'gravaty/parsers/format'
require 'gravaty/parsers/pixelsize'
require 'gravaty/parsers/rating'
require 'gravaty/parsers/secure'
require 'gravaty/parsers/type'
require 'gravaty/test_parsable_duck_type'
the IDE underlines all the require 'gravaty/ statements saying Unresolved Ruby reference. I've added my lib directory to the Load Path preference of the IDE, but nothing changes.
Is there a way to make it work in both environments? Should I "carve" the full require path in each file and in each require statement?
I've found the solution: instead of using the Test::Unit/Shoulda/Minitest Run/Debug configuration, I can just run the tests through a simple Rake task Run/Debug configuration.
Related
I have the problem that I want to require a ruby file.
File overview:
ExcelConversion.rb
main.rb
/classes/excelReader.rb
/classes/elementIdentifier.rb
the main.rb has the header:
require classes/excelReader
require classes/elementIdentifier
the excelReader.rb has the header:
require 'rubyXL
the elementIdentifier.rb has the header:
require 'inifile
The problem is that if I execute the main.rb file, everything works fine. But, as soon as I write require main within the ExcelConversion file I get the error:
Error Loading File ExcelConversion.rb
Error: #<LoadError: cannot load such file -- rubyXL>
C:/Program Files (x86)/SketchUp/SketchUp 2018/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:54:in 'require'
C:/Program Files (x86)/SketchUp/SketchUp 2018/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:54:in 'require'
C:/shortenedPath/ExcelConversion/classes/ExcelReader.rb:1:in '<top (required)>'
C:/shortenedPath/ExcelConversion/main.rb:1:in 'require_relative'
C:/shortenedPath/ExcelConversion/main.rb:1:in '<top (required)>'
C:/shortenedPath/ExcelConversion.rb:4:in 'require_relative'
C:/shortenedPath/ExcelConversion.rb:4:in '<top (required)>'
I don't understand how that is even possible to throw an error, since the main.rb runs without problems.
Edit: Solution
I managed to find the solution. SketchUp needs to install the gems separately. This can be achieved through the Ruby Console and the command:
Gem.install "nameOfTheGem"
Then SketchUp stores a copy of the gem within it's own path and one can require it as usual.
getting error on running cmd in windows Please help me what to do: here is rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for
example lib/tasks/capistrano.rake, and they will automatically be available to
Rake.
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
and here is the cmd window
c:\Fedena>rake db:create --trace
(in c:/Fedena)
rake aborted!
c:/Fedena/Rakefile:3: syntax error, unexpected kAND
...lib/tasks/capistrano.rake, and they will automatically be av...
^
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2383:in `load'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2383:in `raw_load_rakefile'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2017:in `load_rakefile'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2016:in `load_rakefile'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2000:in `run'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31
C:/Ruby187/bin/rake:23:in `load'
C:/Ruby187/bin/rake:23
Shouldn't this part be commented out?
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for
#example lib/tasks/capistrano.rake, and they will automatically be available to
#Rake.
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
Looks like it's not commented out and ruby is trying to run it as code
My installation is successful. Below is my rake file content:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
Copy and paste the above content in your rake file and try again
I am trying to get test-first-ruby-master as instructed but I am getting multiple errors. This is the structure of the files/directory in test-first-ruby-master:
Gemfile Gemfile.lock README.md lib spec
lib contains ruby files and spec contains spec files.
I am trying to execute 00_hello_spec.rb file on my terminal but it doesn't work.
What I have tried
require "lib/00_hello.rb"
require_relative "lib/00_hello.rb"
Errors I get
supritkumars-MacBook-Pro:test-first-ruby-master supritkumarshah$ rspec spec/00_hello_spec.rb
/Users/supritkumarshah/Desktop/test-first-ruby-master/spec/00_hello_spec.rb:103:in `require': cannot load such file -- lib/00_hello.rb (LoadError)
from /Users/supritkumarshah/Desktop/test-first-ruby-master/spec/00_hello_spec.rb:103:in `<top (required)>'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `load'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `each'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:97:in `setup'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `load'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `<main>'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Thank you
The require is failing because you're requiring a relative path, 'lib/00_hello.rb', but the directory it's relative to (the root of your project) is not in the list of directories that Ruby is looking in for files to require (the load path, $LOAD_PATH).
There are lots of ways to fix this. Here are some:
Tell rspec to add the root of your project to the load path. Since that's your current directory, you can use .:
rspec -I. spec/00_hello_spec.rb
Change your spec to require_relative '../lib/00_hello'. This works regardless of the current directory or load path — a good idea in command-line programs, although not so important for a learning project.
As B Seven answered, change your spec to require './lib/00_hello'. This requires you to run rspec in the root of your project.
You can even remove the require altogether! When you run rspec, if the current directory has subdirectories named lib and/or spec, rspec adds them to the load path. This also requires you to run rspec in the root of your project. This solution won't work if you need the require when running your program for real (not under rspec).
Note that including the .rb in the filename you required did not cause the problem, but it's not necessary.
Although Dave's answer is correct, I prefer to use:
require './lib/00_hello'
Did you tried adding require 'bundler/setup' at the top of your test helper file?
I'm using Ruby 2.1.5 and whenever I download code that uses require to include a file, I get errors. Changing require to require_relative fixes the problem. For example, if I use the example code from rspec, I get the following error output.
mario#crunchbang:~/projects/rspec_test$ rspec bowling_spec.rb
/home/mario/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- bowling (LoadError)
from /home/mario/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/mario/projects/rspec_test/bowling_spec.rb:2:in `<top (required)>'
from /home/mario/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load'
from /home/mario/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
from /home/mario/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
from /home/mario/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
from /home/mario/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
from /home/mario/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
from /home/mario/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
from /home/mario/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
from /home/mario/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
from /home/mario/.rvm/gems/ruby-2.1.5/bin/rspec:23:in `load'
from /home/mario/.rvm/gems/ruby-2.1.5/bin/rspec:23:in `<main>'
from /home/mario/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `eval'
from /home/mario/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `<main>'
Is there something wrong with my Ruby set up or can require no longer be used the way it was in earlier versions of Ruby?
require works exactly the same as it did before: it searches the $LOAD_PATH. What has changed is the default $LOAD_PATH: the current directory . was removed from it, for various maintenance and security reasons.
In almost all cases, you don't want to load a file relative to the current working directory anyway (after all, the CWD is controlled by the user, so you don't even know what it is, how could you then reliably load a file from there?), you want to load it relative to the current file … which is exactly what require_relative does.
By the way: this change was relased 7 years ago, made before that, and announced even before that, I don't know where you are getting that code from, but I would be highly suspicious of code that hasn't been maintained for such an extended period of time (almost 10 years).
Require only searches for files in Ruby's load path. Prior to 1.9, the current folder (.) was included in the load path. See Kernel#require
if you've been writing code with paths relative to the current directory it's normal that require doesn't work. You can either add . to the load path:
rspec -I . my_script.rb
Or as you have found, require_relative resolves the argument relative to the path to the file it is contained in. People also used to use __FILE__ to achieve this before require_relative was available.
You can also add the -I option to your .rspec file or setup your load paths in your spec_helper.rb
When you use require you need to pass the absolute path for the file.
require_relative is working as it depends on the relative path.
Edit the code part where you require the file to absolute file path and it should work well.
Here is my project structure:
/app
--/lib
----/porter.rb
--/spec
----/porter_spec.rb
In file porter_spec.rb i have include directive:
require '../lib/porter'
Now I'm trying to run the test:
cd app
rspec
and get the error:
C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- ../lib/porter (LoadError)
from C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from C:/Dropbox/development/myprojects/lj-parser/spec/porter_spec.rb:3:in `<top (required)>'
How can I require file on lib folder?
Apparently, your current dir is not what you think it is.
You should use require_relative (ruby 1.9+):
require_relative '../lib/porter'
In ruby1.9, you can use:
require_relative '../lib/porter'
In ruby1.8 or higher, you can use:
require File.expand_path("../lib/porter", __FILE__)