sibilant 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sibilant.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  require "sibilant/version"
2
2
  require 'json'
3
+ require 'open3'
3
4
 
4
5
  module Sibilant
6
+ class CompilationError < RuntimeError; end
7
+
5
8
  class << self
6
9
  def [](sibilant_code)
7
10
  Sibilant::Compiler.new.translate sibilant_code
@@ -30,11 +33,16 @@ module Sibilant
30
33
  end
31
34
 
32
35
  def translate(sibilant_code)
33
- IO.popen("#{sibilant_cli} -i", 'r+') do |sibilant|
34
- sibilant.puts sibilant_code
35
- sibilant.close_write
36
- sibilant.read
37
- end.strip
36
+ Open3.popen3 sibilant_cli, '-i' do |i,o,e,t|
37
+ i.puts sibilant_code
38
+ i.close_write
39
+
40
+ if t.value.success?
41
+ o.read.strip
42
+ else
43
+ raise Sibilant::CompilationError.new(e.read.strip)
44
+ end
45
+ end
38
46
  end
39
47
  end
40
48
  end
@@ -5,6 +5,30 @@ module Sinatra
5
5
  def sibilant(*args)
6
6
  content_type 'application/javascript'
7
7
  render :sibilant, *args
8
+ rescue ::Sibilant::CompilationError => error
9
+ if settings.development?
10
+ display_sibilant_compilation_error error
11
+ else
12
+ 500
13
+ end
14
+ end
15
+
16
+ def display_sibilant_compilation_error(error)
17
+ <<-END_JS
18
+ ;(function() {
19
+ var pre = document.createElement('PRE');
20
+ pre.innerHTML = '<h1>Sibilant Compilation Error</h1><code>' +
21
+ '#{Rack::Utils.escape_html(error.message).gsub("'", "\\'").gsub("\n", "\\n")}' +
22
+ '</code>';
23
+ pre.style.background = 'rgba(255,0,0,0.5)';
24
+ pre.style.position = 'absolute';
25
+ pre.style.padding = '25px';
26
+ pre.style.top = '10px';
27
+ pre.style.left = '10px';
28
+ pre.style.margin = '0';
29
+ document.body.appendChild(pre);
30
+ })();
31
+ END_JS
8
32
  end
9
33
  end
10
34
  end
@@ -1,3 +1,3 @@
1
1
  module Sibilant
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -34,5 +34,19 @@ describe Sibilant do
34
34
  @compiler.should_receive(:package_json).once.and_return version: test_version
35
35
  @compiler.version.should eq(test_version)
36
36
  end
37
+
38
+ describe 'on compilation failure' do
39
+ it 'should raise a Sibilant::CompilationError' do
40
+ expect { @compiler.translate('(foo') }.to raise_error(Sibilant::CompilationError)
41
+ end
42
+
43
+ it 'should pass through the error message' do
44
+ begin
45
+ @compiler.translate('(foo')
46
+ rescue => error
47
+ error.message.should match(/Error: unexpected EOF, probably missing a \)/)
48
+ end
49
+ end
50
+ end
37
51
  end
38
52
  end
@@ -8,6 +8,7 @@ class SibilantApp < Sinatra::Base
8
8
  set :views, ->{ root }
9
9
  get('/fixture.js') { sibilant :fixture }
10
10
  get('/inline.js') { sibilant '(alert "hello world")' }
11
+ get('/compilation-error.js') { sibilant '(defun' }
11
12
  end
12
13
 
13
14
  describe SibilantApp do
@@ -45,4 +46,40 @@ describe SibilantApp do
45
46
  last_response.body.should == 'thisIsSibilantCode((1 + 2 + 3));'
46
47
  end
47
48
  end
49
+
50
+ describe 'error handling' do
51
+ describe 'in development mode' do
52
+ before :each do
53
+ app.set :environment, :development
54
+ get '/compilation-error.js'
55
+ end
56
+
57
+ it 'should succeed' do
58
+ last_response.should be_ok
59
+ end
60
+
61
+ it 'should display the error' do
62
+ last_response.body.should match(/unexpected EOF/)
63
+ end
64
+
65
+ it 'should still render javascript' do
66
+ last_response.content_type.should match('application/javascript')
67
+ end
68
+
69
+ it 'should display a nice header message' do
70
+ last_response.body.should match('Sibilant Compilation Error')
71
+ end
72
+ end
73
+
74
+ describe 'in production mode' do
75
+ before :each do
76
+ app.set :environment, :production
77
+ get '/compilation-error.js'
78
+ end
79
+
80
+ it 'should error' do
81
+ last_response.should_not be_ok
82
+ end
83
+ end
84
+ end
48
85
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sibilant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-04 00:00:00.000000000 Z
12
+ date: 2013-06-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler