-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.rb
75 lines (65 loc) · 1.77 KB
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'getoptlong'
require 'active_support/dependencies'
ActiveSupport::Dependencies.autoload_paths << File.expand_path('../lib', __FILE__)
Dir.chdir(File.join(File.dirname(__FILE__), 'test', 'fixtures', 'textile_to_markdown'))
def temp_file(name, content)
file = Tempfile.new(name)
file.write(content)
file.close
file.path
end
test_pattern = '*'
show_diff = true
overwrite = false
opts = GetoptLong.new(
[ '--stdout', '-c', GetoptLong::NO_ARGUMENT ],
[ '--write-md', '-w', GetoptLong::NO_ARGUMENT ]
)
opts.each do |opt|
case opt
when '--stdout'
show_diff = false
overwrite = false
when '--write-md'
show_diff = false
overwrite = true
end
end
test_pattern = ARGV[0] unless ARGV.length.zero?
failed = 0
succeeded = 0
converter = RedmineReformat::Converters::TextileToMarkdown::Converter.new
Dir.glob("#{test_pattern}.textile").each do |textile|
md = textile.sub(/(\.textile)?$/, '.md')
md = '/dev/null' unless overwrite or File.exists?(md)
name = textile.sub(/\.textile$/, '')
ctx = RedmineReformat::Context.new(ref: name, to_formatting: 'markdown')
input = File.read(textile)
actual = converter.convert(input, ctx)
if show_diff
expected = File.read(md)
if actual != expected
a = temp_file(name, actual)
STDERR.puts "#{name}: TEST FAILED!"
STDERR.flush
puts `colordiff -u #{md} #{a}`
STDOUT.flush
failed += 1
else
STDERR.puts "#{name}: TEST SUCCESS!"
STDERR.flush
succeeded += 1
end
else
out = if overwrite then File.open(md, 'w') else $stdout.dup end
out.write(actual)
out.close
end
end
exit unless show_diff
if failed.zero?
STDERR.puts "All #{succeeded} tests succeeded."
else
STDERR.puts "#{failed} of #{succeeded + failed} tests failed."
exit 1
end