Permalink
Browse files

Merge branch 'master' of github.com:documentcloud/closure-compiler

  • Loading branch information...
2 parents 18c44b6 + 8315c19 commit 4f99f4a96f1b828298cad840cec98a910b85f0d8 @jashkenas jashkenas committed Aug 12, 2011
View
@@ -1,12 +1,9 @@
-desc 'Run all tests'
+require "rake/testtask"
-task :default => :test
-task :test do
- $LOAD_PATH.unshift(File.expand_path('lib'))
- $LOAD_PATH.unshift(File.expand_path('test'))
- require 'redgreen' if Gem.available?('redgreen') and RUBY_VERSION < "1.9"
- require 'test/unit'
- Dir['test/**/test_*.rb'].each {|test| require test }
+Rake::TestTask.new do |t|
+ t.libs += ["lib", "test"]
+ t.test_files = FileList["test/**/*_test.rb"]
+ t.verbose = true
end
namespace :gem do
@@ -22,4 +19,6 @@ namespace :gem do
sh "sudo gem uninstall -x closure-compiler"
end
-end
+end
+
+task :default => :test
@@ -12,5 +12,4 @@ module Closure
end
-require 'stringio'
require 'closure/compiler'
@@ -1,4 +1,5 @@
-require 'closure/popen'
+require 'stringio'
+require 'tempfile'
module Closure
@@ -20,28 +21,28 @@ def initialize(options={})
# JavaScript as a string or yields an IO object containing the response to a
# block, for streaming.
def compile(io)
- result, error = nil, nil
- status = Closure::Popen.popen(command) do |stdin, stdout, stderr|
- if io.respond_to? :read
- while buffer = io.read(4096) do
- stdin.write(buffer)
- end
- else
- stdin.write(io.to_s)
- end
- stdin.close
- if Closure::Popen::WINDOWS
- stderr.close
- result = stdout.read
- error = "Stderr cannot be read on Windows."
- else
- out_thread = Thread.new { result = stdout.read }
- err_thread = Thread.new { error = stderr.read }
- out_thread.join and err_thread.join
+ tempfile = Tempfile.new('closure_compiler')
+ if io.respond_to? :read
+ while buffer = io.read(4096) do
+ tempfile.write(buffer)
end
- yield(StringIO.new(result)) if block_given?
+ else
+ tempfile.write(io.to_s)
+ end
+ tempfile.flush
+
+ begin
+ result = `#{command} --js #{tempfile.path}`
+ rescue Exception
+ raise Error, "compression failed"
+ ensure
+ tempfile.close!
end
- raise Error, error unless status.success?
+ unless $?.exitstatus.zero?
+ raise Error, result
+ end
+
+ yield(StringIO.new(result)) if block_given?
result
end
alias_method :compress, :compile
@@ -51,7 +52,13 @@ def compile(io)
# Serialize hash options to the command-line format.
def serialize_options(options)
- options.map {|k, v| ["--#{k}", v.to_s] }.flatten
+ options.map do |k, v|
+ if (v.is_a?(Array))
+ v.map {|v2| ["--#{k}", v2.to_s]}
+ else
+ ["--#{k}", v.to_s]
+ end
+ end.flatten
end
def command
View
@@ -1,66 +0,0 @@
-module Closure
-
- # A slightly modified version of Ruby 1.8's Open3, that doesn't use a
- # grandchild process, and returns the pid of the external process.
- module Popen
-
- WINDOWS = RUBY_PLATFORM.match(/(win|w)32$/)
- ONE_NINE = RUBY_VERSION >= "1.9"
- if WINDOWS
- if ONE_NINE
- require 'open3'
- else
- require 'rubygems'
- require 'win32/open3'
- end
- end
-
- def self.popen(cmd)
- if WINDOWS
- error = nil
- Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thread|
- yield(stdin, stdout, stderr) if block_given?
- stdout.read unless stdout.closed? or stdout.eof?
- unless stderr.closed?
- stderr.rewind
- error = stderr.read
- end
- return wait_thread.value if wait_thread.is_a? Thread
- end
- else
- # pipe[0] for read, pipe[1] for write
- pw, pr, pe = IO.pipe, IO.pipe, IO.pipe
-
- pid = fork {
- pw[1].close
- STDIN.reopen(pw[0])
- pw[0].close
-
- pr[0].close
- STDOUT.reopen(pr[1])
- pr[1].close
-
- pe[0].close
- STDERR.reopen(pe[1])
- pe[1].close
-
- exec(cmd)
- }
-
- pw[0].close
- pr[1].close
- pe[1].close
- pi = [pw[1], pr[0], pe[0]]
- pw[1].sync = true
- begin
- yield(*pi) if block_given?
- ensure
- pi.each{|p| p.close unless p.closed?}
- end
- Process.waitpid pid
- end
- $?
- end
-
- end
-end
View
@@ -1,3 +1,8 @@
+if defined?(Gem) and Gem.available?('redgreen')
+ require 'redgreen' if RUBY_VERSION < "1.9"
+end
+require 'test/unit'
+
require 'closure-compiler'
class Test::Unit::TestCase
@@ -32,21 +32,25 @@ def test_block_syntax
end
assert result == COMPILED_ADVANCED
end
-
+
def test_jar_and_java_specifiation
- if !Closure::Popen::WINDOWS
- jar = Dir['vendor/closure-compiler-*.jar'].first
- java = `which java`.strip
- compiler = Compiler.new(:java => java, :jar_file => jar)
+ jar = Dir['vendor/closure-compiler-*.jar'].first
+ unless java = ( `which java` rescue nil )
+ java = `where java` rescue nil # works on newer windows
+ end
+ if java
+ compiler = Compiler.new(:java => java.strip, :jar_file => jar)
assert compiler.compress(ORIGINAL) == COMPILED_SIMPLE
+ else
+ puts "could not `which/where java` skipping test"
end
end
def test_exceptions
- assert_raises(Closure::Error) do
+ assert_raise(Closure::Error) do
Compiler.new.compile('1++')
end
- assert_raises(Closure::Error) do
+ assert_raise(Closure::Error) do
Compiler.new.compile('obj = [1 2, 3]')
end
end
@@ -60,4 +64,18 @@ def test_permissions
assert File.executable?(COMPILER_JAR)
end
+ def test_serialize_options
+ options = { 'externs' => 'library1.js', "compilation_level" => "ADVANCED_OPTIMIZATIONS" }
+ # ["--externs", "library1.js", "--compilation_level", "ADVANCED_OPTIMIZATIONS"]
+ # although Hash in 1.8 might change the order to :
+ # ["--compilation_level", "ADVANCED_OPTIMIZATIONS", "--externs", "library1.js"]
+ expected_options = options.to_a.map { |arr| [ "--#{arr[0]}", arr[1] ] }.flatten
+ assert_equal expected_options, Closure::Compiler.new.send(:serialize_options, options)
+ end
+
+ def test_serialize_options_for_arrays
+ compiler = Closure::Compiler.new('externs' => ['library1.js', "library2.js"])
+ assert_equal ["--externs", "library1.js", "--externs", "library2.js"], compiler.send(:serialize_options, 'externs' => ['library1.js', "library2.js"])
+ end
+
end

0 comments on commit 4f99f4a

Please sign in to comment.