Class: TestRun

Inherits:
ActiveRecord::Base show all
Includes:
Generic_Table
Defined in:
app/models/test_run.rb

Constant Summary

Constants included from ColumnGroup::ClassMethods

ColumnGroup::ClassMethods::History_columns

Constants included from GenericGrep::ClassMethods

GenericGrep::ClassMethods::ASSOCIATION_MACRO_LETTERS, GenericGrep::ClassMethods::ASSOCIATION_MACRO_PATTERN

Constants included from GenericTableAssociation::Examples

GenericTableAssociation::Examples::First_stream_method

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Generic_Table

activeRecordTableNotCreatedYet?, classDefiniton, classReference, class_of_name, eval_constant, is_ActiveRecord_table?, is_generic_table?, is_generic_table_name?, is_table?, rails_MVC_class?, rubyClassName, table_exists?

Methods included from Generic_Table::ClassMethods

#is_active_record_method?, #model_file_name, #nesting, #one_pass_statistics, #sample, #sample_burst

Methods included from ColumnGroup::ClassMethods

#attribute_ddl, #attribute_rails_type, #attribute_ruby_type, #candidate_logical_keys_from_indexes, #categorical?, #column_symbols, #default_logical_primary_key, #defaulted_primary_logical_key?, #history_type?, #is_logical_primary_key?, #logical_attributes, #logical_primary_key, #logical_primary_key_recursive, #numerical?, #probably_categorical?, #probably_numerical?, #sequential_id?

Methods included from GenericGrep::ClassMethods

#all_associations, #association_grep, #association_grep_pattern, #association_macro_type, #association_type, #belongs_to_association?, #grep_all_associations_command, #grep_command, #has_many_association?, #has_one_association?, #model_grep, #model_grep_command

Methods included from GenericTableHtml::ClassMethods

#column_order, #header_html, #table_html

Methods included from GenericTableAssociation

#associated_foreign_key_records, #associated_to_s, #association_class, #association_has_data, #association_state, #foreign_key_points_to_me?, #foreign_key_to_association, #foreign_key_value, #logical_primary_key_recursive_value, #logical_primary_key_value, #name_to_association

Methods included from GenericTableAssociation::Assertions::ClassMethods

#assert_foreign_keys_not_nil, #assert_invariant, #assert_post_conditions, #assert_pre_conditions

Methods included from GenericTableAssociation::Assertions

#assert_associated_foreign_key, #assert_associated_foreign_key_name, #assert_association, #assert_association_methods, #assert_association_to_many, #assert_association_to_one, #assert_foreign_key_association_names, #assert_foreign_key_name, #assert_foreign_key_not_nil, #assert_foreign_keys_not_nil, #assert_invariant, #assert_post_conditions, #assert_pre_conditions

Methods included from GenericTableHtml

#column_html, #rails_route, #row_html

Methods included from GenericTableAssociation::ClassMethods

#associated_foreign_key_name, #association_arity, #association_class, #association_default_class_name?, #association_method_plurality, #association_method_symbol, #association_methods, #association_names, #association_names_to_many, #association_names_to_one, #association_patterns, #foreign_key_association_names, #foreign_key_names, #foreign_key_to_association_name, #is_association?, #is_association_patterns?, #is_association_to_many?, #is_association_to_one?, #is_foreign_key_name?, #is_matching_association?, #is_polymorphic_association?, #match_association_patterns?, #name_symbol

Constructor Details

#initialize(testType = nil, singular_table = nil, plural_table = nil, test = nil) ⇒ TestRun

Returns a new instance of TestRun.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/test_run.rb', line 13

def initialize(testType=nil, singular_table=nil, plural_table=nil, test=nil)
	if testType.instance_of?(Hash) then
		super(testType) # actually hash of attributes
#		attributes=testType 
	else
		super(nil) #
		if !testType.nil? then
			raise "initialize test run with bad testType=#{testType}" unless [:unit,:controller].include?(testType.to_sym)
			#~ puts "testType is not nil. testType=#{testType} singular_table=#{singular_table}"
			self[:test_type]=testType
			if singular_table.nil? then
				if plural_table.nil? then
					@singular_table = "code_base"
					@plural_table = "code_bases"
				else
					@singular_table = plural_table.singularize
					@plural_table = plural_table
				end #if
			else
				if plural_table.nil? then
					@singular_table = singular_table
					@plural_table = singular_table.pluralize
				else
					@singular_table = singular_table
					@plural_table = plural_table
				end #if
			end #if
			#~ puts "@singular_table=#{@singular_table} @plural_table=#{@plural_table}"
			#~ model = @singular_table # canonical form since plurals are more irregular?
			#~ puts "model=#{model} self.model=#{self.model} self['model']=#{self['model']}"
			self.model = @singular_table # canonical form since plurals are more irregular?
			#~ puts "model=#{model} self.model=#{self.model} self['model']=#{self['model']}"
			#~ self[:model] = @singular_table # canonical form since plurals are more irregular?
			#~ puts "model=#{model} self.model=#{self.model} self['model']=#{self['model']}"
			#~ self['model'] = @singular_table # canonical form since class is accessible
			#~ puts "model=#{model} self.model=#{self.model} self['model']=#{self['model']}"
			self.test = test 
		else
			#~ puts "nil testType"
		end #if

	end #if
