Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal-rails
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 54b2625e911f
Choose a base ref
...
head repository: opal/opal-rails
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f05d36a4fcdc
Choose a head ref
  • 2 commits
  • 8 files changed
  • 2 contributors

Commits on Feb 24, 2015

  1. get rake opal:spec task working, change default location of specs

    outside of asset pipeline, handle compilation of files outside of asset
    pipeline, make location configurable, update docs
    fkchang committed Feb 24, 2015
    Copy the full SHA
    909d286 View commit details
  2. Merge pull request #36 from fkchang/master

    Improve spec running (rake, in browser and configurable)
    elia committed Feb 24, 2015
    Copy the full SHA
    f05d36a View commit details
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -138,12 +138,12 @@ Of course you need to require `haml-rails` separately since its presence is not

### Spec!

Add specs into `app/assets/javascripts/spec`:
Add specs into `/spec-opal`:

and then a spec folder with you specs!

```ruby
# app/assets/javascripts/spec/example_spec.js.rb
# spec-opal/example_spec.js.rb

describe 'a spec' do
it 'has successful examples' do
@@ -154,6 +154,23 @@ end

Then visit `/opal_spec` from your app and **reload at will** or use the command line with `rake opal:spec`.

#### CHANGE from versions pre 0.7.1

Specs used to run out of app/assets/javascripts/spec which was problematic because require_tree . would cause opal/sprockets to compile the specs for non spec running. This could result in bad specs that prevent your application Opal code from compiling, and even if it compiles you'll get exceptions about the test framework methods not being defined. To address this, specs have been moved out of the app/assets/javascripts to Rails.root/spec-opal. The name spec-opal was chosen to put it close to the spec directory. We don't want to put specs in spec directory because we don't want the "backend" rspec to run those.

The location of specs is configurable. To restore the old location of app/assets/javascripts/spec add an initializer file like the below.

```ruby
# config/initializers/opal.rb
Rails.application.config.opal.spec_location = "app/assets/javascripts/spec"

```

Similarly, you can put the opal specs in another location via a similar initializer call.




![1 examples, 0 failures](http://f.cl.ly/items/001n0V0g0u0v14160W2G/Schermata%2007-2456110%20alle%201.06.29%20am.png)


17 changes: 15 additions & 2 deletions app/controllers/opal_spec_controller.rb
Original file line number Diff line number Diff line change
@@ -4,6 +4,15 @@ class OpalSpecController < ActionController::Base
def run
end

def file
spec_file = Dir["#{spec_location}/#{params[:path]}*.{rb,opal}"].first
Opal.paths.concat Rails.application.config.assets.paths
builder = Opal::Builder.new
file = File.new spec_file
builder.build_str file.read, spec_file

render js: builder.to_s
end

private

@@ -25,8 +34,12 @@ def all_spec_files
end

def spec_files_for_glob glob = '**'
Dir[Rails.root.join("{app,lib}/assets/javascripts/spec/#{glob}.{rb,opal}")].map do |path|
path.split('assets/javascripts/spec/').flatten.last.gsub(/(\.rb|\.opal)/, '')
Dir[Rails.root.join("#{spec_location}/#{glob}.{rb,opal}")].map do |path|
path.split("#{spec_location}/").flatten.last.gsub(/(\.rb|\.opal)/, '')
end.uniq
end

def spec_location
Rails.application.config.opal.spec_location
end
end
11 changes: 11 additions & 0 deletions app/helpers/opal_helper.rb
Original file line number Diff line number Diff line change
@@ -4,4 +4,15 @@ def opal_tag(&block)
js_code = Opal.compile(opal_code)
javascript_tag js_code
end

def spec_include_tag(*sources)
options = sources.extract_options!.stringify_keys
path_options = options.extract!('protocol', 'extname').symbolize_keys
sources.uniq.map { |source|
tag_options = {
"src" => "/opal_spec_files/#{source}"
}.merge!(options)
content_tag(:script, "", tag_options)
}.join("\n").html_safe
end
end
2 changes: 1 addition & 1 deletion app/views/layouts/opal_spec.html.erb
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
<meta charset="utf-8">
<title>Opal Spec Runner</title>
<%= javascript_include_tag 'opal-rspec-runner' %>
<%= javascript_include_tag *spec_files.map { |f| "spec/#{f}"} %>
<%= spec_include_tag *spec_files.map { |f| "#{f}"} %>
<style>
body { font-family: sans-serif; }
</style>
11 changes: 11 additions & 0 deletions lib/assets/javascripts/sprockets_runner.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is used by Opal::Server to basically load all spec files that
# can be found in the spec/ directory.

require 'opal'
require 'opal-rspec'
<% spec_location = Rails.application.config.opal.spec_location %>
<% Dir.glob("#{spec_location}/**/*_spec*.{rb,opal}").each do |s| %>
require <%= s.sub(/^#{spec_location}\//, '').sub(/\.(rb|opal)$/, '').sub(/\.js/,'').inspect %>
<% end %>

Opal::RSpec::Runner.autorun
4 changes: 3 additions & 1 deletion lib/opal/rails/engine.rb
Original file line number Diff line number Diff line change
@@ -8,7 +8,8 @@ class Engine < ::Rails::Engine
config.app_generators.javascript_engine :opal

config.opal = ActiveSupport::OrderedOptions.new

# new default location, override-able in a Rails initializer
config.opal.spec_location = "spec-opal"

# Cache eager_load_paths now, otherwise the assets dir is added
# and its .rb files are eagerly loaded.
@@ -42,6 +43,7 @@ class Engine < ::Rails::Engine
end

get '/opal_spec' => 'opal_spec#run'
get '/opal_spec_files/*path' => 'opal_spec#file'
end
end

1 change: 1 addition & 0 deletions lib/tasks/opal-rails_tasks.rake
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ Opal::RSpec::RakeTask.new('opal:spec' => :environment) do |server|

server.sprockets.clear_paths
asset_paths << File.dirname(tempfile.path)
asset_paths << Rails.application.config.opal.spec_location
server.main = File.basename(tempfile.path, '.js.rb')

asset_paths.each { |path| server.append_path path }
2 changes: 2 additions & 0 deletions test_app/config/initializers/opal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# needs to be full pathname instead of relative pathname to run in the opal-rails spec
Rails.application.config.opal.spec_location = "#{Rails.root}/app/assets/javascripts/spec"