Executing chef test from a rakefile - ruby

I have some chef unit tests that I can run via the command : chef exec rspec
However, I am trying to run these from TeamCity and I am using a rakefile. When I try to execute the rakefile the require 'chefspec' line in the test file causes an error "cannot load such file -- chefspec (LoadError)"
I know chefspec is installed. I am new to Ruby, chefspec, rspec. I know there is mention of a gemspec file and I have tried to create one in the directory where I can run the command chef exec rspec to execute the test. However, when I try to run bundle install I get the error Could not locate Gemfile or .bundle/ directory. Is there a default location of a gemfile when you install ruby? I am pretty lost at this point.
C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire': cannot load such file -- chefspec (LoadErr
or)
from C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in require'
from D:/../spec/octopus_tentacle_spec.rb:1:in'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in load'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:inblock in load_
spec_files'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in each'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:inload_spec_file
s'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in setup'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:inrun'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in run'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:ininvoke'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/exe/rspec:4:in <top (required)>'
from C:/Ruby22/bin/rspec:23:inload'
from C:/Ruby22/bin/rspec:23:in <main>'

The gemspec files are normally used when you are creating a Ruby gem, and contains the metadata about the Gem you are creating.
when you use bundle install it requires a Gemfile in the directory. The Gemfile works as a manifest file for the gems required for the project in the directory. It is possible that chef exec is looking for this file and will only work with gems specified here.
Try creating a Gemfile with the content
source 'https://rubygems.org'
gem 'rspec'
gem 'chefspec'
you should list other gems you are using in the file.
and then run bundle install

Related

bundler exec "LoadError: cannot load such file"

Source code of the project for anyone interested: https://github.com/clockworkpc/verbalyser
Ruby version: ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
From the root directory of this project ("verbalyser") I am trying to run the simple command:
bundle exec exe/verbalyser
But I keep on getting this error message:
bundler: failed to load command: exe/verbalyser (exe/verbalyser)
LoadError: cannot load such file -- verbalyser
exe/verbalyser:2:in `require'
exe/verbalyser:2:in `<top (required)>'
Here are the contents of the file verbalyser/exe/verbalyser, which has been made executable (chmod +x):
#!/usr/bin/env ruby
require "verbalyser"
Here are the contents of the Ruby file that this executable is trying to load:
require "verbalyser/version"
require "verbalyser/conjugation_scraper"
require "verbalyser/cooljugator_scraper"
require "verbalyser/endings_grouper"
require "verbalyser/lemma_matcher"
require "verbalyser/verb_shortlister"
Here is my directory structure:
I had a problem like this. I found the answer to my specific problem somewhere on stack overflow but I can't find the link so I'll repeat what I remember of it.
Basically, the file was recently created and did not exist in bundler's own internal git repository, so I had to add it, and once I did that it worked. I didn't even have to commit the file - just stage it.

How to use Gemfile in iron.io worker

I'd like to use gems specified from Gemfile that I uploaded. However, I keep getting
/usr/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- capybara (LoadError) from /usr/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' from app.rb:3:in `<main>'
Seems like the code cannot find the required gems. This is my .worker file:
runtime "ruby"
stack "ruby-2.1"
exec "app.rb"
gemfile "Gemfile"
full_remote_build true
My Gemfile:
source "https://rubygems.org"
gem "capybara"
gem "capybara-webkit"
I uploaded my work using this command:
zip -r my_work.zip . ; iron worker upload --zip my_work.zip --name my_work iron/ruby ruby app.rb ; iron worker queue --wait my_work
Thanks in advance.
There's a couple things here:
1) you are using full_remote_build with a .worker file, but then uploading the zip. Those two things don't go together, it's one or the other.
2) Be sure you are vendoring the gems so they are included when you zip them. To vendor: docker run --rm -v "$PWD":/worker -w /worker iron/ruby:dev bundle install --standalone --clean. Then at the top of your script, add: require_relative 'bundle/bundler/setup'.
See the documentation here for more details: https://github.com/iron-io/dockerworker/tree/master/ruby

Gmail gem error when run inside Rspec - 'cannot load such file -- gmail/client'

I'm using this gem called gmail and it works perfectly when run in just a plain ruby script. However, when used inside Rspec, I keep getting this error.
Failure/Error: #gmailnew = Gmail.new()
LoadError:
cannot load such file -- gmail/client
# /Library/Ruby/Gems/2.0.0/gems/gmail-0.6.0/lib/gmail.rb:50:in new'
# ./temp.rb:7:inblock (2 levels) in '
Any idea what causes this?
- I've already tried reinstalling it since it seems to be not finding a file.
I could not figure out what causes the conflict, similar problems by others.
Why is autoload failing to load files for gems
Ruby autoload conflicts between gmail and parse_resource gems
I was able to resolve this by using Bundler.
1) Added gem inside the Gemfile (gem 'gmail')
2) $ bundle install - this installs all gems inside the Gemfile, and avoids the conflicts
3) $ bundle exec rspec my_spec_file.rb - executes Rspec

Using rspec with a local gem

I am using a local gem (on my machine) with another application that is a command line app.
I have something like this in the gemfile to refer to the local gem:
gem 'mygem', :path => '/Users/devmachine/Projects/mygem'
When I run bundle console I am able to use the gem and all is well. However, whenever I run my test suite (rspec) I get the following message:
ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mygem (LoadError)
I'm confused. Any ideas?
You need to run:
bundle exec rspec
If you are not using the bundle evironment rspec won't know what you put in your Gemfile and just use the Gems it finds installed.

Can't load a gem I created

I'm trying to build my first gem. Using Ryan Biggs' tutorial as my guide, I did the following:
1) Created the gem scaffolding:
$ bundle gem hello_world
2) Edited the lib/hello_world.rb file:
require "hello_world/version"
module HelloWorld
def hi
"Hello world!"
end
end
3) Installed the gem via bundler:
$ cd hello_world
$ bundle install
At this point, if I run
$ bundle show hello_world
it shows
/Users/ykessler/gems/hello_world
so it looks like it installed.
But when I try to require the gem from irb:
require '/Users/ykessler/gems/hello_world'
it can't load it:
2.0.0-p195 :003 > require '/Users/ykessler/gems/hello_world'
LoadError: cannot load such file -- /Users/ykessler/gems/hello_world
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from (irb):3
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/bin/irb:16:in `<main>'
Where am I going wrong?
You need to run gem build hello_world.gemspec
Then to install it, you run gem install hello_world from the root of your gem project. That will install your local gem using the .gem file that we just created in your directory (not the gem from rubygems.org if it exists).
Now, if you run gem list, you should see it. You should now be able to require your gem and and access your library from other ruby code. All you have to write is require 'hello_world'. There is no need to type the full path. In fact, that's a bad idea.
This is all explained pretty clearly in the rubygems.org documentation (http://guides.rubygems.org/make-your-own-gem/). It's very clear, helpeful, and it's where I learned how to make my first gem.

Resources