Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds '--choose-env' flag to use rbenv if available, otherwise rvm
- Loading branch information
Showing
7 changed files
with
132 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.bundle | ||
Gemfile.lock | ||
pkg/* | ||
/vendor/bundle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module Subcontractor | ||
class Command | ||
def self.build(parts, options) | ||
new(parts.dup, options).build | ||
end | ||
|
||
def initialize(parts, options) | ||
@parts = parts | ||
@options = options | ||
end | ||
|
||
def build | ||
if _use_command?(:rbenv) | ||
@parts.unshift("#{_set_rbenv_version} rbenv exec") | ||
elsif _use_command?(:rvm) | ||
@parts.unshift("rvm #{_env_specifier(:rvm)} exec") | ||
end | ||
|
||
@parts.join(" ") | ||
end | ||
|
||
def _use_command?(command) | ||
@options.has_key?(command) || _choose_env_and_command_present?(command) | ||
end | ||
|
||
def _choose_env_and_command_present?(command) | ||
@options.has_key?(:choose_env) && system("which #{command} > /dev/null 2>&1") | ||
end | ||
|
||
def _set_rbenv_version | ||
env_specifier = _env_specifier(:rbenv) | ||
env_specifier = "`rbenv local`" if env_specifier == "." | ||
"env RBENV_VERSION=#{env_specifier}" | ||
end | ||
|
||
def _env_specifier(command) | ||
@options[command] || @options[:choose_env] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "subcontractor" | ||
require "subcontractor/cli" | ||
|
||
RSpec.configure do |config| | ||
config.color = true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require "spec_helper" | ||
|
||
describe Subcontractor::CLI do | ||
before(:each) do | ||
Object.instance_eval{ remove_const(:ARGV) } | ||
end | ||
|
||
describe "#run" do | ||
it "uses rvm with --rvm" do | ||
ARGV = ["--rvm", ".", "test"] | ||
SafePty.should_receive(:spawn).with("rvm . exec test") | ||
Subcontractor::CLI.new.run | ||
end | ||
|
||
context "with --rbenv" do | ||
it "specifies a rbenv" do | ||
ARGV = ["--rbenv", "1.9.3", "test"] | ||
SafePty.should_receive(:spawn).with("env RBENV_VERSION=1.9.3 rbenv exec test") | ||
Subcontractor::CLI.new.run | ||
end | ||
|
||
it "uses 'rbenv local' if a '.' is given as the rbenv version" do | ||
ARGV = ["--rbenv", ".", "test"] | ||
SafePty.should_receive(:spawn).with("env RBENV_VERSION=`rbenv local` rbenv exec test") | ||
Subcontractor::CLI.new.run | ||
end | ||
end | ||
|
||
it "creates a valid command if no environment manager is specifed" do | ||
ARGV = ["test"] | ||
SafePty.should_receive(:spawn).with("test") | ||
Subcontractor::CLI.new.run | ||
end | ||
|
||
context "with --choose-env" do | ||
it "uses rbenv when rbenv is present" do | ||
ARGV = ["--choose-env", "1.9.3", "test"] | ||
SafePty.should_receive(:spawn).with("env RBENV_VERSION=1.9.3 rbenv exec test") | ||
command = Subcontractor::Command.any_instance | ||
command.should_receive(:system).with("which rbenv > /dev/null 2>&1").and_return(true) | ||
Subcontractor::CLI.new.run | ||
end | ||
|
||
it "uses rvm when rvm is present and rbenv isn't" do | ||
ARGV = ["--choose-env", ".", "test"] | ||
SafePty.should_receive(:spawn).with("rvm . exec test") | ||
command = Subcontractor::Command.any_instance | ||
command.should_receive(:system).with("which rbenv > /dev/null 2>&1").and_return(false) | ||
command.should_receive(:system).with("which rvm > /dev/null 2>&1").and_return(true) | ||
Subcontractor::CLI.new.run | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters