dia 2.0.2 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ 2010-08-12 Robert Gleeson <rob@flowof.info> (v2.1.0)
2
+
3
+ * Implemented Dia::RubyBlock#value.
4
+
5
+ * Dia::RubyBlock#exception_raised? is deprecated.
6
+ Dia::RubyBlock#exception_rescued? should be used instead.
7
+
8
+ * Dia::Rubyblock#redirect_stdout=, Dia::RubyBlock#redirect_stderr=,
9
+ and Dia::RubyBlock#rescue_exception= are now documented as setters.
10
+
11
+ * Correct inconsistencies in the documentation for
12
+ Dia::RubyBlock#redirect_stdout= , Dia::RubyBlock#redirect_stderr=,
13
+ Dia::RubyBlock#exception_rescued? (formerly Dia::RubyBlock#exception_raised?),
14
+ Dia::RubyBlock#rescue_exception?, Dia::RubyBlock#rescue_exception=,
15
+ Dia::RubyBlock#exception.
16
+
17
+ * Added Changelog to track changes.
@@ -15,15 +15,11 @@ Gem::Specification.new do |s|
15
15
  The Ruby API was designed to be simple, and a joy to use.
16
16
  I hope you feel the same way :-)}
17
17
 
18
- s.summary = %q{Through the use of technology found on Apple's Leopard and Snow Leopard
19
- operating systems, Dia can create dynamic and robust sandbox environments
20
- for applications and for blocks of ruby code.
21
- The Ruby API was designed to be simple, and a joy to use.
22
- I hope you feel the same way :-)}
23
-
18
+ s.summary = s.description
24
19
 
25
20
  s.require_paths = ["lib"]
26
- s.files = Dir["lib/**/*.rb"] + Dir["test/**/*.rb"] + %w(COPYING README.mkd dia.gemspec)
21
+ s.files = Dir["lib/**/*.rb"] + Dir["test/**/*.rb"] + %w(COPYING Changelog README.mkd
22
+ dia.gemspec)
27
23
  s.test_files = Dir["test/**/*.rb"]
28
24
 
29
25
 
data/lib/dia.rb CHANGED
@@ -11,5 +11,5 @@ require(File.expand_path('dia/exception_struct' , File.dirname(__FILE__)))
11
11
 
12
12
  # The Dia module provides a namespace for all classes and modules Dia needs to operate.
13
13
  module Dia
14
- VERSION = '2.0.2'
14
+ VERSION = '2.1.0'
15
15
  end
@@ -11,6 +11,10 @@ module Dia
11
11
  attr_reader :stdout
12
12
  attr_reader :exception
13
13
 
14
+ attr_writer :redirect_stderr
15
+ attr_writer :redirect_stdout
16
+ attr_writer :rescue_exception
17
+
14
18
  alias_method :e, :exception
15
19
 
16
20
  # @param [String] Profile Accepts one of five profiles which can be found
@@ -34,9 +38,8 @@ module Dia
34
38
  end
35
39
 
36
40
 
37
- # Provides access to the Standard Output stream of the process last used to execute
38
- # your sandbox.
39
- # This feature is disabled by default.
41
+ # Provides access to the Standard Output stream of the process that was last used to execute
42
+ # a sandbox. This feature is disabled by default.
40
43
  #
41
44
  # @return [String] Returns the contents of stdout as a String.
42
45
  #
@@ -59,7 +62,7 @@ module Dia
59
62
  @stdout
60
63
  end
61
64
 
62
- # This method can enable or disable a feature that will capture Standard Output
65
+ # This method can enable or disable a feature that will redirect Standard Output
63
66
  # in the process that is spawned to execute a sandbox.
64
67
  #
65
68
  # @param [true] Enable Passing true will enable the redirection of Standard Output.
@@ -90,9 +93,8 @@ module Dia
90
93
  !!@redirect_stdout
91
94
  end
92
95
 
93
- # Provides access to the Standard Error stream of the process last used to execute
94
- # a sandbox.
95
- # This feature is disabled by default.
96
+ # Provides access to the Standard Error stream of the process that was last used to execute
97
+ # a sandbox. This feature is disabled by default.
96
98
  #
97
99
  # @return [String] Returns the contents of stderr as a String.
98
100
  #
@@ -114,7 +116,7 @@ module Dia
114
116
  @stderr
115
117
  end
116
118
 
117
- # This method can enable or disable a feature that will capture Standard Error output
119
+ # This method can enable or disable a feature that will redirect Standard Error output
118
120
  # in the process that is spawned to execute a sandbox.
119
121
  #
120
122
  # @param [true] Enable Passing true will enable the redirection of Standard Error output.
