Skip to content

Commit

Permalink
Switch to POpen4 for invoking the compressor and raise on non-zero ex…
Browse files Browse the repository at this point in the history
…it status
  • Loading branch information
sstephenson committed Mar 30, 2011
1 parent a7c0f8f commit 610395b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/yui/compressor.rb
@@ -1,4 +1,5 @@
require "open3"
require "popen4"
require "shellwords"
require "stringio"

module YUI #:nodoc:
Expand All @@ -9,7 +10,7 @@ class Error < StandardError; end
class OptionError < Error; end
class RuntimeError < Error; end

attr_reader :options, :command
attr_reader :options

def self.default_options #:nodoc:
{ :charset => "utf-8", :line_break => nil }
Expand All @@ -24,6 +25,10 @@ def initialize(options = {}) #:nodoc:
@command = [path_to_java, "-jar", path_to_jar_file, *(command_option_for_type + command_options)]
end

def command #:nodoc:
@command.map { |word| Shellwords.escape(word) }.join(" ")
end

# Compress a stream or string of code with YUI Compressor. (A stream is
# any object that responds to +read+ and +close+ like an IO.) If a block
# is given, you can read the compressed code from the block's argument.
Expand Down Expand Up @@ -59,21 +64,28 @@ def initialize(options = {}) #:nodoc:
#
def compress(stream_or_string)
streamify(stream_or_string) do |stream|
Open3.popen3(*command) do |stdin, stdout, stderr|
output = true
status = POpen4.popen4(command, "b") do |stdout, stderr, stdin, pid|
begin
stdin.binmode
transfer(stream, stdin)

if block_given?
yield stdout
else
stdout.read
output = stdout.read
end

rescue Exception => e
raise RuntimeError, "compression failed"
end
end

if status.exitstatus.zero?
output
else
raise RuntimeError, "compression failed"
end
end
end

Expand Down Expand Up @@ -110,6 +122,7 @@ def transfer(from_stream, to_stream)
while buffer = from_stream.read(4096)
to_stream.write(buffer)
end
from_stream.close
to_stream.close
end

Expand Down
9 changes: 9 additions & 0 deletions test/compressor_test.rb
Expand Up @@ -34,6 +34,8 @@ class CompressorTest < Test::Unit::TestCase
})("hello");
END_JS

FIXTURE_ERROR_JS = "var x = {class: 'name'};"

def test_compressor_should_raise_when_instantiated
assert_raises YUI::Compressor::Error do
YUI::Compressor.new
Expand Down Expand Up @@ -101,5 +103,12 @@ def test_preserve_semicolons_option_should_preserve_semicolons
@compressor = YUI::JavaScriptCompressor.new(:preserve_semicolons => true)
assert_equal "var Foo={a:1};Foo.bar=(function(baz){if(false){doSomething();}else{for(var index=0;index<baz.length;index++){doSomething(baz[index]);}}})(\"hello\");", @compressor.compress(FIXTURE_JS)
end

def test_compress_should_raise_on_javascript_syntax_error
@compressor = YUI::JavaScriptCompressor.new
assert_raise YUI::Compressor::RuntimeError do
@compressor.compress(FIXTURE_ERROR_JS)
end
end
end
end
1 change: 1 addition & 0 deletions yui-compressor.gemspec
Expand Up @@ -11,4 +11,5 @@ Gem::Specification.new do |s|
s.authors = ["Sam Stephenson"]
s.files = Dir["Rakefile", "lib/**/*", "test/**/*"]
s.test_files = Dir["test/*_test.rb"] unless $SAFE > 0
s.add_dependency "POpen4", ">= 0.1.4"
end

0 comments on commit 610395b

Please sign in to comment.