Class: MSpec::Opal::Environment

Inherits:
Opal::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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'opal/lib/mspec/opal/rake_task.rb', line 107

def initialize(basedir = nil, pattern = nil)
  ::Opal::Processor.arity_check_enabled = true
  ::Opal::Processor.dynamic_require_severity = :ignore
  super()
  @pattern = pattern || DEFAULT_PATTERN
  @basedir = basedir = File.expand_path(basedir || DEFAULT_BASEDIR)
  append_path basedir
  append_path "#{basedir}/rubyspec"
  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



105
106
107
# File 'opal/lib/mspec/opal/rake_task.rb', line 105

def basedir
  @basedir
end

#patternObject (readonly)

Returns the value of attribute pattern



105
106
107
# File 'opal/lib/mspec/opal/rake_task.rb', line 105

def pattern
  @pattern
end

Instance Method Details

#add_files(specs) ⇒ Object



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

def add_files specs
  files.concat specs.flatten
end

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



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

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

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



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

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



145
146
147
# File 'opal/lib/mspec/opal/rake_task.rb', line 145

def files
  @files ||= []
end

#files_to_run(pattern = nil) ⇒ Object



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

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")

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

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

#paths_from_glob(pattern) ⇒ Object



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

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

#rubyspec_pathsObject



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

def rubyspec_paths
  File.read("#{basedir}/rubyspecs").split("\n").reject do |line|
    line.empty? || line.start_with?('#')
  end
end

#specsObject



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

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

#stubsObject



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

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