capture_io 0.1.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/.autotest +23 -0
- data/.gemtest +0 -0
- data/History.txt +6 -0
- data/Manifest.txt +8 -0
- data/README.txt +54 -0
- data/Rakefile +12 -0
- data/bin/capture_io +3 -0
- data/lib/capture_io.rb +75 -0
- data/test/test_capture_io.rb +119 -0
- metadata +98 -0
data/.autotest
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'autotest/restart'
|
4
|
+
|
5
|
+
# Autotest.add_hook :initialize do |at|
|
6
|
+
# at.extra_files << "../some/external/dependency.rb"
|
7
|
+
#
|
8
|
+
# at.libs << ":../some/external"
|
9
|
+
#
|
10
|
+
# at.add_exception 'vendor'
|
11
|
+
#
|
12
|
+
# at.add_mapping(/dependency.rb/) do |f, _|
|
13
|
+
# at.files_matching(/test_.*rb$/)
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# %w(TestA TestB).each do |klass|
|
17
|
+
# at.extra_class_map[klass] = "test/test_misc.rb"
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
|
21
|
+
# Autotest.add_hook :run_command do |at|
|
22
|
+
# system "rake build"
|
23
|
+
# end
|
data/.gemtest
ADDED
File without changes
|
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
= capture_io
|
2
|
+
|
3
|
+
* http://cyberconnect.biz/opensource
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Capture STDOUT given a start/stop interface
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Often times one needs the ability to capture standard output around
|
12
|
+
a section of code. And there are times required when this needs
|
13
|
+
to be done transparently without storing the output in variables
|
14
|
+
in the code requiring logging.
|
15
|
+
|
16
|
+
== SYNOPSIS:
|
17
|
+
|
18
|
+
capture_io = CaptureIO.new
|
19
|
+
capture_io.start
|
20
|
+
puts "some stdout should be captured here"
|
21
|
+
out = capture_io.stop
|
22
|
+
|
23
|
+
== REQUIREMENTS:
|
24
|
+
|
25
|
+
* platform_helpers
|
26
|
+
|
27
|
+
== INSTALL:
|
28
|
+
|
29
|
+
* gem install capture_io
|
30
|
+
|
31
|
+
== LICENSE:
|
32
|
+
|
33
|
+
(The MIT License)
|
34
|
+
|
35
|
+
Copyright (c) 2011
|
36
|
+
|
37
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
38
|
+
a copy of this software and associated documentation files (the
|
39
|
+
'Software'), to deal in the Software without restriction, including
|
40
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
41
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
42
|
+
permit persons to whom the Software is furnished to do so, subject to
|
43
|
+
the following conditions:
|
44
|
+
|
45
|
+
The above copyright notice and this permission notice shall be
|
46
|
+
included in all copies or substantial portions of the Software.
|
47
|
+
|
48
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
49
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
50
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
51
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
52
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
53
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
54
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/bin/capture_io
ADDED
data/lib/capture_io.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# TODO add support for stderr
|
2
|
+
require 'fileutils'
|
3
|
+
require 'platform_helpers'
|
4
|
+
class CaptureIO
|
5
|
+
VERSION = '0.1.0'
|
6
|
+
|
7
|
+
attr_reader :stdout_file, :stderr_file
|
8
|
+
def initialize
|
9
|
+
unless File.directory?('io_redirect')
|
10
|
+
FileUtils.mkdir 'io_redirect'
|
11
|
+
end
|
12
|
+
|
13
|
+
base = "#{__FILE__}".gsub("#{File.dirname(__FILE__)}/", "")
|
14
|
+
|
15
|
+
@stdout_file = "io_redirect/#{Time.to_md5}_#{base}_stdout"
|
16
|
+
@stderr_file = "io_redirect/#{Time.to_md5}_#{base}_stderr"
|
17
|
+
@stdout_fd = File.open(@stdout_file, 'w+')
|
18
|
+
@stderr_fd = File.open(@stderr_file, 'w+')
|
19
|
+
end
|
20
|
+
|
21
|
+
def start
|
22
|
+
begin
|
23
|
+
@orig_stdout = $stdout
|
24
|
+
@orig_stderr = $stderr
|
25
|
+
|
26
|
+
@new_stdout = IO.new(@stdout_fd.fileno, 'w+')
|
27
|
+
@new_stderr = IO.new(@stderr_fd.fileno, 'w+')
|
28
|
+
|
29
|
+
$stdout = @new_stdout
|
30
|
+
$stderr = @new_stderr
|
31
|
+
rescue => e
|
32
|
+
p e
|
33
|
+
end
|
34
|
+
end
|
35
|
+
def stop(delete_tmp=true)
|
36
|
+
begin
|
37
|
+
@new_stdout.flush
|
38
|
+
@new_stderr.flush
|
39
|
+
|
40
|
+
$stdout = @orig_stdout
|
41
|
+
$stderr = @orig_stderr
|
42
|
+
|
43
|
+
stdout_content = File.readlines(@stdout_file)
|
44
|
+
stderr_content = File.readlines(@stderr_file)
|
45
|
+
|
46
|
+
rescue => e
|
47
|
+
p e
|
48
|
+
ensure
|
49
|
+
@new_stdout.close
|
50
|
+
@new_stderr.close
|
51
|
+
FileUtils.rm_rf 'io_redirect' if delete_tmp
|
52
|
+
end
|
53
|
+
|
54
|
+
{:stdout => stdout_content, :stderr => stderr_content }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module WrapIO
|
59
|
+
def self.auto(to=30, &blk)
|
60
|
+
raise ArgumentError, "No Block Given" unless block_given?
|
61
|
+
c = CaptureIO.new
|
62
|
+
c.start
|
63
|
+
|
64
|
+
results = blk.call
|
65
|
+
|
66
|
+
timeout(to) {
|
67
|
+
while results.kind_of?(Proc)
|
68
|
+
results = results.call
|
69
|
+
end
|
70
|
+
}
|
71
|
+
|
72
|
+
out = c.stop
|
73
|
+
out
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
base = File.expand_path(File.dirname(__FILE__))
|
2
|
+
require 'platform_helpers'
|
3
|
+
require 'fileutils'
|
4
|
+
require "#{base}/../lib/capture_io"
|
5
|
+
require 'timeout'
|
6
|
+
require 'minitest/unit'
|
7
|
+
|
8
|
+
# NOTE: There are times when something might fail and if the
|
9
|
+
# 'io_redirect' directory has been removed by CaptureIO.stop it
|
10
|
+
# will be impossible to know what went wrong as the error may be in
|
11
|
+
# the re-directed out. For debug purposes set 'delete_tmp=false'
|
12
|
+
|
13
|
+
MiniTest::Unit.autorun
|
14
|
+
|
15
|
+
class TestCaptureIO < MiniTest::Unit::TestCase
|
16
|
+
|
17
|
+
def setup
|
18
|
+
@capture_io = CaptureIO.new
|
19
|
+
@capture_io.start
|
20
|
+
end
|
21
|
+
|
22
|
+
def teardown
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_puts
|
26
|
+
|
27
|
+
expected = 'testing puts'
|
28
|
+
puts "<TEST_PUTS>#{expected}</TEST_PUTS>"
|
29
|
+
|
30
|
+
# stop capture so we see results
|
31
|
+
stdout = @capture_io.stop(delete_tmp=true)[:stdout]
|
32
|
+
assert_equal expected, stdout.to_s.scan(/<TEST_PUTS>(.*)<\/TEST_PUTS>/m).pp
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_p
|
36
|
+
expected = 'testing p'
|
37
|
+
p "<TEST_P>#{expected}</TEST_P>"
|
38
|
+
|
39
|
+
# stop capture so we see results
|
40
|
+
stdout = @capture_io.stop(delete_tmp=true)[:stdout]
|
41
|
+
assert_equal expected, stdout.to_s.scan(/<TEST_P>(.*)<\/TEST_P>/m).pp
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_print
|
46
|
+
expected = 'testing print'
|
47
|
+
puts "<TEST_PRINT>#{expected}</TEST_PRINT>"
|
48
|
+
|
49
|
+
# stop capture so we see results
|
50
|
+
stdout = @capture_io.stop(delete_tmp=true)[:stdout]
|
51
|
+
assert_equal expected, stdout.to_s.scan(/<TEST_PRINT>(.*)<\/TEST_PRINT>/m).pp
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_auto_block
|
56
|
+
# stop instance created during setup
|
57
|
+
@capture_io.stop
|
58
|
+
|
59
|
+
# test auto
|
60
|
+
out = WrapIO.auto { puts 'in blk with puts' ; p 'in blk with p'}
|
61
|
+
puts out.inspect
|
62
|
+
|
63
|
+
assert out[:stdout].grep(/in blk with puts/m).nitems == 1 , "Could not find IO.puts"
|
64
|
+
|
65
|
+
# account for both p and puts in the grep..KISS (keeping it simple)
|
66
|
+
assert out[:stdout].grep(/in blk with p/m).nitems == 2, "Could not find IO.p"
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_auto_block_with_loaded_code
|
70
|
+
|
71
|
+
# here we want to ensure that vars set are seen by block
|
72
|
+
$global_= '$global test'
|
73
|
+
|
74
|
+
# stop instance created during setup
|
75
|
+
@capture_io.stop
|
76
|
+
FileUtils.rm_rf 'io_redirect'
|
77
|
+
|
78
|
+
code =<<-EOF
|
79
|
+
base = File.expand_path(File.dirname(__FILE__))
|
80
|
+
require 'platform_helpers'
|
81
|
+
require 'capture_io'
|
82
|
+
c = CaptureIO.new
|
83
|
+
c.start
|
84
|
+
p 'in blk with p'
|
85
|
+
p "with global: \#{$global_}"
|
86
|
+
$new_var=Time.now
|
87
|
+
$captured_out = c.stop(delete_tmp=true)
|
88
|
+
EOF
|
89
|
+
|
90
|
+
proc = Proc.new do
|
91
|
+
eval code
|
92
|
+
end
|
93
|
+
|
94
|
+
proc.call
|
95
|
+
|
96
|
+
assert_equal $captured_out[:stdout].grep(/in blk with p/m).nitems, 1, "could not find IO.p"
|
97
|
+
assert_equal $captured_out[:stdout].grep(/with global: \$global test/m).nitems, 1, "could not find $global"
|
98
|
+
|
99
|
+
# test that the var set inside of the code exec by proc is available here
|
100
|
+
assert $new_var.kind_of?(Time)
|
101
|
+
end
|
102
|
+
|
103
|
+
# FIXME: update capture_io.rb to handle STDERR
|
104
|
+
def ntest_stderr
|
105
|
+
expected = 'testing puts'
|
106
|
+
$stderr.puts "<TEST_P>#{expected}</TEST_P>"
|
107
|
+
# stop capture so we see results
|
108
|
+
stderr = @capture_io.stop(delete_tmp=false)[:stderr]
|
109
|
+
assert_equal expected, stderr.to_s.scan(/<TEST_P>(.*)<\/TEST_P>/m).to_s
|
110
|
+
end
|
111
|
+
|
112
|
+
def ntest_std_error_auto_block
|
113
|
+
raise StandardError, "Testing redefine of stderr"
|
114
|
+
|
115
|
+
@capture_io.stop(delete_tmp=true)
|
116
|
+
assert false, "TODO add test with supporting code in CaptureIO"
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: capture_io
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Cliff Cyphers
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-10-02 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: platform_helpers
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: minitest
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: hoe
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "2.12"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
description: Capture STDOUT given a start/stop interface
|
49
|
+
email:
|
50
|
+
- cliff.cyphers@gmail.com
|
51
|
+
executables:
|
52
|
+
- capture_io
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files:
|
56
|
+
- History.txt
|
57
|
+
- Manifest.txt
|
58
|
+
- README.txt
|
59
|
+
files:
|
60
|
+
- .autotest
|
61
|
+
- History.txt
|
62
|
+
- Manifest.txt
|
63
|
+
- README.txt
|
64
|
+
- Rakefile
|
65
|
+
- bin/capture_io
|
66
|
+
- lib/capture_io.rb
|
67
|
+
- test/test_capture_io.rb
|
68
|
+
- .gemtest
|
69
|
+
homepage: http://cyberconnect.biz/opensource
|
70
|
+
licenses: []
|
71
|
+
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options:
|
74
|
+
- --main
|
75
|
+
- README.txt
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project: capture_io
|
93
|
+
rubygems_version: 1.8.10
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Capture STDOUT given a start/stop interface
|
97
|
+
test_files:
|
98
|
+
- test/test_capture_io.rb
|