Skip to content

Commit

Permalink
Add support for other particular arrangements under rubyw.exe, but pr…
Browse files Browse the repository at this point in the history
…ovide a relatively accurate stdio_danger?
  • Loading branch information
raggi committed Jan 19, 2009
1 parent 3f6d31e commit 98f61e6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/rubyw_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class RubywHelper

Version = VERSION = '0.1.4'
Version = VERSION = '0.1.5'
def self.version; Version; end

app_name = File.basename($0)
Expand All @@ -28,8 +28,26 @@ def initialize(out = nil, err = nil, inn = nil)
@old_out, @old_err, @old_in = $stdout, $stderr, $stdin
end

# Returns true for the two common cases when you would not receive data from
# stdout, stderr and stdin, because they're nulled out in some way, closed,
# or in some other way unusable. This specific implementation provides
# checks for rubyw.exe behavior, where all IOs are closed, and Win32::Daemon
# behavior, where they're all nulled.
# Sometimes may not be accurate, recommendation is to redirect by
# configuration, and use this as a guide only where appropriate.
def stdio_danger?
$stdout.closed? && $stderr.closed? && $stdin.closed?
# rubyw.exe running as a user:
$stdout.closed? && $stderr.closed? && $stdin.closed? ||
# rubyw.exe + Win32::Daemon started:
[$stdout, $stderr, $stdin].all? { |io| io.inspect =~ /NUL/ } ||
# rubyw.exe running as SYSTEM, pre Win32::Daemon started:
begin
open("CONIN$") {}
open("CONOUT$", "w") {}
false
rescue SystemCallError
true
end
end

# Takes a block, because under these conditions, it really helps developers
Expand Down Expand Up @@ -69,7 +87,10 @@ def ensure_files!
fatal! "Cannot read from #{@stdin}" unless File.readable? @stdin
[@stdout, @stderr].each do |f|
dir = File.dirname(f)
safely { FileUtils.mkdir_p(dir) unless File.directory?(dir) }
safely do
FileUtils.mkdir_p(dir) unless File.directory?(dir)
open(f, 'w') {} unless File.exists?(f)
end
next if File.writable? f
fatal! "Cannot write to #{f}"
end
Expand Down
12 changes: 12 additions & 0 deletions spec/spec_rubyw_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ def temp_io
$stdout, $stderr, $stdin = STDOUT, STDERR, STDIN
end

should "have stdio_danger? when stdout, stderr, and stdin are all nulled" do
$stdout, $stderr, $stdin = Array.new(3) do
s = StringIO.new('')
def s.inspect
'#<IO:NUL>'
end
s
end
RubywHelper.new.stdio_danger?.should.eql true
$stdout, $stderr, $stdin = STDOUT, STDERR, STDIN
end

should "restore the old stdios" do
oout, oerr, oin = $stdout, $stderr, $stdin
h = RubywHelper.new
Expand Down

0 comments on commit 98f61e6

Please sign in to comment.