drep 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ # DRep - Modular Open Software Tester.
2
+ # Copyright (C) 2009 Dmitrii Toksaitov
3
+ #
4
+ # This file is part of DRep.
5
+ #
6
+ # DRep is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # DRep is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with DRep. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ def extend_load_paths(src_file_name)
20
+ unless src_file_name.nil?
21
+ req_dir_name = File.dirname(src_file_name)
22
+ req_abs_dir_name = File.expand_path(req_dir_name)
23
+
24
+ unless $LOAD_PATH.include?(req_dir_name) or
25
+ $LOAD_PATH.include?(req_abs_dir_name) then
26
+ $LOAD_PATH.unshift(req_dir_name)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,221 @@
1
+ # DRep - Modular Open Software Tester.
2
+ # Copyright (C) 2009 Dmitrii Toksaitov
3
+ #
4
+ # This file is part of DRep.
5
+ #
6
+ # DRep is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # DRep is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with DRep. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ class Object
20
+ def message(message)
21
+ if defined? @env.stdout
22
+ if not defined? @env.options.quiet or !@env.options.quiet
23
+ report(@env.stdout, message)
24
+ end
25
+ else
26
+ report(STDOUT, message)
27
+ end
28
+ end
29
+ alias msg message
30
+
31
+ def error(message)
32
+ if defined? @env.stderr
33
+ if not defined? @env.options.quiet or not @env.options.quiet
34
+ report(@env.stderr, message)
35
+ end
36
+ else
37
+ report(STDERR, message)
38
+ end
39
+ end
40
+ alias err error
41
+
42
+ def report(ios, message)
43
+ if ios.is_a?(IO) and
44
+ message.is_a?(String) then
45
+ ios.puts(message)
46
+ end
47
+ end
48
+
49
+ def form_str_from_list(list, separator = ', ')
50
+ result = ''
51
+
52
+ if list.is_a?(Array)
53
+ list.each_index do |i|
54
+ result += list[i].to_s()
55
+ result += (i != list.size - 1) ? separator.to_s() : ''
56
+ end
57
+ end
58
+
59
+ return result
60
+ end
61
+
62
+ def first_valid(item, *args)
63
+ result = item
64
+
65
+ if result.nil? and not args.nil?
66
+ args.each do |obj|
67
+ unless obj.nil?
68
+ result = obj; break
69
+ end
70
+ end
71
+ end
72
+
73
+ return result
74
+ end
75
+
76
+ def first_valid_text(strs, *patterns)
77
+ result = nil
78
+
79
+ if strs.is_a?(Array)
80
+ strs.each do |str|
81
+ if str.is_a?(String)
82
+ temp_str = str
83
+
84
+ if not temp_str.empty? and patterns.is_a?(Array)
85
+ temp_str.remove_nl!(); temp_str.br_to_nl!()
86
+ temp_str.strip_tags!(); temp_str.safe_strip!()
87
+
88
+ result = temp_str
89
+
90
+ patterns.each do |pattern|
91
+ if pattern.is_a?(Regexp) and
92
+ not pattern.match(result).to_s().size == result.size
93
+ result = nil
94
+ break
95
+ end
96
+ end
97
+ end
98
+
99
+ end
100
+
101
+ break unless result.nil?
102
+ end
103
+ end
104
+
105
+ if result.nil?
106
+ result = ''
107
+ else
108
+ result.safe_strip!()
109
+ end
110
+
111
+ return result
112
+ end
113
+ alias first_text first_valid_text
114
+ end
115
+
116
+ class Array
117
+ def strip_tags()
118
+ result = []
119
+
120
+ self.each do |str|
121
+ if str.is_a?(String)
122
+ result << str.strip_tags()
123
+ end
124
+ end
125
+
126
+ return result
127
+ end
128
+ end
129
+
130
+ class Hash
131
+ def strip_tags()
132
+ result = {}
133
+
134
+ self.each do |key, str|
135
+ if str.is_a?(String)
136
+ result[key] = str.strip_tags()
137
+ end
138
+ end
139
+
140
+ return result
141
+ end
142
+ end
143
+
144
+ class String
145
+ def remove_nl!()
146
+ self.gsub!(/\n+/, '')
147
+ self.gsub!(/\r+/, '')
148
+ self.gsub!('\n', '')
149
+
150
+ return self
151
+ end
152
+
153
+ def remove_nl()
154
+ result = ''
155
+
156
+ result = self.gsub(/\n+/, '')
157
+
158
+ result.gsub!(/\r+/, '')
159
+ result.gsub!('\n', '')
160
+
161
+ return result
162
+ end
163
+
164
+ def safe_strip!()
165
+ self.gsub!(/\A[\s]+|[\s]+\z/m, '')
166
+
167
+ return self
168
+ end
169
+
170
+ def safe_strip()
171
+ return self.gsub(/\A[\s]+|[\s]+\z/m, '')
172
+ end
173
+
174
+ def strip_tags!()
175
+ self.gsub!(/<.*?>/, '')
176
+
177
+ return self
178
+ end
179
+
180
+ def strip_tags()
181
+ return self.gsub(/<.*?>/, '')
182
+ end
183
+
184
+ def br_to_nl!()
185
+ self.replace(self.split(/<br\s*[\/]?>/).join("\n"))
186
+
187
+ return self
188
+ end
189
+
190
+ def br_to_nl()
191
+ return self.split(/<br\s*[\/]?>/).join("\n")
192
+ end
193
+
194
+ def lspart(arg)
195
+ result = ''
196
+
197
+ pos = index(arg)
198
+ unless pos.nil?
199
+ result = self[(pos + 1)..-1]
200
+ unless result.nil?
201
+ result.safe_strip!()
202
+ end
203
+ end
204
+
205
+ return result
206
+ end
207
+
208
+ def rspart(arg)
209
+ result = ''
210
+
211
+ pos = index(arg)
212
+ unless pos.nil?
213
+ result = self[0..(pos - 1)]
214
+ unless result.nil?
215
+ result.safe_strip!()
216
+ end
217
+ end
218
+
219
+ return result
220
+ end
221
+ end
@@ -0,0 +1,117 @@
1
+ # DRep - Modular Open Software Tester.
2
+ # Copyright (C) 2009 Dmitrii Toksaitov
3
+ #
4
+ # This file is part of DRep.
5
+ #
6
+ # DRep is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # DRep is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with DRep. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ class Object
20
+ def valid(item, *args, &block)
21
+ if valid?(item, *args)
22
+ yield block if block_given?
23
+ end
24
+ end
25
+
26
+ def valid?(item, *args, &block)
27
+ result = item.nil? ? false : true
28
+
29
+ if result and args.is_a?(Enumerable)
30
+ args.each do |obj|
31
+ if obj.nil? or obj.is_a?(FalseClass)
32
+ result = false; break
33
+ end
34
+ end
35
+ end
36
+
37
+ if block_given?
38
+ yield block if result
39
+ end
40
+
41
+ return result
42
+ end
43
+
44
+ def valid_string(item, *args, &block)
45
+ if valid_string?(item, *args)
46
+ yield block if block_given?
47
+ end
48
+ end
49
+
50
+ def valid_string?(item, *args, &block)
51
+ if item.is_a?(String) and !item.strip().empty?
52
+ result = true
53
+ else
54
+ result = false
55
+ end
56
+
57
+ if result and args.is_a?(Enumerable)
58
+ args.each do |obj|
59
+ if obj.nil? or
60
+ !obj.is_a?(String) or
61
+ obj.strip().empty? then
62
+ result = false; break
63
+ end
64
+ end
65
+ end
66
+
67
+ if block_given?
68
+ yield block if result
69
+ end
70
+
71
+ return result
72
+ end
73
+
74
+ def valid_hash_args(*args, &block)
75
+ if valid_hash_args?(args)
76
+ yield block if block_given?
77
+ end
78
+ end
79
+
80
+ def valid_hash_args?(*args, &block)
81
+ result = args.is_a?(Array) ? true : false
82
+
83
+ if result
84
+ args.each do |property|
85
+
86
+ if property.is_a?(Hash)
87
+ property.each do |name, value|
88
+ unless name.is_a?(Symbol) or
89
+ name.is_a?(String) then
90
+ result = false
91
+ end
92
+ end
93
+ else
94
+ result = false
95
+ end
96
+
97
+ break unless result
98
+ end
99
+ end
100
+
101
+ yield block if result and block_given?
102
+
103
+ return result
104
+ end
105
+
106
+ def matched?(str, pattern)
107
+ result = false
108
+
109
+ if str.is_a?(String) and pattern.is_a?(Regexp)
110
+ if pattern.match(str).to_s().size == str.size
111
+ result = true
112
+ end
113
+ end
114
+
115
+ return result
116
+ end
117
+ end
@@ -0,0 +1,102 @@
1
+ # DRep - Modular Open Software Tester.
2
+ # Copyright (C) 2009 Dmitrii Toksaitov
3
+ #
4
+ # This file is part of DRep.
5
+ #
6
+ # DRep is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # DRep is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with DRep. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ extend_load_paths __FILE__
20
+ require 'alterers/drep_helpers'
21
+ require 'alterers/drep_validators'
22
+
23
+ module DRep
24
+
25
+ class DRepBinder
26
+ attr_reader :env
27
+
28
+ attr_reader :__vars_data__
29
+
30
+ def initialize(environment, vars_data)
31
+ unless environment.nil?
32
+ @env = environment
33
+ else
34
+ raise(ArgumentError, "Invalid environment", caller())
35
+ end
36
+
37
+ @__vars_data__ = vars_data
38
+ inject_vars()
39
+ end
40
+
41
+ def get_binding()
42
+ return binding()
43
+ end
44
+
45
+ private
46
+ def inject_vars()
47
+ if @__vars_data__.is_a?(Array)
48
+
49
+ @__vars_data__.each do |data_blob|
50
+ if data_blob.is_a?(Hash)
51
+
52
+ data_blob.each do |name, value|
53
+ var_str_name = "@#{name.to_s()}"
54
+ unless var_str_name.nil?
55
+ if instance_variable_defined?(var_str_name)
56
+ err("Variables overlapping occurred in #{var_str_name}")
57
+ end
58
+ instance_variable_set(var_str_name.intern(), value)
59
+ else
60
+ err('Variable name is in incorrect format')
61
+ end
62
+ end
63
+
64
+ else
65
+ err('Invalid inner data blob format')
66
+ end
67
+ end
68
+
69
+ else
70
+ err('Invalid variables data format')
71
+ end
72
+ end
73
+
74
+ def method_missing(mid, *args)
75
+ result = ''
76
+
77
+ if args.size == 0
78
+ if @__vars_data__.is_a?(Array)
79
+ temp_data = nil
80
+ @__vars_data__.each do |data_blob|
81
+ if data_blob.is_a?(Hash)
82
+ var_value = data_blob[mid]
83
+ temp_data = var_value unless var_value.nil?
84
+ else
85
+ err('Invalid inner data blob format')
86
+ end
87
+ end
88
+ valid temp_data do
89
+ result = temp_data
90
+ end
91
+ else
92
+ err('Invalid variables data format')
93
+ end
94
+ else
95
+ err("Value binding failed. Invalid method call: #{mid}")
96
+ end
97
+
98
+ return result
99
+ end
100
+ end
101
+
102
+ end
@@ -0,0 +1,47 @@
1
+ # DRep - Modular Open Software Tester.
2
+ # Copyright (C) 2009 Dmitrii Toksaitov
3
+ #
4
+ # This file is part of DRep.
5
+ #
6
+ # DRep is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # DRep is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with DRep. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ extend_load_paths __FILE__
20
+ require 'alterers/drep_helpers'
21
+ require 'alterers/drep_validators'
22
+
23
+ module DRep
24
+
25
+ class DRepEnvironment
26
+ attr_reader :stdin, :stdout, :stderr
27
+
28
+ attr_reader :options
29
+
30
+ def initialize(stdin, stdout, stderr, options)
31
+ if valid?(stdin, stdout, stderr)
32
+ @stdin = stdin
33
+ @stdout = stdout
34
+ @stderr = stderr
35
+ else
36
+ raise('Not all standard streams are valid')
37
+ end
38
+
39
+ if valid?(options)
40
+ @options = options
41
+ else
42
+ raise('Options were not specified')
43
+ end
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,199 @@
1
+ # DRep - Modular Open Software Tester.
2
+ # Copyright (C) 2009 Dmitrii Toksaitov
3
+ #
4
+ # This file is part of DRep.
5
+ #
6
+ # DRep is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # DRep is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with DRep. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ require 'erb'
20
+
21
+ require 'date'
22
+
23
+ extend_load_paths __FILE__
24
+ require 'alterers/drep_helpers'
25
+ require 'alterers/drep_validators'
26
+
27
+ require 'interfaces/drep_runnable'
28
+
29
+ require 'drep_binder'
30
+
31
+ module DRep
32
+
33
+ class DRepExecuter
34
+ include DRepRunnable
35
+
36
+ attr_reader :env
37
+
38
+ attr_reader :providers
39
+ attr_reader :templates
40
+
41
+ def initialize(environment, providers, templates)
42
+ unless environment.nil?
43
+ @env = environment
44
+ else
45
+ raise(ArgumentError, "Invalid environment", caller())
46
+ end
47
+
48
+ @providers = providers
49
+ @templates = templates
50
+ end
51
+
52
+ def run()
53
+ perform_tasks()
54
+ end
55
+
56
+ private
57
+ def perform_tasks()
58
+ vars = []
59
+
60
+ if valid?(@providers)
61
+ msg("Provider processing is about to start\n" +
62
+ "Number of providers to process: #{@providers.size}")
63
+
64
+ @providers.each do |provider|
65
+ extracted_vars = process_provider(provider)
66
+
67
+ unless extracted_vars.nil? or extracted_vars.empty?
68
+ vars << extracted_vars
69
+ end
70
+ end
71
+ else
72
+ err("Providers were not specified")
73
+ end
74
+
75
+ if valid?(@templates) and !vars.empty?
76
+ msg("Template processing is about to start\n" +
77
+ "Number of templates to process: #{@templates.size}")
78
+
79
+ @templates.each do |template|
80
+ process_template(template, vars)
81
+ end
82
+ else
83
+ err("Templates were not specified")
84
+ end
85
+ end
86
+
87
+ def process_provider(provider_def)
88
+ result = {}
89
+
90
+ extracted_vars = nil
91
+
92
+ provider = provider_def[:path]
93
+ if valid?(provider)
94
+ begin
95
+ msg("Processing provider: #{provider}")
96
+
97
+ require File.expand_path(provider)
98
+ if defined? Task.new()
99
+ extracted_vars = Task.new(@env).run(provider_def)
100
+ end
101
+ rescue Exception => e
102
+ err("Provider execution failed: #{e.message}")
103
+ end
104
+
105
+ if valid?(extracted_vars)
106
+ result = extracted_vars
107
+ else
108
+ err("Failed to get data from provider: #{provider}")
109
+ end
110
+ else
111
+ err("Invalid provider defenition")
112
+ end
113
+
114
+ return result
115
+ end
116
+
117
+ def process_template(template_def, vars)
118
+ if template_def.is_a?(Hash)
119
+ template_path = template_def[:path]
120
+ output = template_def[:output]
121
+
122
+ if valid?(template_path)
123
+ begin
124
+ msg("Processing template: #{template_path}")
125
+ template_content = get_content(template_path)
126
+ valid template_content do
127
+ report = generate_report(template_content, vars)
128
+ output_ios = get_ios(output)
129
+
130
+ valid report, output_ios do
131
+ output_ios.write(report)
132
+ end
133
+ end
134
+ rescue Exception => e
135
+ err("Template population failed: #{e.message}")
136
+ end
137
+ else
138
+ err("Invalid template path")
139
+ end
140
+ else
141
+ err("Template defenition has invalid format")
142
+ end
143
+ end
144
+
145
+ def generate_report(template_content, vars)
146
+ result = nil
147
+
148
+ begin
149
+ msg("Forming variables binding")
150
+ data_binding = DRepBinder.new(@env, vars)
151
+
152
+ msg("Generating result")
153
+ erb_template = ERB.new(template_content, nil, '%<>')
154
+ result = erb_template.result(data_binding.get_binding())
155
+ rescue Exception => e
156
+ err("Report generation failed: #{e.message}")
157
+ end
158
+
159
+ return result
160
+ end
161
+
162
+ def get_content(path)
163
+ result = nil
164
+
165
+ begin
166
+ msg("Getting content: #{path}")
167
+ open(path) do |content|
168
+ result = content.read
169
+ end
170
+ rescue Exception => e
171
+ err("Failed to get content: #{e.message}")
172
+ end
173
+
174
+ return result
175
+ end
176
+
177
+ def get_ios(output)
178
+ result = nil
179
+
180
+ begin
181
+ msg("Checking type of output")
182
+ if output.is_a?(String)
183
+ msg("Output destination: #{output}")
184
+ result = File.open(output, 'w+')
185
+ elsif output.is_a?(IO)
186
+ msg("Output provider: #{output.class}")
187
+ result = output
188
+ else
189
+ err("Result output object is invalid")
190
+ end
191
+ rescue Exception => e
192
+ err("Failed to check type of output: #{e.message}")
193
+ end
194
+
195
+ return result
196
+ end
197
+ end
198
+
199
+ end