#	puts "End of initialize: self=#{self.inspect}"
#	puts "End of initialize: testType=#{testType.inspect}"
end

Class Method Details

.file_bug_reports(ruby_source, log_file, test = nil) ⇒ Object

file_bug_reports


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/test_run.rb', line 152

def TestRun.file_bug_reports(ruby_source,log_file,test=nil)
	table,test_type=CodeBase.test_type_from_source(ruby_source)
	header,errors,summary=parse_log_file(log_file)
	if summary.nil? then
		puts "summary is nil. probable rake failure."
		stop=true
	else
		sysout,run_time=TestRun.parse_header(header)
		puts "sysout='#{sysout.inspect}'"
		puts "run_time='#{run_time}'"
		tests,assertions,failures,tests_stop_on_error=TestRun.parse_summary(summary)
		#~ puts "failures+tests_stop_on_error=#{failures+tests_stop_on_error}"
		if    (failures+tests_stop_on_error)==0 then
			stop=false
		else
			stop=true
		end #if
		open('db/tests.sql',"a" ) {|f| f.write("insert into test_runs(model,test,test_type,environment,tests,assertions,failures,tests_stop_on_error,created_at,updated_at) values('#{table}','#{ENV["TEST"]}','#{test_type}','#{ENV["RAILS_ENV"]}',#{tests},#{assertions},#{failures},#{tests_stop_on_error},'#{Time.now.rfc2822}','#{Time.now.rfc2822}');\n") }
	end #if
	if !errors.nil? then
		errors.each do |error|
			Bug.new(test_type,table,error)
#			puts "error='#{error}'"
		end #each
	end #if 
#	puts "ARGF.argv.inspect=#{ARGF.argv.inspect}"
	puts "file_bug_reports stop=#{stop}"
	puts "summary='#{summary}'"
	return stop
end

.log_passed?(log_file) ⇒ Boolean

parse_log_file

Returns:

  • (Boolean)

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/models/test_run.rb', line 190

def TestRun.log_passed?(log_file)
	if !File.size?(log_file) then
		return false # no file or empty file, no evidence of passing
	end #if
	header,errors,summary=TestRun.parse_log_file(log_file)
	if summary.nil? then
		return false
	else
		tests,assertions,failures,tests_stop_on_error=TestRun.parse_summary(summary)
		if    (failures+tests_stop_on_error)==0 then
			return true
		else
			return false
		end #if
	end #if
end

.parse_header(header) ⇒ Object

parse_summary


220
221
222
223
224
225
# File 'app/models/test_run.rb', line 220

def TestRun.parse_header(header)
	headerArray=header.split("\n")
	sysout=headerArray[0..-2]
	run_time=headerArray[-1].split(' ')[2].to_f
	return [sysout,run_time]
end

.parse_log_file(log_file) ⇒ Object

file_bug_reports


182
183
184
185
186
187
188
189
# File 'app/models/test_run.rb', line 182

def TestRun.parse_log_file(log_file)
	blocks=IO.read(log_file).split("\n\n")# delimited by multiple successive newlines
#	puts "blocks=#{blocks.inspect}"
	header= blocks[0]
	errors=blocks[1..-2]
	summary=blocks[-1]
	return [header,errors,summary]
end

.parse_summary(summary) ⇒ Object

def


212
213
214
215
216
217
218
219
# File 'app/models/test_run.rb', line 212

def TestRun.parse_summary(summary)
	summary=summary.split(' ')
	tests=summary[0].to_i
	assertions=summary[2].to_i
	failures=summary[4].to_i
	tests_stop_on_error=summary[6].to_i
	return [tests,assertions,failures,tests_stop_on_error]
end

.ruby(args, &proc) ⇒ Object

Run rubyinterpreter passing arguments


104
105
106
# File 'app/models/test_run.rb', line 104

