jekyll-minibundle 1.5.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -6
  3. data/README.md +34 -24
  4. data/Rakefile +19 -14
  5. data/jekyll-minibundle.gemspec +5 -4
  6. data/lib/jekyll/minibundle/asset_bundle.rb +24 -4
  7. data/lib/jekyll/minibundle/asset_file_operations.rb +0 -8
  8. data/lib/jekyll/minibundle/asset_file_properties.rb +26 -8
  9. data/lib/jekyll/minibundle/asset_file_registry.rb +57 -21
  10. data/lib/jekyll/minibundle/asset_tag_markup.rb +13 -4
  11. data/lib/jekyll/minibundle/bundle_file.rb +9 -6
  12. data/lib/jekyll/minibundle/compatibility.rb +11 -1
  13. data/lib/jekyll/minibundle/development_file.rb +6 -0
  14. data/lib/jekyll/minibundle/development_file_collection.rb +3 -16
  15. data/lib/jekyll/minibundle/environment.rb +5 -12
  16. data/lib/jekyll/minibundle/files.rb +18 -0
  17. data/lib/jekyll/minibundle/hashes.rb +20 -0
  18. data/lib/jekyll/minibundle/mini_bundle_block.rb +52 -12
  19. data/lib/jekyll/minibundle/mini_stamp_tag.rb +27 -8
  20. data/lib/jekyll/minibundle/stamp_file.rb +9 -6
  21. data/lib/jekyll/minibundle/version.rb +1 -1
  22. data/test/fixture/site/_bin/with_count +6 -5
  23. data/test/integration/known_caveats_test.rb +89 -0
  24. data/test/integration/minibundle_development_mode_test.rb +171 -26
  25. data/test/integration/minibundle_production_mode_test.rb +234 -42
  26. data/test/integration/ministamp_development_mode_test.rb +145 -0
  27. data/test/integration/{ministamp_test.rb → ministamp_production_mode_test.rb} +72 -23
  28. data/test/integration/static_files_as_asset_sources_test.rb +3 -0
  29. data/test/support/assertions.rb +24 -0
  30. data/test/support/fixture_config.rb +13 -10
  31. data/test/support/static_file_api_config.rb +9 -3
  32. data/test/support/test_case.rb +7 -6
  33. data/test/unit/asset_bundle_test.rb +16 -2
  34. data/test/unit/asset_file_registry_test.rb +117 -14
  35. data/test/unit/asset_tag_markup_test.rb +17 -5
  36. data/test/unit/bundle_file_properties_test.rb +32 -8
  37. data/test/unit/bundle_file_writing_test.rb +10 -11
  38. data/test/unit/development_file_collection_properties_test.rb +47 -15
  39. data/test/unit/environment_test.rb +0 -13
  40. data/test/unit/files_test.rb +46 -0
  41. data/test/unit/hashes_test.rb +41 -0
  42. data/test/unit/mini_bundle_block_test.rb +56 -1
  43. data/test/unit/mini_stamp_tag_test.rb +8 -8
  44. data/test/unit/stamp_file_properties_test.rb +32 -13
  45. data/test/unit/stamp_file_writing_test.rb +1 -5
  46. metadata +38 -16
  47. data/test/unit/development_file_collection_writing_test.rb +0 -43
  48. data/test/unit/jekyll_payload_test.rb +0 -64
