coulis 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/coulis.rb +133 -130
  2. data/test/coulis_test.rb +157 -151
  3. metadata +3 -3
data/lib/coulis.rb CHANGED
@@ -1,136 +1,139 @@
1
1
  require "timeout"
2
2
 
3
3
  class Coulis
4
- class << self
5
- attr_accessor :args, :_definitions, :_bin, :timeout
6
-
7
- def exec(*args, &block)
8
- self.new.exec *args, &block
9
- end
10
-
11
- def options(&block)
12
- self.new(&block)
13
- end
14
-
15
- def bin(p)
16
- @_bin = p.to_s
17
- end
18
-
19
- def _timeout(t)
20
- @timeout = t
21
- end
22
-
23
- def adef(name, option=nil, &block)
24
- (@_definitions||={})[name.to_sym] = (option || Proc.new { self.instance_eval(&block).flatten })
25
- end
26
-
27
- def method_missing(m, args=nil)
28
- m = m.to_s.gsub("=", "")
29
- @args ||= []
30
- definition = @_definitions[m.to_sym] rescue nil
31
-
32
- #puts "m: #{m}, def: #{definition.inspect} | args:#{args}"
33
- if definition.is_a?(Proc)
34
- definition.call
35
- else
36
- if args.to_s.empty?
37
- @args << [ definition || "#{"-" if m[0..0] != "-"}#{m}" ]
38
- else
39
- q = ""
40
- q = "'" if args.to_s[0..0] != "'"
41
- @args << [ definition || "#{"-" if m[0..0] != "-"}#{m}" , "#{q}#{args}#{q}" ]
42
- end
43
- end
44
- end
45
- end
46
-
47
- attr_accessor :args
48
-
49
- def initialize(&block)
50
- self.class.instance_eval(&block) if block_given?
51
- @args = self.class.args
52
- self.class.args = []
53
- self
54
- end
55
-
56
- def options(&block)
57
- self.class.args = @args
58
- self.class.new(&block)
59
- end
60
-
61
- def remove(*args)
62
- new_args = []
63
- args.each do |a|
64
- @args.select {|b| b[0] == (self.class._definitions[a] || "-#{a}")}.each do |b|
65
- @args.delete(b)
66
- end
67
- end
68
- self.class.args = @args
69
- self
70
- end
71
-
72
- def reset
73
- @args = []
74
- self.class.args = []
75
- end
76
-
77
- def build_args
78
- return if @args.nil? or @args.empty?
79
- @args.flatten.join(" ")
80
- end
81
-
82
- def command
83
- "#{self.class._bin || self.class.to_s.downcase} #{build_args}".strip
84
- end
85
-
86
- def fire_command(&block)
87
- puts command + " (timeout: #{self.class.timeout || -1}) + #{block_given?}" if $DEBUG
88
- res = "" unless block_given?
89
- IO.popen(command + " 3>&2 2>&1") do |pipe|
90
- pipe.each("\r") do |line|
91
- if block_given?
92
- yield parse_output(line)
4
+ class << self
5
+ attr_accessor :args, :_definitions, :_bin, :timeout
6
+
7
+ def exec(*args, &block)
8
+ self.new.exec *args, &block
9
+ end
10
+
11
+ def options(&block)
12
+ self.new(&block)
13
+ end
14
+
15
+ def bin(p)
16
+ @_bin = p.to_s
17
+ end
18
+
19
+ def _timeout(t)
20
+ @timeout = t
21
+ end
22
+
23
+ def adef(name, option=nil, &block)
24
+ (@_definitions||={})[name.to_sym] = (option || Proc.new { self.instance_eval(&block).flatten })
25
+ end
26
+
27
+ def method_missing(m, args=nil)
28
+ m = m.to_s.gsub("=", "")
29
+ @args ||= []
30
+ definition = @_definitions[m.to_sym] rescue nil
31
+
32
+ #puts "m: #{m}, def: #{definition.inspect} | args:#{args}"
33
+ if definition.is_a?(Proc)
34
+ definition.call
35
+ else
36
+ arg_name = "#{"-" if m[0..0] != "-"}#{m}"
37
+ arg_name = "-" + arg_name if arg_name.size > 2
38
+
39
+ if args.to_s.empty?
40
+ @args << [ definition || arg_name ]
93
41
  else
94
- res << line
42
+ q = ""
43
+ q = "'" if args.to_s[0..0] != "'"
44
+ @args << [ definition || arg_name , "#{q}#{args}#{q}" ]
95
45
  end
96
46
  end