def TestRun.ruby(args, &proc)
	shell("ruby #{args}",&proc)
end

.ruby_run_and_log(ruby_source, log_file, test = nil) ⇒ Object

ruby_run_and_log


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/test_run.rb', line 111

def TestRun.ruby_run_and_log(ruby_source,log_file,test=nil)
	if test.nil? then
		ruby_test=ruby_source
	else
		ruby_test="#{ruby_source} -n #{test}"
	end #if
	puts "ruby_test=#{ruby_test}"
	stop=ruby %Q{-I test #{ruby_test} | tee #{log_file}}  do |ok, res|
		if  ok
#		puts "ruby ok(status = #{res.inspect})"
			#~ sh "git add #{ruby_source}"
			 puts "IO.read('#{log_file}')='#{IO.read(log_file)}'"
		else
			puts "ruby failed(status = #{res.exitstatus})!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
			#~ sh "tail --lines=2 #{log_file}"
		end
#		puts "calling file_bug_reports"
		stop=TestRun.file_bug_reports(ruby_source,log_file,test)
		#c#		puts "local_variables=#{local_variables.inspect}"
		return stop
	end # ruby
	if local_variables.include?('stop') then
		puts "stop is defined here."
		return stop
	else
		puts "stop is nil or undefined? local_variables=#{local_variables.inspect}"
		puts "Did ruby block not execute?"
		return true
	end
rescue StandardError => exception_raised
	puts  '-StandardError Error: ' + exception_raised.inspect 
	puts exception_raised.backtrace.join("\n")

	return true
rescue SyntaxError => exception_raised
	puts  '-SyntaxError Error: ' + exception_raised.inspect 
	return true
end

.shell(command, &proc) ⇒ Object

Run a shell


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/test_run.rb', line 88

def TestRun.shell(command, &proc)
#	puts "command='#{command}'"
	output=`#{command}`
#	puts "$?=#{$?}"
#	puts "output='#{output}'"
	if $?==0 then
		proc.call(true,output)
#		puts output
		return output
	else
		proc.call(false,"$?=#{$?}"+output)
#		puts output
		return nil
	end #if
end

.summarizeObject

def


206
207
208
209
210
211
# File 'app/models/test_run.rb', line 206

def TestRun.summarize
	sh %Q(ls -1 -s log/{unit,functional}|grep " 0 "|cut --delim=' ' -f 3 >log/empty_tests.tmp)	
	sh %Q{grep "[0-9 ,][0-9 ][1-9] error" log/{unit,functional}/* | cut --delim='/' -f 3  >log/error_tests.tmp}
	sh %Q{grep "[0-9 ,][0-9 ][1-9] failures," log/{unit,functional}/* | cut --delim='/' -f 3  >log/failure_tests.tmp}
	sh %Q{cat log/empty_tests.tmp log/error_tests.tmp log/failure_tests.tmp|sort|uniq >log/failed_tests.log}
end

Instance Method Details

#file_bug_reportsObject

ruby_run_and_log


149
150
151
# File 'app/models/test_run.rb', line 149

def file_bug_reports
	TestRun.file_bug_reports(test_file,log_file,self[:test])
end

#log_fileObject

log_file => String Filename of log file from test run


69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/test_run.rb', line 69

def log_file
	if self[:test].nil? then
		test_suffix=''
	else
		test_suffix=".#{self[:test]}"
	end #if
	case self[:test_type].to_sym
	when :unit
		return CodeBase.unit_target(@singular_table.to_s+test_suffix)
	when :controller
		return CodeBase.controller_target(@plural_table.to_s+test_suffix)
	else raise "Unnown self[:test_type]=#{self[:test_type]} for singular_table=#{singular_table}"
	end #case
end

#ruby_run_and_logObject

ruby


107
108
109
# File 'app/models/test_run.rb', line 107

def ruby_run_and_log
	TestRun.ruby_run_and_log(test_file,log_file,self[:test])
end

#runObject

Unconditionally run the test


84
85
86
# File 'app/models/test_run.rb', line 84

def run
	TestRun.ruby_run_and_log(test_file,log_file,self[:test])
end

#test_fileObject

initialize


58
59
60
61
62
63
64
65
66
# File 'app/models/test_run.rb', line 58

def test_file
	case self[:test_type].to_sym
	when :unit
		return "test/unit/#{@singular_table}_test.rb"
	when :controller
		return "test/functional/#{@plural_table}_controller_test.rb"
	else raise "Unnown self[:test_type]=#{self[:test_type]} for singular_table=#{singular_table}"
	end #case
end