# module Hoe::Test

Test plugin for hoe.

### Tasks Provided:¶ ↑

audit

  Run ZenTest against the package.

default

  Run the default task(s).

multi

  Run the test suite using multiruby.

test

  Run the test suite.

test_deps

  Show which test files fail when run alone.

### Constants

FILTER

  Used to add flags to test_unit (e.g., -n test_borked).

  eg FILTER=“-n test_blah”

SUPPORTED_TEST_FRAMEWORKS

  Configuration for the supported test frameworks for test task.

### Attributes

multiruby_skip[RW]

Optional: Array of incompatible versions for multiruby filtering. Used as a regex.

rspec_dirs[RW]

Optional: RSpec dirs. [default: %w(spec lib)]

rspec_options[RW]

Optional: RSpec options. [default: []]

test_prelude[RW]

Optional: Additional ruby to run before the test framework is loaded.

testlib[RW]

Optional: What test library to require [default: :minitest]

### Public Instance Methods

define_test_tasks() click to toggle source

Define tasks for plugin.

    # File lib/hoe/test.rb, line 75
    def define_test_tasks
      default_tasks = []

      task :test

      if File.directory? "test" then
        desc 'Run the test suite. Use FILTER or TESTOPTS to add flags/args.'
        task :test do
          ruby make_test_cmd
        end

        desc 'Print out the test command. Good for profiling and other tools.'
        task :test_cmd do
          puts make_test_cmd
        end

        desc 'Run the test suite using multiruby.'
        task :multi do
          ENV["EXCLUDED_VERSIONS"] = multiruby_skip.join(":")
          system "multiruby -S rake"
        end

        desc 'Show which test files fail when run alone.'
        task :test_deps do
          tests = Dir[*self.test_globs].uniq

          paths = ['bin', 'lib', 'test'].join(File::PATH_SEPARATOR)
          null_dev = Hoe::WINDOZE ? '> NUL 2>&1' : '> /dev/null 2>&1'

          tests.each do |test|
            if not system "ruby -I#{paths} #{test} #{null_dev}" then
              puts "Dependency Issues: #{test}"
            end
          end
        end

        if testlib == :minitest then
          desc "Show bottom 25 tests wrt time."
          task "test:slow" do
            sh "rake TESTOPTS=-v | sort -n -k2 -t= | tail -25"
          end
        end

        default_tasks << :test
      end

      if File.directory? "spec" then
        found = try_loading_rspec2 || try_loading_rspec1

        if found then
          default_tasks << :spec
        else
          warn "Found spec dir, but couldn't load rspec (1 or 2) task. skipping."
        end
      end

      desc 'Run the default task(s).'
      task :default => default_tasks

      unless default_tasks.empty? then
        ##
        # This is for Erik Hollensbe's rubygems-test project. Hoe is
        # test-happy, so by using this plugin you're already testable. For
        # more information, see: <https://github.com/erikh/rubygems-test>
        # and/or <http://www.gem-testers.org/>

        gemtest = ".gemtest"

        gemtest.encode!(Encoding::UTF_8) if gemtest.respond_to?(:encoding)

        self.spec.files += [gemtest]

        pkg  = pkg_path
        turd = "#{pkg}/.gemtest"

        file turd => pkg_path do
          touch turd
        end

        file "#{pkg}.gem" => turd
      end

      desc 'Run ZenTest against the package.'
      task :audit do
        libs = %w(lib test ext).join(File::PATH_SEPARATOR)
        sh "zentest -I=#{libs} #{spec.files.grep(/^(lib|test)/).join(' ')}"
      end
    end

initialize_test() click to toggle source

Initialize variables for plugin.

    # File lib/hoe/test.rb, line 64
    def initialize_test
      self.multiruby_skip ||= []
      self.testlib        ||= :minitest
      self.test_prelude   ||= nil
      self.rspec_dirs     ||= %w(spec lib)
      self.rspec_options  ||= []
    end

make_test_cmd() click to toggle source

Generate the test command-line.

    # File lib/hoe/test.rb, line 167
    def make_test_cmd
      unless SUPPORTED_TEST_FRAMEWORKS.has_key?(testlib)
        raise "unsupported test framework #{testlib}"
      end

      framework = SUPPORTED_TEST_FRAMEWORKS[testlib]

      tests = ["rubygems"]
      tests << framework if framework
      tests << test_globs.sort.map { |g| Dir.glob(g) }
      tests.flatten!
      tests.map! {|f| %Q(require "#{f}")}

      tests.insert 1, test_prelude if test_prelude

      "#{Hoe::RUBY_FLAGS} -e '#{tests.join("; ")}' -- #{FILTER}"
    end

try_loading_rspec1() click to toggle source

Attempt to load RSpec 1, returning true if successful

    # File lib/hoe/test.rb, line 207
    def try_loading_rspec1
      require 'spec/rake/spectask'

      desc "Run all specifications"
      Spec::Rake::SpecTask.new(:spec) do |t|
        t.libs = self.rspec_dirs
        t.spec_opts = self.rspec_options
      end
      true
    rescue LoadError => err
      warn "%p while trying to load RSpec 1: %s" % [ err.class, err.message ]
      false
    end

try_loading_rspec2() click to toggle source

Attempt to load RSpec 2, returning true if successful

    # File lib/hoe/test.rb, line 188
    def try_loading_rspec2
      require 'rspec/core/rake_task'

      desc "Run all specifications"
      RSpec::Core::RakeTask.new(:spec) do |t|
        t.rspec_opts = self.rspec_options
        t.rspec_opts << "-I#{self.rspec_dirs.join(":")}" unless
        rspec_dirs.empty?
      end

      true
    rescue LoadError => err
      warn "%p while trying to load RSpec 2: %s" % [ err.class, err.message ]
      false
    end