# encoding: utf-8 # Set up gem loading (necessary for cri dependency) require File.dirname(__FILE__) + '/gem_loader.rb' # Load unit testing stuff require 'minitest/unit' require 'minitest/spec' require 'minitest/mock' require 'mocha/setup' require 'vcr' # Setup coverage require 'coveralls' Coveralls.wear! # Load nanoc $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib')) require 'nanoc' require 'nanoc/cli' require 'nanoc/tasks' # Load miscellaneous requirements require 'tmpdir' require 'stringio' VCR.configure do |c| c.cassette_library_dir = 'test/fixtures/vcr_cassettes' c.hook_into :webmock end module Nanoc::TestHelpers LIB_DIR = File.expand_path(File.dirname(__FILE__) + '/../lib') def disable_nokogiri? ENV.key?('DISABLE_NOKOGIRI') end def if_have(*libs) libs.each do |lib| if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby' && lib == 'nokogiri' && disable_nokogiri? skip 'Pure Java Nokogiri has issues that cause problems with nanoc (see https://github.com/nanoc/nanoc/pull/422) -- run without DISABLE_NOKOGIRI to enable Nokogiri tests' return end begin require lib rescue LoadError skip "requiring #{lib} failed" return end end yield end def if_implemented yield rescue NotImplementedError, NameError skip $ERROR_INFO return end def with_site(params = {}) # Build site name site_name = params[:name] if site_name.nil? @site_num ||= 0 site_name = "site-#{@site_num}" @site_num += 1 end # Build rules rules_content = < ...') end end File.open('nanoc.yaml', 'w') { |io| io.write('stuff: 12345') } File.open('Rules', 'w') { |io| io.write(rules_content) } end end # Yield site FileUtils.cd(site_name) do yield Nanoc::Site.new('.') end end def setup # Check skipped if ENV['skip'] if ENV['skip'].split(',').include?(self.class.to_s) skip 'manually skipped' end end # Clean up GC.start # Go quiet unless ENV['QUIET'] == 'false' @orig_stdout = $stdout @orig_stderr = $stderr $stdout = StringIO.new $stderr = StringIO.new end # Enter tmp @tmp_dir = Dir.mktmpdir('nanoc-test') @orig_wd = FileUtils.pwd FileUtils.cd(@tmp_dir) # Let us get to the raw errors Nanoc::CLI::ErrorHandler.disable end def teardown # Restore normal error handling Nanoc::CLI::ErrorHandler.enable # Exit tmp FileUtils.cd(@orig_wd) FileUtils.rm_rf(@tmp_dir) # Go unquiet unless ENV['QUIET'] == 'false' $stdout = @orig_stdout $stderr = @orig_stderr end end def capturing_stdio(&_block) # Store orig_stdout = $stdout orig_stderr = $stderr # Run $stdout = StringIO.new $stderr = StringIO.new yield { stdout: $stdout.string, stderr: $stderr.string } ensure # Restore $stdout = orig_stdout $stderr = orig_stderr end # Adapted from http://github.com/lsegal/yard-examples/tree/master/doctest def assert_examples_correct(object) P(object).tags(:example).each do |example| # Classify lines = example.text.lines.map do |line| [line =~ /^\s*# ?=>/ ? :result : :code, line] end # Join pieces = [] lines.each do |line| if !pieces.empty? && pieces.last.first == line.first pieces.last.last << line.last else pieces << line end end lines = pieces.map(&:last) # Test b = binding lines.each_slice(2) do |pair| actual_out = eval(pair.first, b) expected_out = eval(pair.last.match(/# ?=>(.*)/)[1], b) assert_equal expected_out, actual_out, "Incorrect example:\n#{pair.first}" end end end def assert_contains_exactly(expected, actual) assert_equal expected.size, actual.size, format('Expected %s to be of same size as %s', actual.inspect, expected.inspect) remaining = actual.dup.to_a expected.each do |e| index = remaining.index(e) remaining.delete_at(index) if index end assert remaining.empty?, format('Expected %s to contain all the elements of %s', actual.inspect, expected.inspect) end def assert_raises_frozen_error error = assert_raises(RuntimeError, TypeError) { yield } assert_match(/(^can't modify frozen |^unable to modify frozen object$)/, error.message) end def with_env_vars(hash, &_block) orig_env_hash = ENV.to_hash hash.each_pair { |k, v| ENV[k] = v } yield ensure orig_env_hash.each_pair { |k, v| ENV[k] = v } end def on_windows? Nanoc.on_windows? end def command?(cmd) which, null = on_windows? ? %w(where NUL) : ['which', '/dev/null'] system("#{which} #{cmd} > #{null} 2>&1") end def symlinks_supported? File.symlink nil, nil rescue NotImplementedError return false rescue return true end def skip_unless_have_command(cmd) skip "Could not find external command \"#{cmd}\"" unless command?(cmd) end def skip_unless_symlinks_supported skip 'Symlinks are not supported by Ruby on Windows' unless symlinks_supported? end end class Nanoc::TestCase < MiniTest::Unit::TestCase include Nanoc::TestHelpers end # Unexpected system exit is unexpected ::MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS.delete(SystemExit) # A more precise inspect method for Time improves assert failure messages. # class Time def inspect strftime("%a %b %d %H:%M:%S.#{format('%06d', usec)} %Z %Y") end end