@@ -0,0 +1,145 @@
1
+ require 'support/test_case'
2
+ require 'support/fixture_config'
3
+
4
+ module Jekyll::Minibundle::Test
5
+ class MiniStampProductionModeTest < TestCase
6
+ include FixtureConfig
7
+
8
+ def test_asset_destination_path_has_no_stamp
9
+ with_precompiled_site(:development) do
10
+ assert_equal STAMP_DESTINATION_PATH, find_css_path_from_index
11
+ assert File.exist?(destination_path(STAMP_DESTINATION_PATH))
12
+ end
13
+ end
14
+
15
+ def test_contents_of_asset_destination_are_equal_to_source
16
+ with_precompiled_site(:development) do
17
+ source_contents = File.read(site_fixture_path(STAMP_SOURCE_PATH))
18
+ destination_contents = File.read(destination_path(STAMP_DESTINATION_PATH))
19
+ assert_equal source_contents, destination_contents
20
+ end
21
+ end
22
+
23
+ def test_changing_asset_source_file_rewrites_destination
24
+ with_site_dir do
25
+ generate_site(:development)
26
+
27
+ org_mtime = mtime_of(destination_path(STAMP_DESTINATION_PATH))
28
+ ensure_file_mtime_changes { File.write(source_path(STAMP_SOURCE_PATH), 'h1 {}') }
29
+
30
+ generate_site(:development, clear_cache: false)
31
+
32
+ new_mtime = mtime_of(destination_path(STAMP_DESTINATION_PATH))
33
+ assert_operator new_mtime, :>, org_mtime
34
+ end
35
+ end
36
+
37
+ def test_touching_asset_source_file_rewrites_destination
38
+ with_site_dir do
39
+ generate_site(:development)
40
+
41
+ org_mtime = mtime_of(destination_path(STAMP_DESTINATION_PATH))
42
+ ensure_file_mtime_changes { FileUtils.touch(source_path(STAMP_SOURCE_PATH)) }
43
+
44
+ generate_site(:development, clear_cache: false)
45
+
46
+ new_mtime = mtime_of(destination_path(STAMP_DESTINATION_PATH))
47
+ assert_operator new_mtime, :>, org_mtime
48
+ end
49
+ end
50
+
51
+ def test_changing_asset_source_path_rewrites_destination
52
+ with_site_dir do
53
+ generate_site(:development)
54
+
55
+ org_mtime = mtime_of(destination_path(STAMP_DESTINATION_PATH))
56
+
57
+ ensure_file_mtime_changes do
58
+ FileUtils.mv(source_path('_tmp/site.css'), source_path('_tmp/site2.css'))
59
+
60
+ find_and_gsub_in_file(
61
+ source_path('_layouts/default.html'),
62
+ '{% ministamp _tmp/site.css assets/screen.css',
63
+ '{% ministamp _tmp/site2.css assets/screen.css'
64
+ )
65
+ end
66
+
67
+ generate_site(:development, clear_cache: false)
68
+
69
+ new_mtime = mtime_of(destination_path(STAMP_DESTINATION_PATH))
70
+
71
+ assert_operator new_mtime, :>, org_mtime
72
+ end
73
+ end
74
+
75
+ def test_changing_asset_destination_path_rewrites_destination
76
+ with_site_dir do
77
+ generate_site(:development)
78
+
79
+ assert File.exist?(destination_path(STAMP_DESTINATION_PATH))
80
+
81
+ ensure_file_mtime_changes do
82
+ find_and_gsub_in_file(
83
+ source_path('_layouts/default.html'),
84
+ '{% ministamp _tmp/site.css assets/screen.css',
85
+ '{% ministamp _tmp/site.css assets/screen2.css'
86
+ )
87
+ end
88
+
89
+ generate_site(:development, clear_cache: false)
90
+
91
+ refute File.exist?(destination_path(STAMP_DESTINATION_PATH))
92
+
93
+ new_destination = 'assets/screen2.css'
94
+
95
+ assert_equal new_destination, find_css_path_from_index
96
+ assert File.exist?(destination_path(new_destination))
97
+ end
98
+ end
99
+
100
+ def test_supports_relative_and_absolute_destination_paths
101
+ with_site_dir do
102
+ generate_site(:development)
103
+
104
+ assert File.exist?(destination_path(STAMP_DESTINATION_PATH))
105
+ assert_equal STAMP_DESTINATION_PATH, find_css_path_from_index
106
+
107
+ find_and_gsub_in_file(
108
+ source_path('_layouts/default.html'),
109
+ '{% ministamp _tmp/site.css assets/screen.css',
110
+ '{% ministamp _tmp/site.css /assets/screen.css'
111
+ )
112
+
113
+ generate_site(:development, clear_cache: false)
114
+
115
+ assert_equal "/#{STAMP_DESTINATION_PATH}", find_css_path_from_index
116
+ end
117
+ end
118
+
119
+ def test_does_not_rewrite_destination_when_nonsource_files_change
120
+ with_site_dir do
121
+ generate_site(:development)
122
+
123
+ expected_path = destination_path(STAMP_DESTINATION_PATH)
124
+ org_mtime = mtime_of(expected_path)
125
+ ensure_file_mtime_changes { File.write(source_path(JS_BUNDLE_SOURCE_DIR, 'dependency.js'), '(function() {})()') }
126
+
127
+ generate_site(:development, clear_cache: false)
128
+
129
+ assert_equal org_mtime, mtime_of(expected_path)
130
+
131
+ ensure_file_mtime_changes { FileUtils.touch('index.html') }
132
+
133
+ generate_site(:development, clear_cache: false)
134
+
135
+ assert_equal org_mtime, mtime_of(expected_path)
136
+ end
137
+ end
138
+
139
+ private
140
+
141
+ def find_css_path_from_index
142
+ find_html_element(File.read(destination_path('index.html')), 'head link').first['href']
143
+ end
144
+ end
145
+ end
@@ -2,17 +2,10 @@ require 'support/test_case'
2
2
  require 'support/fixture_config'