@@ -145,44 +147,52 @@ module Dia
145
147
  !!@redirect_stderr
146
148
  end
147
149
 
148
- # This method will tell you if an exception has been raised in the process
150
+ # This method will tell you if an exception has been rescued in the process that is
149
151
  # spawned to execute a sandbox.
150
152
  #
151
- # @return [true] Returns true when an exception has been rasied.
153
+ # @return [true] Returns true when an exception has been rescued.
152
154
  #
153
- # @return [false] Returns false when an exception has not been raised.
155
+ # @return [false] Returns false when an exception has not been rescued.
154
156
  #
155
- # @return [false] Returns false if Dia was not set to capture exceptions
157
+ # @return [false] Returns false if Dia was not set to rescue exceptions
156
158
  # before a call to {#run} or {#run_nonblock}.
157
159
  #
158
- # @see #rescue_exception= The capture of exceptions can be enabled or disabled through
160
+ # @see #rescue_exception= The rescue of exceptions can be enabled or disabled through
159
161
  # #rescue_exception=
160
162
  #
161
163
  # @see #exception An exception can be accessed through the #exception
162
164
  # method.
165
+ def exception_rescued?
166
+ !!exception
167
+ end
168
+
169
+ # @return [Boolean]
163
170
  #
164
- #
171
+ # @deprecated This method is deprecated. {Dia::RubyBlock#exception_rescued?}
172
+ # should be used instead.
165
173
  def exception_raised?
174
+ $stderr.puts 'WARNING: Dia::RubyBlock#exception_raised? is deprecated and will be ' \
175
+ 'removed from Dia in a future release.'
166
176
  !!exception
167
177
  end
168
178
 
169
- # This method will tell you if an exception raised in the process spawned to
170
- # spawn a sandbox will be captured/rescued.
171
- #
172
- # @return [true] Returns true when exceptions are being captured.
179
+ # This method will tell you if an exception will be rescued in the process that is
180
+ # spawned to execute a sandbox.
181
+ #
182
+ # @return [true] Returns true when exceptions are being rescued.
173
183
  #
174
184
  # @return [false] Returns false when are exceptions are not being
175
- # captured.
185
+ # rescued.
176
186
  # @since 2.0.0
177
187
  #
178
- # @see #rescue_exception= The capture of exceptions can be enabled or disabled through
188
+ # @see #rescue_exception= The rescue of exceptions can be enabled or disabled through
179
189
  # #rescue_exception=
180
190
  def rescue_exception?
181
191
  !!@rescue
182
192
  end
183
193
 
184
- # This method can enable or disable a feature that will capture/rescue
185
- # exceptions that are raised in the process spawned to execute your sandbox.
194
+ # This method can enable or disable a feature that will try to rescue exceptions
195
+ # that are raised in the process that is spawned to execute a sandbox.
186
196
  #
187
197
  # @param [true] Enable Passing true will enable the capture of exceptions.
188
198
  #
@@ -198,22 +208,22 @@ module Dia
198
208
  end
199
209
 
200
210
 
201
- # Provides access to the data of an exception object raised in the process last used to
202
- # execute a sandbox.
203
- # This feature is disabled by default.
204
- #
211
+ # Provides access to the data of an exception object that has been rescued in the process
212
+ # that was last used to execute a sandbox. This feature is disabled by default.
213
+ #
205
214
  # @return [Dia::ExceptionStruct] Returns an instance of {ExceptionStruct} when an
206
- # exception has been captured.
215
+ # exception has been rescued.
207
216
  #
208
217
  # @return [nil] Returns nil when there is no exception available.
209
218
  #
210
- # @return [nil] Returns nil if Dia was not set to capture exceptions before a
219
+ # @return [nil] Returns nil if Dia was not set to rescue exceptions before a
211
220
  # call to {#run} or {#run_nonblock}.
212
221
  #
213
- # @see #rescue_exception= The capture of exceptions can be enabled or disabled through
222
+ # @see #rescue_exception= The rescue of exceptions can be enabled or disabled through
214
223
  # #rescue_exception=
215
224
  #
216
- # @see Dia::ExceptionStruct Dia::ExceptionStruct.
225
+ # @see Dia::ExceptionStruct Dia::ExceptionStruct
226
+ #
217
227
  # @since 1.5
218
228
  def exception
219
229
  if pipes_readable?(@pipes[:exception_reader], @pipes[:exception_writer])
@@ -226,6 +236,21 @@ module Dia
226
236
  @e
227
237
  end
228
238
 
