Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test a ruby gem binary

I am developing a ruby gem which will have a binary.

I am trying to develop the binary but i am worried its not finding my requires because the gem isnt installed as a gem is there a way to test the binary without packaging it as a gem?

#!/usr/bin/env ruby

require "middleman_ember_scaffold/load_paths"

# Start the CLI
MiddlemanEmberScaffold::Cli::Base.start

sits in a file named mse and ive added my bin folder of gem to path

.
└── middleman_ember_scaffold
    ├── Gemfile
    ├── LICENSE.txt
    ├── README.md
    ├── Rakefile
    ├── bin
    │   └── mes
    ├── lib
    │   ├── middleman_ember_scaffold
    │   │   ├── cli.rb
    │   │   ├── load_paths.rb
    │   │   └── version.rb
    │   └── middleman_ember_scaffold.rb
    └── middleman_ember_scaffold.gemspec

4 directories, 10 files

when i run mes i get

/Users/justin/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- middleman_ember_scaffold/load_paths (LoadError)
    from /Users/justin/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/justin/middleman-generator/middleman_ember_scaffold/bin/mes:7:in `<main>'

i'd like to be able to run and develop mes without re-packaging everytime i make a change.

like image 284
j_mcnally Avatar asked May 01 '13 23:05

j_mcnally


2 Answers

Probably a "better" way would be to do the following:

$ ruby -I./lib bin/mes

It does the same as changing your load path, but it only does it for the command you're executing.

like image 161
Matt Aimonetti Avatar answered Nov 12 '22 13:11

Matt Aimonetti


Use RUBYLIB Environment Variable

The problem you're facing is that your source directory isn't getting some of magic applied to installed gems, and therefore doesn't have your lib directory in the $LOAD_PATH. While there are other ways to deal with this, for testing I'd recommend just adding your lib directory to the RUBYLIB environment variable. For example:

RUBYLIB="/path/to/middleman_ember_scaffold/lib:$RUBYLIB"
export RUBYLIB
bin/mes

should work for any Bourne-compatible shell. If you're running Bash, and don't have anything else stored in RUBYLIB, you might even be able to shorten the invocation to:

RUBYLIB="/path/to/middleman_ember_scaffold/lib" bin/mes

Either way, once Ruby knows what directories it should add to the $LOAD_PATH everything should work just fine.

like image 39
Todd A. Jacobs Avatar answered Nov 12 '22 14:11

Todd A. Jacobs