forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helper.rb
492 lines (427 loc) · 13.8 KB
/
test_helper.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
149
150
151
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# frozen_string_literal: true
ASCIIDOCTOR_TEST_DIR = File.absolute_path __dir__
ASCIIDOCTOR_LIB_DIR = ENV['ASCIIDOCTOR_LIB_DIR'] || (File.join ASCIIDOCTOR_TEST_DIR, '../lib')
require 'simplecov' if ENV['COVERAGE'] == 'true'
require File.join ASCIIDOCTOR_LIB_DIR, 'asciidoctor'
Dir.chdir Asciidoctor::ROOT_DIR
require 'nokogiri'
# NOTE rouge has all sorts of warnings we don't want to see, so silence them
proc do
old_verbose, $VERBOSE = $VERBOSE, nil
require 'rouge'
$VERBOSE = old_verbose
end.call
require 'socket'
require 'tempfile'
require 'tmpdir'
autoload :FileUtils, 'fileutils'
autoload :Pathname, 'pathname'
RE_XMLNS_ATTRIBUTE = / xmlns="[^"]+"/
RE_DOCTYPE = /\s*<!DOCTYPE (.*)/
require 'minitest/autorun'
# Minitest 4 doesn't have Minitest::Test
Minitest::Test = MiniTest::Unit::TestCase unless defined? Minitest::Test
class Minitest::Test
def jruby?
RUBY_ENGINE == 'jruby'
end
def self.windows?
/mswin|msys|mingw/.match? RbConfig::CONFIG['host_os']
end
def windows?
Minitest::Test.windows?
end
def disk_root
%(#{windows? ? (Asciidoctor::ROOT_DIR.partition '/')[0] : ''}/)
end
def empty_document options = {}
options[:parse] ? (Asciidoctor::Document.new [], options).parse : (Asciidoctor::Document.new [], options)
end
def empty_safe_document options = {}
Asciidoctor::Document.new [], (options.merge safe: :safe)
end
def sample_doc_path name
unless (name = name.to_s).include? '.'
%w(adoc asciidoc txt).each do |ext|
if File.exist? fixture_path %(#{name}.#{ext})
name = %(#{name}.#{ext})
break
end
end
end
fixture_path name
end
def bindir
File.join Asciidoctor::ROOT_DIR, 'bin'
end
def testdir
ASCIIDOCTOR_TEST_DIR
end
def fixturedir
File.join testdir, 'fixtures'
end
def fixture_path name
File.join fixturedir, name
end
def example_document name, opts = {}
document_from_string (File.read (sample_doc_path name), mode: Asciidoctor::FILE_READ_MODE), opts
end
def xmlnodes_at_css css, content, count = nil
xmlnodes_at_path :css, css, content, count
end
def xmlnodes_at_xpath xpath, content, count = nil
xmlnodes_at_path :xpath, xpath, content, count
end
def xmlnodes_at_path type, path, content, count = nil
doc = xmldoc_from_string content
case type
when :xpath
namespaces = (doc.respond_to? :root) ? doc.root.namespaces : {}
results = doc.xpath %(#{path.sub '/', './'}), namespaces
when :css
results = doc.css path
end
count == 1 ? results.first : results
end
# Generate an xpath attribute matcher that matches a name in the class attribute
def contains_class name
%(contains(concat(' ', normalize-space(@class), ' '), ' #{name} '))
end
def assert_css css, content, count = nil
assert_path :css, css, content, count
end
def assert_xpath xpath, content, count = nil
assert_path :xpath, xpath, content, count
end
def assert_path type, path, content, count = nil
case type
when :xpath
type_name = 'XPath'
when :css
type_name = 'CSS'
end
results = xmlnodes_at_path type, path, content
if count == true || count == false
if count == results
assert true
else
flunk %(#{type_name} #{path} yielded #{results} rather than #{count} for:\n#{content})
end
elsif count && results.size != count
flunk %(#{type_name} #{path} yielded #{results.size} elements rather than #{count} for:\n#{content})
elsif count.nil? && results.empty?
flunk %(#{type_name} #{path} not found in:\n#{content})
else
assert true
end
end
def assert_include expected, actual
assert_includes actual, expected
end
def refute_include not_expected, actual
refute_includes actual, not_expected
end
def assert_message logger, severity, expected_message, kind = String, idx = nil
unless idx
assert_equal 1, logger.messages.size
idx = 0
end
message = logger.messages[idx]
assert_equal severity, message[:severity]
assert_kind_of kind, message[:message]
if kind == String
actual_message = message[:message]
else
refute_nil message[:message][:source_location]
actual_message = message[:message].inspect
end
if expected_message.start_with? '~'
assert_includes actual_message, expected_message[1..-1]
else
assert_equal expected_message, actual_message
end
end
def assert_messages logger, expected_messages
assert_equal expected_messages.size, logger.messages.size
expected_messages.each_with_index do |expected_message_details, idx|
severity, expected_message, kind = expected_message_details
assert_message logger, severity, expected_message, (kind || String), idx
end
end
def xmldoc_from_string content
if (content.start_with? '<?xml ') || (RE_XMLNS_ATTRIBUTE.match? content)
Nokogiri::XML::Document.parse content
elsif !(RE_DOCTYPE =~ content)
Nokogiri::HTML::DocumentFragment.parse content
elsif $1.start_with? 'html'
Nokogiri::HTML::Document.parse content
else
Nokogiri::XML::Document.parse content
end
end
def document_from_string src, opts = {}
assign_default_test_options opts
opts[:parse] ? (Asciidoctor::Document.new src.lines, opts).parse : (Asciidoctor::Document.new src.lines, opts)
end
def block_from_string src, opts = {}
(document_from_string src, (opts.merge standalone: false)).blocks.first
end
def convert_string src, opts = {}
keep_namespaces = opts.delete :keep_namespaces
if keep_namespaces
(document_from_string src, opts).convert
else
# this is required because nokogiri is easily confused by namespaces
result = (document_from_string src, opts).convert
result ? (result.sub RE_XMLNS_ATTRIBUTE, '') : result
end
end
def convert_string_to_embedded src, opts = {}
(document_from_string src, (opts.merge standalone: false)).convert
end
def convert_inline_string src, opts = {}
(document_from_string src, (opts.merge doctype: :inline)).convert
end
def parse_header_metadata source, doc = nil
reader = Asciidoctor::Reader.new source.split Asciidoctor::LF
[(Asciidoctor::Parser.parse_header_metadata reader, doc), reader]
end
def assign_default_test_options opts
opts[:standalone] = true unless opts.key? :standalone
opts[:parse] = true unless opts.key? :parse
if opts[:standalone]
# don't embed stylesheet unless test requests the default behavior
if opts.key? :linkcss_default
opts.delete :linkcss_default
else
opts[:attributes] ||= {}
opts[:attributes]['linkcss'] = ''
end
end
if (template_dir = ENV['TEMPLATE_DIR'])
opts[:template_dir] = template_dir unless opts.key? :template_dir
end
nil
end
# Decode the numeric character reference, such as 8212, to a Unicode glyph
# so it may be used in an XPath expression.
#
# Examples
#
# decode_char 60
# # => "<"
#
# Returns the decoded String that corresponds to the numeric character reference
def decode_char number
[number].pack 'U1'
end
def invoke_cli_with_filenames argv = [], filenames = [], &block
filepaths = []
filenames.each do |filename|
if filenames.nil? || (Pathname.new filename).absolute?
filepaths << filename
else
filepaths << (fixture_path filename)
end
end
invoker = Asciidoctor::Cli::Invoker.new argv + filepaths
invoker.invoke!(&block)
invoker
end
def invoke_cli_to_buffer argv = [], filename = 'sample.adoc', &block
invoke_cli argv, filename, [StringIO.new, StringIO.new], &block
end
def invoke_cli argv = [], filename = 'sample.adoc', buffers = nil, &block
if filename.nil? || filename == '-' || (Pathname.new filename).absolute?
filepath = filename
else
filepath = fixture_path filename
end
invoker = Asciidoctor::Cli::Invoker.new argv + [filepath]
invoker.redirect_streams(*buffers) if buffers
invoker.invoke!(&block)
invoker
end
def redirect_streams
old_stdout, $stdout = $stdout, StringIO.new
old_stderr, $stderr = $stderr, StringIO.new
old_logger = Asciidoctor::LoggerManager.logger
old_logger_level = old_logger.level
new_logger = (Asciidoctor::LoggerManager.logger = Asciidoctor::Logger.new $stderr)
new_logger.level = old_logger_level
yield $stdout, $stderr
ensure
$stdout, $stderr = old_stdout, old_stderr
Asciidoctor::LoggerManager.logger = old_logger
end
def resolve_localhost
Socket.ip_address_list.find(&:ipv4?).ip_address
end
def using_memory_logger level = nil
old_logger = Asciidoctor::LoggerManager.logger
memory_logger = Asciidoctor::MemoryLogger.new
memory_logger.level = level if level
begin
Asciidoctor::LoggerManager.logger = memory_logger
yield memory_logger
ensure
Asciidoctor::LoggerManager.logger = old_logger
end
end
def in_verbose_mode
begin
old_logger_level, Asciidoctor::LoggerManager.logger.level = Asciidoctor::LoggerManager.logger.level, Logger::Severity::DEBUG
yield
ensure
Asciidoctor::LoggerManager.logger.level = old_logger_level
end
end
def asciidoctor_cmd ruby_args = nil
[Gem.ruby, *ruby_args, (File.join bindir, 'asciidoctor')]
end
def run_command cmd, *args, &block
if Array === cmd
args.unshift(*cmd)
cmd = args.shift
end
kw_args = Hash === args[-1] ? args.pop : {}
env = kw_args[:env]
(env ||= {})['RUBYOPT'] = nil unless kw_args[:use_bundler]
opts = { err: [:child, :out] }
if env
# NOTE while JRuby 9.2.10.0 implements support for unsetenv_others, it doesn't work in child
#if jruby? && (Gem::Version.new JRUBY_VERSION) < (Gem::Version.new '9.2.10.0')
if jruby?
begin
old_env, env = ENV, (ENV.merge env)
env.each {|key, val| env.delete key if val.nil? } if env.value? nil
ENV.replace env
IO.popen [cmd, *args, opts], &block
ensure
ENV.replace old_env
end
elsif env.value? nil
env = env.reduce ENV.to_h do |accum, (key, val)|
val.nil? ? (accum.delete key) : (accum[key] = val)
accum
end
IO.popen [env, cmd, *args, (opts.merge unsetenv_others: true)], &block
else
IO.popen [env, cmd, *args, opts], &block
end
else
IO.popen [cmd, *args, opts], &block
end
end
def using_test_webserver host = resolve_localhost, port = 9876
base_dir = testdir
server = TCPServer.new host, port
server_thread = Thread.start do
while (session = server.accept)
request = session.gets
if /^GET (\S+) HTTP\/1\.1$/ =~ request.chomp
resource = (resource = $1) == '' ? '.' : resource
else
session.print %(HTTP/1.1 405 Method Not Allowed\r\nContent-Type: text/plain\r\n\r\n)
session.print %(405 - Method not allowed\n)
session.close
next
end
if resource == '/name/asciidoctor'
session.print %(HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n)
session.print %({"name": "asciidoctor"}\n)
elsif File.file?(resource_file = (File.join base_dir, resource))
mimetype = if (ext = File.extname(resource_file)[1..-1])
ext == 'adoc' ? 'text/plain' : %(image/#{ext})
else
'text/plain'
end
session.print %(HTTP/1.1 200 OK\r\nContent-Type: #{mimetype}\r\n\r\n)
File.open resource_file, Asciidoctor::FILE_READ_MODE do |fd|
until fd.eof? do
buffer = fd.read 256
session.write buffer
end
end
else
session.print %(HTTP/1.1 404 File Not Found\r\nContent-Type: text/plain\r\n\r\n)
session.print %(404 - Resource not found.\n)
end
session.close
end
end
begin
yield
ensure
server_thread.exit
server_thread.value
server.close
end
end
end
###
#
# Context goodness provided by @citrusbyte's contest.
# See https://github.com/citrusbyte/contest
#
###
# Contest adds +teardown+, +test+ and +context+ as class methods, and the
# instance methods +setup+ and +teardown+ now iterate on the corresponding
# blocks. Note that all setup and teardown blocks must be defined with the
# block syntax. Adding setup or teardown instance methods defeats the purpose
# of this library.
class Minitest::Test
class << self
def setup &block
define_method :setup do
super(&block)
instance_eval(&block)
end
end
def teardown &block
define_method :teardown do
instance_eval(&block)
super(&block)
end
end
def context name, opts = {}, &block
if opts.key? :if
return unless opts[:if]
elsif opts.key? :unless
return if opts[:unless]
end
subclass = Class.new self
remove_tests subclass
subclass.class_eval(&block) if block_given?
const_set (context_name name), subclass
end
def test name, opts = {}, &block
if opts.key? :if
return unless opts[:if]
elsif opts.key? :unless
return if opts[:unless]
end
define_method (test_name name), &block
end
def remove_tests subclass
subclass.public_instance_methods.each do |m|
subclass.send :undef_method, m if m.to_s.start_with? 'test_'
end
end
alias should test
alias describe context
private
def context_name name
%(Test#{(sanitize_name name).gsub(/(^| )(\w)/) { $2.upcase }}).to_sym
end
def test_name name
%(test_#{((sanitize_name name).gsub %r/\s+/, '_')}).to_sym
end
def sanitize_name name
(name.gsub %r/\W+/, ' ').strip
end
end
end
def context name, &block
Minitest::Test.context name, &block
end