Class: MSpecScript

Inherits:
Object show all
Defined in:
lib/mspec/utils/script.rb

Overview

MSpecScript provides a skeleton for all the MSpec runner scripts.

Direct Known Subclasses

MSpecCI, MSpecMain, MSpecRun, MSpecTag

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMSpecScript

Returns a new instance of MSpecScript.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mspec/utils/script.rb', line 27

def initialize
  config[:formatter] = nil
  config[:includes]  = []
  config[:excludes]  = []
  config[:patterns]  = []
  config[:xpatterns] = []
  config[:tags]      = []
  config[:xtags]     = []
  config[:profiles]  = []
  config[:xprofiles] = []
  config[:atags]     = []
  config[:astrings]  = []
  config[:ltags]     = []
  config[:abort]     = true
end

Class Method Details

.configObject

Returns the config object. Maintained at the class level to easily enable simple config files. See the class method set.



9
10
11
12
13
14
# File 'lib/mspec/utils/script.rb', line 9

def self.config
  @config ||= {
    :path => ['.', 'spec'],
    :config_ext => '.mspec'
  }
end

.mainObject

Instantiates an instance and calls the series of methods to invoke the script.



146
147
148
149
150
151
152
153
154
155
# File 'lib/mspec/utils/script.rb', line 146

def self.main
  $VERBOSE = nil unless ENV['OUTPUT_WARNINGS']
  script = new
  script.load_default
  script.load '~/.mspecrc'
  script.options
  script.signals
  script.register
  script.run
end

.set(key, value) ⇒ Object

Associates value with key in the config object. Enables simple config files of the form:

class MSpecScript
  set :target, "ruby"
  set :files, ["one_spec.rb", "two_spec.rb"]
end


23
24
25
# File 'lib/mspec/utils/script.rb', line 23

def self.set(key, value)
  config[key] = value
end

Instance Method Details

#configObject

Returns the config object maintained by the instance’s class. See the class methods set and config.



45
46
47
# File 'lib/mspec/utils/script.rb', line 45

def config
  MSpecScript.config
end

#entries(pattern) ⇒ Object

Resolves pattern as a file name, directory name or pattern. If it is a file name, returns the name as an entry in an array. If it is a directory, returns all *_spec.rb files in the directory and subdirectory. Otherwise, passes pattern to Dir[].



123
124
125
126
127
128
# File 'lib/mspec/utils/script.rb', line 123

def entries(pattern)
  expanded = File.expand_path(pattern)
  return [pattern] if File.file?(expanded)
  return Dir[pattern+"/**/*_spec.rb"].sort if File.directory?(expanded)
  Dir[pattern]
end

#files(list) ⇒ Object

Resolves each entry in list to a set of files. If the entry has a leading ‘^’ character, the list of files is subtracted from the list of files accumulated to that point.



133
134
135
136
137
138
139
140
141
142
# File 'lib/mspec/utils/script.rb', line 133

def files(list)
  list.inject([]) do |files, item|
    if item[0] == ?^
      files -= entries(item[1..-1])
    else
      files += entries(item)
    end
    files
  end
end

#load(target) ⇒ Object

Returns true if the file was located in config[:path], possibly appending +config. Returns false otherwise.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mspec/utils/script.rb', line 52

def load(target)
  names = [target]
  unless target[-6..-1] == config[:config_ext]
    names << target + config[:config_ext]
  end

  names.each do |name|
    return Kernel.load(name) if File.exist?(File.expand_path(name))

    config[:path].each do |dir|
      file = File.join dir, name
      return Kernel.load(file) if File.exist? file
    end
  end

  false
end

#load_defaultObject

Attempts to load a default config file. First tries to load ‘default.mspec’. If that fails, attempts to load a config file name constructed from the value of RUBY_ENGINE and the first two numbers in RUBY_VERSION. For example, on MRI 1.8.6, the file name would be ‘ruby.1.8.mspec’.



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mspec/utils/script.rb', line 75

def load_default
  return if load 'default.mspec'

  if Object.const_defined?(:RUBY_ENGINE)
    engine = RUBY_ENGINE
  else
    engine = 'ruby'
  end
  version = RUBY_VERSION.split('.')[0,2].join('.')

  load "#{engine}.#{version}.mspec"
end

#registerObject

Registers all filters and actions.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mspec/utils/script.rb', line 89

def register
  if config[:formatter].nil?
    config[:formatter] = @files.size < 50 ? DottedFormatter : FileFormatter
  end
  config[:formatter].new(config[:output]).register if config[:formatter]

  MatchFilter.new(:include, *config[:includes]).register    unless config[:includes].empty?
  MatchFilter.new(:exclude, *config[:excludes]).register    unless config[:excludes].empty?
  RegexpFilter.new(:include, *config[:patterns]).register   unless config[:patterns].empty?
  RegexpFilter.new(:exclude, *config[:xpatterns]).register  unless config[:xpatterns].empty?
  TagFilter.new(:include, *config[:tags]).register          unless config[:tags].empty?
  TagFilter.new(:exclude, *config[:xtags]).register         unless config[:xtags].empty?
  ProfileFilter.new(:include, *config[:profiles]).register  unless config[:profiles].empty?
  ProfileFilter.new(:exclude, *config[:xprofiles]).register unless config[:xprofiles].empty?

  DebugAction.new(config[:atags], config[:astrings]).register if config[:debugger]
  GdbAction.new(config[:atags], config[:astrings]).register   if config[:gdb]
end

#signalsObject

Sets up signal handlers. Only a handler for SIGINT is registered currently.



110
111
112
113
114
115
116
117
# File 'lib/mspec/utils/script.rb', line 110

def signals
  if config[:abort]
    Signal.trap "INT" do
      puts "\nProcess aborted!"
      exit! 1
    end
  end
end