# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |c|
  c.treat_symbols_as_metadata_keys_with_true_values = true
  c.run_all_when_everything_filtered = true
  c.filter_run :focus

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  c.order = 'random'

  c.before :suite do
    compile_opal_coffee.should be_true
  end

  def compile_opal_coffee
    system "coffee -c #{File.expand_path('../../lib/opal-node.coffee', __FILE__)}"
  end

  def opal_node path, include_dir = nil
    base_dir = File.expand_path('../..', __FILE__)
    node_path = [include_dir, "#{base_dir}/lib", '$NODE_PATH'].compact
    command = %Q{NODE_PATH="#{node_path.join(':')}" node #{base_dir}/bin/opal-node #{path}}
    `#{command}`
  end

  def a_file_with_name name, options
    require 'tempfile'
    path = File.expand_path("../../tmp/#{name}", __FILE__)
    contents = options[:and_contents]
    File.open(path, 'w') { |file| file << contents }
    path
  end

  def run code
    rb_file = a_file_with_name 'rb_file.rb', and_contents: code
    opal_node(rb_file, File.dirname(rb_file))
  end

  def from_dir_of file
    pwd = Dir.pwd
    Dir.chdir File.dirname(file)
    yield
    Dir.chdir pwd
  end
end


