aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/vendor/thor-0.11.2/lib/thor/tasks/spec.rb
blob: c7d00968e86fcaba2b2ef95447cb45926ee47399 (plain) (blame)
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
require "fileutils"

class Thor
  # Creates a spec task.
  #
  # ==== Parameters
  # files<Array> - Array of files to spec
  #
  # ==== Options
  # :name     - The name of the task. It can be rcov or spec. Spec is the default.
  # :rcov     - A hash with rcov specific options.
  # :rcov_dir - Where rcov reports should be printed.
  # :verbose  - Sets the default value for verbose, although it can be specified
  #             also through the command line.
  #
  # All other options are added to rspec.
  #
  def self.spec_task(files, options={})
    name        = (options.delete(:name) || 'spec').to_s
    tasks[name] = Thor::SpecTask.new(name, files, options)
  end

  class SpecTask < Task
    attr_accessor :name, :files, :rcov_dir, :rcov_config, :spec_config

    def initialize(name, files, config={})
      options = { :verbose => Thor::Option.parse(:verbose, config.delete(:verbose) || false) }
      super(name, "#{name.capitalize} task", name, options)

      @name        = name
      @files       = files.map{ |f| %["#{f}"] }.join(" ")
      @rcov_dir    = config.delete(:rdoc_dir) || File.join(Dir.pwd, 'coverage')
      @rcov_config = config.delete(:rcov) || {}
      @spec_config = { :format => 'specdoc', :color => true }.merge(config)
    end

    def run(instance, args=[])
      rcov_opts = Thor::Options.to_switches(rcov_config)
      spec_opts = Thor::Options.to_switches(spec_config)

      require 'rbconfig'
      cmd  = RbConfig::CONFIG['ruby_install_name'] << " "

      if rcov?
        FileUtils.rm_rf(rcov_dir)
        cmd << "-S #{where('rcov')} -o #{rcov_dir} #{rcov_opts} "
      end

      cmd << [where('spec'), rcov? ? " -- " : nil, files, spec_opts].join(" ")

      puts cmd if instance.options.verbose?
      system(cmd)
      exit($?.exitstatus)
    end

    private

      def rcov?
        name == "rcov"
      end

      def where(file)
        ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
          file_with_path = File.join(path, file)
          next unless File.exist?(file_with_path) && File.executable?(file_with_path)
          return File.expand_path(file_with_path)
        end
      end
  end
end