juicer 1.0.16 → 1.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. data/Gemfile.lock +27 -11
  2. data/History.txt +3 -0
  3. data/Rakefile +1 -1
  4. data/VERSION +1 -1
  5. data/lib/juicer.rb +1 -1
  6. data/lib/juicer/install/yui_compressor_installer.rb +2 -2
  7. metadata +29 -113
  8. data/GPATH +0 -0
  9. data/GRTAGS +0 -0
  10. data/GSYMS +0 -0
  11. data/GTAGS +0 -0
  12. data/test/bin/jslint-1.0.js +0 -523
  13. data/test/bin/jslint.js +0 -523
  14. data/test/bin/rhino1_7R1.zip +0 -0
  15. data/test/bin/rhino1_7R2-RC1.jar +0 -0
  16. data/test/bin/rhino1_7R2-RC1.zip +0 -0
  17. data/test/bin/rhino1_7R3.jar +0 -0
  18. data/test/bin/rhino1_7R3.zip +0 -0
  19. data/test/bin/yuicompressor +0 -0
  20. data/test/bin/yuicompressor-2.3.5.zip +0 -0
  21. data/test/bin/yuicompressor-2.4.2.jar +0 -0
  22. data/test/bin/yuicompressor-2.4.2.zip +0 -0
  23. data/test/data/Changelog.txt +0 -10
  24. data/test/data/a.css +0 -3
  25. data/test/data/a.js +0 -5
  26. data/test/data/a1.css +0 -5
  27. data/test/data/b.css +0 -1
  28. data/test/data/b.js +0 -5
  29. data/test/data/b1.css +0 -5
  30. data/test/data/b2.css +0 -5
  31. data/test/data/c1.css +0 -3
  32. data/test/data/css/2.gif +0 -1
  33. data/test/data/css/test.css +0 -15
  34. data/test/data/css/test2.css +0 -1
  35. data/test/data/css/test3.css +0 -12
  36. data/test/data/d1.css +0 -3
  37. data/test/data/images/1.png +0 -1
  38. data/test/data/my_app.js +0 -2
  39. data/test/data/not-ok.js +0 -2
  40. data/test/data/ok.js +0 -3
  41. data/test/data/path_test.css +0 -5
  42. data/test/data/path_test2.css +0 -14
  43. data/test/data/pkg/module/moda.js +0 -2
  44. data/test/data/pkg/module/modb.js +0 -3
  45. data/test/data/pkg/pkg.js +0 -1
  46. data/test/fixtures/rhino-download.html +0 -42
  47. data/test/fixtures/yui-download.html +0 -375
  48. data/test/test_helper.rb +0 -204
  49. data/test/unit/juicer/asset/path_resolver_test.rb +0 -76
  50. data/test/unit/juicer/asset/path_test.rb +0 -376
  51. data/test/unit/juicer/cache_buster_test.rb +0 -128
  52. data/test/unit/juicer/chainable_test.rb +0 -94
  53. data/test/unit/juicer/command/install_test.rb +0 -58
  54. data/test/unit/juicer/command/list_test.rb +0 -81
  55. data/test/unit/juicer/command/merge_test.rb +0 -162
  56. data/test/unit/juicer/command/util_test.rb +0 -58
  57. data/test/unit/juicer/command/verify_test.rb +0 -48
  58. data/test/unit/juicer/css_cache_buster_test.rb +0 -122
  59. data/test/unit/juicer/datafy_test.rb +0 -37
  60. data/test/unit/juicer/dependency_resolver/css_dependency_resolver_test.rb +0 -36
  61. data/test/unit/juicer/dependency_resolver/javascript_dependency_resolver_test.rb +0 -50
  62. data/test/unit/juicer/ext/string_test.rb +0 -59
  63. data/test/unit/juicer/ext/symbol_test.rb +0 -27
  64. data/test/unit/juicer/image_embed_test.rb +0 -339
  65. data/test/unit/juicer/install/installer_base_test.rb +0 -214
  66. data/test/unit/juicer/install/jslint_installer_test.rb +0 -54
  67. data/test/unit/juicer/install/rhino_installer_test.rb +0 -57
  68. data/test/unit/juicer/install/yui_compressor_test.rb +0 -56
  69. data/test/unit/juicer/jslint_test.rb +0 -60
  70. data/test/unit/juicer/merger/base_test.rb +0 -122
  71. data/test/unit/juicer/merger/javascript_merger_test.rb +0 -74
  72. data/test/unit/juicer/merger/stylesheet_merger_test.rb +0 -262
  73. data/test/unit/juicer/minifyer/closure_compressor_test.rb +0 -107
  74. data/test/unit/juicer/minifyer/yui_compressor_test.rb +0 -133
  75. data/test/unit/juicer_test.rb +0 -1
