ion1-mischacks 0.0.2 → 0.0.3
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/History.txt +6 -0
- data/lib/mischacks.rb +14 -9
- data/mischacks.gemspec +1 -1
- data/spec/mischacks_spec.rb +55 -18
- data/spec/spec_helper.rb +1 -1
- metadata +1 -1
data/History.txt
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
=== 0.0.3 / 2009-06-28
|
2
|
+
|
3
|
+
* Rename checking_exit_status as fork_and_check to make it clear it forks.
|
4
|
+
* Add catching_exit, which is now used by the do_and_exit methods.
|
5
|
+
* Pass a fallthrough exit status to do_and_exit.
|
6
|
+
|
1
7
|
=== 0.0.2 / 2009-06-28
|
2
8
|
|
3
9
|
* Initial release
|
data/lib/mischacks.rb
CHANGED
@@ -14,7 +14,7 @@
|
|
14
14
|
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
15
|
|
16
16
|
module MiscHacks
|
17
|
-
VERSION = '0.0.
|
17
|
+
VERSION = '0.0.3'
|
18
18
|
|
19
19
|
class ChildError < RuntimeError
|
20
20
|
attr_reader :status
|
@@ -25,7 +25,7 @@ module MiscHacks
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
def self.
|
28
|
+
def self.fork_and_check
|
29
29
|
fork do
|
30
30
|
yield
|
31
31
|
end.tap do |pid|
|
@@ -36,27 +36,32 @@ module MiscHacks
|
|
36
36
|
nil
|
37
37
|
end
|
38
38
|
|
39
|
-
def self.
|
40
|
-
|
39
|
+
def self.catching_exit final_proc, fallthrough_status
|
40
|
+
status = fallthrough_status
|
41
41
|
|
42
|
-
status = 1
|
43
42
|
begin
|
44
43
|
yield
|
45
44
|
rescue SystemExit => e
|
46
45
|
status = e.status
|
47
46
|
ensure
|
48
|
-
|
47
|
+
final_proc.call status
|
49
48
|
end
|
49
|
+
|
50
|
+
status
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.do_and_exit status=1, &block
|
54
|
+
catching_exit method(:exit), status, &block
|
50
55
|
end
|
51
56
|
|
52
|
-
def self.do_and_exit! &block
|
53
|
-
|
57
|
+
def self.do_and_exit! status=1, &block
|
58
|
+
catching_exit method(:exit!), status, &block
|
54
59
|
end
|
55
60
|
|
56
61
|
def self.sh cmd, *args
|
57
62
|
env = if args.last.is_a? Hash then args.pop else {} end
|
58
63
|
|
59
|
-
|
64
|
+
fork_and_check do
|
60
65
|
do_and_exit! do
|
61
66
|
begin
|
62
67
|
env.each_pair do |k, v| ENV[k.to_s] = v.to_s end
|
data/mischacks.gemspec
CHANGED
data/spec/mischacks_spec.rb
CHANGED
@@ -22,27 +22,65 @@ mh = MiscHacks
|
|
22
22
|
ce = MiscHacks::ChildError
|
23
23
|
|
24
24
|
describe mh do
|
25
|
-
describe '
|
25
|
+
describe 'fork_and_check' do
|
26
26
|
it 'should raise an error when child fails' do
|
27
|
-
lambda do mh.
|
28
|
-
lambda do mh.
|
27
|
+
lambda do mh.fork_and_check do exit 1 end end.should raise_error ce
|
28
|
+
lambda do mh.fork_and_check do exit! 1 end end.should raise_error ce
|
29
29
|
|
30
|
-
lambda do mh.
|
31
|
-
lambda do mh.
|
30
|
+
lambda do mh.fork_and_check do exit 0 end end.should_not raise_error
|
31
|
+
lambda do mh.fork_and_check do exit! 0 end end.should_not raise_error
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'should handle exec' do
|
35
|
-
lambda do mh.
|
36
|
-
lambda do mh.
|
35
|
+
lambda do mh.fork_and_check do exec 'false' end end.should raise_error ce
|
36
|
+
lambda do mh.fork_and_check do exec 'true' end end.should_not raise_error
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'catching_exit' do
|
41
|
+
it 'should call final_proc with the fallthrough status when the block does not exit' do
|
42
|
+
[0, 1, 42, 255].each do |i|
|
43
|
+
foo = nil
|
44
|
+
mh.catching_exit(lambda {|status| foo = status }, 2) do end
|
45
|
+
foo.should == 2
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should call final_proc with the exit status when the block exits' do
|
51
|
+
[0, 1, 42, 255].each do |i|
|
52
|
+
foo = nil
|
53
|
+
mh.catching_exit(lambda {|status| foo = status }, 2) do exit i end
|
54
|
+
foo.should == i
|
55
|
+
end
|
37
56
|
end
|
38
57
|
end
|
39
58
|
|
40
59
|
describe 'do_and_exit' do
|
41
|
-
it 'should have
|
60
|
+
it 'should have the proper exit status when the block does not exit' do
|
42
61
|
lambda do
|
43
62
|
mh.do_and_exit do end
|
44
63
|
exit 2 # Should never reach this.
|
45
64
|
end.should exit_with 1
|
65
|
+
|
66
|
+
lambda do
|
67
|
+
mh.do_and_exit! do end
|
68
|
+
exit! 2 # Should never reach this.
|
69
|
+
end.should exit_with 1
|
70
|
+
|
71
|
+
[0, 1, 42, 255].each do |i|
|
72
|
+
lambda do
|
73
|
+
mh.do_and_exit i do end
|
74
|
+
exit 2 # Should never reach this.
|
75
|
+
end.should exit_with i
|
76
|
+
end
|
77
|
+
|
78
|
+
[0, 1, 42, 255].each do |i|
|
79
|
+
lambda do
|
80
|
+
mh.do_and_exit! i do end
|
81
|
+
exit! 2 # Should never reach this.
|
82
|
+
end.should exit_with i
|
83
|
+
end
|
46
84
|
end
|
47
85
|
|
48
86
|
it 'should have the proper exit status when the block exits' do
|
@@ -52,13 +90,20 @@ describe mh do
|
|
52
90
|
exit 2 # Should never reach this.
|
53
91
|
end.should exit_with i
|
54
92
|
end
|
93
|
+
|
94
|
+
[0, 1, 42, 255].each do |i|
|
95
|
+
lambda do
|
96
|
+
mh.do_and_exit! do exit! i end
|
97
|
+
exit! 2 # Should never reach this.
|
98
|
+
end.should exit_with i
|
99
|
+
end
|
55
100
|
end
|
56
101
|
|
57
102
|
it 'should handle exec' do
|
58
103
|
[0, 1, 42, 255].each do |i|
|
59
104
|
lambda do
|
60
|
-
mh.do_and_exit do exec *%W{sh -c #{'exit "$1"'} sh #{i}} end
|
61
|
-
exit 2 # Should never reach this.
|
105
|
+
mh.do_and_exit! do exec *%W{sh -c #{'exit "$1"'} sh #{i}} end
|
106
|
+
exit! 2 # Should never reach this.
|
62
107
|
end.should exit_with i
|
63
108
|
end
|
64
109
|
end
|
@@ -72,14 +117,6 @@ describe mh do
|
|
72
117
|
end
|
73
118
|
end.should exit_with 2
|
74
119
|
|
75
|
-
lambda do
|
76
|
-
begin
|
77
|
-
mh.do_and_exit true do end
|
78
|
-
rescue SystemExit => e
|
79
|
-
exit 2 # Should never reach this.
|
80
|
-
end
|
81
|
-
end.should exit_with 1
|
82
|
-
|
83
120
|
lambda do
|
84
121
|
begin
|
85
122
|
mh.do_and_exit! do end
|
data/spec/spec_helper.rb
CHANGED