3
3
 
4
4
  module Jekyll::Minibundle::Test
5
- class MiniStampTest < TestCase
5
+ class MiniStampProductionModeTest < TestCase
6
6
  include FixtureConfig
7
7
 
8
- def test_asset_destination_path_has_no_stamp_in_development_mode
9
- with_precompiled_site(:development) do
10
- assert_equal STAMP_DESTINATION_PATH, find_css_path_from_index
11
- assert File.exist?(destination_path(STAMP_DESTINATION_PATH))
12
- end
13
- end
14
-
15
- def test_asset_destination_path_has_stamp_in_production_mode
8
+ def test_asset_destination_path_has_stamp
16
9
  with_precompiled_site(:production) do
17
10
  assert_equal STAMP_DESTINATION_FINGERPRINT_PATH, find_css_path_from_index
18
11
  assert File.exist?(destination_path(STAMP_DESTINATION_FINGERPRINT_PATH))
@@ -27,11 +20,12 @@ module Jekyll::Minibundle::Test
27
20
  end
28
21
  end
29
22
 
30
- def test_changing_asset_source_rewrites_destination
23
+ def test_changing_asset_source_file_rewrites_destination
31
24
  with_site_dir do
32
25
  generate_site(:production)
33
- org_mtime = mtime_of(destination_path(STAMP_DESTINATION_FINGERPRINT_PATH))
26
+
34
27
  ensure_file_mtime_changes { File.write(source_path(STAMP_SOURCE_PATH), 'h1 {}') }
28
+
35
29
  generate_site(:production, clear_cache: false)
36
30
 
37
31
  refute File.exist?(destination_path(STAMP_DESTINATION_FINGERPRINT_PATH))
@@ -40,51 +34,106 @@ module Jekyll::Minibundle::Test
40
34
 
41
35
  assert_equal new_destination, find_css_path_from_index
42
36
  assert File.exist?(destination_path(new_destination))
43
- assert_operator mtime_of(destination_path(new_destination)), :>, org_mtime
44
37
  end
45
38
  end
46
39
 
47
- def test_touching_asset_source_rewrites_destination
40
+ def test_touching_asset_source_file_rewrites_destination
48
41
  with_site_dir do
49
42
  generate_site(:production)
50
- destination = STAMP_DESTINATION_FINGERPRINT_PATH
51
- org_mtime = mtime_of(destination_path(destination))
43
+
44
+ org_mtime = mtime_of(destination_path(STAMP_DESTINATION_FINGERPRINT_PATH))
52
45
  ensure_file_mtime_changes { FileUtils.touch(source_path(STAMP_SOURCE_PATH)) }
53
46
  generate_site(:production, clear_cache: false)
54
47
 
