test/test_suite.rb in octopress-3.0.0.rc.11 vs test/test_suite.rb in octopress-3.0.0.rc.12
- old
+ new
@@ -2,45 +2,92 @@
require 'find'
# This is a makeshift integration test-suite.
# It is unapologetically pragmatic.
+
+# Build Jekyll
+#
+def build(options={})
+ if options[:octopress_config]
+ FileUtils.cp options[:octopress_config], '_octopress.yml'
+ end
+
+ config = ['_config.yml'] << options[:config]
+ cmd = "rm -rf site && bundle exec jekyll build --config #{config.join(',')}"
+
+ `#{cmd}`
+ `rm _octopress.yml` if options[:octopress_config]
+end
+
+
# Find all files in a given directory
#
def dir_files(dir)
Find.find(dir).to_a.reject!{|f| File.directory?(f) }
end
# Recursively diff two directories
#
# This will walk through dir1 and diff matching paths in dir2
#
-def compare_directories(dir1, dir2)
+def test_dirs(desc, dir1, dir2)
+
+ test_missing_files(desc, dir1, dir2)
+
dir_files(dir1).each do |file|
file2 = file.sub(dir1, dir2)
if File.exist?(file2)
- diff = diff_file(file, file2)
- if diff =~ /(<.+?\n)?(---\n)?(>.+)/
+ if diff = diff_file(file, file2)
@failures << {
- desc: "Diff of file: #{file} in #{dir2}",
- expected: $1,
- result: $3
+ desc: "#{desc}\nDiff of file: #{file.sub(dir1+'/', '')}\n",
+ result: format_diff(diff)
}
pout 'F'.red
else
pout '.'.green
end
- else
- @failures << {
- desc: "Diff of file: #{file} in #{dir2}",
- message: "No such file or directory: #{file2}"
- }
- pout 'F'.red
end
end
end
+def format_diff(diff)
+ "#{diff.gsub(/\A.+?\n/,'').gsub(/^[^><].+/,'---').gsub(/^>.+/){|m|
+ m.green
+ }.gsub(/^(<.+?)$/){ |m|
+ m.red
+ }}"
+end
+
+# List differences between files in two directories
+#
+def test_missing_files(desc, dir1, dir2)
+ files1 = dir_files(dir1).map {|f| f.sub(dir1,'') }
+ files2 = dir_files(dir2).map {|f| f.sub(dir2,'') }
+
+ missing = []
+
+ (files2 - files1).each do |file|
+ missing << File.join(dir1, file)
+ end
+
+ (files1 - files2).each do |file|
+ missing << File.join(dir2, file)
+ end
+
+ if !missing.empty?
+ @failures << {
+ desc: "#{desc}\nMissing files:\n",
+ result: " - " + missing.join("\n - ")
+ }
+
+ pout 'F'.red
+ else
+ pout '.'.green
+ end
+end
+
# Diff two files
#
def diff_file(file1, file2)
diff = `diff #{file1} #{file2}`
if diff.size > 0
@@ -48,35 +95,43 @@
else
false
end
end
-# Run test cases
+# Test command output
#
# Input: options hash, format:
# {
# desc: description of task
# cmd: system command to be run, (String or Array)
# expect: expected output from command
# }
#
-def test(options)
+def test_cmd(options)
if cmd = options[:cmd]
cmd = [cmd] unless cmd.is_a? Array
+
+ # In debug mode command output is printed
+ #
if options[:debug]
system cmd.join('; ')
else
output = `#{cmd.join('; ')}`.gsub(/#{Dir.pwd}\/*/,'').strip
+
+ # Remove character color codes
+ output = output.gsub("\e",'').gsub(/\[\d+m/,'').gsub("\[0m",'')
end
- if options[:expect].strip == output
+ if options[:expect] && options[:expect].strip == output
pout '.'.green
else
pout 'F'.red
@failures << {
- desc: options[:desc],
- expected: options[:expect],
- result: output,
+ desc: options[:desc]+"\n",
+ result: <<-HERE
+expected: #{(options[:expect] || '').strip.green}
+result: #{(output || '').strip.red}
+HERE
}
end
end
end
@@ -91,17 +146,12 @@
# Ouptut nicely formatted failure messages
#
def print_results
if !@failures.empty?
@failures.each do |test|
- puts "\nFailed: #{test[:desc]}"
- if test[:message]
- puts test[:message].yellow
- else
- puts (test[:expected] || '').green
- puts test[:result].red
- end
- # print a newline for easier reading
+ pout "\nFailed: #{test[:desc]}"
+ puts test[:result]
+ # print a newline for easier reading
puts ""
end
abort
else
puts "All passed!".green