Class: MSpec::Opal::Environment

Inherits:
Sprockets::Environment
  • Object
show all
Defined in:
opal/lib/mspec/opal/rake_task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Environment



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'opal/lib/mspec/opal/rake_task.rb', line 99

def initialize(basedir = nil, pattern = nil)
  ::Opal::Processor.arity_check_enabled = true
  ::Opal::Processor.dynamic_require_severity = :error

  @pattern = pattern
  @basedir = basedir = File.expand_path(basedir || DEFAULT_BASEDIR)

  ::Opal.append_path basedir
  ::Opal.use_gem 'mspec'

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

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

  super()

  ::Opal.paths.each { |p| append_path p }
end

Instance Attribute Details

#basedirObject (readonly)

Returns the value of attribute basedir



97
98
99
# File 'opal/lib/mspec/opal/rake_task.rb', line 97

def basedir
  @basedir
end

#patternObject (readonly)

Returns the value of attribute pattern



97
98
99
# File 'opal/lib/mspec/opal/rake_task.rb', line 97

def pattern
  @pattern
end

Instance Method Details

#add_files(specs, tag = '') ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'opal/lib/mspec/opal/rake_task.rb', line 146

def add_files specs, tag = ''
  tag = "[#{tag}] "
  puts "#{tag}Adding #{specs.size} spec files..."
  specs = specs.flatten.map do |path|
    dirname = File.join([basedir, path])
    if File.directory? dirname
      rubyspec_paths_in_dir(dirname, path)
    else
      path
    end
  end.flatten
  files.concat specs
end

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



137
138
139
140
# File 'opal/lib/mspec/opal/rake_task.rb', line 137

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



200
201
202
203
204
205
# File 'opal/lib/mspec/opal/rake_task.rb', line 200

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



142
143
144
# File 'opal/lib/mspec/opal/rake_task.rb', line 142

def files
  @files ||= []
end

#files_to_run(pattern = nil) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'opal/lib/mspec/opal/rake_task.rb', line 180

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"), :filters

  if pattern
    # add custom opal specs from spec/
    add_files paths_from_glob(pattern) & rubyspec_white_list, :rubyspec_custom
    add_files paths_from_glob(pattern).grep(/(?!spec\/(corelib|stdlib)\/)/), :other_custom

  else
    # add opal specific specs
    add_files paths_from_glob("#{basedir}/opal/**/*_spec.rb"),       :shared
    add_files paths_from_glob("#{basedir}/lib/lexer_spec.rb"),       :lexer
    add_files paths_from_glob("#{basedir}/lib/parser/**/*_spec.rb"), :parser

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

#paths_from_glob(pattern) ⇒ Object



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

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

#rubyspec_paths_in_dir(dirname, path) ⇒ Object



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

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



174
175
176
177
178
# File 'opal/lib/mspec/opal/rake_task.rb', line 174

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

#specsObject



133
134
135
# File 'opal/lib/mspec/opal/rake_task.rb', line 133

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

#stubsObject



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

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