239
+ # Provides access to the return value of the block that has been supplied to the constructor.
240
+ #
241
+ # @return [String] Returns the result of #inspect on the return value of the block
242
+ # that has been executed.
243
+ #
244
+ # @return [nil] Returns nil if {#run} or {#run_nonblock} has not been called yet.
245
+ def value
246
+ if pipes_readable?(@pipes[:return_reader], @pipes[:return_writer])
247
+ @pipes[:return_writer].close
248
+ @return = @pipes[:return_reader].read
249
+ @pipes[:return_reader].close
250
+ end
251
+ @return
252
+ end
253
+
229
254
  # The run method will spawn a child process and execute the block supplied to the constructor
230
255
  # in a sandbox.
231
256
  # This method will block. See {#run_nonblock} for the non-blocking form of this method.
@@ -233,7 +258,7 @@ module Dia
233
258
  # **Side Effects:**
234
259
  #
235
260
  # * When this method is called, it will reset the instance variables returned by {#exception},
236
- # {#stdout}, and {#stderr} to nil.
261
+ # {#stdout}, {#stderr}, and {#value} to nil.
237
262
  #
238
263
  # @param [Arguments] Arguments A variable amount of arguments that will be passed onto the
239
264
  # the block supplied to the constructor.
@@ -268,7 +293,7 @@ module Dia
268
293
  private
269
294
  # @api private
270
295
  def launch(*args)
271
- @e = @stdout = @stderr = nil
296
+ @e = @stdout = @stderr = @return = nil
272
297
  close_pipes_if_needed
273
298
  open_pipes_if_needed
274
299
 
@@ -278,7 +303,7 @@ module Dia
278
303
  if @rescue
279
304
  begin
280
305
  initialize_sandbox
281
- @proc.call(*args)
306
+ write_return_value(@proc.call(*args))
282
307
  rescue SystemExit, SignalException, NoMemoryError => e
283
308
  raise(e)
284
309
  rescue Exception => e
@@ -296,7 +321,7 @@ module Dia
296
321
  else
297
322
  begin
298
323
  initialize_sandbox
299
- @proc.call(*args)
324
+ write_return_value(@proc.call(*args))
300
325
  ensure
301
326
  write_stdout_and_stderr_if_needed
302
327
  close_pipes_if_needed
@@ -305,6 +330,12 @@ module Dia
305
330
  end
306
331
  end
307
332
 
333
+ def write_return_value(result)
334
+ @pipes[:return_reader].close
335
+ @pipes[:return_writer].write(result.inspect)
336
+ @pipes[:return_writer].close
337
+ end
338
+
308
339
  # @api private
309
340
  def write_stdout_and_stderr_if_needed
310
341
  if @redirect_stdout
@@ -333,6 +364,7 @@ module Dia
333
364
 
334
365
  # @api private
335
366
  def open_pipes_if_needed
367
+ @pipes[:return_reader] , @pipes[:return_writer] = IO.pipe
336
368
  @pipes[:exception_reader], @pipes[:exception_writer] = IO.pipe if @rescue
337
369
  @pipes[:stdout_reader] , @pipes[:stdout_writer] = IO.pipe if @redirect_stdout
338
370
  @pipes[:stderr_reader] , @pipes[:stderr_writer] = IO.pipe if @redirect_stderr
@@ -341,8 +373,8 @@ module Dia
341
373
  # @api private
342
374
  def write_exception(e)
343
375
  @pipes[:exception_writer].write(Marshal.dump({ :klass => e.class.to_s ,
344
- :backtrace => e.backtrace.join("\n"),
345
- :message => e.message.to_s }) )
376
+ :backtrace => e.backtrace.join("\n"),
377
+ :message => e.message.to_s }) )
346
378
  end
347
379
 
348
380
  # @api private
@@ -1,17 +1,17 @@
1
1
  suite('RubyBlock') do
2
- suite('#exception_raised?') do
2
+ suite('#exception_rescued?') do
3
3
  suite('Return values') do
4
4
 
5
- exercise('When an exception has been raised.') do
5
+ exercise('When an exception has been rescued.') do
6
6
  dia = Dia::RubyBlock.new(Dia::Profiles::NO_INTERNET) do
7
7
  raise(StandardError, "raised")
8
8
  end
9
9
  dia.rescue_exception = true
10
10
  dia.run
11
- @result = dia.exception_raised?
11
+ @result = dia.exception_rescued?
12
12
  end
13
13
 
14
- verify('#exception_raised? returns true.') do
14
+ verify('#exception_rescued? returns true.') do
15
15
  @result == true
16
16
  end
17
17
 
@@ -19,10 +19,10 @@ suite('RubyBlock') do
19
19
  dia = Dia::RubyBlock.new(Dia::Profiles::NO_INTERNET) { }
20
20
  dia.rescue_exception = true
21
21
  dia.run
22
- @result = dia.exception_raised?
22
+ @result = dia.exception_rescued?
23
23
  end
24
24
 
25
- verify('#exception_raised? returns false.') do
25
+ verify('#exception_rescued? returns false.') do
26
26
  @result == false
27
27
  end
28
28
 
@@ -33,10 +33,10 @@ suite('RubyBlock') do
33
33
  dia.rescue_exception = false # default, but lets be explicit.
34
34
  dia.redirect_stderr = true # I don't want the tests to be flooded with a backtrace.
35
35
  dia.run
36
- @result = dia.exception_raised?
36
+ @result = dia.exception_rescued?
37
37
  end
38
38
 
39
- verify('#exception_raised? returns false') do
39
+ verify('#exception_rescued? returns false') do
40
40
  @result == false
41
41
  end
42
42
 
@@ -0,0 +1,30 @@
1
+ suite('RubyBlock') do
2
+ suite('#value') do
3
+ suite('Return values') do
4
+
5
+ exercise('When #value has finished executing.') do
6
+ dia = Dia::RubyBlock.new(Dia::Profiles::NO_INTERNET) do
7
+ 1
8
+ end
9
+ dia.run
10
+ @result = dia.value
11
+ end
12
+
13
+ verify('#value returns the return value of the executed block.') do
14
+ @result == "1"
15
+ end
16
+
17
+ exercise('When #run or #run_nonblock has not been called.') do
18
+ dia = Dia::RubyBlock.new(Dia::Profiles::NO_INTERNET) do
19
+ 1
20
+ end
21
+ @result = dia.value
22
+ end
23
+
24
+ verify('#value returns nil.') do
25
+ @result == nil
26
+ end
27
+
28
+ end
29
+ end
30
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 2
7
+ - 1
7
8
  - 0
8
- - 2
9
- version: 2.0.2
9
+ version: 2.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Robert Gleeson
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-31 00:00:00 +01:00
17
+ date: 2010-08-12 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -80,18 +80,20 @@ files:
80
80
  - test/suite/lib/dia/RubyBlock#run_nonblock.rb
81
81
  - test/suite/lib/dia/RubyBlock#stderr.rb
82
82
  - test/suite/lib/dia/RubyBlock#stdout.rb
83
+ - test/suite/lib/dia/RubyBlock#value.rb
83
84
  - test/suite/lib/dia/SharedFeatures#exit_status.rb
84
85
  - test/suite/lib/dia/SharedFeatures#pid.rb
85
86
  - test/suite/lib/dia/SharedFeatures#running?.rb
86
87
  - test/suite/lib/dia/SharedFeatures#terminate.rb
87
88
  - COPYING
89
+ - Changelog
88
90
  - README.mkd
89
91
  - dia.gemspec
90
92
  has_rdoc: yard
91
93
  homepage:
92
94
  licenses: []
93
95
 
94
- post_install_message: " -------------------------------------------------------------------------------- \n Thanks for installing Dia, 2.0.2! \n The >=2.0.0 releases are not backward compatiable with the 1.0 series.\n \n Resources: \n * API Documentation http://yardoc.org/docs/robgleeson-Dia/\n * Github http://github.com/robgleeson/dia \n * Mailing List http://groups.google.com/group/ruby-dia\n\n Release notes are available at the mailing list.\n\n Happy Hacking!\n -------------------------------------------------------------------------------- \n"
96
+ post_install_message: " -------------------------------------------------------------------------------- \n Thanks for installing Dia, 2.1.0! \n The >=2.0.0 releases are not backward compatiable with the 1.0 series.\n \n Resources: \n * API Documentation http://yardoc.org/docs/robgleeson-Dia/\n * Github http://github.com/robgleeson/dia \n * Mailing List http://groups.google.com/group/ruby-dia\n\n Release notes are available at the mailing list.\n\n Happy Hacking!\n -------------------------------------------------------------------------------- \n"
95
97
  rdoc_options: []
96
98
 
97
99
  require_paths:
@@ -133,6 +135,7 @@ test_files:
133
135
  - test/suite/lib/dia/RubyBlock#run_nonblock.rb
134
136
  - test/suite/lib/dia/RubyBlock#stderr.rb
135
137
  - test/suite/lib/dia/RubyBlock#stdout.rb
138
+ - test/suite/lib/dia/RubyBlock#value.rb
136
139
  - test/suite/lib/dia/SharedFeatures#exit_status.rb
137
140
  - test/suite/lib/dia/SharedFeatures#pid.rb
138
141
  - test/suite/lib/dia/SharedFeatures#running?.rb