nkryptic-sandbox 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,219 @@
1
+
2
+ require File.dirname( __FILE__ ) + '/../spec_helper'
3
+
4
+ describe Sandbox::Installer, "(mocked)" do
5
+
6
+ before( :each ) do
7
+ Sandbox.instance_eval { instance_variables.each { |v| remove_instance_variable v } }
8
+ end
9
+
10
+ describe "creating an instance" do
11
+ # initialize( options={} )
12
+ it "should set it's options" do
13
+ opts = { :somewhere => true, :nowhere => false }
14
+ installer = Sandbox::Installer.new( opts )
15
+ installer.options[ :somewhere ].should be_true
16
+ installer.options[ :nowhere ].should be_false
17
+ end
18
+ end
19
+
20
+ describe "instance" do
21
+ # target
22
+ describe "when target called" do
23
+ it "should validate target directory once" do
24
+ path = '/some/new/target'
25
+ opts = { :target => path }
26
+ @installer = Sandbox::Installer.new( opts )
27
+ @installer.expects( :resolve_target ).with( path ).once.then.returns( path )
28
+ @installer.target.should == path
29
+ @installer.target.should == path
30
+ end
31
+ end
32
+
33
+ # populate
34
+ describe "when populate called" do
35
+ it "should call all steps of populate process" do
36
+ @installer = Sandbox::Installer.new
37
+ @installer.expects( :create_directories )
38
+ @installer.expects( :install_scripts )
39
+ @installer.expects( :install_gems )
40
+ @installer.populate
41
+ end
42
+ end
43
+
44
+ # create_directories
45
+ describe "when create_directories called" do
46
+ before( :each ) do
47
+ @path = '/some/new/target'
48
+ @installer = Sandbox::Installer.new
49
+ @installer.stubs( :target ).returns( @path )
50
+ end
51
+ it "should create sandbox directory structure" do
52
+ FileUtils.expects( :mkdir_p ).with( @path + '/rubygems/bin' )
53
+ FileUtils.stubs( :ln_s )
54
+ @installer.create_directories
55
+ end
56
+
57
+ it "should symlink gem bin directory" do
58
+ FileUtils.stubs( :mkdir_p )
59
+ FileUtils.expects( :ln_s ).with( @path + '/rubygems/bin', @path + '/bin' )
60
+ @installer.create_directories
61
+ end
62
+ end
63
+
64
+ # install_scripts
65
+ describe "when install_scripts called" do
66
+ before( :each ) do
67
+ @path = '/some/new/target'
68
+ @installer = Sandbox::Installer.new
69
+ @installer.stubs( :target ).returns( @path )
70
+ end
71
+
72
+ it "should read template file" do
73
+ File.expects( :read ).with( regexp_matches( /templates\/activate_sandbox\.erb/ ) ).returns( '<%= target %>' )
74
+ File.stubs( :open )
75
+ @installer.install_scripts
76
+ end
77
+
78
+ it "should write out activate script to SANDBOX/bin/activate_sandbox" do
79
+ file = StringIO.new
80
+ File.stubs( :read ).returns( '<%= target %>' )
81
+ File.expects( :open ).with( @path + '/bin/activate_sandbox', 'w' ).yields( file )
82
+ @installer.install_scripts
83
+ file.string.should == @path
84
+ end
85
+ end
86
+
87
+ # install_gems
88
+ describe "when install_gems called" do
89
+ before( :each ) do
90
+ @installer = Sandbox::Installer.new( :gems => [ 'mygem' ] )
91
+ @installer.stubs( :setup_sandbox_env )
92
+ @installer.stubs( :restore_sandbox_env )
93
+ end
94
+
95
+ it "should install a good gem" do
96
+ @installer.expects( :shell_out ).with( 'gem install mygem' ).returns( [ true, 'blah' ] )
97
+ @installer.install_gems
98
+ end
99
+
100
+ it "should gracefully handle a bad gem" do
101
+ @installer.expects( :shell_out ).with( 'gem install mygem' ).returns( [ false, 'blah' ] )
102
+ @installer.expects( :warn )
103
+ @installer.install_gems
104
+ end
105
+ end
106
+
107
+ # resolve_target( path )
108
+ describe "when resolve_target called" do
109
+ before( :each ) do
110
+ @path = '/absolute/path/to/parent'
111
+ @installer = Sandbox::Installer.new
112
+ @installer.stubs( :fix_path ).returns( @path )
113
+ end
114
+
115
+ it "should raise error when it path exists" do
116
+ File.expects( :exists? ).with( @path ).returns( true )
117
+ lambda { @installer.resolve_target( @path ) }.should raise_error( Sandbox::Error )
118
+ end
119
+
120
+ it "should return path when parent directory passes check_path!" do
121
+ File.expects( :exists? ).with( @path ).returns( false )
122
+ @installer.expects( :check_path! ).with( '/absolute/path/to' ).returns( true )
123
+ @installer.resolve_target( @path ).should == @path
124
+ end
125
+
126
+ it "should return path when any parent directory passes check_path!" do
127
+ File.expects( :exists? ).with( @path ).returns( false )
128
+ @installer.expects( :check_path! ).with( '/absolute/path/to' ).returns( false )
129
+ @installer.expects( :check_path! ).with( '/absolute/path' ).returns( false )
130
+ @installer.expects( :check_path! ).with( '/absolute' ).returns( true )
131
+ @installer.resolve_target( @path ).should == @path
132
+ end
133
+ end
134
+
135
+ # check_path!( path )
136
+ describe "when check_path! called" do
137
+ before( :each ) do
138
+ @path = '/absolute/path/to/parent'
139
+ @installer = Sandbox::Installer.new
140
+ end
141
+
142
+ it "should raise error when it is not writable" do
143
+ File.expects( :directory? ).with( @path ).returns( true )
144
+ File.expects( :writable? ).with( @path ).returns( false )
145
+ lambda { @installer.check_path!( @path ) }.should raise_error( Sandbox::Error )
146
+ end
147
+
148
+ it "should raise error when it is not a directory" do
149
+ File.expects( :directory? ).with( @path ).returns( false )
150
+ File.expects( :exists? ).with( @path ).returns( true )
151
+ lambda { @installer.check_path!( @path ) }.should raise_error( Sandbox::Error )
152
+ end
153
+
154
+ it "should return false when it doesn't exist" do
155
+ File.expects( :directory? ).with( @path ).returns( false )
156
+ File.expects( :exists? ).with( @path ).returns( false )
157
+ @installer.check_path!( @path ).should be_false
158
+ end
159
+
160
+ it "should return true when it can be created" do
161
+ File.expects( :directory? ).with( @path ).returns( true )
162
+ File.expects( :writable? ).with( @path ).returns( true )
163
+ @installer.check_path!( @path ).should == true
164
+ end
165
+ end
166
+
167
+ # fix_path( path )
168
+ describe "when fix_path called" do
169
+ it "should not change absolute path" do
170
+ path = '/absolute/path/to/target'
171
+ @installer = Sandbox::Installer.new
172
+ @installer.fix_path( path ).should == path
173
+ end
174
+
175
+ it "should make relative into absolute path" do
176
+ abs_path = '/absolute/working/directory'
177
+ path = 'relative/path/to/target'
178
+ FileUtils.expects( :pwd ).returns( abs_path )
179
+ @installer = Sandbox::Installer.new
180
+ @installer.fix_path( path ).should == abs_path + '/' + path
181
+ end
182
+ end
183
+ end
184
+ end
185
+
186
+ describe Sandbox::Installer, "(using tmpdir)" do
187
+ def tmppath() File.join( Dir.tmpdir, "sandbox_testing" ) end
188
+ def rmtmppath() FileUtils.rm_rf( tmppath ) end
189
+ def mktmppath() FileUtils.mkdir_p( tmppath ) end
190
+
191
+ def in_dir( dir = tmppath )
192
+ old_pwd = Dir.pwd
193
+ begin
194
+ Dir.chdir( dir )
195
+ yield
196
+ ensure
197
+ Dir.chdir( old_pwd )
198
+ end
199
+ end
200
+
201
+ before( :each ) do
202
+ mktmppath
203
+ end
204
+
205
+ after( :each ) do
206
+ rmtmppath
207
+ end
208
+
209
+ it "should create target directory structure" do
210
+ target = tmppath + '/target'
211
+ @installer = Sandbox::Installer.new( :target => target )
212
+ @installer.create_directories
213
+ File.directory?( target + '/rubygems/bin' ).should be_true
214
+ File.symlink?( target + '/bin' ).should be_true
215
+ end
216
+ end
217
+
218
+
219
+
@@ -0,0 +1,25 @@
1
+
2
+ require File.dirname( __FILE__ ) + '/spec_helper.rb'
3
+
4
+ describe Sandbox do
5
+ before( :each ) do
6
+ Sandbox.instance_eval { instance_variables.each { |v| remove_instance_variable v } }
7
+ end
8
+
9
+ it "should have verbosity level" do
10
+ Sandbox.verbosity.should == 0
11
+ end
12
+
13
+ it "should increase verbosity level" do
14
+ Sandbox.verbosity.should == 0
15
+ Sandbox.increase_verbosity
16
+ Sandbox.verbosity.should == 1
17
+ end
18
+
19
+ it "should decrease verbosity level" do
20
+ Sandbox.verbosity.should == 0
21
+ Sandbox.decrease_verbosity
22
+ Sandbox.verbosity.should == -1
23
+ end
24
+ end
25
+
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format specdoc
@@ -0,0 +1,63 @@
1
+ # begin
2
+ # require 'spec'
3
+ # rescue LoadError
4
+ # require 'rubygems'
5
+ # # gem 'rspec'
6
+ # require 'spec'
7
+ # end
8
+ #
9
+ # $:.unshift( File.dirname( __FILE__ ) + '/../lib' )
10
+
11
+ $:.unshift( File.dirname( __FILE__ ) + '/../lib' )
12
+ require 'rubygems'
13
+ require 'mocha'
14
+ require 'spec'
15
+ require 'stringio'
16
+ require 'ostruct'
17
+ require 'tempfile'
18
+
19
+ require 'sandbox'
20
+
21
+ Spec::Runner.configure do |config|
22
+ # == Mock Framework
23
+ # RSpec uses it's own mocking framework by default. If you prefer to
24
+ # use mocha, flexmock or RR, uncomment the appropriate line:
25
+ config.mock_with :mocha
26
+ # config.mock_with :flexmock
27
+ # config.mock_with :rr
28
+
29
+ def capture
30
+ results = OpenStruct.new
31
+
32
+ begin
33
+ $stdout = StringIO.new
34
+ $stderr = StringIO.new
35
+ yield
36
+ results.stdout = $stdout.string
37
+ results.stderr = $stderr.string
38
+ ensure
39
+ $stdout = STDOUT
40
+ $stderr = STDERR
41
+ end
42
+
43
+ return results
44
+ end
45
+
46
+ alias silence capture
47
+
48
+ end
49
+
50
+
51
+ ## thanks to Jay Fields (http://blog.jayfields.com/2007/11/ruby-testing-private-methods.html)
52
+ class Class
53
+ def publicize_methods
54
+ saved_private_instance_methods = self.private_instance_methods
55
+ begin
56
+ self.class_eval { public *saved_private_instance_methods }
57
+ yield
58
+ ensure
59
+ self.class_eval { private *saved_private_instance_methods }
60
+ end
61
+ end
62
+ end
63
+
@@ -0,0 +1,25 @@
1
+ begin
2
+ require 'cucumber'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'cucumber'
6
+ end
7
+ begin
8
+ require 'cucumber/rake/task'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use cucumber for testing you must install cucumber gem:
12
+ gem install cucumber
13
+ EOS
14
+ exit( 0 )
15
+ end
16
+
17
+ # Try these:
18
+ #
19
+ # rake features
20
+ # rake features PROFILE=html
21
+ Cucumber::Rake::Task.new do |t|
22
+ # profile = ENV[ 'PROFILE' ] || 'default'
23
+ # t.cucumber_opts = "--profile #{profile}"
24
+ t.cucumber_opts = "--format pretty"
25
+ end
data/tasks/dcov.rake ADDED
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'dcov'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ begin
6
+ require 'dcov'
7
+ rescue LoadError
8
+ puts <<-EOS
9
+ To generate documentation coverage with dcov you must install dcov gem:
10
+ gem install dcov
11
+ EOS
12
+ exit( 0 )
13
+ end
14
+ end
15
+
16
+ desc "Generate coverage report for lib directory"
17
+ task :dcov do
18
+ root = Dir.pwd
19
+ dcov_dir = File.join( root, 'coverage' )
20
+ lib_dir = File.join( root, 'lib' )
21
+
22
+ unless File.directory?( lib_dir )
23
+ puts "Aborting: please run from the root of the project"
24
+ exit( 0 )
25
+ end
26
+
27
+ # files = Dir[ File.join( lib_dir, '**', '*.rb' ) ]
28
+ options = {
29
+ :path => dcov_dir,
30
+ :output_format => 'html',
31
+ :files => lib_dir
32
+ # :files => files
33
+ }
34
+
35
+ Dir.mkdir( dcov_dir ) unless File.directory?( dcov_dir )
36
+ Dcov::Analyzer.new( options )
37
+ end
data/tasks/gem.rake ADDED
@@ -0,0 +1,5 @@
1
+
2
+ desc "Create this gem's .gemspec file"
3
+ task :gemspec do
4
+ Rake::Task[ 'build_gemspec' ].invoke
5
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,51 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run all the specs in spec directory"
18
+ Spec::Rake::SpecTask.new( :spec ) do |t|
19
+ t.spec_opts = [ '--options', "spec/spec.opts" ]
20
+ t.spec_files = FileList[ 'spec/**/*_spec.rb' ]
21
+ end
22
+
23
+ namespace :spec do
24
+ desc "Run all specs in spec directory with RCov"
25
+ Spec::Rake::SpecTask.new( :rcov ) do |t|
26
+ t.spec_opts = [ '--options', "spec/spec.opts" ]
27
+ t.spec_files = FileList[ 'spec/**/*_spec.rb' ]
28
+ t.rcov = true
29
+ # t.rcov_opts = [ '--exclude', "spec/*" ]
30
+ t.rcov_opts = [ '--exclude', "spec" ]
31
+ end
32
+
33
+ desc "Print Specdoc for all specs in spec directory"
34
+ Spec::Rake::SpecTask.new( :doc ) do |t|
35
+ t.spec_opts = [ "--format", "specdoc", "--dry-run" ]
36
+ # t.spec_opts = [ "--format", "specdoc" ]
37
+ t.spec_files = FileList[ 'spec/**/*_spec.rb' ]
38
+ end
39
+
40
+ desc "Run all the specs in spec directory individually"
41
+ task :deps do
42
+ individual_specs = Dir["spec/**/*_spec.rb"]
43
+ individual_specs.each do |single_spec|
44
+ if not system "spec #{single_spec} --options spec/spec.opts &> /dev/null"
45
+ puts "Dependency Issues: #{single_spec}"
46
+ else
47
+ puts "OK: #{single_spec}"
48
+ end
49
+ end
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nkryptic-sandbox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Jacob Radford
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-01 00:00:00 -08:00
13
+ default_executable: sandbox
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: echoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ - - "="
24
+ - !ruby/object:Gem::Version
25
+ version: "3.0"
26
+ version:
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ - - "="
36
+ - !ruby/object:Gem::Version
37
+ version: 1.1.0
38
+ version:
39
+ - !ruby/object:Gem::Dependency
40
+ name: mocha
41
+ version_requirement:
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ - - "="
48
+ - !ruby/object:Gem::Version
49
+ version: "0.9"
50
+ version:
51
+ description: Create virtual ruby/rubygems sandboxes.
52
+ email: nkryptic@gmail.com
53
+ executables:
54
+ - sandbox
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - CHANGELOG
59
+ - TODO
60
+ - README.rdoc
61
+ - tasks/rspec.rake
62
+ - tasks/cucumber.rake
63
+ - tasks/gem.rake
64
+ - tasks/dcov.rake
65
+ - lib/sandbox.rb
66
+ - lib/sandbox/installer.rb
67
+ - lib/sandbox/cli.rb
68
+ - lib/sandbox/templates/activate_sandbox.erb
69
+ - lib/sandbox/version.rb
70
+ - lib/sandbox/errors.rb
71
+ - bin/sandbox
72
+ files:
73
+ - sandbox.gemspec
74
+ - CHANGELOG
75
+ - TODO
76
+ - spec/sandbox/cli_spec.rb
77
+ - spec/sandbox/installer_spec.rb
78
+ - spec/sandbox_spec.rb
79
+ - spec/spec.opts
80
+ - spec/spec_helper.rb
81
+ - Manifest
82
+ - features/steps/common.rb
83
+ - features/steps/env.rb
84
+ - features/development.feature
85
+ - README.rdoc
86
+ - tasks/rspec.rake
87
+ - tasks/cucumber.rake
88
+ - tasks/gem.rake
89
+ - tasks/dcov.rake
90
+ - Rakefile
91
+ - lib/sandbox.rb
92
+ - lib/sandbox/installer.rb
93
+ - lib/sandbox/cli.rb
94
+ - lib/sandbox/templates/activate_sandbox.erb
95
+ - lib/sandbox/version.rb
96
+ - lib/sandbox/errors.rb
97
+ - bin/sandbox
98
+ has_rdoc: true
99
+ homepage: http://github.com/nkryptic/sandbox
100
+ post_install_message:
101
+ rdoc_options:
102
+ - --line-numbers
103
+ - --inline-source
104
+ - --title
105
+ - Sandbox
106
+ - --main
107
+ - README.rdoc
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: "0"
115
+ version:
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: "1.2"
121
+ version:
122
+ requirements: []
123
+
124
+ rubyforge_project: sandbox
125
+ rubygems_version: 1.2.0
126
+ signing_key:
127
+ specification_version: 2
128
+ summary: Create virtual ruby/rubygems sandboxes.
129
+ test_files: []
130
+