capture_output_streams 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +104 -0
- data/Rakefile +6 -0
- data/capture_output_streams.gemspec +28 -0
- data/lib/capture_output_streams.rb +51 -0
- data/lib/capture_output_streams/backticks.rb +36 -0
- data/lib/capture_output_streams/system.rb +37 -0
- data/lib/capture_output_streams/version.rb +3 -0
- data/spec/capture_output_streams_spec.rb +25 -0
- data/spec/spec_helper.rb +2 -0
- metadata +142 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Marc Sutter
|
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,104 @@
|
|
1
|
+
# CaptureOutputStreams
|
2
|
+
|
3
|
+
Control output streams of third party gems or libraries.
|
4
|
+
|
5
|
+
There are so many cool ruby gems and libraries out there. What if you want to use one of them in your cli application without having a way to capture or modify the output streams.
|
6
|
+
|
7
|
+
While writing my own cli tool, i wanted to use the great [git-up](https://github.com/aanand/git-up) gem to update my local git branches.
|
8
|
+
|
9
|
+
I also wanted to control and format the console output of my own cli tools, but the author of the git-up gem uses 'puts' and 'system' statement in his code and I did not want to alter his code.
|
10
|
+
|
11
|
+
Here an example with the git-up gem.
|
12
|
+
|
13
|
+
git-up is a command line utility, but I wanted to use it in my code.
|
14
|
+
So, to sync a repo, one would do:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
[1] pry(main)> require 'git-up'
|
18
|
+
=> true
|
19
|
+
|
20
|
+
[2] pry(main)> GitUp.new.run([])
|
21
|
+
Fetching origin
|
22
|
+
master up to date
|
23
|
+
=> nil
|
24
|
+
```
|
25
|
+
|
26
|
+
My repo is synced now, but the method is only returning nil and the outputs are directly pushed to the console.
|
27
|
+
|
28
|
+
So if I want to hide the output from the console, I could do something like:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
[3] pry(main)> require 'capture_output_streams'
|
32
|
+
=> true
|
33
|
+
|
34
|
+
[4] pry(main)> cos = capture_output_streams {GitUp.new.run([])}
|
35
|
+
=> #<struct stdout="Fetching origin\n\e[1mmaster \e[0m\e[32mup to date\e[0m\n", stderr="">
|
36
|
+
|
37
|
+
[5] pry(main)> cos.stdout
|
38
|
+
=> "Fetching origin\n\e[1mmaster \e[0m\e[32mup to date\e[0m\n"
|
39
|
+
|
40
|
+
[6] pry(main)> puts cos.stdout
|
41
|
+
Fetching origin
|
42
|
+
master up to date
|
43
|
+
=> nil
|
44
|
+
```
|
45
|
+
|
46
|
+
## Installation
|
47
|
+
|
48
|
+
Add this line to your application's Gemfile:
|
49
|
+
|
50
|
+
gem 'capture_output_streams'
|
51
|
+
|
52
|
+
And then execute:
|
53
|
+
|
54
|
+
$ bundle
|
55
|
+
|
56
|
+
Or install it yourself as:
|
57
|
+
|
58
|
+
$ gem install capture_output_streams
|
59
|
+
|
60
|
+
## Usage
|
61
|
+
|
62
|
+
Pass your code within a block to capture_output_streams, and you'll get a struct back with the output streams.
|
63
|
+
|
64
|
+
a simple puts statment
|
65
|
+
```ruby
|
66
|
+
[1] pry(main)> require 'capture_output_streams'
|
67
|
+
=> true
|
68
|
+
|
69
|
+
[2] pry(main)> capture_output_streams { puts "hello world" }
|
70
|
+
=> #<struct stdout="hello world\n", stderr="">
|
71
|
+
```
|
72
|
+
|
73
|
+
a more complete example:
|
74
|
+
```ruby
|
75
|
+
[3] pry(main)> cos = capture_output_streams do
|
76
|
+
[3] pry(main)* system('date')
|
77
|
+
[3] pry(main)* puts "--------------------"
|
78
|
+
[3] pry(main)* $stderr.puts "im a failure"
|
79
|
+
[3] pry(main)* end
|
80
|
+
=> #<struct stdout="Fri Nov 14 11:37:23 CET 2014\n--------------------\n", stderr="im a failure\n">
|
81
|
+
|
82
|
+
[4] pry(main)> puts cos.stdout
|
83
|
+
Fri Nov 14 11:37:23 CET 2014
|
84
|
+
--------------------
|
85
|
+
=> nil
|
86
|
+
|
87
|
+
[5] pry(main)> puts cos.stderr
|
88
|
+
im a failure
|
89
|
+
=> nil
|
90
|
+
```
|
91
|
+
|
92
|
+
## Note
|
93
|
+
The kernel system and backticks (``) methods are also dynamically mocked
|
94
|
+
during the block code execution as they don't use $stdout.
|
95
|
+
|
96
|
+
Open4 is used for these mockings.
|
97
|
+
|
98
|
+
## Contributing
|
99
|
+
|
100
|
+
1. Fork it ( http://github.com/msutter/capture_output_streams/fork )
|
101
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
102
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
103
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
104
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'capture_output_streams/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "capture_output_streams"
|
9
|
+
spec.version = CaptureOutputStreams::VERSION
|
10
|
+
spec.authors = ["Marc Sutter"]
|
11
|
+
spec.email = ["marc.sutter@swisscom.com"]
|
12
|
+
spec.summary = %q{control output streams of third party gems or libraries}
|
13
|
+
spec.description = %q{hide or reformat output streams of required third party code}
|
14
|
+
spec.homepage = "https://github.com/msutter/capture_output_streams"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "open4"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "pry"
|
28
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'open4'
|
2
|
+
|
3
|
+
module CaptureOutputStreams
|
4
|
+
|
5
|
+
Kernel.module_eval do
|
6
|
+
|
7
|
+
def capture_output_streams(&block)
|
8
|
+
path = File.dirname(__FILE__)
|
9
|
+
|
10
|
+
# save original kernel methods
|
11
|
+
Kernel.module_eval do
|
12
|
+
alias_method :orig_system, :system
|
13
|
+
alias_method :orig_backticks, :`
|
14
|
+
end
|
15
|
+
|
16
|
+
# save original output streams
|
17
|
+
original_stdout = $stdout
|
18
|
+
original_stderr = $stderr
|
19
|
+
|
20
|
+
# override original kernel methods
|
21
|
+
load File.join(path, 'capture_output_streams/system.rb')
|
22
|
+
load File.join(path, 'capture_output_streams/backticks.rb')
|
23
|
+
|
24
|
+
# override original output streams
|
25
|
+
$stdout = fake_stdout = StringIO.new
|
26
|
+
$stderr = fake_stderr = StringIO.new
|
27
|
+
|
28
|
+
begin
|
29
|
+
yield
|
30
|
+
|
31
|
+
ensure
|
32
|
+
# restore original kernel methods
|
33
|
+
Kernel.module_eval do
|
34
|
+
alias_method :system, :orig_system
|
35
|
+
alias_method :`, :orig_backticks
|
36
|
+
end
|
37
|
+
|
38
|
+
# restore original output streams
|
39
|
+
$stdout = original_stdout
|
40
|
+
$stderr = original_stderr
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
capture = Struct.new :stdout, :stderr
|
45
|
+
capture.new(fake_stdout.string, fake_stderr.string)
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module CaptureOutputStreams
|
2
|
+
|
3
|
+
Kernel.module_eval do
|
4
|
+
|
5
|
+
def `(*args)
|
6
|
+
command = args.join(' ')
|
7
|
+
status = Open4::popen4(command) do |pid, stdin, stdout, stderr|
|
8
|
+
@pid=pid
|
9
|
+
@stdin=command
|
10
|
+
@stdout=""
|
11
|
+
@stderr=""
|
12
|
+
|
13
|
+
while(line=stdout.gets)
|
14
|
+
@stdout+=line
|
15
|
+
end
|
16
|
+
|
17
|
+
while(line=stderr.gets)
|
18
|
+
@stderr+=line
|
19
|
+
end
|
20
|
+
|
21
|
+
unless @stdout.nil?
|
22
|
+
@stdout=@stdout.strip
|
23
|
+
end
|
24
|
+
|
25
|
+
unless @stderr.nil?
|
26
|
+
@stderr=@stderr.strip
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
@status = status.to_i
|
31
|
+
$stderr.puts @stderr unless @stderr.empty?
|
32
|
+
@stdout
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module CaptureOutputStreams
|
2
|
+
|
3
|
+
Kernel.module_eval do
|
4
|
+
|
5
|
+
def system(*args)
|
6
|
+
command = args.join(' ')
|
7
|
+
status = Open4::popen4(command) do |pid, stdin, stdout, stderr|
|
8
|
+
@pid=pid
|
9
|
+
@stdin=command
|
10
|
+
@stdout=""
|
11
|
+
@stderr=""
|
12
|
+
|
13
|
+
while(line=stdout.gets)
|
14
|
+
@stdout+=line
|
15
|
+
$stdout.puts line
|
16
|
+
end
|
17
|
+
|
18
|
+
while(line=stderr.gets)
|
19
|
+
@stderr+=line
|
20
|
+
$stderr.puts line
|
21
|
+
end
|
22
|
+
|
23
|
+
unless @stdout.nil?
|
24
|
+
@stdout=@stdout.strip
|
25
|
+
end
|
26
|
+
|
27
|
+
unless @stderr.nil?
|
28
|
+
@stderr=@stderr.strip
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
@status = status.to_i
|
33
|
+
@status == 0 ? true : false
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CaptureOutputStreams do
|
4
|
+
|
5
|
+
it 'should have a version number' do
|
6
|
+
CaptureOutputStreams::VERSION.should_not be_nil
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '.capture_output_streams' do
|
10
|
+
|
11
|
+
it 'should accept a block' do
|
12
|
+
expect {
|
13
|
+
capture_output_streams{}
|
14
|
+
}.to_not raise_error
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'does not accept arguments' do
|
18
|
+
expect {
|
19
|
+
capture_output_streams(23)
|
20
|
+
}.to raise_error ArgumentError
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: capture_output_streams
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Marc Sutter
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-11-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: open4
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
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: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.5'
|
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: '1.5'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
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: rspec
|
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: pry
|
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
|
+
description: hide or reformat output streams of required third party code
|
95
|
+
email:
|
96
|
+
- marc.sutter@swisscom.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .rspec
|
103
|
+
- .travis.yml
|
104
|
+
- Gemfile
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- capture_output_streams.gemspec
|
109
|
+
- lib/capture_output_streams.rb
|
110
|
+
- lib/capture_output_streams/backticks.rb
|
111
|
+
- lib/capture_output_streams/system.rb
|
112
|
+
- lib/capture_output_streams/version.rb
|
113
|
+
- spec/capture_output_streams_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
homepage: https://github.com/msutter/capture_output_streams
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ! '>='
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 1.8.23
|
137
|
+
signing_key:
|
138
|
+
specification_version: 3
|
139
|
+
summary: control output streams of third party gems or libraries
|
140
|
+
test_files:
|
141
|
+
- spec/capture_output_streams_spec.rb
|
142
|
+
- spec/spec_helper.rb
|