55
- assert_equal destination, find_css_path_from_index
56
- assert File.exist?(destination_path(destination))
57
- assert_operator mtime_of(destination_path(destination)), :>, org_mtime
48
+ assert_equal STAMP_DESTINATION_FINGERPRINT_PATH, find_css_path_from_index
49
+
50
+ new_mtime = mtime_of(destination_path(STAMP_DESTINATION_FINGERPRINT_PATH))
51
+ assert_operator new_mtime, :>, org_mtime
52
+ end
53
+ end
54
+
55
+ def test_changing_asset_source_path_rewrites_destination
56
+ with_site_dir do
57
+ generate_site(:production)
58
+
59
+ org_mtime = mtime_of(destination_path(STAMP_DESTINATION_FINGERPRINT_PATH))
60
+
61
+ ensure_file_mtime_changes do
62
+ FileUtils.mv(source_path('_tmp/site.css'), source_path('_tmp/site2.css'))
63
+
64
+ find_and_gsub_in_file(
65
+ source_path('_layouts/default.html'),
66
+ '{% ministamp _tmp/site.css assets/screen.css',
67
+ '{% ministamp _tmp/site2.css assets/screen.css'
68
+ )
69
+ end
70
+
71
+ generate_site(:production, clear_cache: false)
72
+
73
+ new_mtime = mtime_of(destination_path(STAMP_DESTINATION_FINGERPRINT_PATH))
74
+
75
+ assert_operator new_mtime, :>, org_mtime
76
+ end
77
+ end
78
+
79
+ def test_changing_asset_destination_path_rewrites_destination
80
+ with_site_dir do
81
+ generate_site(:production)
82
+
83
+ assert File.exist?(destination_path(STAMP_DESTINATION_FINGERPRINT_PATH))
84
+
85
+ ensure_file_mtime_changes do
86
+ find_and_gsub_in_file(
87
+ source_path('_layouts/default.html'),
88
+ '{% ministamp _tmp/site.css assets/screen.css',
89
+ '{% ministamp _tmp/site.css assets/screen2.css'
90
+ )
91
+ end
92
+
93
+ generate_site(:production, clear_cache: false)
94
+
95
+ refute File.exist?(destination_path(STAMP_DESTINATION_FINGERPRINT_PATH))
96
+
97
+ new_destination = "assets/screen2-#{STAMP_FINGERPRINT}.css"
98
+
99
+ assert_equal new_destination, find_css_path_from_index
100
+ assert File.exist?(destination_path(new_destination))
58
101
  end
59
102
  end
60
103
 
61
104
  def test_supports_relative_and_absolute_destination_paths
62
105
  with_site_dir do
63
106
  generate_site(:production)
64
- expected_path = destination_path(STAMP_DESTINATION_FINGERPRINT_PATH)
65
107
 
66
- assert File.exist?(expected_path)
108
+ assert File.exist?(destination_path(STAMP_DESTINATION_FINGERPRINT_PATH))
67
109
  assert_equal STAMP_DESTINATION_FINGERPRINT_PATH, find_css_path_from_index
68
110
 
69
- find_and_gsub_in_file(source_path('_layouts/default.html'), %r{assets/screen.css}, '/\0')
111
+ find_and_gsub_in_file(
112
+ source_path('_layouts/default.html'),
113
+ '{% ministamp _tmp/site.css assets/screen.css',
114
+ '{% ministamp _tmp/site.css /assets/screen.css'
115
+ )
116
+
70
117
  generate_site(:production, clear_cache: false)
71
118
 
72
- assert File.exist?(expected_path)
73
119
  assert_equal "/#{STAMP_DESTINATION_FINGERPRINT_PATH}", find_css_path_from_index
74
120
  end
75
121
  end
76
122
 
77
- def test_does_not_rewrite_destination_when_nonsource_files_change
123
+ def test_does_not_rewrite_destination_when_changing_nonsource_files
78
124
  with_site_dir do
79
125
  generate_site(:production)
126
+
80
127
  expected_path = destination_path(STAMP_DESTINATION_FINGERPRINT_PATH)
81
128
  org_mtime = mtime_of(expected_path)
82
129
  ensure_file_mtime_changes { File.write(source_path(JS_BUNDLE_SOURCE_DIR, 'dependency.js'), '(function() {})()') }
130
+
83
131
  generate_site(:production, clear_cache: false)
84
132
 
85
133
  assert_equal org_mtime, mtime_of(expected_path)
