Class: MSpec::Opal::Environment

Inherits:
Opal::Environment show all
Defined in:
opal/lib/mspec/opal/rake_task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Opal::Environment

#use_gem

Constructor Details

#initialize(basedir = nil, pattern = nil) ⇒ Environment

Returns a new instance of Environment



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'opal/lib/mspec/opal/rake_task.rb', line 120

def initialize(basedir = nil, pattern = nil)
  ::Opal::Processor.arity_check_enabled = true
  ::Opal::Processor.dynamic_require_severity = :ignore
  super()
  @pattern = pattern
  @basedir = basedir = File.expand_path(basedir || DEFAULT_BASEDIR)
  append_path basedir
  use_gem 'mspec'

  stubs.each do |asset|
    ::Opal::Processor.stub_file asset
  end

  ENV['OPAL_SPEC'] ||= files_to_run(pattern).join(',')
end

Instance Attribute Details

#basedirObject (readonly)

Returns the value of attribute basedir



118
119
120
# File 'opal/lib/mspec/opal/rake_task.rb', line 118

def basedir
  @basedir
end

#patternObject (readonly)

Returns the value of attribute pattern



118
119
120
# File 'opal/lib/mspec/opal/rake_task.rb', line 118

def pattern
  @pattern
end

Instance Method Details

#add_files(specs) ⇒ Object



162
163
164
165
# File 'opal/lib/mspec/opal/rake_task.rb', line 162

def add_files specs
  puts "Adding #{specs.size} spec files..."
  files.concat specs.flatten
end

#build_min(file = "#{basedir}/build/specs.min.js") ⇒ Object



153
154
155
156
# File 'opal/lib/mspec/opal/rake_task.rb', line 153

def build_min file = "#{basedir}/build/specs.min.js"
  require 'opal/util'
  build ::Opal::Util.uglify(specs.to_s), file
end

#build_specs(file = "#{basedir}/build/specs.js") ⇒ Object



215
216
217
218
219
220
# File 'opal/lib/mspec/opal/rake_task.rb', line 215

def build_specs file = "#{basedir}/build/specs.js"
  code = specs.to_s
  FileUtils.mkdir_p File.dirname(file)
  puts "Building #{file}..."
  File.open(file, 'w+') { |o| o << code }
end

#filesObject



158
159
160
# File 'opal/lib/mspec/opal/rake_task.rb', line 158

def files
  @files ||= []
end

#files_to_run(pattern = nil) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'opal/lib/mspec/opal/rake_task.rb', line 198

def files_to_run(pattern=nil)
  # add any filters in spec/filters of specs we dont want to run
  add_files paths_from_glob("#{basedir}/filters/**/*.rb")

  if pattern
    # add custom opal specs from spec/
    add_files paths_from_glob(pattern) & rubyspec_paths

  else
    # add opal specific specs
    add_files paths_from_glob("#{basedir}/{opal}/**/*_spec.rb")

    # add any rubyspecs we want to run (defined in spec/rubyspecs)
    add_files rubyspec_paths
  end
end

#paths_from_glob(pattern) ⇒ Object



167
168
169
170
171
# File 'opal/lib/mspec/opal/rake_task.rb', line 167

def paths_from_glob pattern
  Dir.glob(File.expand_path(pattern)).map do |s|
    s.sub(/^#{basedir}\//, '').sub(/\.rb$/, '')
  end
end

#rubyspec_pathsObject



173
174
175
176
177
178
179
180
181
182
# File 'opal/lib/mspec/opal/rake_task.rb', line 173

def rubyspec_paths
  rubyspec_white_list.map do |path|
    dirname = File.join([basedir, path])
    if File.directory? dirname
      rubyspec_paths_in_dir(dirname, path)
    else
      path
    end
  end
end

#rubyspec_paths_in_dir(dirname, path) ⇒ Object



184
185
186
187
188
189
190
# File 'opal/lib/mspec/opal/rake_task.rb', line 184

def rubyspec_paths_in_dir(dirname, path)
  Dir.entries(dirname).select do |spec|
    spec.end_with? '.rb'
  end.map do |spec|
    File.join path, spec
  end
end

#rubyspec_white_listObject



192
193
194
195
196
# File 'opal/lib/mspec/opal/rake_task.rb', line 192

def rubyspec_white_list
  File.read("#{basedir}/rubyspecs").split("\n").reject do |line|
    line.sub(/#.*/, '').strip.empty?
  end
end

#specsObject



149
150
151
# File 'opal/lib/mspec/opal/rake_task.rb', line 149

def specs
  @specs ||= self['mspec/opal/main'] || raise("Cannot find mspec/opal/main inside #{paths.inspect}")
end

#stubsObject



136
137
138
139
140
141
142
143
144
145
146
147
# File 'opal/lib/mspec/opal/rake_task.rb', line 136

def stubs
  # missing stdlib
  stubs = %w[fileutils iconv yaml]

  # use x-strings which generate bad javascript
  stubs << "mspec/helpers/tmp"
  stubs << "mspec/helpers/environment"
  stubs << "mspec/guards/block_device"
  stubs << "mspec/guards/endian"

  stubs
end