ruby-virtualenv 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,97 @@
1
+ # This file must be used with "source bin/activate" *from bash*
2
+ # you cannot run it directly
3
+
4
+ forget_cached_commands () {
5
+
6
+ # This should detect bash and zsh, which have a hash command that must
7
+ # be called to get it to forget past commands. Without forgetting
8
+ # past commands the $PATH changes we made may not be respected
9
+ if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
10
+ hash -r
11
+ fi
12
+
13
+ }
14
+
15
+ deactivate () {
16
+
17
+ if [ -n "$_OLD_SANDBOX_GEM_HOME" ] ; then
18
+ GEM_HOME="$_OLD_SANDBOX_GEM_HOME"
19
+ export GEM_HOME
20
+ else
21
+ unset GEM_HOME
22
+ fi
23
+
24
+ if [ -n "$_OLD_SANDBOX_GEM_PATH" ] ; then
25
+ GEM_PATH="$_OLD_SANDBOX_GEM_PATH"
26
+ export GEM_PATH
27
+ else
28
+ unset GEM_PATH
29
+ fi
30
+
31
+ if [ -n "$_OLD_SANDBOX_PATH" ] ; then
32
+ PATH="$_OLD_SANDBOX_PATH"
33
+ export PATH
34
+ fi
35
+
36
+ if [ -n "$_OLD_SANDBOX_HOME" ] ; then
37
+ HOME="$_OLD_SANDBOX_HOME"
38
+ export HOME
39
+ fi
40
+
41
+ if [ -n "$_OLD_SANDBOX_PS1" ] ; then
42
+ PS1="$_OLD_SANDBOX_PS1"
43
+ export PS1
44
+ fi
45
+
46
+ unset _OLD_SANDBOX_PATH
47
+ unset _OLD_SANDBOX_HOME
48
+ unset _OLD_SANDBOX_GEM_HOME
49
+ unset _OLD_SANDBOX_GEM_PATH
50
+ unset _OLD_SANDBOX_PS1
51
+ unset SANDBOX_ENV
52
+
53
+ forget_cached_commands
54
+
55
+ if [ ! "$1" = "nondestructive" ] ; then
56
+ # Self destruct!
57
+ unset deactivate
58
+ unset forget_cached_commands
59
+ fi
60
+ }
61
+
62
+ activate () {
63
+
64
+ # unset irrelavent variables
65
+ deactivate nondestructive
66
+
67
+ # preserve current variables
68
+ _OLD_SANDBOX_GEM_HOME="$GEM_HOME"
69
+ _OLD_SANDBOX_GEM_PATH="$GEM_PATH"
70
+ _OLD_SANDBOX_PATH="$PATH"
71
+ _OLD_SANDBOX_HOME="$HOME"
72
+ _OLD_SANDBOX_PS1="$PS1"
73
+
74
+ # set the sandbox-aware states
75
+ SANDBOX_ENV="<%= target %>"
76
+ PATH="$SANDBOX_ENV/bin:$PATH"
77
+ HOME="$SANDBOX_ENV"
78
+ GEM_HOME="$SANDBOX_ENV/rubygems"
79
+ GEM_PATH="$GEM_HOME"
80
+ if [ "`basename \"$SANDBOX_ENV\"`" = "__" ] ; then
81
+ # special case for Aspen magic directories
82
+ # see http://www.zetadev.com/software/aspen/
83
+ PS1="[ruby-virtualenv:`basename \`dirname \"$SANDBOX_ENV\"\``] $PS1"
84
+ else
85
+ PS1="(ruby-virtualenv:`basename \"$SANDBOX_ENV\"`) $PS1"
86
+ fi
87
+
88
+ export SANDBOX_ENV PATH HOME GEM_HOME GEM_PATH PS1
89
+
90
+ forget_cached_commands
91
+
92
+ unset activate
93
+ }
94
+
95
+ activate
96
+
97
+ # Thanks to the virtualenv developers (http://pypi.python.org/pypi/virtualenv)
@@ -0,0 +1,7 @@
1
+ ---
2
+ :benchmark: false
3
+ :update_sources: true
4
+ :bulk_threshold: 1000
5
+ :verbose: true
6
+ :backtrace: false
7
+ gem: --no-ri --no-rdoc
@@ -0,0 +1,3 @@
1
+ module Sandbox
2
+ VERSION = "0.5.0"
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sandbox/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jacob Radford", "Francesc Esplugas"]
6
+ gem.email = ["nkryptic@gmail.com", "francesc.esplugas@gmail.com"]
7
+ gem.description = %q{Create virtual ruby/rubygems environments.}
8
+ gem.summary = %q{Create virtual ruby/rubygems environments.}
9
+ gem.homepage = "http://github.com/fesplugas/ruby-virtualenv"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "ruby-virtualenv"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Sandbox::VERSION
17
+
18
+ gem.add_development_dependency "rspec", "~> 2.6.0"
19
+ gem.add_development_dependency "mocha", "~> 0.10.0"
20
+ end
@@ -0,0 +1,234 @@
1
+ require 'spec_helper'
2
+ require 'sandbox/cli'
3
+
4
+ describe Sandbox::CLI do
5
+
6
+ it "should raise an error when running from a loaded sandbox" do
7
+ begin
8
+ ENV[ 'SANDBOX' ] = 'something'
9
+ lambda { Sandbox::CLI.verify_environment! }.should raise_error( Sandbox::LoadedSandboxError )
10
+ ensure
11
+ ENV[ 'SANDBOX' ] = nil
12
+ end
13
+ end
14
+
15
+ describe "calling execute" do
16
+ before( :each ) do
17
+ @instance = stub_everything()
18
+ end
19
+
20
+ it "should handle all errors" do
21
+ Sandbox::CLI.stubs( :new ).raises( StandardError )
22
+ Sandbox::CLI.expects( :handle_error ).with( instance_of( StandardError ) )
23
+ Sandbox::CLI.execute
24
+ end
25
+
26
+ it "should attempt to parse ARGV by default" do
27
+ Sandbox::CLI.expects( :parse ).with( ARGV ).returns( @instance )
28
+ Sandbox::CLI.execute
29
+ end
30
+
31
+ it "should create a new instance" do
32
+ Sandbox::CLI.expects( :new ).returns( @instance )
33
+ Sandbox::CLI.execute
34
+ end
35
+
36
+ it "should call parse_args! on the new instance" do
37
+ @instance.expects( :parse_args! )
38
+ Sandbox::CLI.expects( :new ).returns( @instance )
39
+ Sandbox::CLI.execute
40
+ end
41
+
42
+ it "should run the instance" do
43
+ @instance.expects( :execute! )
44
+ Sandbox::CLI.expects( :parse ).returns( @instance )
45
+ Sandbox::CLI.execute
46
+ end
47
+ end
48
+
49
+ describe "handling errors" do
50
+ it "should just print Sandbox::Error message" do
51
+ err = Sandbox::Error.new( "spec test msg" )
52
+ Sandbox::CLI.expects( :tell_unless_really_quiet ).once.with( regexp_matches( /^Sandbox error: spec test msg/ ) )
53
+ Sandbox::CLI.handle_error( err )
54
+ end
55
+
56
+ it "should just print wrapped StandardError message" do
57
+ err = StandardError.new( "spec test msg" )
58
+ Sandbox::CLI.expects( :tell_unless_really_quiet ).once.with( regexp_matches( /^Error: spec test msg/ ) )
59
+ Sandbox::CLI.handle_error( err )
60
+ end
61
+
62
+ it "should have simple message for Interrupt" do
63
+ err = Interrupt.new( "spec test msg" )
64
+ Sandbox::CLI.expects( :tell_unless_really_quiet ).once.with( 'Interrupted' )
65
+ Sandbox::CLI.handle_error( err )
66
+ end
67
+
68
+ it "should reraise other errors" do
69
+ err = NotImplementedError.new( 'you should have implemented it, dummy' )
70
+ Sandbox::CLI.expects( :raise ).once.with( err )
71
+ Sandbox::CLI.handle_error( err )
72
+ end
73
+ end
74
+
75
+ describe "a new instance" do
76
+
77
+ before( :each ) do
78
+ @cli = Sandbox::CLI.new
79
+ end
80
+
81
+ it "should have no default 'gems to install'" do
82
+ @cli.options[ :gems ].should == []
83
+ end
84
+
85
+ describe "instance calling parse_args!" do
86
+ def processor( *args ); lambda { @cli.parse_args!( args ) }; end
87
+ def process( *args ); processor( *args ).call; end
88
+
89
+ describe "using NO arguments" do
90
+ it "should use raise nothing" do
91
+ process()
92
+ @cli.options[ :original_args ].should == []
93
+ @cli.options[ :args ].should == []
94
+ end
95
+ end
96
+
97
+ describe "using VALID arguments" do
98
+ [ '-V', '--version' ].each do |arg|
99
+ it "should print the version for switch '#{arg}'" do
100
+ @cli.expects( :tell_unless_really_quiet ).with( Sandbox::VERSION )
101
+ processor( arg ).should raise_error( SystemExit ) { |error| error.status.should == 0 }
102
+ end
103
+ end
104
+
105
+ [ '-V', '--version' ].each do |arg|
106
+ it "should ignore additional arguments after '#{arg}'" do
107
+ # @cli.stubs(:puts)
108
+ @cli.expects( :tell_unless_really_quiet ).with( Sandbox::VERSION ).times(2)
109
+ processor( arg, '-x' ).should raise_error( SystemExit ) { |error| error.status.should == 0 }
110
+ processor( arg, 'unknown' ).should raise_error( SystemExit ) { |error| error.status.should == 0 }
111
+ end
112
+ end
113
+
114
+ [ '-h', '--help' ].each do |arg|
115
+ it "should show help for '#{arg}'" do
116
+ @cli.expects( :tell_unless_really_quiet ).with( instance_of( OptionParser ) )
117
+ processor( arg ).should raise_error( SystemExit ) { |error| error.status.should == 0 }
118
+ end
119
+ end
120
+
121
+ [ '-h', '--help' ].each do |arg|
122
+ it "should ignore additional arguments after '#{arg}'" do
123
+ @cli.expects( :tell_unless_really_quiet ).with( instance_of( OptionParser ) ).times(2)
124
+ processor( arg, '-x' ).should raise_error( SystemExit ) { |error| error.status.should == 0 }
125
+ processor( arg, 'unknown' ).should raise_error( SystemExit ) { |error| error.status.should == 0 }
126
+ end
127
+ end
128
+
129
+ [ '-H', '--long-help' ].each do |arg|
130
+ it "should show long help for '#{arg}'" do
131
+ @cli.expects( :tell_unless_really_quiet )
132
+ @cli.expects( :long_help )
133
+ processor( arg ).should raise_error( SystemExit ) { |error| error.status.should == 0 }
134
+ end
135
+ end
136
+
137
+ [ '-v', '--verbose' ].each do |arg|
138
+ it "should increase verbosity with '#{arg}'" do
139
+ Sandbox.expects( :increase_verbosity )
140
+ process( arg )
141
+ @cli.options[ :original_args ].should == [ arg ]
142
+ @cli.options[ :args ].should == []
143
+ end
144
+ end
145
+
146
+ [ [ '-v', '-v' ], [ '--verbose', '--verbose' ] ].each do |args|
147
+ it "should increase verbosity twice with '#{args.join(' ')}'" do
148
+ Sandbox.expects( :increase_verbosity ).times(2)
149
+ process( *args )
150
+ @cli.options[ :original_args ].should == args
151
+ @cli.options[ :args ].should == []
152
+ end
153
+ end
154
+
155
+ it "should require additional arguments with switch '-g'" do
156
+ processor( '-g' ).should raise_error( Sandbox::ParseError )
157
+ end
158
+
159
+ it "should set 'gems to install' with switch '-g'" do
160
+ args = [ '-g', 'somegem,anothergem' ]
161
+ process( *args )
162
+ @cli.options[ :original_args ].should == args
163
+ @cli.options[ :args ].should == []
164
+ @cli.options[ :gems ].should == [ 'somegem', 'anothergem' ]
165
+ end
166
+
167
+ it "should clear 'gems to install' with switch '-n'" do
168
+ process( '-n' )
169
+ @cli.options[ :original_args ].should == [ '-n' ]
170
+ @cli.options[ :args ].should == []
171
+ @cli.options[ :gems ].should == []
172
+ end
173
+
174
+ it "should store leftover arguments in options for arguments '/path/to/somewhere'" do
175
+ args = [ '/path/to/somewhere' ]
176
+ process( *args )
177
+ @cli.options[ :original_args ].should == args
178
+ @cli.options[ :args ].should == args
179
+ end
180
+
181
+ it "should store leftover arguments in options for arguments '-v /path/to/somewhere'" do
182
+ args = [ '-v', '/path/to/somewhere' ]
183
+ process( *args )
184
+ @cli.options[ :original_args ].should == args
185
+ @cli.options[ :args ].should == [ args.last ]
186
+ end
187
+ end
188
+
189
+ describe "using INVALID arguments" do
190
+ it "should exit with message for invalid switch '-x'" do
191
+ processor( '-x' ).
192
+ should raise_error( Sandbox::ParseError ) { |error| error.message.should =~ /-x\b/ }
193
+ end
194
+ end
195
+ end
196
+
197
+ describe "instance calling execute!" do
198
+ def processor; lambda { @cli.execute! }; end
199
+ def process; processor.call; end
200
+
201
+ it "should raise error with no target specified" do
202
+ @cli.options[ :args ] = []
203
+ processor.should raise_error
204
+ end
205
+
206
+ it "should raise error with more than one target" do
207
+ @cli.options[ :args ] = [ 'one', 'two' ]
208
+ processor.should raise_error
209
+ end
210
+
211
+ it "should instantiate an Installer and call populate with one target" do
212
+ @cli.options.delete( :gems )
213
+ @cli.options[ :args ] = [ 'one' ]
214
+ installer = mock( 'Installer', :populate )
215
+ Sandbox::Installer.expects( :new ).with( { :target => 'one' } ).returns( installer )
216
+ process
217
+ end
218
+
219
+ end
220
+
221
+ describe "instance calling long_help" do
222
+ it "should return a long descriptive string" do
223
+ @cli.long_help.split( "\n" ).size.should be > 20
224
+ @cli.long_help.should =~ /activate/
225
+ @cli.long_help.should =~ /deactivate/
226
+ @cli.long_help.should =~ /NOTES:/
227
+ @cli.long_help.should =~ /WARNINGS:/
228
+ end
229
+ end
230
+
231
+ end
232
+
233
+ end
234
+
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'new', Sandbox::Error do
4
+ it "should wrap it's message with 'Sandbox error'" do
5
+ Sandbox::Error.new( 'msg' ).message.should == 'Sandbox error: msg'
6
+ end
7
+ end
8
+
9
+ describe 'new', Sandbox::LoadedSandboxError do
10
+ it "should have informative default msg" do
11
+ Sandbox::LoadedSandboxError.new.message.should =~ /loaded sandbox/
12
+ end
13
+ end
14
+
15
+ describe 'new', Sandbox::ParseError do
16
+ it "should accept reason with array" do
17
+ Sandbox::ParseError.new( 'testing', [ 1, 2, 3, 4 ] ).
18
+ message.should =~ /testing => 1 2 3 4/
19
+ end
20
+
21
+ it "should accept reason with string" do
22
+ Sandbox::ParseError.new( 'testing', "1, 2, 3, 4" ).
23
+ message.should =~ /testing => 1, 2, 3, 4/
24
+ end
25
+
26
+ it "should fall back to reason alone" do
27
+ Sandbox::ParseError.new( 'testing', [] ).
28
+ message.should =~ /testing$/
29
+ Sandbox::ParseError.new( 'testing', '' ).
30
+ message.should =~ /testing$/
31
+ end
32
+ end
@@ -0,0 +1,303 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sandbox::Installer, "(mocked)" do
4
+
5
+ before( :each ) do
6
+ Sandbox.instance_eval { instance_variables.each { |v| remove_instance_variable v } }
7
+ end
8
+
9
+ describe "creating an instance" do
10
+ # initialize( options={} )
11
+ it "should set it's options" do
12
+ opts = { :somewhere => true, :nowhere => false }
13
+ installer = Sandbox::Installer.new( opts )
14
+ installer.options[ :somewhere ].should be_true
15
+ installer.options[ :nowhere ].should be_false
16
+ end
17
+ end
18
+
19
+ describe "instance" do
20
+ # target
21
+ describe "when target called" do
22
+ it "should validate target directory once" do
23
+ path = '/some/new/target'
24
+ opts = { :target => path }
25
+ @installer = Sandbox::Installer.new( opts )
26
+ @installer.expects( :resolve_target ).with( path ).once.then.returns( path )
27
+ @installer.target.should == path
28
+ @installer.target.should == path
29
+ end
30
+ end
31
+
32
+ # populate
33
+ describe "when populate called" do
34
+ it "should call all steps of populate process" do
35
+ @installer = Sandbox::Installer.new
36
+ @installer.stubs( :tell )
37
+ @installer.stubs( :target ).returns( '/tmp/sandbox' )
38
+ @installer.expects( :create_directories )
39
+ @installer.expects( :install_scripts )
40
+ @installer.expects( :install_gemrc )
41
+ @installer.expects( :install_gems )
42
+ @installer.populate
43
+ end
44
+ end
45
+
46
+ # create_directories
47
+ describe "when create_directories called" do
48
+ before( :each ) do
49
+ @path = '/some/new/target'
50
+ @installer = Sandbox::Installer.new
51
+ @installer.stubs( :target ).returns( @path )
52
+ end
53
+ it "should create sandbox directory structure" do
54
+ FileUtils.expects( :mkdir_p ).with( @path + '/rubygems/bin' )
55
+ FileUtils.stubs( :ln_s )
56
+ @installer.create_directories
57
+ end
58
+
59
+ it "should symlink gem bin directory" do
60
+ FileUtils.stubs( :mkdir_p )
61
+ FileUtils.expects( :ln_s ).with( @path + '/rubygems/bin', @path + '/bin' )
62
+ @installer.create_directories
63
+ end
64
+ end
65
+
66
+ # install_scripts
67
+ describe "when install_scripts called" do
68
+ before( :each ) do
69
+ @path = '/some/new/target'
70
+ @installer = Sandbox::Installer.new
71
+ @installer.stubs( :target ).returns( @path )
72
+ end
73
+
74
+ it "should read template file" do
75
+ File.expects( :read ).with( regexp_matches( /templates\/activate\.erb/ ) ).returns( '<%= target %>' )
76
+ File.stubs( :open )
77
+ @installer.install_scripts
78
+ end
79
+
80
+ it "should write out activate script to SANDBOX/bin/activate" do
81
+ file = StringIO.new
82
+ File.stubs( :read ).returns( '<%= target %>' )
83
+ File.expects( :open ).with( @path + '/bin/activate', 'w' ).yields( file )
84
+ @installer.install_scripts
85
+ file.string.should == @path
86
+ end
87
+ end
88
+
89
+ # install_gemrc
90
+ describe "when install_gemrc called" do
91
+ before( :each ) do
92
+ @path = '/some/new/target'
93
+ @installer = Sandbox::Installer.new
94
+ @installer.stubs( :target ).returns( @path )
95
+ end
96
+
97
+ it "should read template file" do
98
+ File.expects( :read ).with( regexp_matches( /templates\/gemrc\.erb/ ) ).returns( '' )
99
+ File.stubs( :open )
100
+ @installer.install_gemrc
101
+ end
102
+
103
+ it "should write out gemrc to SANDBOX/.gemrc" do
104
+ file = StringIO.new
105
+ File.stubs( :read ).returns( 'gemrc' )
106
+ File.expects( :open ).with( @path + '/.gemrc', 'w' ).yields( file )
107
+ @installer.install_gemrc
108
+ file.string.should == 'gemrc'
109
+ end
110
+ end
111
+
112
+ # install_gems
113
+ describe "when install_gems called" do
114
+ before( :each ) do
115
+ @installer = Sandbox::Installer.new( :gems => [ 'mygem' ] )
116
+ @installer.stubs( :setup_sandbox_env )
117
+ @installer.stubs( :restore_sandbox_env )
118
+ @installer.stubs( :tell )
119
+ @installer.stubs( :tell_unless_really_quiet )
120
+ end
121
+
122
+ # it "should skip install when network is not available" do
123
+ # Ping.expects( :pingecho ).with( 'gems.rubyforge.org' ).returns( false )
124
+ # @installer.install_gems.should be_false
125
+ # end
126
+
127
+ it "should install a good gem" do
128
+ @installer.expects( :shell_out ).with( 'gem install mygem' ).returns( [ true, 'blah' ] )
129
+ @installer.install_gems
130
+ end
131
+
132
+ it "should gracefully handle a bad gem" do
133
+ @installer.expects( :shell_out ).with( 'gem install mygem' ).returns( [ false, 'blah' ] )
134
+ @installer.expects( :tell_unless_really_quiet ).with( regexp_matches( /failed/ ) )
135
+ @installer.install_gems
136
+ end
137
+ end
138
+
139
+ # resolve_target( path )
140
+ describe "when resolve_target called" do
141
+ before( :each ) do
142
+ @path = '/absolute/path/to/parent'
143
+ @installer = Sandbox::Installer.new
144
+ @installer.stubs( :fix_path ).returns( @path )
145
+ end
146
+
147
+ it "should raise error when it path exists" do
148
+ File.expects( :exists? ).with( @path ).returns( true )
149
+ lambda { @installer.resolve_target( @path ) }.should raise_error( Sandbox::Error )
150
+ end
151
+
152
+ it "should return path when parent directory passes check_path!" do
153
+ File.expects( :exists? ).with( @path ).returns( false )
154
+ @installer.expects( :check_path! ).with( '/absolute/path/to' ).returns( true )
155
+ @installer.resolve_target( @path ).should == @path
156
+ end
157
+
158
+ it "should return path when any parent directory passes check_path!" do
159
+ File.expects( :exists? ).with( @path ).returns( false )
160
+ @installer.expects( :check_path! ).with( '/absolute/path/to' ).returns( false )
161
+ @installer.expects( :check_path! ).with( '/absolute/path' ).returns( false )
162
+ @installer.expects( :check_path! ).with( '/absolute' ).returns( true )
163
+ @installer.resolve_target( @path ).should == @path
164
+ end
165
+
166
+ it "should have emergency safeguard for dirname on root" do
167
+ @installer.stubs( :fix_path ).returns( '/absolute' )
168
+ @installer.expects( :check_path! ).with( '/' ).returns( false )
169
+ lambda { @installer.resolve_target( '/absolute' ) }.should raise_error( RuntimeError )
170
+ end
171
+ end
172
+
173
+ # check_path!( path )
174
+ describe "when check_path! called" do
175
+ before( :each ) do
176
+ @path = '/absolute/path/to/parent'
177
+ @installer = Sandbox::Installer.new
178
+ end
179
+
180
+ it "should raise error when it is not writable" do
181
+ File.expects( :directory? ).with( @path ).returns( true )
182
+ File.expects( :writable? ).with( @path ).returns( false )
183
+ lambda { @installer.check_path!( @path ) }.should raise_error( Sandbox::Error )
184
+ end
185
+
186
+ it "should raise error when it is not a directory" do
187
+ File.expects( :directory? ).with( @path ).returns( false )
188
+ File.expects( :exists? ).with( @path ).returns( true )
189
+ lambda { @installer.check_path!( @path ) }.should raise_error( Sandbox::Error )
190
+ end
191
+
192
+ it "should return false when it doesn't exist" do
193
+ File.expects( :directory? ).with( @path ).returns( false )
194
+ File.expects( :exists? ).with( @path ).returns( false )
195
+ @installer.check_path!( @path ).should be_false
196
+ end
197
+
198
+ it "should return true when it can be created" do
199
+ File.expects( :directory? ).with( @path ).returns( true )
200
+ File.expects( :writable? ).with( @path ).returns( true )
201
+ @installer.check_path!( @path ).should == true
202
+ end
203
+ end
204
+
205
+ # fix_path( path )
206
+ describe "when fix_path called" do
207
+ it "should not change absolute path" do
208
+ path = '/absolute/path/to/target'
209
+ @installer = Sandbox::Installer.new
210
+ @installer.fix_path( path ).should == path
211
+ end
212
+
213
+ it "should make relative into absolute path" do
214
+ abs_path = '/absolute/working/directory'
215
+ path = 'relative/path/to/target'
216
+ FileUtils.expects( :pwd ).returns( abs_path )
217
+ @installer = Sandbox::Installer.new
218
+ @installer.fix_path( path ).should == abs_path + '/' + path
219
+ end
220
+ end
221
+
222
+ # shell_out( cmd )
223
+ describe "when shell_out called" do
224
+ it "should record true when successful" do
225
+ @installer = Sandbox::Installer.new
226
+ result = @installer.shell_out( 'true' )
227
+ result.first.should be_true
228
+ end
229
+
230
+ it "should record false when unsuccessful" do
231
+ @installer = Sandbox::Installer.new
232
+ result = @installer.shell_out( 'false' )
233
+ result.first.should_not be_true
234
+ end
235
+
236
+ it "should record std output" do
237
+ @installer = Sandbox::Installer.new
238
+ result = @installer.shell_out( 'ls -d /' )
239
+ result.last.chomp.should == '/'
240
+ end
241
+
242
+ it "should ignore std error" do
243
+ @installer = Sandbox::Installer.new
244
+ result = @installer.shell_out( 'ls -d / 1>/dev/null' )
245
+ result.last.chomp.should == ''
246
+ end
247
+ end
248
+
249
+ describe "setup and restore sandbox env called" do
250
+ it "should set and restore the environment" do
251
+ orig_home = ENV[ 'HOME' ]
252
+ orig_gem_home = ENV[ 'GEM_HOME' ]
253
+ orig_gem_path = ENV[ 'GEM_PATH' ]
254
+ @installer = Sandbox::Installer.new
255
+ @installer.stubs( :target ).returns( 'dummypath' )
256
+
257
+ @installer.setup_sandbox_env
258
+ ENV[ 'HOME' ].should == 'dummypath'
259
+ ENV[ 'GEM_HOME' ].should == 'dummypath/rubygems'
260
+ ENV[ 'GEM_PATH' ].should == 'dummypath/rubygems'
261
+ @installer.restore_sandbox_env
262
+ ENV[ 'HOME' ].should == orig_home
263
+ ENV[ 'GEM_HOME' ].should == orig_gem_home
264
+ ENV[ 'GEM_PATH' ].should == orig_gem_path
265
+ end
266
+ end
267
+ end
268
+ end
269
+
270
+ describe Sandbox::Installer, "(using tmpdir)" do
271
+ def tmppath() File.join( Dir.tmpdir, "sandbox_testing" ) end
272
+ def rmtmppath() FileUtils.rm_rf( tmppath ) end
273
+ def mktmppath() FileUtils.mkdir_p( tmppath ) end
274
+
275
+ def in_dir( dir = tmppath )
276
+ old_pwd = Dir.pwd
277
+ begin
278
+ Dir.chdir( dir )
279
+ yield
280
+ ensure
281
+ Dir.chdir( old_pwd )
282
+ end
283
+ end
284
+
285
+ before( :each ) do
286
+ mktmppath
287
+ end
288
+
289
+ after( :each ) do
290
+ rmtmppath
291
+ end
292
+
293
+ it "should create target directory structure" do
294
+ target = tmppath + '/target'
295
+ @installer = Sandbox::Installer.new( :target => target )
296
+ @installer.create_directories
297
+ File.directory?( target + '/rubygems/bin' ).should be_true
298
+ File.symlink?( target + '/bin' ).should be_true
299
+ end
300
+ end
301
+
302
+
303
+