1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
require 'mspec/guards/feature'
class IOStub
def initialize
@buffer = []
@output = ''
end
def write(*str)
self << str.join
end
def << str
@buffer << str
self
end
def print(*str)
write(str.join + $\.to_s)
end
def method_missing(name, *args, &block)
to_s.send(name, *args, &block)
end
def == other
to_s == other
end
def =~ other
to_s =~ other
end
def puts(*str)
if str.empty?
write "\n"
else
write(str.collect { |s| s.to_s.chomp }.concat([nil]).join("\n"))
end
end
def printf(format, *args)
self << sprintf(format, *args)
end
def flush
@output += @buffer.join('')
@buffer.clear
self
end
def to_s
flush
@output
end
alias_method :to_str, :to_s
def inspect
to_s.inspect
end
end
# Creates a "bare" file descriptor (i.e. one that is not associated
# with any Ruby object). The file descriptor can safely be passed
# to IO.new without creating a Ruby object alias to the fd.
def new_fd(name, mode="w:utf-8")
mode = options_or_mode(mode)
if mode.kind_of? Hash
if mode.key? :mode
mode = mode[:mode]
else
raise ArgumentError, "new_fd options Hash must include :mode"
end
end
IO.sysopen name, fmode(mode)
end
# Creates an IO instance for a temporary file name. The file
# must be deleted.
def new_io(name, mode="w:utf-8")
IO.new new_fd(name, options_or_mode(mode)), options_or_mode(mode)
end
def find_unused_fd
Dir.entries("/dev/fd").map(&:to_i).max + 1
end
# This helper simplifies passing file access modes regardless of
# whether the :encoding feature is enabled. Only the access specifier
# itself will be returned if :encoding is not enabled. Otherwise,
# the full mode string will be returned (i.e. the helper is a no-op).
def fmode(mode)
if FeatureGuard.enabled? :encoding
mode
else
mode.split(':').first
end
end
# This helper simplifies passing file access modes or options regardless of
# whether the :encoding feature is enabled. Only the access specifier itself
# will be returned if :encoding is not enabled. Otherwise, the full mode
# string or option will be returned (i.e. the helper is a no-op).
def options_or_mode(oom)
return fmode(oom) if oom.kind_of? String
if FeatureGuard.enabled? :encoding
oom
else
fmode(oom[:mode] || "r:utf-8")
end
end
|