stdiotrap 1.0.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.
- data/.gitignore +17 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +15 -0
- data/lib/stdiotrap.rb +50 -0
- data/lib/stdiotrap/version.rb +3 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/stdiotrap_spec.rb +194 -0
- data/stdiotrap.gemspec +27 -0
- metadata +161 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 moe@busyloop.net
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# StdioTrap [](https://travis-ci.org/busyloop/stdiotrap) [](https://gemnasium.com/busyloop/stdiotrap)
|
2
|
+
|
3
|
+
StdioTrap lets you capture stdout, stderr at runtime,
|
4
|
+
e.g. for inspection inside a unit-test. It can also fake stdin.
|
5
|
+
|
6
|
+
## Example (rspec)
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
require 'stdiotrap'
|
10
|
+
|
11
|
+
describe StdioTrap do
|
12
|
+
|
13
|
+
describe 'capture' do
|
14
|
+
it "captures stdout and stderr" do
|
15
|
+
trapped = StdioTrap.capture {
|
16
|
+
$stdout.puts "Hello world!"
|
17
|
+
$stderr.puts "Hello other world!"
|
18
|
+
}
|
19
|
+
trapped[:stdout].should == "Hello world!\n"
|
20
|
+
trapped[:stderr].should == "Hello other world!\n"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'trap!, release!' do
|
25
|
+
before :each do
|
26
|
+
StdioTrap.trap!
|
27
|
+
end
|
28
|
+
|
29
|
+
after :each do
|
30
|
+
StdioTrap.release!
|
31
|
+
end
|
32
|
+
|
33
|
+
it "captures stdout and stderr" do
|
34
|
+
puts "Hello world!"
|
35
|
+
$stderr.puts "Hello other world!"
|
36
|
+
StdioTrap.stdout.should == "Hello world!\n"
|
37
|
+
StdioTrap.stderr.should == "Hello other world!\n"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
For more usage examples please refer to the [spec suite](https://github.com/busyloop/stdiotrap/blob/master/spec/stdiotrap_spec.rb).
|
45
|
+
|
46
|
+
## Credits
|
47
|
+
|
48
|
+
StdioTrap is based on [this code snippet](http://rails-bestpractices.com/questions/1-test-stdin-stdout-in-rspec) (bottom of page) by [Ng Tze Yang](http://tyenglog.blogspot.de)
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
task :default => :test
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new("test:spec") do |t|
|
8
|
+
t.pattern = 'spec/**/*_spec.rb'
|
9
|
+
#t.rspec_opts = '-b -c -f progress --tag ~benchmark'
|
10
|
+
t.rspec_opts = '--fail-fast -b -c -f documentation --tag ~benchmark'
|
11
|
+
end
|
12
|
+
|
13
|
+
desc 'Run full test suite'
|
14
|
+
#task :test => [ 'test:spec', 'test:coverage' ]
|
15
|
+
task :test => [ 'test:spec' ]
|
data/lib/stdiotrap.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require "stdiotrap/version"
|
2
|
+
|
3
|
+
module StdioTrap
|
4
|
+
require 'stringio'
|
5
|
+
|
6
|
+
$stdio_trap = nil
|
7
|
+
class << self
|
8
|
+
def trap!(stdin='', mode=:trap)
|
9
|
+
raise RuntimeError, "Stdio is already trapped" unless $stdio_trap.nil?
|
10
|
+
$stdio_trap, $o_stdin, $o_stdout, $o_stderr = mode, $stdin, $stdout, $stderr
|
11
|
+
$stdin, $stdout, $stderr = StringIO.new(stdin), StringIO.new, StringIO.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def release!(force=false)
|
15
|
+
raise RuntimeError, "Stdio is not currently trapped" if $stdio_trap.nil?
|
16
|
+
raise RuntimeError, "Can not release inside capture{}" if $stdio_trap == :capture and !force
|
17
|
+
$stdio_trap, $stdin, $stdout, $stderr = nil, $o_stdin, $o_stdout, $o_stderr
|
18
|
+
end
|
19
|
+
|
20
|
+
def status
|
21
|
+
!$stdio_trap.nil?
|
22
|
+
end
|
23
|
+
|
24
|
+
def capture(stdin='')
|
25
|
+
begin
|
26
|
+
require 'stringio'
|
27
|
+
trap!(stdin, :capture)
|
28
|
+
yield
|
29
|
+
{:stdout => $stdout.string, :stderr => $stderr.string}
|
30
|
+
ensure
|
31
|
+
release!(true)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def stdout
|
36
|
+
raise RuntimeError, "Stdio is not currently trapped" if $stdio_trap.nil?
|
37
|
+
$stdout.string
|
38
|
+
end
|
39
|
+
|
40
|
+
def stderr
|
41
|
+
raise RuntimeError, "Stdio is not currently trapped" if $stdio_trap.nil?
|
42
|
+
$stderr.string
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_h
|
46
|
+
raise RuntimeError, "Stdio is not currently trapped" if $stdio_trap.nil?
|
47
|
+
{ :stdout => $stdout.string, :stderr => $stderr.string }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'stdiotrap'
|
4
|
+
|
5
|
+
describe StdioTrap do
|
6
|
+
describe 'trap!' do
|
7
|
+
it "raises RuntimeError when called twice" do
|
8
|
+
lambda {
|
9
|
+
begin
|
10
|
+
StdioTrap.trap!
|
11
|
+
StdioTrap.trap!
|
12
|
+
ensure
|
13
|
+
StdioTrap.release!
|
14
|
+
end
|
15
|
+
}.should raise_error RuntimeError
|
16
|
+
end
|
17
|
+
|
18
|
+
it "raises RuntimeError when called inside capture{}" do
|
19
|
+
lambda {
|
20
|
+
begin
|
21
|
+
StdioTrap.trap!
|
22
|
+
StdioTrap.trap!
|
23
|
+
ensure
|
24
|
+
StdioTrap.release!
|
25
|
+
end
|
26
|
+
}.should raise_error RuntimeError
|
27
|
+
end
|
28
|
+
|
29
|
+
it "can fake stdin" do
|
30
|
+
StdioTrap.trap!('foobar')
|
31
|
+
input = $stdin.read
|
32
|
+
StdioTrap.release!
|
33
|
+
input.should == 'foobar'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'release!' do
|
38
|
+
it "raises RuntimeError when called twice" do
|
39
|
+
lambda {
|
40
|
+
begin
|
41
|
+
StdioTrap.trap!
|
42
|
+
StdioTrap.release!
|
43
|
+
StdioTrap.release!
|
44
|
+
ensure
|
45
|
+
StdioTrap.release!
|
46
|
+
end
|
47
|
+
}.should raise_error RuntimeError
|
48
|
+
end
|
49
|
+
|
50
|
+
it "raises RuntimeError when called before trap!" do
|
51
|
+
lambda {
|
52
|
+
StdioTrap.release!
|
53
|
+
}.should raise_error RuntimeError
|
54
|
+
end
|
55
|
+
|
56
|
+
it "raises RuntimeError when called inside capture{}" do
|
57
|
+
lambda {
|
58
|
+
trapped = StdioTrap.capture {
|
59
|
+
puts "Hello world!"
|
60
|
+
StdioTrap.release!
|
61
|
+
}
|
62
|
+
}.should raise_error RuntimeError
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe 'trap!, release!' do
|
67
|
+
before :each do
|
68
|
+
StdioTrap.trap!
|
69
|
+
end
|
70
|
+
|
71
|
+
after :each do
|
72
|
+
StdioTrap.release!
|
73
|
+
end
|
74
|
+
|
75
|
+
it "captures stdout" do
|
76
|
+
puts "Hello world!"
|
77
|
+
StdioTrap.stdout.should == "Hello world!\n"
|
78
|
+
end
|
79
|
+
|
80
|
+
it "captures stderr" do
|
81
|
+
$stderr.puts "Hello world!"
|
82
|
+
StdioTrap.stderr.should == "Hello world!\n"
|
83
|
+
end
|
84
|
+
|
85
|
+
it "captures stdout and stderr" do
|
86
|
+
puts "Hello world!"
|
87
|
+
$stderr.puts "Hello other world!"
|
88
|
+
StdioTrap.stdout.should == "Hello world!\n"
|
89
|
+
StdioTrap.stderr.should == "Hello other world!\n"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe 'capture' do
|
94
|
+
it "captures stdout" do
|
95
|
+
trapped = StdioTrap.capture {
|
96
|
+
puts "Hello world!"
|
97
|
+
}
|
98
|
+
trapped[:stdout].should == "Hello world!\n"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "captures stderr" do
|
102
|
+
trapped = StdioTrap.capture {
|
103
|
+
$stderr.puts "Hello world!"
|
104
|
+
}
|
105
|
+
trapped[:stderr].should == "Hello world!\n"
|
106
|
+
end
|
107
|
+
|
108
|
+
it "captures stdout and stderr" do
|
109
|
+
trapped = StdioTrap.capture {
|
110
|
+
$stdout.puts "Hello world!"
|
111
|
+
$stderr.puts "Hello other world!"
|
112
|
+
}
|
113
|
+
trapped[:stdout].should == "Hello world!\n"
|
114
|
+
trapped[:stderr].should == "Hello other world!\n"
|
115
|
+
end
|
116
|
+
|
117
|
+
it "can fake stdin" do
|
118
|
+
trapped = StdioTrap.capture('foobar') {
|
119
|
+
input = $stdin.read
|
120
|
+
input.should == 'foobar'
|
121
|
+
}
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe 'status' do
|
126
|
+
it "returns correct status before trap!" do
|
127
|
+
StdioTrap.status.should == false
|
128
|
+
end
|
129
|
+
|
130
|
+
it "returns correct status after trap!" do
|
131
|
+
StdioTrap.trap!
|
132
|
+
StdioTrap.status.should == true
|
133
|
+
StdioTrap.release!
|
134
|
+
end
|
135
|
+
|
136
|
+
it "returns correct status after trap!, release!" do
|
137
|
+
StdioTrap.trap!
|
138
|
+
StdioTrap.release!
|
139
|
+
StdioTrap.status.should == false
|
140
|
+
end
|
141
|
+
|
142
|
+
it "returns correct status inside capture{}" do
|
143
|
+
StdioTrap.capture {
|
144
|
+
StdioTrap.status.should == true
|
145
|
+
}
|
146
|
+
end
|
147
|
+
|
148
|
+
it "returns correct status after capture{}" do
|
149
|
+
StdioTrap.capture {
|
150
|
+
}
|
151
|
+
StdioTrap.status.should == false
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe 'to_h' do
|
156
|
+
it "works inside capture{}" do
|
157
|
+
StdioTrap.capture {
|
158
|
+
puts "Hello world!"
|
159
|
+
$stderr.puts "Hello other world!"
|
160
|
+
state = StdioTrap.to_h
|
161
|
+
state.keys.length.should == 2
|
162
|
+
state[:stdout].should == "Hello world!\n"
|
163
|
+
state[:stderr].should == "Hello other world!\n"
|
164
|
+
}
|
165
|
+
end
|
166
|
+
|
167
|
+
it "works between trap! and release!" do
|
168
|
+
StdioTrap.trap!
|
169
|
+
puts "Hello world!"
|
170
|
+
$stderr.puts "Hello other world!"
|
171
|
+
state = StdioTrap.to_h
|
172
|
+
state.keys.length.should == 2
|
173
|
+
state[:stdout].should == "Hello world!\n"
|
174
|
+
state[:stderr].should == "Hello other world!\n"
|
175
|
+
StdioTrap.release!
|
176
|
+
end
|
177
|
+
|
178
|
+
it "raises RuntimeError when called before trap!" do
|
179
|
+
lambda {
|
180
|
+
StdioTrap.to_h
|
181
|
+
}.should raise_error RuntimeError
|
182
|
+
end
|
183
|
+
|
184
|
+
it "raises RuntimeError when called after release!" do
|
185
|
+
lambda {
|
186
|
+
StdioTrap.trap!
|
187
|
+
puts "Hello world!"
|
188
|
+
StdioTrap.release!
|
189
|
+
StdioTrap.to_h
|
190
|
+
}.should raise_error RuntimeError
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
data/stdiotrap.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'stdiotrap/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "stdiotrap"
|
8
|
+
gem.version = StdioTrap::VERSION
|
9
|
+
gem.authors = ["Moe"]
|
10
|
+
gem.email = ["moe@busyloop.net"]
|
11
|
+
gem.description = %q{Easily capture stdout, stderr at runtime}
|
12
|
+
gem.summary = %q{Easily capture stdout, stderr at runtime}
|
13
|
+
gem.homepage = "https://github.com/busyloop/stdiotrap"
|
14
|
+
gem.has_rdoc = false
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
gem.add_development_dependency 'rspec'
|
23
|
+
gem.add_development_dependency 'rb-inotify'
|
24
|
+
gem.add_development_dependency 'guard'
|
25
|
+
gem.add_development_dependency 'guard-rspec'
|
26
|
+
gem.add_development_dependency 'simplecov'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stdiotrap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Moe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rb-inotify
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: guard
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: guard-rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: simplecov
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: Easily capture stdout, stderr at runtime
|
111
|
+
email:
|
112
|
+
- moe@busyloop.net
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- .gitignore
|
118
|
+
- .travis.yml
|
119
|
+
- Gemfile
|
120
|
+
- Guardfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- lib/stdiotrap.rb
|
125
|
+
- lib/stdiotrap/version.rb
|
126
|
+
- spec/spec_helper.rb
|
127
|
+
- spec/stdiotrap_spec.rb
|
128
|
+
- stdiotrap.gemspec
|
129
|
+
homepage: https://github.com/busyloop/stdiotrap
|
130
|
+
licenses: []
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
none: false
|
137
|
+
requirements:
|
138
|
+
- - ! '>='
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
segments:
|
142
|
+
- 0
|
143
|
+
hash: -2260607833105567468
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ! '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
segments:
|
151
|
+
- 0
|
152
|
+
hash: -2260607833105567468
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 1.8.23
|
156
|
+
signing_key:
|
157
|
+
specification_version: 3
|
158
|
+
summary: Easily capture stdout, stderr at runtime
|
159
|
+
test_files:
|
160
|
+
- spec/spec_helper.rb
|
161
|
+
- spec/stdiotrap_spec.rb
|