97
- end
98
- return (block_given? ? $? : parse_output(res))
99
- end
100
-
101
- def parse_output(output)
102
- output
103
- end
104
-
105
- def method_missing(m, args=nil)
106
- self.class.args = @args
107
- self.class.method_missing(m, args)
108
- @args = self.class.args
109
- self
110
- end
111
-
112
- def inspect
113
- "#<#{self.class.to_s} command=#{command} timeout=#{self.class.timeout || -1}>"
114
- end
115
-
116
- def exec(*args, &block)
117
- if args.size > 0
118
- i = 0
119
- (args.size / 2).times do
120
- self.class.send(args[i], args[i+1])
121
- i+=2
122
- end
123
-
124
- @args = self.class.args
125
- self.class.args = []
126
- end
127
-
128
- if self.class.timeout
129
- Timeout::timeout(self.class.timeout) do
130
- fire_command(&block)
131
- end
132
- else
133
- fire_command(&block)
134
- end
135
- end
136
- end
47
+ end
48
+ end
49
+
50
+ attr_accessor :args
51
+
52
+ def initialize(&block)
53
+ self.class.instance_eval(&block) if block_given?
54
+ @args = self.class.args
55
+ self.class.args = []
56
+ self
57
+ end
58
+
59
+ def options(&block)
60
+ self.class.args = @args
61
+ self.class.new(&block)
62
+ end
63
+
64
+ def remove(*args)
65
+ new_args = []
66
+ args.each do |a|
67
+ @args.select {|b| b[0] == (self.class._definitions[a] || "-#{a}")}.each do |b|
68
+ @args.delete(b)
69
+ end
70
+ end
71
+ self.class.args = @args
72
+ self
73
+ end
74
+
75
+ def reset
76
+ @args = []
77
+ self.class.args = []
78
+ end
79
+
80
+ def build_args
81
+ return if @args.nil? or @args.empty?
82
+ @args.flatten.join(" ")
83
+ end
84
+
85
+ def command
86
+ "#{self.class._bin || self.class.to_s.downcase} #{build_args}".strip
87
+ end
88
+
89
+ def fire_command(&block)
90
+ puts command + " (timeout: #{self.class.timeout || -1}) + #{block_given?}" if $DEBUG
91
+ res = "" unless block_given?
92
+ IO.popen(command + " 3>&2 2>&1") do |pipe|
93
+ pipe.each("\r") do |line|
94
+ if block_given?
95
+ yield parse_output(line)
96
+ else
97
+ res << line
98
+ end
99
+ end
100
+ end
101
+ return (block_given? ? $? : parse_output(res))
102
+ end
103
+
104
+ def parse_output(output)
105
+ output
106
+ end
107
+
108
+ def method_missing(m, args=nil)
109
+ self.class.args = @args
110
+ self.class.method_missing(m, args)
111
+ @args = self.class.args
112
+ self
113
+ end
114
+
115
+ def inspect
116
+ "#<#{self.class.to_s} command=#{command} timeout=#{self.class.timeout || -1}>"
117
+ end
118
+
119
+ def exec(*args, &block)
120
+ if args.size > 0
121
+ i = 0
122
+ (args.size / 2).times do
123
+ self.class.send(args[i], args[i+1])
124
+ i+=2
125
+ end
126
+
127
+ @args = self.class.args
128
+ self.class.args = []
129
+ end
130
+
131
+ if self.class.timeout
132
+ Timeout::timeout(self.class.timeout) do
133
+ fire_command(&block)
134
+ end
135
+ else
136
+ fire_command(&block)
137
+ end
138
+ end
139
+ end
data/test/coulis_test.rb CHANGED
@@ -2,164 +2,170 @@ require "test/unit"
2
2
  require "coulis"
3
3
 
4
4
  class Ls < Coulis
5
- adef :all, "-a"
6
- adef :human, "-h"
5
+ adef :all, "-a"
6
+ adef :human, "-h"
7
7
 
8
- adef :full do
9
- all; human
10
- end
8
+ adef :full do
9
+ all; human
10
+ end
11
11
  end
12
12
 
13
13
  class Ping < Coulis
14
- bin `whereis ping`.strip
15
- adef :count, "-c"
14
+ bin `whereis ping`.strip
15
+ adef :count, "-c"
16
16
  end
17
17
 
18
18
  class NSLookup < Coulis
