Skip to content

Commit

Permalink
Move spec runners into tasks/mspec.rake
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 25, 2013
1 parent 302ded2 commit b225ee6
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 119 deletions.
122 changes: 3 additions & 119 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ require 'bundler'
Bundler.require
Bundler::GemHelper.install_tasks

import 'tasks/mspec.rake'

namespace :github do
desc "Upload assets to github"
task :upload_assets do
require 'octokit'
# https://github.com/octokit/octokit.rb#oauth-access-tokens
Expand All @@ -21,91 +24,6 @@ namespace :github do
end
end

require 'rack'
require 'webrick'
require 'opal-sprockets'

# rubyspec uses a top level "language_version" to require relative specs.
# We can't do this at runtime, so we hijack the method (and make sure we only
# do this at the top level). We figure out which file we are including, and
# add it to our require list
class Opal::Nodes::CallNode
alias_method :mspec_handle_special, :handle_special

def handle_special
if meth == :language_version and scope.top?
lang_type = arglist[2][1]
target = "rubyspec/language/versions/#{lang_type}_1.9"

if File.exist?(target)
compiler.requires << target
end

return fragment("nil")
end

mspec_handle_special
end
end

class SpecEnvironment < Opal::Environment
def initialize
super
append_path 'spec'
append_path 'rubyspec'
use_gem 'mspec'
end

def specs
@specs ||= self['ospec/main'].to_s
end

def build_min file = 'build/specs.min.js'
build uglify(specs), file
end

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

class RunSpec
def initialize(file=nil)
Opal::Processor.arity_check_enabled = true

ENV['OPAL_SPEC'] = file.nil? ? ["#{Dir.pwd}/spec/"].join(',') : file.join(',')

build_specs

server = fork do
app = Rack::Builder.app do
use Rack::ShowExceptions
run Rack::Directory.new('.')
end

Rack::Server.start(:app => app, :Port => 9999, :AccessLog => [],
:Logger => WEBrick::Log.new("/dev/null"))
end

system "phantomjs \"spec/ospec/sprockets.js\" \"http://localhost:9999/spec/index.html\""
success = $?.success?

exit 1 unless success

rescue => e
puts e.message
ensure
Process.kill(:SIGINT, server)
Process.wait
end

def build_specs
SpecEnvironment.new.build
end
end

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:mri_spec) do |t|
t.pattern = 'mri_spec/**/*_spec.rb'
Expand All @@ -128,40 +46,6 @@ task :build_specs do
env.build
end

desc <<-DESC
Run task with spec:dir:file helper
Example: to run all antive specs in /spec/opal/native
type:
rake spec:opal:native
DESC
namespace :spec do
task 'dirs' do
end
rule '' do |task|

#build path for spec files\dirs.
#Example:
#spec:core => spec/core/
#spec:core:array:allocate => spec/core/array/allocate_spec.rb
def path(dirs)
path = "#{Dir.pwd}"
dirs.each do |dir|
base = path + "/#{dir}"
if Dir.exists?(base)
path = base
else
path = Dir.glob("#{base}_spec.rb")
end
end
path = [path].flatten
raise ArgumentError, "File or Dir with task #{dirs.join('/')} not found." if path.empty?
path
end

RunSpec.new(path(task.name.split(":")))
end
end

desc "Build opal.js and opal-parser.js to build/"
task :dist do
Opal::Processor.arity_check_enabled = false
Expand Down
118 changes: 118 additions & 0 deletions tasks/mspec.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
require 'rack'
require 'webrick'
require 'opal-sprockets'

# rubyspec uses a top level "language_version" to require relative specs.
# We can't do this at runtime, so we hijack the method (and make sure we only
# do this at the top level). We figure out which file we are including, and
# add it to our require list
class Opal::Nodes::CallNode
alias_method :mspec_handle_special, :handle_special

def handle_special
if meth == :language_version and scope.top?
lang_type = arglist[2][1]
target = "rubyspec/language/versions/#{lang_type}_1.9"

if File.exist?(target)
compiler.requires << target
end

return fragment("nil")
end

mspec_handle_special
end
end

class SpecEnvironment < Opal::Environment
def initialize
super
append_path 'spec'
append_path 'rubyspec'
use_gem 'mspec'
end

def specs
@specs ||= self['ospec/main'].to_s
end

def build_min file = 'build/specs.min.js'
build uglify(specs), file
end

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

class RunSpec
def initialize(file=nil)
Opal::Processor.arity_check_enabled = true

ENV['OPAL_SPEC'] = file.nil? ? ["#{Dir.pwd}/spec/"].join(',') : file.join(',')

build_specs

server = fork do
app = Rack::Builder.app do
use Rack::ShowExceptions
run Rack::Directory.new('.')
end

Rack::Server.start(:app => app, :Port => 9999, :AccessLog => [],
:Logger => WEBrick::Log.new("/dev/null"))
end

system "phantomjs \"spec/ospec/sprockets.js\" \"http://localhost:9999/spec/index.html\""
success = $?.success?

exit 1 unless success

rescue => e
puts e.message
ensure
Process.kill(:SIGINT, server)
Process.wait
end

def build_specs
SpecEnvironment.new.build
end
end

desc <<-DESC
Run task with spec:dir:file helper
Example: to run all antive specs in /spec/opal/native
type:
rake spec:opal:native
DESC
namespace :spec do
task 'dirs' do
end
rule '' do |task|

#build path for spec files\dirs.
#Example:
#spec:core => spec/core/
#spec:core:array:allocate => spec/core/array/allocate_spec.rb
def path(dirs)
path = "#{Dir.pwd}"
dirs.each do |dir|
base = path + "/#{dir}"
if Dir.exists?(base)
path = base
else
path = Dir.glob("#{base}_spec.rb")
end
end
path = [path].flatten
raise ArgumentError, "File or Dir with task #{dirs.join('/')} not found." if path.empty?
path
end

RunSpec.new(path(task.name.split(":")))
end
end

0 comments on commit b225ee6

Please sign in to comment.