Class: MSpecMain

Inherits:
MSpecScript show all
Defined in:
lib/mspec/commands/mspec.rb

Instance Method Summary collapse

Methods inherited from MSpecScript

config, #config, #custom_options, #custom_register, #entries, #files, get, #load, #load_default, main, set, #signals

Constructor Details

#initializeMSpecMain

Returns a new instance of MSpecMain.



14
15
16
17
18
19
20
21
22
# File 'lib/mspec/commands/mspec.rb', line 14

def initialize
  config[:includes] = []
  config[:requires] = []
  config[:target]   = ENV['RUBY'] || 'ruby'
  config[:flags]    = []
  config[:command]  = nil
  config[:options]  = []
  config[:launch]   = []
end

Instance Method Details

#fork(&block) ⇒ Object



98
99
100
# File 'lib/mspec/commands/mspec.rb', line 98

def fork(&block)
  parallel ? Kernel.fork(&block) : block.call
end

#multi_exec(argv) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/mspec/commands/mspec.rb', line 127

def multi_exec(argv)
  timer = TimerAction.new
  timer.start

  files = config[:ci_files].inject([]) do |list, item|
    name = tmp "mspec-ci-multi-#{list.size}"

    rest = argv + ["-o", name, item]
    fork { system [config[:target], *rest].join(" ") }

    list << name
  end

  Process.waitall
  timer.finish
  report files, timer
end

#options(argv = ARGV) ⇒ Object



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
# File 'lib/mspec/commands/mspec.rb', line 24

def options(argv=ARGV)
  config[:command] = argv.shift if ["ci", "run", "tag"].include?(argv[0])

  options = MSpecOptions.new "mspec [COMMAND] [options] (FILE|DIRECTORY|GLOB)+", 30, config

  options.doc " The mspec command sets up and invokes the sub-commands"
  options.doc " (see below) to enable, for instance, running the specs"
  options.doc " with different implementations like ruby, jruby, rbx, etc.\n"

  options.configure do |f|
    load f
    config[:options] << '-B' << f
  end

  options.targets

  options.on("-D", "--gdb", "Run under gdb") do
    config[:use_gdb] = true
  end

  options.on("-A", "--valgrind", "Run under valgrind") do
    config[:use_valgrind] = true
  end

  options.on("--warnings", "Don't supress warnings") do
    config[:flags] << '-w'
    ENV['OUTPUT_WARNINGS'] = '1'
  end

  options.on("-j", "--multi", "Run multiple (possibly parallel) subprocesses") do
    config[:multi] = true
    config[:options] << "-fy"
  end

  options.version MSpec::VERSION do
    if config[:command]
      config[:options] << "-v"
    else
      puts "#{File.basename $0} #{MSpec::VERSION}"
      exit
    end
  end

  options.help do
    if config[:command]
      config[:options] << "-h"
    else
      puts options
      exit 1
    end
  end

  options.doc "\n Custom options"
  custom_options options

  # The rest of the help output
  options.doc "\n where COMMAND is one of:\n"
  options.doc "   run - Run the specified specs (default)"
  options.doc "   ci  - Run the known good specs"
  options.doc "   tag - Add or remove tags\n"
  options.doc " mspec COMMAND -h for more options\n"
  options.doc "   example: $ mspec run -h\n"

  options.on_extra { |o| config[:options] << o }
  config[:options].concat options.parse(argv)
end

#parallelObject



93
94
95
96
# File 'lib/mspec/commands/mspec.rb', line 93

def parallel
  @parallel ||= !(Object.const_defined?(:JRUBY_VERSION) ||
                /(mswin|mingw)/ =~ RUBY_PLATFORM)
end

#registerObject



91
# File 'lib/mspec/commands/mspec.rb', line 91

def register; end

#report(files, timer) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mspec/commands/mspec.rb', line 102

def report(files, timer)
  require 'yaml'

  exceptions = []
  tally = Tally.new

  files.each do |file|
    d = File.open(file, "r") { |f| YAML.load f }
    File.delete file

    exceptions += Array(d['exceptions'])
    tally.files!        d['files']
    tally.examples!     d['examples']
    tally.expectations! d['expectations']
    tally.errors!       d['errors']
    tally.failures!     d['failures']
  end

  print "\n"
  exceptions.each_with_index do |exc, index|
    print "\n#{index+1})\n", exc, "\n"
  end
  print "\n#{timer.format}\n\n#{tally.format}\n"
end

#runObject



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
# File 'lib/mspec/commands/mspec.rb', line 145

def run
  ENV['MSPEC_RUNNER'] = '1'
  ENV['RUBY_EXE']     = config[:target]
  ENV['RUBY_FLAGS']   = config[:flags].join " "

  argv = []

  argv.concat config[:launch]
  argv.concat config[:flags]
  argv.concat config[:includes]
  argv.concat config[:requires]
  argv << "-v"
  argv << "#{MSPEC_HOME}/bin/mspec-#{ config[:command] || "run" }"
  argv.concat config[:options]

  if config[:multi] and config[:command] == "ci"
    multi_exec argv
  else
    if config[:use_valgrind]
      more = ["--db-attach=#{config[:use_gdb] ? 'yes' : 'no'}", config[:target]] + argv
      exec "valgrind", *more
    elsif config[:use_gdb]
      more = ["--args", config[:target]] + argv
      exec "gdb", *more
    else
      cmd, *rest = config[:target].split(/\s+/)
      argv = rest + argv unless rest.empty?
      exec cmd, *argv
    end
  end
end