mkvtoolnix/tests/test.d/simple_test.rb

222 lines
5.6 KiB
Ruby
Raw Normal View History

2012-01-27 18:29:02 +00:00
class SimpleTest
EXIT_CODE_ALIASES = {
:success => 0,
:warning => 1,
:error => 2,
2012-01-27 18:29:02 +00:00
}
def self.instantiate class_name
file_name = class_name_to_file_name class_name
content = IO.readlines(file_name).join("")
if ! /class\s+.*?\s+<\s+Test/.match(content)
content = %Q!
class ::#{class_name} < SimpleTest
def initialize
super
#{content}
end
end
!
else
content.gsub!(/class\s+/, 'class ::')
end
eval content, nil, file_name, 5
2012-01-27 18:29:02 +00:00
constantize(class_name).new
2012-01-27 18:29:02 +00:00
end
def initialize
@commands = []
@tmp_num = 0
@tmp_num_mutex = Mutex.new
@blocks = {
:setup => [],
:tests => [],
:cleanup => [],
}
2012-01-27 18:29:02 +00:00
end
def commands
@commands
2012-01-27 18:29:02 +00:00
end
def describe description
@description = description
2012-01-27 18:29:02 +00:00
end
def setup &block
@blocks[:setup] << block
2012-01-27 18:29:02 +00:00
end
def cleanup &block
@blocks[:cleanup] << block
end
def tmp_name_prefix
[ "/tmp/mkvtoolnix-auto-test-#{self.class.name}", $$.to_s, Thread.current[:number] ].join("-") + "-"
end
def tmp_name
@tmp_num_mutex.lock
@tmp_num ||= 0
@tmp_num += 1
result = self.tmp_name_prefix + @tmp_num.to_s
@tmp_num_mutex.unlock
2012-01-27 18:29:02 +00:00
result
end
def tmp
@tmp ||= tmp_name
2012-01-27 18:29:02 +00:00
end
def hash_file name
2012-01-27 18:29:02 +00:00
md5 name
end
def hash_tmp erase = true
output = hash_file @tmp
2012-01-27 18:29:02 +00:00
if erase
File.unlink(@tmp) if File.exists?(@tmp) && (ENV["KEEP_TMPFILES"] != "1")
@tmp = nil
2012-01-27 18:29:02 +00:00
end
output
end
def unlink_tmp_files
2012-01-27 18:29:02 +00:00
return if ENV["KEEP_TMPFILES"] == "1"
re = /^#{self.tmp_name_prefix}/
Dir.entries("/tmp").each do |entry|
file = "/tmp/#{entry}"
File.unlink(file) if re.match(file) and File.exists?(file)
end
end
def test name, &block
@blocks[:tests] << { :name => name, :block => block }
2012-01-27 18:29:02 +00:00
end
def test_merge file, *args
2012-01-27 18:29:02 +00:00
options = args.extract_options!
2012-01-31 13:23:10 +00:00
full_command_line = [ options[:args], file ].flatten.join(' ')
2012-01-27 18:29:02 +00:00
options[:name] ||= full_command_line
@blocks[:tests] << {
2012-01-27 18:29:02 +00:00
:name => full_command_line,
:block => lambda {
2012-03-07 19:03:29 +00:00
merge full_command_line, :exit_code => options[:exit_code]
2012-01-31 15:55:31 +00:00
options[:keep_tmp] ? hash_file(tmp) : hash_tmp
2012-01-27 18:29:02 +00:00
},
}
end
def test_identify file, *args
options = args.extract_options!
options[:verbose] = true if options[:verbose].nil?
full_command_line = [ options[:verbose] ? "--identify-verbose" : "--identify", options[:args], file ].flatten.join(' ')
options[:name] ||= full_command_line
@blocks[:tests] << {
:name => full_command_line,
:block => lambda {
2012-02-06 18:15:48 +00:00
sys "../src/mkvmerge #{full_command_line} > #{tmp}", :exit_code => options[:exit_code]
options[:keep_tmp] ? hash_file(tmp) : hash_tmp
},
}
end
def test_merge_unsupported file, *args
options = args.extract_options!
full_command_line = [ options[:args], file ].flatten.join(' ')
options[:name] ||= full_command_line
@blocks[:tests] << {
:name => full_command_line,
:block => lambda {
sys "../src/mkvmerge --identify-verbose #{full_command_line} > #{tmp}", :exit_code => 3
/unsupported container/.match(IO.readlines(tmp).first || '') ? :ok : :bad
},
}
end
2012-02-06 18:15:48 +00:00
def test_ui_locale locale, *args
options = args.extract_options!
@blocks[:tests] << {
:name => "mkvmerge UI locale #{locale}",
:block => lambda {
sys "../src/mkvmerge -o /dev/null --ui-language #{locale} data/avi/v.avi | head -n 2 | tail -n 1 > #{tmp}-#{locale}"
hash_file "#{tmp}-#{locale}"
},
}
@blocks[:tests] << {
:name => "mkvinfo UI locale #{locale}",
:block => lambda {
sys "../src/mkvinfo --ui-language #{locale} data/mkv/complex.mkv | head -n 2 > #{tmp}-#{locale}"
hash_file "#{tmp}-#{locale}"
},
}
end
def description
@description || fail("Class #{self.class.name} misses its description")
2012-01-27 18:29:02 +00:00
end
def run_test
@blocks[:setup].each &:call
2012-01-27 18:29:02 +00:00
results = @blocks[:tests].collect do |test|
2012-01-27 18:29:02 +00:00
result = nil
begin
result = test[:block].call
rescue RuntimeError => ex
show_message "Test case '#{self.class.name}', sub-test '#{test[:name]}': #{ex}"
2012-01-27 18:29:02 +00:00
end
result
end
@blocks[:cleanup].each &:call
2012-01-27 18:29:02 +00:00
2012-01-31 15:55:31 +00:00
unlink_tmp_files
2012-01-27 18:29:02 +00:00
results.join '-'
end
def merge *args
2012-01-27 18:29:02 +00:00
options = args.extract_options!
fail ArgumentError if args.empty?
output = options[:output] || self.tmp
command = "../src/mkvmerge --engage no_variable_data -o #{output} #{args.first}"
self.sys command, :exit_code => options[:exit_code]
end
def extract *args
2012-01-31 15:55:31 +00:00
options = args.extract_options!
fail ArgumentError if args.empty?
mode = options[:mode] || :tracks
command = "../src/mkvextract --engage no_variable_data #{mode} #{args.first} " + options.keys.select { |key| key.is_a?(Numeric) }.sort.collect { |key| "#{key}:#{options[key]}" }.join(' ')
self.sys command, :exit_code => options[:exit_code]
end
def sys *args
2012-01-31 13:23:17 +00:00
options = args.extract_options!
options[:exit_code] = EXIT_CODE_ALIASES[ options[:exit_code] ] || options[:exit_code] || 0
2012-01-27 18:29:02 +00:00
fail ArgumentError if args.empty?
command = args.shift
@commands << command
command << " >/dev/null 2>/dev/null " unless />/.match(command)
2012-01-27 18:29:02 +00:00
self.error "system command failed: #{command} (#{$? >> 8})" if !system(command) && (options[:exit_code] != ($? >> 8))
end
def error reason
2012-01-27 18:29:02 +00:00
show_message " Failed. Reason: #{reason}"
raise "test failed"
end
end