86
134
 
87
135
  ensure_file_mtime_changes { FileUtils.touch('index.html') }
136
+
88
137
  generate_site(:production, clear_cache: false)
89
138
 
90
139
  assert_equal org_mtime, mtime_of(expected_path)
@@ -23,6 +23,7 @@ module Jekyll::Minibundle::Test
23
23
  contents = 'h2 {}'
24
24
  File.write(source_path('assets/shared.css'), contents)
25
25
  find_and_gsub_in_file(source_path('_layouts/default.html'), 'ministamp _tmp/site.css', 'ministamp assets/shared.css')
26
+
26
27
  generate_site(env)
27
28
 
28
29
  asset_files = Dir[destination_path('assets') + '/screen*.css']
@@ -41,6 +42,7 @@ module Jekyll::Minibundle::Test
41
42
  File.write(source_path('assets/dependency.js'), dep_contents)
42
43
  File.write(source_path('assets/app.js'), app_contents)
43
44
  find_and_gsub_in_file(source_path('_layouts/default.html'), 'source_dir: _assets/scripts', 'source_dir: assets')
45
+
44
46
  generate_site(:development)
45
47
 
46
48
  assert_equal dep_contents, File.read(destination_path('assets/dependency.js'))
@@ -56,6 +58,7 @@ module Jekyll::Minibundle::Test
56
58
  File.write(source_path('assets/dependency.js'), dep_contents)
57
59
  File.write(source_path('assets/app.js'), app_contents)
58
60
  find_and_gsub_in_file(source_path('_layouts/default.html'), 'source_dir: _assets/scripts', 'source_dir: assets')
61
+
59
62
  generate_site(:production)
60
63
 
61
64
  asset_files = Dir[destination_path('assets/site-*.js')]
@@ -0,0 +1,24 @@
1
+ module Jekyll::Minibundle::Test
2
+ module Assertions
3
+ def assert_contains_only(collection, expected_elements)
4
+ assert_respond_to collection, :size
5
+
6
+ collection_size = collection.size
7
+ expected_elements_size = expected_elements.size
8
+
9
+ assert_equal(expected_elements_size, collection_size, lambda do
10
+ "Expected #{mu_pp(collection)} to have size #{expected_elements_size} instead of #{collection_size}"
11
+ end)
12
+
13
+ remaining = collection.dup.to_a
14
+ expected_elements.each do |e|
15
+ index = remaining.index(e)
16
+ remaining.delete_at(index) if index
17
+ end
18
+
19
+ assert(remaining.empty?, lambda do
20
+ "Expected #{mu_pp(collection)} to include only #{mu_pp(expected_elements)}"
21
+ end)
22
+ end
23
+ end
24
+ end
@@ -1,15 +1,18 @@
1
1
  module Jekyll::Minibundle::Test
2
2
  module FixtureConfig
3
- STAMP_SOURCE_PATH = '_tmp/site.css'
4
- STAMP_DESTINATION_PATH = 'assets/screen.css'
5
- STAMP_DESTINATION_FINGERPRINT_PATH = 'assets/screen-d57c1404fe726e66d57128a1bd190cbb.css'
3
+ STAMP_SOURCE_PATH = '_tmp/site.css'.freeze
4
+ STAMP_DESTINATION_PATH = 'assets/screen.css'.freeze
5
+ STAMP_FINGERPRINT = 'd57c1404fe726e66d57128a1bd190cbb'.freeze
6
+ STAMP_DESTINATION_FINGERPRINT_PATH = "assets/screen-#{STAMP_FINGERPRINT}.css".freeze
6
7
 
