class QUnited::RakeTask

Attributes

driver[RW]

The driver to use to run the QUnit tests.

fail_on_test_failure[RW]

Fail rake task when tests fail.

default:

true
fixture_files[RW]

Array or glob pattern of fixture files. These are included under the qunit-fixture element on the test page. These will be included in order if specified as an array.

helper_files[RW]

Array or glob pattern of test helper files. These include extra libraries for mocks or other test tools. These are loaded after source files and before test files. These will be loaded in order if specified as an array.

name[RW]

Name of task.

default:

:qunited
server_port[RW]

The port to use if running the server.

default:

3040
source_files[RW]

Array or glob pattern of JavaScript source files (and any dependencies). These will be loaded in order if specified as an array.

test_files[RW]

Array or glob pattern of QUnit test files. These will be loaded in order if specified as an array.

verbose[RW]

Use verbose output. If this is true, the task will print the QUnited command to stdout.

default:

true

Public Class Methods

new(*args) { |self| ... } click to toggle source
# File lib/qunited/rake_task.rb, line 62
def initialize(*args)
  @name = args.shift || :qunited
  @verbose = true
  @fail_on_test_failure = true
  @server_port = nil

  yield self if block_given?

  desc('Run QUnit JavaScript tests') unless ::Rake.application.last_comment

  task name do
    RakeFileUtils.send(:verbose, verbose) do
      if source_files_to_include.empty?
        msg = if source_files.is_a? String
          "No JavaScript source files match the pattern '#{source_files}'"
        else
          'No JavaScript source files specified'
        end
        fail msg
      elsif test_files_to_run.empty?
        msg = if test_files.is_a? String
          "No QUnit test files match the pattern '#{test_files}'"
        else
          'No QUnit test files specified'
        end
        fail msg
      else
        command = test_command
        puts command if verbose
        success = system(command)

        unless success
          if $?.exitstatus == 10
            # 10 is our test failure status code
            fail 'QUnit tests failed' if @fail_on_test_failure
          else
            # Other status codes should mean unexpected crashes
            fail 'Something went wrong when running tests with QUnited'
          end
        end
      end
    end
  end

  desc('Run server for QUnit JavaScript tests')

  task (name.to_s + ':server') do
    require 'qunited/server'
    server_options = {
      :source_files => source_and_helper_files,
      :test_files => test_files_to_run,
      :fixture_files => fixture_files_to_include,
      :verbose => verbose
    }
    server_options[:port] = @server_port if @server_port
    ::QUnited::Server.new(server_options).start
  end
end

Public Instance Methods

source_files_pattern=(pattern) click to toggle source

DEPRECATED: Please use source_files=, which now takes either an array of files or a glob pattern string.

# File lib/qunited/rake_task.rb, line 13
def source_files_pattern=(pattern)
  warn 'source_files_pattern= is deprecated in QUnited rake task config, use source_files= with a pattern'
  @source_files = pattern
end
test_files_pattern=(pattern) click to toggle source

DEPRECATED: Please use test_files=, which now takes either an array of files or a glob pattern string.

# File lib/qunited/rake_task.rb, line 24
def test_files_pattern=(pattern)
  warn 'test_files_pattern= is deprecated in QUnited rake task config, use test_files= with a pattern'
  @test_files = pattern
end

Private Instance Methods

files_array(files) click to toggle source

Force convert to array of files if glob pattern

# File lib/qunited/rake_task.rb, line 151
def files_array(files)
  return [] unless files
  files.is_a?(Array) ? files : pattern_to_filelist(files.to_s)
end
fixture_files_to_include() click to toggle source
# File lib/qunited/rake_task.rb, line 138
def fixture_files_to_include
  files_array fixture_files
end
helper_files_to_include() click to toggle source
# File lib/qunited/rake_task.rb, line 134
def helper_files_to_include
  files_array helper_files
end
pattern_to_filelist(pattern) click to toggle source
# File lib/qunited/rake_task.rb, line 156
def pattern_to_filelist(pattern)
  FileList[pattern].map { |f| f.gsub(/"/, '\"').gsub(/'/, "\\\\'") }
end
source_and_helper_files() click to toggle source
# File lib/qunited/rake_task.rb, line 146
def source_and_helper_files
  source_files_to_include + helper_files_to_include
end
source_files_to_include() click to toggle source
# File lib/qunited/rake_task.rb, line 130
def source_files_to_include
  files_array source_files
end
test_command() click to toggle source
# File lib/qunited/rake_task.rb, line 123
def test_command
  cmd = 'qunited'
  cmd << " --driver #{driver}" if driver
  cmd << " --fixtures #{fixture_files_to_include.join(',')}" if fixture_files
  cmd << " #{source_and_helper_files.join(' ')} -- #{test_files_to_run.join(' ')}"
end
test_files_to_run() click to toggle source
# File lib/qunited/rake_task.rb, line 142
def test_files_to_run
  files_array test_files
end