Skip to content

Commit 4f99f4a

Browse files
committed
Merge branch 'master' of github.com:documentcloud/closure-compiler
2 parents 18c44b6 + 8315c19 commit 4f99f4a

6 files changed

Lines changed: 67 additions & 105 deletions

File tree

Rakefile

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
desc 'Run all tests'
1+
require "rake/testtask"
22

3-
task :default => :test
4-
task :test do
5-
$LOAD_PATH.unshift(File.expand_path('lib'))
6-
$LOAD_PATH.unshift(File.expand_path('test'))
7-
require 'redgreen' if Gem.available?('redgreen') and RUBY_VERSION < "1.9"
8-
require 'test/unit'
9-
Dir['test/**/test_*.rb'].each {|test| require test }
3+
Rake::TestTask.new do |t|
4+
t.libs += ["lib", "test"]
5+
t.test_files = FileList["test/**/*_test.rb"]
6+
t.verbose = true
107
end
118

129
namespace :gem do
@@ -22,4 +19,6 @@ namespace :gem do
2219
sh "sudo gem uninstall -x closure-compiler"
2320
end
2421

25-
end
22+
end
23+
24+
task :default => :test

lib/closure-compiler.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ module Closure
1212

1313
end
1414

15-
require 'stringio'
1615
require 'closure/compiler'

lib/closure/compiler.rb

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
require 'closure/popen'
1+
require 'stringio'
2+
require 'tempfile'
23

34
module Closure
45

@@ -20,28 +21,28 @@ def initialize(options={})
2021
# JavaScript as a string or yields an IO object containing the response to a
2122
# block, for streaming.
2223
def compile(io)
23-
result, error = nil, nil
24-
status = Closure::Popen.popen(command) do |stdin, stdout, stderr|
25-
if io.respond_to? :read
26-
while buffer = io.read(4096) do
27-
stdin.write(buffer)
28-
end
29-
else
30-
stdin.write(io.to_s)
31-
end
32-
stdin.close
33-
if Closure::Popen::WINDOWS
34-
stderr.close
35-
result = stdout.read
36-
error = "Stderr cannot be read on Windows."
37-
else
38-
out_thread = Thread.new { result = stdout.read }
39-
err_thread = Thread.new { error = stderr.read }
40-
out_thread.join and err_thread.join
24+
tempfile = Tempfile.new('closure_compiler')
25+
if io.respond_to? :read
26+
while buffer = io.read(4096) do
27+
tempfile.write(buffer)
4128
end
42-
yield(StringIO.new(result)) if block_given?
29+
else
30+
tempfile.write(io.to_s)
31+
end
32+
tempfile.flush
33+
34+
begin
35+
result = `#{command} --js #{tempfile.path}`
36+
rescue Exception
37+
raise Error, "compression failed"
38+
ensure
39+
tempfile.close!
4340
end
44-
raise Error, error unless status.success?
41+
unless $?.exitstatus.zero?
42+
raise Error, result
43+
end
44+
45+
yield(StringIO.new(result)) if block_given?
4546
result
4647
end
4748
alias_method :compress, :compile
@@ -51,7 +52,13 @@ def compile(io)
5152

5253
# Serialize hash options to the command-line format.
5354
def serialize_options(options)
54-
options.map {|k, v| ["--#{k}", v.to_s] }.flatten
55+
options.map do |k, v|
56+
if (v.is_a?(Array))
57+
v.map {|v2| ["--#{k}", v2.to_s]}
58+
else
59+
["--#{k}", v.to_s]
60+
end
61+
end.flatten
5562
end
5663

5764
def command

lib/closure/popen.rb

Lines changed: 0 additions & 66 deletions
This file was deleted.

test/test_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
if defined?(Gem) and Gem.available?('redgreen')
2+
require 'redgreen' if RUBY_VERSION < "1.9"
3+
end
4+
require 'test/unit'
5+
16
require 'closure-compiler'
27

38
class Test::Unit::TestCase
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,25 @@ def test_block_syntax
3232
end
3333
assert result == COMPILED_ADVANCED
3434
end
35-
35+
3636
def test_jar_and_java_specifiation
37-
if !Closure::Popen::WINDOWS
38-
jar = Dir['vendor/closure-compiler-*.jar'].first
39-
java = `which java`.strip
40-
compiler = Compiler.new(:java => java, :jar_file => jar)
37+
jar = Dir['vendor/closure-compiler-*.jar'].first
38+
unless java = ( `which java` rescue nil )
39+
java = `where java` rescue nil # works on newer windows
40+
end
41+
if java
42+
compiler = Compiler.new(:java => java.strip, :jar_file => jar)
4143
assert compiler.compress(ORIGINAL) == COMPILED_SIMPLE
44+
else
45+
puts "could not `which/where java` skipping test"
4246
end
4347
end
4448

4549
def test_exceptions
46-
assert_raises(Closure::Error) do
50+
assert_raise(Closure::Error) do
4751
Compiler.new.compile('1++')
4852
end
49-
assert_raises(Closure::Error) do
53+
assert_raise(Closure::Error) do
5054
Compiler.new.compile('obj = [1 2, 3]')
5155
end
5256
end
@@ -60,4 +64,18 @@ def test_permissions
6064
assert File.executable?(COMPILER_JAR)
6165
end
6266

67+
def test_serialize_options
68+
options = { 'externs' => 'library1.js', "compilation_level" => "ADVANCED_OPTIMIZATIONS" }
69+
# ["--externs", "library1.js", "--compilation_level", "ADVANCED_OPTIMIZATIONS"]
70+
# although Hash in 1.8 might change the order to :
71+
# ["--compilation_level", "ADVANCED_OPTIMIZATIONS", "--externs", "library1.js"]
72+
expected_options = options.to_a.map { |arr| [ "--#{arr[0]}", arr[1] ] }.flatten
73+
assert_equal expected_options, Closure::Compiler.new.send(:serialize_options, options)
74+
end
75+
76+
def test_serialize_options_for_arrays
77+
compiler = Closure::Compiler.new('externs' => ['library1.js', "library2.js"])
78+
assert_equal ["--externs", "library1.js", "--externs", "library2.js"], compiler.send(:serialize_options, 'externs' => ['library1.js', "library2.js"])
79+
end
80+
6381
end

0 commit comments

Comments
 (0)