7
- CSS_BUNDLE_SOURCE_DIR = '_assets/styles'
8
- CSS_BUNDLE_DESTINATION_PATH = 'assets/site'
9
- CSS_BUNDLE_DESTINATION_FINGERPRINT_PATH = CSS_BUNDLE_DESTINATION_PATH + '-b2e0ecc1c100effc2c7353caad20c327.css'
10
-
11
- JS_BUNDLE_SOURCE_DIR = '_assets/scripts'
12
- JS_BUNDLE_DESTINATION_PATH = 'assets/site'
13
- JS_BUNDLE_DESTINATION_FINGERPRINT_PATH = JS_BUNDLE_DESTINATION_PATH + '-4782a1f67803038d4f8351051e67deb8.js'
8
+ CSS_BUNDLE_SOURCE_DIR = '_assets/styles'.freeze
9
+ CSS_BUNDLE_DESTINATION_PATH = 'assets/site'.freeze
10
+ CSS_BUNDLE_FINGERPRINT = 'b2e0ecc1c100effc2c7353caad20c327'.freeze
11
+ CSS_BUNDLE_DESTINATION_FINGERPRINT_PATH = "#{CSS_BUNDLE_DESTINATION_PATH}-#{CSS_BUNDLE_FINGERPRINT}.css".freeze
12
+
13
+ JS_BUNDLE_SOURCE_DIR = '_assets/scripts'.freeze
14
+ JS_BUNDLE_DESTINATION_PATH = 'assets/site'.freeze
15
+ JS_BUNDLE_FINGERPRINT = '4782a1f67803038d4f8351051e67deb8'.freeze
16
+ JS_BUNDLE_DESTINATION_FINGERPRINT_PATH = "#{JS_BUNDLE_DESTINATION_PATH}-#{JS_BUNDLE_FINGERPRINT}.js".freeze
14
17
  end
15
18
  end
@@ -1,10 +1,16 @@
1
1
  module Jekyll::Minibundle::Test
2
2
  module StaticFileAPIConfig
3
3
  STATIC_FILE_API_PROPERTIES = [
4
- :to_liquid,
5
- :extname,
4
+ :defaults,
6
5
  :destination_rel_dir,
6
+ :extname,
7
+ :modified_time,
8
+ :mtime,
9
+ :placeholders,
10
+ :relative_path,
11
+ :to_liquid,
12
+ :type,
7
13
  :write?
8
- ]
14
+ ].freeze
9
15
  end
10
16
  end
@@ -3,11 +3,13 @@ require 'ostruct'
3
3
  require 'tempfile'
4
4
  require 'minitest/autorun'
5
5
  require 'nokogiri'
6
+ require 'support/assertions'
6
7
  require 'jekyll'
7
8
  require 'jekyll/minibundle'
8
9
 
9
10
  module Jekyll::Minibundle::Test
10
11
  class TestCase < ::Minitest::Test
12
+ include Assertions
11
13
  include ::Jekyll::Minibundle
12
14
 
13
15
  FIXTURE_DIR = File.expand_path(File.join(File.dirname(__FILE__), '../fixture'))
@@ -54,10 +56,10 @@ module Jekyll::Minibundle::Test
54
56
  end
55
57
 
56
58
  def with_site_dir(&block)
57
- Dir.mktmpdir("jekyll-minibundle-test-site-") do |dir|
59
+ Dir.mktmpdir('jekyll-minibundle-test-site-') do |dir|
58
60
  Dir.chdir(dir) do
59
61
  _copy_fixture_site_dir(Dir.pwd)
60
- yield dir
62
+ block.call(dir)
61
63
  end
62
64
  end
63
65
  end
@@ -76,7 +78,7 @@ module Jekyll::Minibundle::Test
76
78
 
77
79
  def with_fake_site(&block)
78
80
  with_site_dir do |dir|
79
- yield new_fake_site(dir)
81
+ block.call(new_fake_site(dir))
80
82
  end
81
83
  end
82
84
 
@@ -95,7 +97,7 @@ module Jekyll::Minibundle::Test
95
97
 
96
98
  def ensure_file_mtime_changes(&block)
97
99
  sleep 1.5
98
- yield
100
+ block.call
99
101
  end
100
102
 
101
103
  def minifier_cmd_to_remove_comments
@@ -115,9 +117,8 @@ module Jekyll::Minibundle::Test
115
117
  end
116
118
 
117
119
  def get_send_results(obj, method_names)
118
- method_names.reduce({}) do |acc, method_name|
120
+ method_names.each_with_object({}) do |method_name, acc|
119
121
  acc[method_name] = obj.send(method_name)
120
- acc
121
122
  end
122
123
  end
123
124