@@ -1,58 +0,0 @@
1
- require "test_helper"
2
-
3
- class Dummy
4
- include Juicer::Command::Util
5
- end
6
-
7
- class TestCommandUtil < Test::Unit::TestCase
8
-
9
- CSS_FILES = %w{a.css a1.css b.css b1.css b2.css c1.css d1.css path_test.css path_test2.css}
10
- JS_FILES = %w{a.js}
11
- ALL_FILES = (CSS_FILES + JS_FILES).sort
12
-
13
- def setup
14
- @impl = Dummy.new
15
- Juicer::Test::FileSetup.new.create
16
- Dir.glob("test/data/*.min.css").each { |file| File.delete(file) }
17
- end
18
-
19
- def test_files_from_single_file
20
- files = @impl.files("test/data/a.css")
21
- assert files.is_a?(Array)
22
- assert_equal "test/data/a.css", files.sort.join
23
- end
24
-
25
- def test_files_from_single_glob_pattern
26
- files = @impl.files("test/data/*.css")
27
- assert files.is_a?(Array)
28
- assert_equal CSS_FILES.collect { |f| "test/data/#{f}" }.join, files.sort.join
29
- end
30
-
31
- def test_files_from_mixed_arguments
32
- files = @impl.files("test/data/*.css", "test/data/a.js")
33
- assert files.is_a?(Array)
34
- assert_equal ALL_FILES.collect { |f| "test/data/#{f}" }.join, files.sort.join
35
- end
36
-
37
- def test_files_from_array
38
- files = @impl.files(["test/data/*.css", "test/data/a.js"])
39
- assert files.is_a?(Array)
40
- assert_equal ALL_FILES.collect { |f| "test/data/#{f}" }.join, files.sort.join
41
- end
42
-
43
- def test_relative_path_single_file
44
- assert_equal "test/data/a.css", @impl.relative("test/data/a.css")
45
- end
46
-
47
- def test_relative_path_many_files
48
- files = @impl.relative(Dir.glob("test/data/*.css"))
49
- assert files.is_a?(Array)
50
- assert_equal CSS_FILES.collect { |f| "test/data/#{f}" }.join, files.sort.join
51
- end
52
-
53
- def test_relative_path_many_files_explicit_reference
54
- files = @impl.relative(Dir.glob("test/data/*.css"), "lib")
55
- assert files.is_a?(Array)
56
- assert_equal CSS_FILES.collect { |f| "../test/data/#{f}" }.join, files.sort.join
57
- end
58
- end
@@ -1,48 +0,0 @@
1
- require "test_helper"
2
-
3
- class TestVerifyCommand < Test::Unit::TestCase
4
- def setup
5
- @io = StringIO.new
6
- @command = Juicer::Command::Verify.new(Logger.new(@io))
7
- @path = Dir.pwd
8
- @juicer = Juicer.home
9
- end
10
-
11
- def teardown
12
- Dir.chdir(@path)
13
- Juicer.home = @juicer
14
- end
15
-
16
- context "executing command" do
17
- should "fail with no files" do
18
- assert_raise ArgumentError do
19
- @command.execute []
20
- end
21
- end
22
-
23
- should "fail if installer is not found" do
24
- Juicer.home = path("somewhere")
25
- Dir.chdir("lib")
26
- command = Juicer::Command::Verify.new(Logger.new(@io))
27
-
28
- assert_raise FileNotFoundError do
29
- command.execute path("a.js")
30
- end
31
- end
32
-
33
- should "verify several files" do
34
- files = %w[file1.js file2.js file3.js]
35
- ok = "OK!\njslint: No problems"
36
-
37
- Juicer::Command::Verify.any_instance.expects(:files).with(files).returns(files)
38
- Juicer::JsLint.any_instance.expects(:check).with(files[0]).returns(Juicer::JsLint::Report.new)
39
- Juicer::JsLint.any_instance.expects(:check).with(files[1]).returns(Juicer::JsLint::Report.new(["Oops"]))
40
- Juicer::JsLint.any_instance.expects(:check).with(files[2]).returns(Juicer::JsLint::Report.new)
41
-
42
- @command.execute(files)
43
-
44
- assert_match(/OK!/, @io.string)
45
- assert_match(/Problems detected/, @io.string)
46
- end
47
- end
48
- end
@@ -1,122 +0,0 @@
1
- require "test_helper"
2
-
3
- class TestCssCacheBuster < Test::Unit::TestCase
4
- def setup
5
- Juicer::Test::FileSetup.new.create
6
- @buster = Juicer::CssCacheBuster.new
7
- end
8
-
9
- def teardown
10
- Juicer::Test::FileSetup.new.delete
11
- Juicer::Test::FileSetup.new.create
12
- end
13
-
14
- context "finding urls" do
15
- should "find all urls" do
16
- urls = @buster.urls(path("css/test.css"))
17
- assert_equal 4, urls.length
18
- assert_equal "../a1.css../images/1.png2.gif2.gif", urls.collect { |a| a.path }.sort.join.gsub(path("/"), "")
19
- end
20
- end
21
-
22
- context "image references" do
23
- should "update image urls" do
24
- file = path("css/test.css")
25
- buster = Juicer::CssCacheBuster.new
26
- buster.save file
27
-
28
- File.read(file).scan(/url\(([^\)]*)\)/m).each do |path|
29
- assert_match(/[^\?]*\?jcb=\d+/, path.first)
30
- end
31
- end
32
-
33
- should "not add multiple cache busters" do
34
- file = path("css/test.css")
35
- buster = Juicer::CssCacheBuster.new
36
- buster.save file
37
-
38
- assert_no_match /2\.gif\?jcb=\d+\?jcb=/, File.read(file)
39
- end
40
-
41
- should "not add multiple cache busters when asset host cycling is used" do
42
- file = path("css/test3.css")
43
- buster = Juicer::CssCacheBuster.new( { :hosts => [ 'http://assets1', 'http://assets2', 'http://assets3' ], :document_root => './test/data' } )
44
- buster.save file
45
-
46
- assert_no_match /2\.gif\?jcb=\d+\?jcb=/, File.read(file)
47
- end
48
-
49
- end
50
-
51
- context "absolute paths" do
52
- # should "fail without document root" do
53
- # file = path("css/test2.css")
54
- # buster = Juicer::CssCacheBuster.new
55
-
56
- # assert_raise FileNotFoundError do
57
- # buster.save file
58
- # end
59
- # end
60
-
61
- should "resolve with document root" do
62
- file = path("css/test.css")
63
- buster = Juicer::CssCacheBuster.new :document_root => path("")
64
-
65
- assert_nothing_raised do
66
- buster.save file
67
- end
68
-
69
- File.read(file).scan(/url\(([^\)]*)\)/m).each do |path|
70
- assert_match(/[^\?]*\?jcb=\d+/, path.first)
71
- end
72
- end
73
- end
74
-
75
- context "cache busters" do
76
- should "add mtime to urls" do
77
- File.open(path("a2.css"), "w") { |f| f.puts "" }
78
- file = path("path_test2.css")
79
- output = path("path_test3.css")
80
- buster = Juicer::CssCacheBuster.new :document_root => path("")
81
- buster.save file, output
82
-
83
- buster.urls(output).each { |url| assert url !~ /(jcb=\d+).*(jcb=\d+)/, url }
84
- end
85
- end
86
-
87
- context "rails cache busters" do
88
- should "should append mtime to urls" do
89
- File.open(path("a2.css"), "w") { |f| f.puts "" }
90
- file = path("path_test2.css")
91
- output = path("path_test3.css")
92
- buster = Juicer::CssCacheBuster.new :document_root => path(""), :type => :rails
93
- buster.save file, output
94
-
95
- buster.urls(output).each { |asset| assert_match /\?\d+$/, asset.path }
96
- end
97
- end
98
-
99
- context "hard cache busters" do
100
- should "should alter file name" do
101
- File.open(path("a2.css"), "w") { |f| f.puts "" }
102
- file = path("path_test2.css")
103
- output = path("path_test3.css")
104
- buster = Juicer::CssCacheBuster.new :document_root => path(""), :type => :hard
105
- buster.save file, output
106
-
107
- buster.urls(output).each { |asset| assert_match /-jcb\d+\.[a-z]{3}$/, asset.path }
108
- end
109
- end
110
-
111
- context "non-existent urls" do
112
- should "not raise" do
113
- File.open(path("a2.css"), "w") { |f| f.puts "a { background: url(i/dont/exist.fck); }" }
114
- file = path("a2.css")
115
- buster = Juicer::CssCacheBuster.new :document_root => path("")
116
-
117
- assert_nothing_raised do
118
- buster.save file
119
- end
120
- end
121
- end
122
- end
@@ -1,37 +0,0 @@
1
- require 'test/unit'
2
- require "test_helper"
3
-
4
- class TC_Datafy < Test::Unit::TestCase
5
-
6
- def test_make_data_uri_text_plain
7
- assert_equal(SHORT_TEXT_PLAIN_URI, Datafy::make_data_uri(SHORT_STRING, TEXT_PLAIN))
8
- assert_equal(LONG_TEXT_PLAIN_URI, Datafy::make_data_uri(LONG_STRING, TEXT_PLAIN))
9
- end
10
-
11
- def test_make_data_uri_octet_stream
12
- assert_equal(SHORT_APPLICATION_OCTET_STREAM_URI, Datafy::make_data_uri(SHORT_STRING, APPLICATION_OCTET_STREAM))
13
- assert_equal(LONG_APPLICATION_OCTET_STREAM_URI, Datafy::make_data_uri(LONG_STRING, APPLICATION_OCTET_STREAM))
14
- end
15
-
16
- # mime types
17
- TEXT_PLAIN = 'text/plain'
18
- APPLICATION_OCTET_STREAM = 'application/octet-stream'
19
-
20
- # string versions
21
- SHORT_STRING = 'this is some text'
22
- SHORT_BASE64 = 'dGhpcyBpcyBzb21lIHRleHQ='
23
- SHORT_URLENCODED = 'this+is+some+text'
24
-
25
- LONG_STRING = 'this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string. this is a really long string.'
26
- LONG_BASE64 = 'dGhpcyBpcyBhIHJlYWxseSBsb25nIHN0cmluZy4gdGhpcyBpcyBhIHJlYWxseSBsb25nIHN0cmluZy4gdGhpcyBpcyBhIHJlYWxseSBsb25nIHN0cmluZy4gdGhpcyBpcyBhIHJlYWxseSBsb25nIHN0cmluZy4gdGhpcyBpcyBhIHJlYWxseSBsb25nIHN0cmluZy4gdGhpcyBpcyBhIHJlYWxseSBsb25nIHN0cmluZy4gdGhpcyBpcyBhIHJlYWxseSBsb25nIHN0cmluZy4gdGhpcyBpcyBhIHJlYWxseSBsb25nIHN0cmluZy4='
27
- LONG_URLENCODED = 'this+is+a+really+long+string.+this+is+a+really+long+string.+this+is+a+really+long+string.+this+is+a+really+long+string.+this+is+a+really+long+string.+this+is+a+really+long+string.+this+is+a+really+long+string.+this+is+a+really+long+string.'
28
-
29
- # data: uris
30
- SHORT_APPLICATION_OCTET_STREAM_URI = "data:#{APPLICATION_OCTET_STREAM};base64,#{SHORT_BASE64}"
31
- SHORT_TEXT_PLAIN_URI = "data:#{TEXT_PLAIN},#{SHORT_URLENCODED}"
32
-
33
- LONG_APPLICATION_OCTET_STREAM_URI = "data:#{APPLICATION_OCTET_STREAM};base64,#{LONG_BASE64}"
34
- LONG_TEXT_PLAIN_URI = "data:#{TEXT_PLAIN},#{LONG_URLENCODED}"
35
-
36
-
37
- end
@@ -1,36 +0,0 @@
1
- require "test_helper"
2
-
3
- class TestCssDependencyResolver < Test::Unit::TestCase
4
- def setup
5
- @resolver = Juicer::CssDependencyResolver.new
6
- Juicer::Test::FileSetup.new.create
7
- end
8
-
9
- def test_init
10
- assert_equal [], @resolver.files
11
- end
12
-
13
- def test_resolve
14
- b_file = path('b.css')
15
- a_file = path('a.css')
16
-
17
- files = @resolver.resolve(path('a.css')) do |file|
18
- assert b_file == file || a_file == file
19
- b_file != file
20
- end
21
-
22
- assert_equal [a_file], files
23
-
24
- files = @resolver.resolve(path('a.css')) do |file|
25
- assert b_file == file || a_file == file
26
- true
27
- end
28
-
29
- assert_equal [a_file, b_file], files.sort
30
- end
31
-
32
- def test_load_order
33
- files = @resolver.resolve(path("a1.css")).collect { |file| file.split("/").pop }
34
- assert_equal "d1.cssb1.cssc1.cssa1.css", files.join
35
- end
36
- end
@@ -1,50 +0,0 @@
1
- require "test_helper"
2
-
3
- class TestJavaScriptDependencyResolver < Test::Unit::TestCase
4
-
5
- def setup
6
- @resolver = Juicer::JavaScriptDependencyResolver.new
7
- Juicer::Test::FileSetup.new.create
8
- end
9
-
10
- def test_init
11
- assert_equal [], @resolver.files
12
- end
13
-
14
- def test_resolve
15
- b_file = path('b.js')
16
- a_file = path('a.js')
17
-
18
- files = @resolver.resolve(a_file) do |file|
19
- assert b_file == file || a_file == file, file
20
- b_file != file
21
- end
22
-
23
- assert_equal [a_file], files
24
-
25
- files = @resolver.resolve(a_file) do |file|
26
- assert b_file == file || a_file == file
27
- true
28
- end
29
-
30
- assert_equal [a_file, b_file], files.sort
31
-
32
- files = @resolver.resolve(b_file) do |file|
33
- assert b_file == file || a_file == file
34
- true
35
- end
36
-
37
- assert_equal [a_file, b_file], files.sort
38
- end
39
-
40
- def test_directory_resolve
41
- my_app = path("my_app.js")
42
- expected_files = %w(pkg/pkg.js
43
- pkg/module/moda.js
44
- pkg/module/modb.js
45
- my_app.js).map{|file| path(file)}
46
-
47
- actual_files = @resolver.resolve(my_app)
48
- assert_equal expected_files, actual_files
49
- end
50
- end
@@ -1,59 +0,0 @@
1
- require "test_helper"
2
-
3
- class TestStringExtensions < Test::Unit::TestCase
4
- context "camel case method" do
5
- should "convert underscored string" do
6
- assert_equal "CamelCase", "camel_case".camel_case
7
- end
8
-
9
- should "convert spaced string" do
10
- assert_equal "Camel case", "camel case".camel_case
11
- end
12
-
13
- should "convert upper-case underscored string" do
14
- assert_equal "CamelCase", "CAMEL_CASE".camel_case
15
- end
16
-
17
- should "convert 'camel cased' underscored string" do
18
- assert_equal "CamelCase", "Camel_Case".camel_case
19
- end
20
- end
21
-
22
- context "to_class method" do
23
- should "return String class" do
24
- assert_equal String, "String".to_class
25
- end
26
-
27
- should "return String class from Object" do
28
- assert_equal String, "String".to_class(Object)
29
- end
30
-
31
- should "return nested class" do
32
- assert_equal Juicer::DependencyResolver, "Juicer::DependencyResolver".to_class
33
- end
34
-
35
- should "return class from module" do
36
- assert_equal Juicer::DependencyResolver, "DependencyResolver".to_class(Juicer)
37
- end
38
-
39
- should "return class from nested module" do
40
- assert_equal Juicer::Install::YuiCompressorInstaller, "YuiCompressorInstaller".to_class(Juicer::Install)
41
- end
42
- end
43
-
44
- context "classify method" do
45
- should "return class from underscored string" do
46
- assert_equal Juicer::DependencyResolver, "dependency_resolver".classify(Juicer)
47
- end
48
-
49
- should "return top level class from underscored string" do
50
- assert_equal FileUtils, "file_utils".classify
51
- end
52
- end
53
-
54
- context "underscore method" do
55
- should "return underscored string from camel cased string" do
56
- assert_equal "stylesheet_merger", "StylesheetMerger".underscore
57
- end
58
- end
59
- end
@@ -1,27 +0,0 @@
1
- require "test_helper"
2
-
3
- class TestSymbolExtensions < Test::Unit::TestCase
4
- context "camel case method" do
5
- should "return camel cased string from underscored symbol" do
6
- assert_equal "CamelCase", :camel_case.camel_case
7
- end
8
-
9
- should "return camel cased string from upper cased underscored symbol" do
10
- assert_equal "CamelCase", :CAMEL_CASE.camel_case
11
- end
12
-
13
- should "return camel cased string from 'camel cased' underscored symbol" do
14
- assert_equal "CamelCase", :Camel_Case.camel_case
15
- end
16
- end
17
-
18
- context "classify method" do
19
- should "return nested class" do
20
- assert_equal Juicer::DependencyResolver, :dependency_resolver.classify(Juicer)
21
- end
22
-
23
- should "return top level class from underscored symbol" do
24
- assert_equal FileUtils, :file_utils.classify
25
- end
26
- end
27
- end
@@ -1,339 +0,0 @@
1
- require "test_helper"
2
- require 'fakefs/safe'
3
-
4
- class TestImageEmbed < Test::Unit::TestCase
5
- include FakeFS
6
-
7
- context "ImageEmbed instance using data_uri," do
8
- setup do
9
- FakeFS.activate!
10
- FileSystem.clear
11
- FileUtils.mkdir_p("/stylesheets")
12
- FileUtils.mkdir_p("/images")
13
-
14
- @supported_assets = [
15
- { :path => '/images/test.png', :content => 'hello png!' },
16
- { :path => '/images/test.gif', :content => 'hello gif!' },
17
- { :path => '/images/test.jpg', :content => 'hello jpg!' },
18
- { :path => '/images/test.jpeg', :content => 'hello jpeg!' },
19
- ]
20
- create_files(@supported_assets)
21
-
22
- @unsupported_assets = [
23
- { :path => '/images/test.bmp', :content => 'hello bmp!' },
24
- { :path => '/images/test.js', :content => 'hello js!' },
25
- { :path => '/images/test.txt', :content => 'hello txt!' },
26
- { :path => '/images/test.swf', :content => 'hello swf!' },
27
- { :path => '/images/test.swf', :content => 'hello swf!' },
28
- { :path => '/images/test.ico', :content => 'hello ico!' },
29
- { :path => '/images/test.tif', :content => 'hello tif!' },
30
- { :path => '/images/test.tiff', :content => 'hello tiff!' },
31
- { :path => '/images/test.applet', :content => 'hello applet!' },
32
- { :path => '/images/test.jar', :content => 'hello jar!' }
33
- ]
34
- create_files(@unsupported_assets)
35
- @embedder = Juicer::ImageEmbed.new(:type => :data_uri, :document_root => '')
36
- end
37
-
38
- teardown do
39
- FakeFS.deactivate!
40
- end
41
-
42
- context "save method" do
43
- context "with files exceeding SIZE_LIMIT" do
44
- setup do
45
- @large_files = [{ :path => '/images/large-file.png',
46
- :content => "hello png!" + (" " * @embedder.size_limit) }]
47
- create_files(@large_files)
48
-
49
- @stylesheets = [{
50
- :path => '/stylesheets/test_embed_duplicates.css',
51
- :content => "body: { background: url(#{@large_files.first[:path]}?embed=true); }"
52
- }]
53
- create_files(@stylesheets)
54
- end
55
-
56
- should "not embed images that exceeds size limit" do
57
- # make sure there are no errors
58
- assert_nothing_raised do
59
- @embedder.save @stylesheets.first[:path]
60
- end
61
-
62
- css_contents = File.read(@stylesheets.first[:path])
63
-
64
- # encode the image
65
- image_contents = File.read( @large_files.first[:path] )
66
- data_uri = Datafy::make_data_uri(image_contents, 'image/png')
67
-
68
- # make sure the encoded data_uri is not present in the stylesheet
69
- assert !css_contents.include?(data_uri)
70
-
71
- # make sure the original url still exist in the stylesheet
72
- assert_match Regexp.new(@large_files.first[:path]), css_contents
73
- end
74
- end
75
-
76
- context "non empty document root" do
77
- setup do
78
- @document_root = '/path/to/public/dir'
79
- @another_embedder = Juicer::ImageEmbed.new(:type => :data_uri, :document_root => @document_root)
80
- @files = [{ :path => "#{@document_root}/images/custom-file.png", :filename => '/images/custom-file.png', :content => "hello png!" }]
81
- create_files(@files)
82
- end
83
-
84
- should "embed urls with embedder" do
85
- stylesheets = [{ :path => "#{@document_root}/stylesheets/test_absolute_path.css", :content => "body: { background: url(#{@files.first[:filename]}?embed=true); }" }]
86
- create_files(stylesheets)
87
-
88
- @another_embedder.save stylesheets.first[:path]
89
- css_contents = File.read(stylesheets.first[:path])
90
-
91
- # encode the image
92
- image_contents = File.read(@files.first[:path])
93
- data_uri = Datafy::make_data_uri(image_contents, 'image/png')
94
-
95
- # make sure the encoded data_uri is present in the stylesheet
96
- assert css_contents.include?(data_uri)
97
- end
98
-
99
- should "not embed urls with embedder" do
100
- stylesheets = [{ :path => "#{@document_root}/stylesheets/test_absolute_path.css", :content => "body: { background: url(#{@files.first[:filename]}?embed=false); }" }]
101
- create_files(stylesheets)
102
-
103
- @another_embedder.save stylesheets.first[:path]
104
- css_contents = File.read(stylesheets.first[:path])
105
-
106
- # encode the image
107
- assert css_contents.include?(@files.first[:filename])
108
- end
109
- end
110
-
111
- context "with duplicated urls" do
112
- setup do
113
- @stylesheets = [{
114
- :path => '/stylesheets/test_embed_duplicates.css',
115
- :content => <<-EOF
116
- body: { background: url(#{@supported_assets.first[:path]}?embed=true); }
117
- div.section: { background: url(#{@supported_assets.first[:path]}?embed=true); }
118
- div.article: { background: url(#{@supported_assets.last[:path]}?embed=true); }
119
- EOF
120
- }]
121
- create_files(@stylesheets)
122
- end
123
-
124
- should_eventually "provide warnings for duplicate urls"
125
-
126
- should "not embed duplicates" do
127
- # make sure there are no errors
128
- assert_nothing_raised do
129
- @embedder.save @stylesheets.first[:path]
130
- end
131
-
132
- css_contents = File.read(@stylesheets.first[:path])
133
-
134
- # encode the image
135
- image_contents = File.read(@supported_assets.first[:path])
136
- data_uri = Datafy::make_data_uri(image_contents, 'image/png')
137
-
138
- # make sure the encoded data_uri is not present in the stylesheet
139
- assert !css_contents.include?(data_uri)
140
- end
141
-
142
- should "embed distinct urls" do
143
- # make sure there are no errors
144
- assert_nothing_raised do
145
- @embedder.save @stylesheets.first[:path]
146
- end
147
-
148
- css_contents = File.read(@stylesheets.first[:path])
149
-
150
- # encode the image
151
- image_contents = File.read(@supported_assets.last[:path])
152
- data_uri = Datafy::make_data_uri(image_contents, 'image/jpeg')
153
-
154
- # make sure the encoded data_uri is not present in the stylesheet
155
- assert css_contents.include?(data_uri)
156
-
157
- assert_no_match Regexp.new(@supported_assets.last[:path]), css_contents
158
- end
159
-
160
- end
161
-
162
- should "embed images into css file" do
163
- @stylesheets = [
164
- {
165
- :path => '/stylesheets/test_embed_true.css',
166
- :content => "body: { background: url(#{@supported_assets.first[:path]}?embed=true); }"
167
- }
168
- ]
169
- create_files( @stylesheets )
170
-
171
- @stylesheets.each do |stylesheet|
172
- old_contents = File.read( stylesheet[:path] )
173
-
174
- # make sure there are no errors
175
- assert_nothing_raised do
176
- @embedder.save stylesheet[:path]
177
- end
178
-
179
- css_contents = File.read( stylesheet[:path] )
180
-
181
- # make sure the original url does not exist anymore
182
- assert_no_match( Regexp.new( @supported_assets.first[:path] ), css_contents )
183
-
184
- # make sure the url has been converted into a data uri
185
- image_contents = File.read( @supported_assets.first[:path] )
186
-
187
- # # create the data uri from the image contents
188
- data_uri = Datafy::make_data_uri( image_contents, 'image/png' )
189
-
190
- # let's see if the data uri exists in the file
191
- assert css_contents.include?( data_uri )
192
- end
193
- end
194
-
195
- should "force embedding images into css file" do
196
- @embedder = Juicer::ImageEmbed.new(:type => :data_uri, :document_root => '', :force => true)
197
- @stylesheets = [
198
- {
199
- :path => '/stylesheets/test_embed_true.css',
200
- :content => "body: { background: url(#{@supported_assets.first[:path]}); }"
201
- }
202
- ]
203
- create_files( @stylesheets )
204
-
205
- @stylesheets.each do |stylesheet|
206
- old_contents = File.read( stylesheet[:path] )
207
-
208
- # make sure there are no errors
209
- assert_nothing_raised do
210
- @embedder.save stylesheet[:path]
211
- end
212
-
213
- css_contents = File.read( stylesheet[:path] )
214
-
215
- # make sure the original url does not exist anymore
216
- assert_no_match( Regexp.new( @supported_assets.first[:path] ), css_contents )
217
-
218
- # make sure the url has been converted into a data uri
219
- image_contents = File.read( @supported_assets.first[:path] )
220
-
221
- # # create the data uri from the image contents
222
- data_uri = Datafy::make_data_uri( image_contents, 'image/png' )
223
-
224
- # let's see if the data uri exists in the file
225
- assert css_contents.include?( data_uri )
226
- end
227
- end
228
-
229
- should "not embed unflagged images" do
230
- @stylesheets = [
231
- {
232
- :path => '/stylesheets/test_embed_true.css',
233
- :content => "
234
- body: { background: url(#{@supported_assets.first[:path]}); }
235
- h1: { background: url(#{@supported_assets.last[:path]}?embed=false); }
236
- "
237
- }
238
- ]
239
- create_files( @stylesheets )
240
-
241
- @stylesheets.each do |stylesheet|
242
- old_contents = File.read( stylesheet[:path] )
243
-
244
- # make sure there are no errors
245
- assert_nothing_raised do
246
- @embedder.save stylesheet[:path]
247
- end
248
-
249
- css_contents = File.read( stylesheet[:path] )
250
-
251
- # make sure the original url still exists
252
- assert_match( Regexp.new( @supported_assets.first[:path] ), css_contents )
253
- assert_match( Regexp.new( @supported_assets.last[:path] ), css_contents )
254
- end
255
- end
256
-
257
-
258
- should "not embed unsupported images" do
259
- @stylesheets = [
260
- {
261
- :path => '/stylesheets/test_embed_true.css',
262
- :content => "
263
- body: { background: url(#{@unsupported_assets.first[:path]}?embed=true); }
264
- h1: { background: url(#{@unsupported_assets.last[:path]}?embed=true); }
265
- "
266
- }
267
- ]
268
- create_files( @stylesheets )
269
-
270
- @stylesheets.each do |stylesheet|
271
- old_contents = File.read( stylesheet[:path] )
272
-
273
- # make sure there are no errors
274
- assert_nothing_raised do
275
- @embedder.save stylesheet[:path]
276
- end
277
-
278
- css_contents = File.read( stylesheet[:path] )
279
-
280
- # make sure the original url still exists
281
- assert_match( Regexp.new( @unsupported_assets.first[:path] ), css_contents )
282
- assert_match( Regexp.new( @unsupported_assets.last[:path] ), css_contents )
283
- end
284
- end
285
-
286
- end # context
287
-
288
- context "embed method" do
289
- should "not modify regular paths" do
290
- path = @supported_assets.first[:path]
291
- assert_equal( path, @embedder.embed_data_uri( path ) )
292
- end
293
-
294
- should "not modify paths flagged as not embeddable" do
295
- path = "#{@supported_assets.first[:path]}?embed=false"
296
- assert_equal( path, @embedder.embed_data_uri( path ) )
297
- end
298
-
299
- should "embed image when path flagged as embeddable" do
300
- path = "#{@supported_assets.first[:path]}?embed=true"
301
- assert_not_equal( path, @embedder.embed_data_uri( path ) )
302
- end
303
-
304
- should "encode all supported asset types" do
305
- @supported_assets.each do |asset|
306
- path = "#{asset[:path]}?embed=true"
307
- assert_not_equal( path, @embedder.embed_data_uri( path ) )
308
- end
309
- end
310
-
311
- should "not encode unsupported asset types" do
312
- @unsupported_assets.each do |asset|
313
- path = "#{asset[:path]}?embed=true"
314
- assert_equal( path, @embedder.embed_data_uri( path ) )
315
- end
316
- end
317
-
318
- should "set correct mimetype for supported extensions" do
319
- @supported_assets.each do |asset|
320
- path = "#{asset[:path]}?embed=true"
321
- extension = /(png|gif|jpg|jpeg)/.match( asset[:path] )
322
- assert_match(/image\/#{extension}/, @embedder.embed_data_uri( path ) )
323
- end
324
- end
325
- end # context
326
- end
327
-
328
- private
329
- # expects the containing path to have been created already
330
- def create_files( files = [] )
331
- files.each do |file|
332
- path = file[:path]
333
- File.open(path, 'w') do |f|
334
- f.write file[:content]
335
- assert File.exists?( file[:path] )
336
- end
337
- end
338
- end
339
- end