19
- def parse_output(output)
20
- output.split("\n").
21
- map{|x| x.match(/Address: ([0-9\.]+)/)[1] rescue nil}.
22
- compact
23
- end
19
+ def parse_output(output)
20
+ output.split("\n").
21
+ map{|x| x.match(/Address: ([0-9\.]+)/)[1] rescue nil}.
22
+ compact
23
+ end
24
24
  end
25
25
 
26
26
  class SimpleCliTest < Test::Unit::TestCase
27
- def teardown
28
- Ls.new.reset
29
- Ping.new.reset
30
- end
31
-
32
- def test_default_bin
33
- assert_equal Ls.new.command, "ls"
34
- end
35
-
36
- def test_defined_bin
37
- assert_equal Ping.new.command, `whereis ping`.strip
38
- assert_equal Ping.new.command, Ping._bin
39
- end
40
-
41
- def test_adef
42
- assert_equal Ls._definitions[:all], "-a"
43
- assert_equal Ls._definitions[:human], "-h"
44
- end
45
-
46
- def test_adef_with_block
47
- assert_instance_of Proc, Ls._definitions[:full]
48
- assert_equal Ls._definitions[:full].call, ["-a", "-h"]
49
- end
50
-
51
- def test_argument_added
52
- ls = Ls.options { all }
53
- assert true, ls.args[0] == "-a"
54
- end
55
-
56
- def test_all_arguments_from_adef_added
57
- ls = Ls.options { full }
58
- assert_equal ls.args.size, 2
59
- end
60
-
61
- def test_not_defined_argument
62
- ls = Ls.options { s }
63
- assert_equal ls.args.size, 1
64
- assert_equal ls.args.to_s, "-s"
65
- end
66
-
67
- def test_command
68
- assert_equal Ls.options { full; s }.command, "ls -a -h -s"
69
- end
70
-
71
- def test_add_options
72
- ls = Ls.options { a }
73
- assert_equal ls.args.flatten.size, 1
74
- ls.options { l; h }
75
- assert_equal ls.args.flatten.size, 3
76
-
77
- assert_equal ls.command, "ls -a -l -h"
78
- end
79
-
80
- def test_add_option
81
- ls = Ls.new
82
- ls.all
83
- assert_equal ls.args.flatten.size, 1
84
- assert_equal ls.command, "ls -a"
85
-
86
- ls.l
87
- assert_equal ls.args.flatten.size, 2
88
- assert_equal ls.command, "ls -a -l"
89
- end
90
-
91
- def test_add_option_with_args
92
- ping = Ping.options {
93
- @args << ["-c", 2] << ["google.com"]
94
- }
95
-
96
- assert_equal ping.command, "#{Ping._bin} -c 2 google.com"
97
- end
98
-
99
- def test_remove_options
100
- ls = Ls.options { a; l; h }
101
- ls.remove :a, :h
102
- assert_equal ls.command, "ls -l"
103
-
104
- ls.reset
105
- ls.all
106
- ls.remove :all
107
- assert_equal ls.command, "ls"
108
- end
109
-
110
- def test_reset
111
- ls = Ls.options { a; l; h }
112
- assert_equal ls.command, "ls -a -l -h"
113
- ls.reset
114
- assert_equal ls.command, "ls"
115
- end
116
-
117
- def test_exec
118
- ls = Ls.options { full }.exec
119
-
120
- assert_instance_of String, ls
121
- assert true, ls.size > 0
122
- end
123
-
124
- def test_exec_with_block
125
- ls = Ls.options { full }
126
- process = ls.exec do |out|
127
- assert_instance_of String, out
128
- assert true, out.size > 0
129
- end
130
-
131
- assert_instance_of Process::Status, process
132
- assert_equal process.exitstatus, 0
133
- end
134
-
135
- def test_timeout
136
- assert_raise Timeout::Error do
137
- Ping.options {
138
- @args << ["google.com"]
139
- _timeout 2
140
- }.exec
141
- end
142
- assert_equal Ping.timeout, 2
143
- end
144
-
145
- def test_stdout
146
- res = ""
147
- Ping.options {
148
- count 2
149
- @args << ["google.com"]
150
- }.exec do |out|
151
- res << out
152
- end
153
-
154
- assert true, res.size > 0
155
- end
156
-
157
- def test_parse_output
158
- NSLookup.options {
159
- @args = ["google.com"]
160
- }.exec do |ips|
161
- assert_instance_of Array, ips
162
- assert true, ips.size > 1
163
- end
164
- end
165
- end
27
+ def teardown
28
+ Ls.new.reset
29
+ Ping.new.reset
30
+ end
31
+
32
+ def test_default_bin
33
+ assert_equal Ls.new.command, "ls"
34
+ end
35
+
36
+ def test_defined_bin
37
+ assert_equal Ping.new.command, `whereis ping`.strip
38
+ assert_equal Ping.new.command, Ping._bin
39
+ end
40
+
41
+ def test_adef
42
+ assert_equal Ls._definitions[:all], "-a"
43
+ assert_equal Ls._definitions[:human], "-h"
44
+ end
45
+
46
+ def test_adef_with_block
47
+ assert_instance_of Proc, Ls._definitions[:full]
48
+ assert_equal Ls._definitions[:full].call, ["-a", "-h"]
49
+ end
50
+
51
+ def test_argument_added
52
+ ls = Ls.options { all }
53
+ assert true, ls.args[0] == "-a"
54
+ end
55
+
56
+ def test_all_arguments_from_adef_added
57
+ ls = Ls.options { full }
58
+ assert_equal ls.args.size, 2
59
+ end
60
+
61
+ def test_not_defined_short_argument
62
+ ls = Ls.options { s }
63
+ assert_equal ls.args.size, 1
64
+ assert_equal ls.args.to_s, "-s"
65
+ end
66
+
67
+ def test_not_defined_long_argument
68
+ ls = Ls.options { color }
69
+ assert_equal ls.args.size, 1
70
+ assert_equal ls.args.to_s, "--color"
71
+ end
72
+
73
+ def test_command
74
+ assert_equal Ls.options { full; s }.command, "ls -a -h -s"
75
+ end
76
+
77
+ def test_add_options
78
+ ls = Ls.options { a }
79
+ assert_equal ls.args.flatten.size, 1
80
+ ls.options { l; h }
81
+ assert_equal ls.args.flatten.size, 3
82
+
83
+ assert_equal ls.command, "ls -a -l -h"
84
+ end
85
+
86
+ def test_add_option
87
+ ls = Ls.new
88
+ ls.all
89
+ assert_equal ls.args.flatten.size, 1
90
+ assert_equal ls.command, "ls -a"
91
+
92
+ ls.l
93
+ assert_equal ls.args.flatten.size, 2
94
+ assert_equal ls.command, "ls -a -l"
95
+ end
96
+
97
+ def test_add_option_with_args
98
+ ping = Ping.options {
99
+ @args << ["-c", 2] << ["google.com"]
100
+ }
101
+
102
+ assert_equal ping.command, "#{Ping._bin} -c 2 google.com"
103
+ end
104
+
105
+ def test_remove_options
106
+ ls = Ls.options { a; l; h }
107
+ ls.remove :a, :h
108
+ assert_equal ls.command, "ls -l"
109
+
110
+ ls.reset
111
+ ls.all
112
+ ls.remove :all
113
+ assert_equal ls.command, "ls"
114
+ end
115
+
116
+ def test_reset
117
+ ls = Ls.options { a; l; h }
118
+ assert_equal ls.command, "ls -a -l -h"
119
+ ls.reset
120
+ assert_equal ls.command, "ls"
121
+ end
122
+
123
+ def test_exec
124
+ ls = Ls.options { full }.exec
125
+
126
+ assert_instance_of String, ls
127
+ assert true, ls.size > 0
128
+ end
129
+
130
+ def test_exec_with_block
131
+ ls = Ls.options { full }
132
+ process = ls.exec do |out|
133
+ assert_instance_of String, out
134
+ assert true, out.size > 0
135
+ end
136
+
137
+ assert_instance_of Process::Status, process
138
+ assert_equal process.exitstatus, 0
139
+ end
140
+
141
+ def test_timeout
142
+ assert_raise Timeout::Error do
143
+ Ping.options {
144
+ @args << ["google.com"]
145
+ _timeout 2
146
+ }.exec
147
+ end
148
+ assert_equal Ping.timeout, 2
149
+ end
150
+
151
+ def test_stdout
152
+ res = ""
153
+ Ping.options {
154
+ count 2
155
+ @args << ["google.com"]
156
+ }.exec do |out|
157
+ res << out
158
+ end
159
+
160
+ assert true, res.size > 0
161
+ end
162
+
163
+ def test_parse_output
164
+ NSLookup.options {
165
+ @args = ["google.com"]
166
+ }.exec do |ips|
167
+ assert_instance_of Array, ips
168
+ assert true, ips.size > 1
169
+ end
170
+ end
171
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Bruno Celeste
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-07-21 00:00:00 +02:00
17
+ date: 2011-07-23 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies: []
20
20