nanoc 3.2.4 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. data/.gemtest +0 -0
  2. data/ChangeLog +3 -0
  3. data/Gemfile +32 -0
  4. data/LICENSE +19 -0
  5. data/NEWS.md +470 -0
  6. data/README.md +114 -0
  7. data/Rakefile +14 -0
  8. data/bin/nanoc +7 -27
  9. data/bin/nanoc3 +3 -0
  10. data/doc/yardoc_templates/default/layout/html/footer.erb +10 -0
  11. data/lib/nanoc.rb +41 -0
  12. data/lib/nanoc/base.rb +49 -0
  13. data/lib/nanoc/base/compilation/checksum_store.rb +57 -0
  14. data/lib/nanoc/base/compilation/compiled_content_cache.rb +62 -0
  15. data/lib/nanoc/base/compilation/compiler.rb +458 -0
  16. data/lib/nanoc/base/compilation/compiler_dsl.rb +214 -0
  17. data/lib/nanoc/base/compilation/dependency_tracker.rb +200 -0
  18. data/lib/nanoc/base/compilation/filter.rb +165 -0
  19. data/lib/nanoc/base/compilation/item_rep_proxy.rb +103 -0
  20. data/lib/nanoc/base/compilation/item_rep_recorder_proxy.rb +102 -0
  21. data/lib/nanoc/base/compilation/outdatedness_checker.rb +223 -0
  22. data/lib/nanoc/base/compilation/outdatedness_reasons.rb +46 -0
  23. data/lib/nanoc/base/compilation/rule.rb +73 -0
  24. data/lib/nanoc/base/compilation/rule_context.rb +84 -0
  25. data/lib/nanoc/base/compilation/rule_memory_calculator.rb +40 -0
  26. data/lib/nanoc/base/compilation/rule_memory_store.rb +53 -0
  27. data/lib/nanoc/base/compilation/rules_collection.rb +243 -0
  28. data/lib/nanoc/base/context.rb +47 -0
  29. data/lib/nanoc/base/core_ext.rb +6 -0
  30. data/lib/nanoc/base/core_ext/array.rb +62 -0
  31. data/lib/nanoc/base/core_ext/hash.rb +63 -0
  32. data/lib/nanoc/base/core_ext/pathname.rb +26 -0
  33. data/lib/nanoc/base/core_ext/string.rb +46 -0
  34. data/lib/nanoc/base/directed_graph.rb +275 -0
  35. data/lib/nanoc/base/errors.rb +211 -0
  36. data/lib/nanoc/base/memoization.rb +67 -0
  37. data/lib/nanoc/base/notification_center.rb +84 -0
  38. data/lib/nanoc/base/ordered_hash.rb +200 -0
  39. data/lib/nanoc/base/plugin_registry.rb +181 -0
  40. data/lib/nanoc/base/result_data/item_rep.rb +492 -0
  41. data/lib/nanoc/base/source_data/code_snippet.rb +58 -0
  42. data/lib/nanoc/base/source_data/configuration.rb +24 -0
  43. data/lib/nanoc/base/source_data/data_source.rb +234 -0
  44. data/lib/nanoc/base/source_data/item.rb +301 -0
  45. data/lib/nanoc/base/source_data/layout.rb +130 -0
  46. data/lib/nanoc/base/source_data/site.rb +361 -0
  47. data/lib/nanoc/base/store.rb +135 -0
  48. data/lib/nanoc/cli.rb +137 -0
  49. data/lib/nanoc/cli/command_runner.rb +104 -0
  50. data/lib/nanoc/cli/commands/autocompile.rb +58 -0
  51. data/lib/nanoc/cli/commands/compile.rb +297 -0
  52. data/lib/nanoc/cli/commands/create_item.rb +60 -0
  53. data/lib/nanoc/cli/commands/create_layout.rb +73 -0
  54. data/lib/nanoc/cli/commands/create_site.rb +411 -0
  55. data/lib/nanoc/cli/commands/debug.rb +117 -0
  56. data/lib/nanoc/cli/commands/deploy.rb +79 -0
  57. data/lib/nanoc/cli/commands/info.rb +98 -0
  58. data/lib/nanoc/cli/commands/nanoc.rb +38 -0
  59. data/lib/nanoc/cli/commands/prune.rb +50 -0
  60. data/lib/nanoc/cli/commands/update.rb +70 -0
  61. data/lib/nanoc/cli/commands/view.rb +82 -0
  62. data/lib/nanoc/cli/commands/watch.rb +124 -0
  63. data/lib/nanoc/cli/error_handler.rb +199 -0
  64. data/lib/nanoc/cli/logger.rb +92 -0
  65. data/lib/nanoc/data_sources.rb +29 -0
  66. data/lib/nanoc/data_sources/deprecated/delicious.rb +42 -0
  67. data/lib/nanoc/data_sources/deprecated/last_fm.rb +87 -0
  68. data/lib/nanoc/data_sources/deprecated/twitter.rb +38 -0
  69. data/lib/nanoc/data_sources/filesystem.rb +299 -0
  70. data/lib/nanoc/data_sources/filesystem_unified.rb +121 -0
  71. data/lib/nanoc/data_sources/filesystem_verbose.rb +91 -0
  72. data/lib/nanoc/extra.rb +24 -0
  73. data/lib/nanoc/extra/auto_compiler.rb +103 -0
  74. data/lib/nanoc/extra/chick.rb +125 -0
  75. data/lib/nanoc/extra/core_ext.rb +6 -0
  76. data/lib/nanoc/extra/core_ext/enumerable.rb +33 -0
  77. data/lib/nanoc/extra/core_ext/pathname.rb +30 -0
  78. data/lib/nanoc/extra/core_ext/time.rb +19 -0
  79. data/lib/nanoc/extra/deployer.rb +47 -0
  80. data/lib/nanoc/extra/deployers.rb +15 -0
  81. data/lib/nanoc/extra/deployers/fog.rb +98 -0
  82. data/lib/nanoc/extra/deployers/rsync.rb +70 -0
  83. data/lib/nanoc/extra/file_proxy.rb +40 -0
  84. data/lib/nanoc/extra/pruner.rb +86 -0
  85. data/lib/nanoc/extra/validators.rb +12 -0
  86. data/lib/nanoc/extra/validators/links.rb +268 -0
  87. data/lib/nanoc/extra/validators/w3c.rb +95 -0
  88. data/lib/nanoc/extra/vcs.rb +66 -0
  89. data/lib/nanoc/extra/vcses.rb +17 -0
  90. data/lib/nanoc/extra/vcses/bazaar.rb +25 -0
  91. data/lib/nanoc/extra/vcses/dummy.rb +24 -0
  92. data/lib/nanoc/extra/vcses/git.rb +25 -0
  93. data/lib/nanoc/extra/vcses/mercurial.rb +25 -0
  94. data/lib/nanoc/extra/vcses/subversion.rb +25 -0
  95. data/lib/nanoc/filters.rb +59 -0
  96. data/lib/nanoc/filters/asciidoc.rb +38 -0
  97. data/lib/nanoc/filters/bluecloth.rb +19 -0
  98. data/lib/nanoc/filters/coderay.rb +21 -0
  99. data/lib/nanoc/filters/coffeescript.rb +20 -0
  100. data/lib/nanoc/filters/colorize_syntax.rb +298 -0
  101. data/lib/nanoc/filters/erb.rb +38 -0
  102. data/lib/nanoc/filters/erubis.rb +34 -0
  103. data/lib/nanoc/filters/haml.rb +27 -0
  104. data/lib/nanoc/filters/kramdown.rb +20 -0
  105. data/lib/nanoc/filters/less.rb +53 -0
  106. data/lib/nanoc/filters/markaby.rb +20 -0
  107. data/lib/nanoc/filters/maruku.rb +20 -0
  108. data/lib/nanoc/filters/mustache.rb +24 -0
  109. data/lib/nanoc/filters/rainpress.rb +19 -0
  110. data/lib/nanoc/filters/rdiscount.rb +22 -0
  111. data/lib/nanoc/filters/rdoc.rb +33 -0
  112. data/lib/nanoc/filters/redcarpet.rb +62 -0
  113. data/lib/nanoc/filters/redcloth.rb +47 -0
  114. data/lib/nanoc/filters/relativize_paths.rb +94 -0
  115. data/lib/nanoc/filters/rubypants.rb +20 -0
  116. data/lib/nanoc/filters/sass.rb +74 -0
  117. data/lib/nanoc/filters/slim.rb +25 -0
  118. data/lib/nanoc/filters/typogruby.rb +23 -0
  119. data/lib/nanoc/filters/uglify_js.rb +42 -0
  120. data/lib/nanoc/filters/xsl.rb +46 -0
  121. data/lib/nanoc/filters/yui_compressor.rb +23 -0
  122. data/lib/nanoc/helpers.rb +16 -0
  123. data/lib/nanoc/helpers/blogging.rb +319 -0
  124. data/lib/nanoc/helpers/breadcrumbs.rb +40 -0
  125. data/lib/nanoc/helpers/capturing.rb +138 -0
  126. data/lib/nanoc/helpers/filtering.rb +50 -0
  127. data/lib/nanoc/helpers/html_escape.rb +55 -0
  128. data/lib/nanoc/helpers/link_to.rb +151 -0
  129. data/lib/nanoc/helpers/rendering.rb +140 -0
  130. data/lib/nanoc/helpers/tagging.rb +71 -0
  131. data/lib/nanoc/helpers/text.rb +44 -0
  132. data/lib/nanoc/helpers/xml_sitemap.rb +76 -0
  133. data/lib/nanoc/tasks.rb +10 -0
  134. data/lib/nanoc/tasks/clean.rake +16 -0
  135. data/lib/nanoc/tasks/clean.rb +29 -0
  136. data/lib/nanoc/tasks/deploy/rsync.rake +16 -0
  137. data/lib/nanoc/tasks/validate.rake +92 -0
  138. data/nanoc.gemspec +49 -0
  139. data/tasks/doc.rake +16 -0
  140. data/tasks/test.rake +46 -0
  141. data/test/base/core_ext/array_spec.rb +73 -0
  142. data/test/base/core_ext/hash_spec.rb +98 -0
  143. data/test/base/core_ext/pathname_spec.rb +27 -0
  144. data/test/base/core_ext/string_spec.rb +37 -0
  145. data/test/base/test_checksum_store.rb +35 -0
  146. data/test/base/test_code_snippet.rb +31 -0
  147. data/test/base/test_compiler.rb +403 -0
  148. data/test/base/test_compiler_dsl.rb +161 -0
  149. data/test/base/test_context.rb +31 -0
  150. data/test/base/test_data_source.rb +46 -0
  151. data/test/base/test_dependency_tracker.rb +262 -0
  152. data/test/base/test_directed_graph.rb +288 -0
  153. data/test/base/test_filter.rb +83 -0
  154. data/test/base/test_item.rb +179 -0
  155. data/test/base/test_item_rep.rb +579 -0
  156. data/test/base/test_layout.rb +59 -0
  157. data/test/base/test_memoization.rb +90 -0
  158. data/test/base/test_notification_center.rb +34 -0
  159. data/test/base/test_outdatedness_checker.rb +394 -0
  160. data/test/base/test_plugin.rb +30 -0
  161. data/test/base/test_rule.rb +19 -0
  162. data/test/base/test_rule_context.rb +65 -0
  163. data/test/base/test_site.rb +190 -0
  164. data/test/cli/commands/test_compile.rb +33 -0
  165. data/test/cli/commands/test_create_item.rb +14 -0
  166. data/test/cli/commands/test_create_layout.rb +28 -0
  167. data/test/cli/commands/test_create_site.rb +24 -0
  168. data/test/cli/commands/test_deploy.rb +74 -0
  169. data/test/cli/commands/test_help.rb +12 -0
  170. data/test/cli/commands/test_info.rb +11 -0
  171. data/test/cli/commands/test_prune.rb +98 -0
  172. data/test/cli/commands/test_update.rb +10 -0
  173. data/test/cli/test_cli.rb +102 -0
  174. data/test/cli/test_error_handler.rb +29 -0
  175. data/test/cli/test_logger.rb +10 -0
  176. data/test/data_sources/test_filesystem.rb +433 -0
  177. data/test/data_sources/test_filesystem_unified.rb +536 -0
  178. data/test/data_sources/test_filesystem_verbose.rb +357 -0
  179. data/test/extra/core_ext/test_enumerable.rb +30 -0
  180. data/test/extra/core_ext/test_pathname.rb +17 -0
  181. data/test/extra/core_ext/test_time.rb +15 -0
  182. data/test/extra/deployers/test_fog.rb +67 -0
  183. data/test/extra/deployers/test_rsync.rb +100 -0
  184. data/test/extra/test_auto_compiler.rb +417 -0
  185. data/test/extra/test_file_proxy.rb +19 -0
  186. data/test/extra/test_vcs.rb +22 -0
  187. data/test/extra/validators/test_links.rb +62 -0
  188. data/test/extra/validators/test_w3c.rb +47 -0
  189. data/test/filters/test_asciidoc.rb +22 -0
  190. data/test/filters/test_bluecloth.rb +18 -0
  191. data/test/filters/test_coderay.rb +44 -0
  192. data/test/filters/test_coffeescript.rb +18 -0
  193. data/test/filters/test_colorize_syntax.rb +379 -0
  194. data/test/filters/test_erb.rb +105 -0
  195. data/test/filters/test_erubis.rb +78 -0
  196. data/test/filters/test_haml.rb +96 -0
  197. data/test/filters/test_kramdown.rb +18 -0
  198. data/test/filters/test_less.rb +113 -0
  199. data/test/filters/test_markaby.rb +24 -0
  200. data/test/filters/test_maruku.rb +18 -0
  201. data/test/filters/test_mustache.rb +25 -0
  202. data/test/filters/test_rainpress.rb +29 -0
  203. data/test/filters/test_rdiscount.rb +31 -0
  204. data/test/filters/test_rdoc.rb +18 -0
  205. data/test/filters/test_redcarpet.rb +73 -0
  206. data/test/filters/test_redcloth.rb +33 -0
  207. data/test/filters/test_relativize_paths.rb +533 -0
  208. data/test/filters/test_rubypants.rb +18 -0
  209. data/test/filters/test_sass.rb +229 -0
  210. data/test/filters/test_slim.rb +35 -0
  211. data/test/filters/test_typogruby.rb +21 -0
  212. data/test/filters/test_uglify_js.rb +30 -0
  213. data/test/filters/test_xsl.rb +105 -0
  214. data/test/filters/test_yui_compressor.rb +44 -0
  215. data/test/gem_loader.rb +11 -0
  216. data/test/helper.rb +207 -0
  217. data/test/helpers/test_blogging.rb +754 -0
  218. data/test/helpers/test_breadcrumbs.rb +81 -0
  219. data/test/helpers/test_capturing.rb +41 -0
  220. data/test/helpers/test_filtering.rb +106 -0
  221. data/test/helpers/test_html_escape.rb +32 -0
  222. data/test/helpers/test_link_to.rb +249 -0
  223. data/test/helpers/test_rendering.rb +89 -0
  224. data/test/helpers/test_tagging.rb +87 -0
  225. data/test/helpers/test_text.rb +24 -0
  226. data/test/helpers/test_xml_sitemap.rb +103 -0
  227. data/test/tasks/test_clean.rb +67 -0
  228. metadata +327 -15
  229. data/bin/nanoc-select +0 -86
  230. data/lib/nanoc-select.rb +0 -11
@@ -0,0 +1,73 @@
1
+ # encoding: utf-8
2
+
3
+ describe 'Array#symbolize_keys' do
4
+
5
+ it 'should convert keys to symbols' do
6
+ array_old = [ :abc, 'xyz', { 'foo' => 'bar', :baz => :qux } ]
7
+ array_new = [ :abc, 'xyz', { :foo => 'bar', :baz => :qux } ]
8
+ array_old.symbolize_keys.must_equal array_new
9
+ end
10
+
11
+ end
12
+
13
+ describe 'Array#stringify_keys' do
14
+
15
+ it 'should convert keys to strings' do
16
+ array_old = [ :abc, 'xyz', { :foo => 'bar', 'baz' => :qux } ]
17
+ array_new = [ :abc, 'xyz', { 'foo' => 'bar', 'baz' => :qux } ]
18
+ array_old.stringify_keys.must_equal array_new
19
+ end
20
+
21
+ end
22
+
23
+ describe 'Array#freeze_recursively' do
24
+
25
+ it 'should prevent first-level elements from being modified' do
26
+ array = [ :a, [ :b, :c ], :d ]
27
+ array.freeze_recursively
28
+
29
+ raised = false
30
+ begin
31
+ array[0] = 123
32
+ rescue => e
33
+ raised = true
34
+ assert_match /^can't modify frozen /, e.message
35
+ end
36
+ assert raised
37
+ end
38
+
39
+ it 'should prevent second-level elements from being modified' do
40
+ array = [ :a, [ :b, :c ], :d ]
41
+ array.freeze_recursively
42
+
43
+ raised = false
44
+ begin
45
+ array[1][0] = 123
46
+ rescue => e
47
+ raised = true
48
+ assert_match /^can't modify frozen /, e.message
49
+ end
50
+ assert raised
51
+ end
52
+
53
+ it 'should not freeze infinitely' do
54
+ a = []
55
+ a << a
56
+
57
+ a.freeze_recursively
58
+
59
+ assert a.frozen?
60
+ assert a[0].frozen?
61
+ assert_equal a, a[0]
62
+ end
63
+
64
+ end
65
+
66
+ describe 'Array#checksum' do
67
+
68
+ it 'should work' do
69
+ expectation = '78468f950645150238a26f5b8f2dde39a75a7028'
70
+ [ [ :foo, 123 ]].checksum.must_equal expectation
71
+ end
72
+
73
+ end
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+
3
+ describe 'Hash#symbolize_keys' do
4
+
5
+ it 'should convert keys to symbols' do
6
+ hash_old = { 'foo' => 'bar' }
7
+ hash_new = { :foo => 'bar' }
8
+ hash_old.symbolize_keys.must_equal hash_new
9
+ end
10
+
11
+ end
12
+
13
+ describe 'Hash#stringify_keys' do
14
+
15
+ it 'should leave strings as strings' do
16
+ hash_old = { 'foo' => 'bar' }
17
+ hash_new = { 'foo' => 'bar' }
18
+ hash_old.stringify_keys.must_equal hash_new
19
+ end
20
+
21
+ it 'should convert symbols to strings' do
22
+ hash_old = { :foo => 'bar' }
23
+ hash_new = { 'foo' => 'bar' }
24
+ hash_old.stringify_keys.must_equal hash_new
25
+ end
26
+
27
+ it 'should convert integers to strings' do
28
+ hash_old = { 123 => 'bar' }
29
+ hash_new = { '123' => 'bar' }
30
+ hash_old.stringify_keys.must_equal hash_new
31
+ end
32
+
33
+ it 'should convert nil to an empty string' do
34
+ hash_old = { nil => 'bar' }
35
+ hash_new = { '' => 'bar' }
36
+ hash_old.stringify_keys.must_equal hash_new
37
+ end
38
+
39
+ end
40
+
41
+ describe 'Hash#freeze_recursively' do
42
+
43
+ it 'should prevent first-level elements from being modified' do
44
+ hash = { :a => { :b => :c } }
45
+ hash.freeze_recursively
46
+
47
+ raised = false
48
+ begin
49
+ hash[:a] = 123
50
+ rescue => e
51
+ raised = true
52
+ assert_match /^can't modify frozen /, e.message
53
+ end
54
+ assert raised
55
+ end
56
+
57
+ it 'should prevent second-level elements from being modified' do
58
+ hash = { :a => { :b => :c } }
59
+ hash.freeze_recursively
60
+
61
+ raised = false
62
+ begin
63
+ hash[:a][:b] = 123
64
+ rescue => e
65
+ raised = true
66
+ assert_match /^can't modify frozen /, e.message
67
+ end
68
+ assert raised
69
+ end
70
+
71
+ it 'should not freeze infinitely' do
72
+ a = {}
73
+ a[:x] = a
74
+
75
+ a.freeze_recursively
76
+
77
+ assert a.frozen?
78
+ assert a[:x].frozen?
79
+ assert_equal a, a[:x]
80
+ end
81
+
82
+ end
83
+
84
+ describe 'Hash#checksum' do
85
+
86
+ it 'should work' do
87
+ expectation = '78468f950645150238a26f5b8f2dde39a75a7028'
88
+ { :foo => 123 }.checksum.must_equal expectation
89
+ [ [ :foo, 123 ]].checksum.must_equal expectation
90
+ end
91
+
92
+ it 'should sort keys' do
93
+ a = { :a => 1, :c => 2, :b => 3 }.checksum
94
+ b = { :a => 1, :b => 3, :c => 2 }.checksum
95
+ a.must_equal b
96
+ end
97
+
98
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ describe 'Pathname#checksum' do
4
+
5
+ it 'should work on empty files' do
6
+ begin
7
+ FileUtils.mkdir_p('tmp')
8
+ File.open('tmp/myfile', 'w') { |io| io.write('') }
9
+ pathname = Pathname.new('tmp/myfile')
10
+ pathname.checksum.must_equal 'da39a3ee5e6b4b0d3255bfef95601890afd80709'
11
+ ensure
12
+ FileUtils.rm_rf('tmp')
13
+ end
14
+ end
15
+
16
+ it 'should work on all files' do
17
+ begin
18
+ FileUtils.mkdir_p('tmp')
19
+ File.open('tmp/myfile', 'w') { |io| io.write('abc') }
20
+ pathname = Pathname.new('tmp/myfile')
21
+ pathname.checksum.must_equal 'a9993e364706816aba3e25717850c26c9cd0d89d'
22
+ ensure
23
+ FileUtils.rm_rf('tmp')
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ describe 'String#cleaned_identifier' do
4
+
5
+ it 'should not convert already clean paths' do
6
+ '/foo/bar/'.cleaned_identifier.must_equal '/foo/bar/'
7
+ end
8
+
9
+ it 'should prepend slash if necessary' do
10
+ 'foo/bar/'.cleaned_identifier.must_equal '/foo/bar/'
11
+ end
12
+
13
+ it 'should append slash if necessary' do
14
+ '/foo/bar'.cleaned_identifier.must_equal '/foo/bar/'
15
+ end
16
+
17
+ it 'should remove double slashes at start' do
18
+ '//foo/bar/'.cleaned_identifier.must_equal '/foo/bar/'
19
+ end
20
+
21
+ it 'should remove double slashes at end' do
22
+ '/foo/bar//'.cleaned_identifier.must_equal '/foo/bar/'
23
+ end
24
+
25
+ end
26
+
27
+ describe 'String#checksum' do
28
+
29
+ it 'should work on empty strings' do
30
+ ''.checksum.must_equal 'da39a3ee5e6b4b0d3255bfef95601890afd80709'
31
+ end
32
+
33
+ it 'should work on all strings' do
34
+ 'abc'.checksum.must_equal 'a9993e364706816aba3e25717850c26c9cd0d89d'
35
+ end
36
+
37
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ class Nanoc::ChecksumStoreTest < MiniTest::Unit::TestCase
4
+
5
+ include Nanoc::TestHelpers
6
+
7
+ def test_get_with_existing_object
8
+ require 'pstore'
9
+
10
+ # Create store
11
+ FileUtils.mkdir_p('tmp')
12
+ pstore = PStore.new('tmp/checksums')
13
+ pstore.transaction do
14
+ pstore[:data] = { [ :item, '/moo/' ] => 'zomg' }
15
+ pstore[:version] = 1
16
+ end
17
+
18
+ # Check
19
+ store = Nanoc::ChecksumStore.new
20
+ store.load
21
+ obj = Nanoc::Item.new('Moo?', {}, '/moo/')
22
+ assert_equal 'zomg', store[obj]
23
+ end
24
+
25
+ def test_get_with_nonexistant_object
26
+ store = Nanoc::ChecksumStore.new
27
+ store.load
28
+
29
+ # Check
30
+ obj = Nanoc::Item.new('Moo?', {}, '/animals/cow/')
31
+ new_checksum = 'Moo?'.checksum + '-' + {}.checksum
32
+ assert_equal nil, store[obj]
33
+ end
34
+
35
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ class Nanoc::CodeSnippetTest < MiniTest::Unit::TestCase
4
+
5
+ include Nanoc::TestHelpers
6
+
7
+ def test_load
8
+ # Initialize
9
+ $complete_insane_parrot = 'meow'
10
+
11
+ # Create code and load it
12
+ code_snippet = Nanoc::CodeSnippet.new("$complete_insane_parrot = 'woof'", 'parrot.rb')
13
+ code_snippet.load
14
+
15
+ # Ensure code is loaded
16
+ assert_equal('woof', $complete_insane_parrot)
17
+ end
18
+
19
+ def test_load_with_toplevel_binding
20
+ # Initialize
21
+ @foo = 'meow'
22
+
23
+ # Create code and load it
24
+ code_snippet = Nanoc::CodeSnippet.new("@foo = 'woof'", 'dog.rb')
25
+ code_snippet.load
26
+
27
+ # Ensure binding is correct
28
+ assert_equal('meow', @foo)
29
+ end
30
+
31
+ end
@@ -0,0 +1,403 @@
1
+ # encoding: utf-8
2
+
3
+ class Nanoc::CompilerTest < MiniTest::Unit::TestCase
4
+
5
+ include Nanoc::TestHelpers
6
+
7
+ def test_compilation_rule_for
8
+ # Mock rules
9
+ rules = [ mock, mock, mock ]
10
+ rules[0].expects(:applicable_to?).returns(false)
11
+ rules[1].expects(:applicable_to?).returns(true)
12
+ rules[1].expects(:rep_name).returns('wrong')
13
+ rules[2].expects(:applicable_to?).returns(true)
14
+ rules[2].expects(:rep_name).returns('right')
15
+
16
+ # Create compiler
17
+ compiler = Nanoc::Compiler.new(nil)
18
+ compiler.rules_collection.instance_eval { @item_compilation_rules = rules }
19
+
20
+ # Mock rep
21
+ rep = mock
22
+ rep.stubs(:name).returns('right')
23
+ item = mock
24
+ rep.stubs(:item).returns(item)
25
+
26
+ # Test
27
+ assert_equal rules[2], compiler.rules_collection.compilation_rule_for(rep)
28
+ end
29
+
30
+ def test_routing_rule_for
31
+ # Mock rules
32
+ rules = [ mock, mock, mock ]
33
+ rules[0].expects(:applicable_to?).returns(false)
34
+ rules[1].expects(:applicable_to?).returns(true)
35
+ rules[1].expects(:rep_name).returns('wrong')
36
+ rules[2].expects(:applicable_to?).returns(true)
37
+ rules[2].expects(:rep_name).returns('right')
38
+
39
+ # Create compiler
40
+ compiler = Nanoc::Compiler.new(nil)
41
+ compiler.rules_collection.instance_eval { @item_routing_rules = rules }
42
+
43
+ # Mock rep
44
+ rep = mock
45
+ rep.stubs(:name).returns('right')
46
+ item = mock
47
+ rep.stubs(:item).returns(item)
48
+
49
+ # Test
50
+ assert_equal rules[2], compiler.rules_collection.routing_rule_for(rep)
51
+ end
52
+
53
+ def test_filter_for_layout_with_existant_layout
54
+ # Mock site
55
+ site = mock
56
+
57
+ # Create compiler
58
+ compiler = Nanoc::Compiler.new(site)
59
+ compiler.rules_collection.layout_filter_mapping[/.*/] = [ :erb, { :foo => 'bar' } ]
60
+
61
+ # Mock layout
62
+ layout = MiniTest::Mock.new
63
+ layout.expect(:identifier, '/some_layout/')
64
+
65
+ # Check
66
+ assert_equal([ :erb, { :foo => 'bar' } ], compiler.rules_collection.filter_for_layout(layout))
67
+ end
68
+
69
+ def test_filter_for_layout_with_existant_layout_and_unknown_filter
70
+ # Mock site
71
+ site = mock
72
+
73
+ # Create compiler
74
+ compiler = Nanoc::Compiler.new(site)
75
+ compiler.rules_collection.layout_filter_mapping[/.*/] = [ :some_unknown_filter, { :foo => 'bar' } ]
76
+
77
+ # Mock layout
78
+ layout = MiniTest::Mock.new
79
+ layout.expect(:identifier, '/some_layout/')
80
+
81
+ # Check
82
+ assert_equal([ :some_unknown_filter, { :foo => 'bar' } ], compiler.rules_collection.filter_for_layout(layout))
83
+ end
84
+
85
+ def test_filter_for_layout_with_nonexistant_layout
86
+ # Mock site
87
+ site = mock
88
+
89
+ # Create compiler
90
+ compiler = Nanoc::Compiler.new(site)
91
+ compiler.rules_collection.layout_filter_mapping[%r{^/foo/$}] = [ :erb, { :foo => 'bar' } ]
92
+
93
+ # Mock layout
94
+ layout = MiniTest::Mock.new
95
+ layout.expect(:identifier, '/bar/')
96
+
97
+ # Check
98
+ assert_equal(nil, compiler.rules_collection.filter_for_layout(layout))
99
+ end
100
+
101
+ def test_filter_for_layout_with_many_layouts
102
+ # Mock site
103
+ site = mock
104
+
105
+ # Create compiler
106
+ compiler = Nanoc::Compiler.new(site)
107
+ compiler.rules_collection.layout_filter_mapping[%r{^/a/b/c/.*/$}] = [ :erb, { :char => 'd' } ]
108
+ compiler.rules_collection.layout_filter_mapping[%r{^/a/.*/$}] = [ :erb, { :char => 'b' } ]
109
+ compiler.rules_collection.layout_filter_mapping[%r{^/a/b/.*/$}] = [ :erb, { :char => 'c' } ] # never used!
110
+ compiler.rules_collection.layout_filter_mapping[%r{^/.*/$}] = [ :erb, { :char => 'a' } ]
111
+
112
+ # Mock layout
113
+ layouts = [ mock, mock, mock, mock ]
114
+ layouts[0].stubs(:identifier).returns('/a/b/c/d/')
115
+ layouts[1].stubs(:identifier).returns('/a/b/c/')
116
+ layouts[2].stubs(:identifier).returns('/a/b/')
117
+ layouts[3].stubs(:identifier).returns('/a/')
118
+
119
+ # Get expectations
120
+ expectations = {
121
+ 0 => 'd',
122
+ 1 => 'b', # never used! not c, because b takes priority
123
+ 2 => 'b',
124
+ 3 => 'a'
125
+ }
126
+
127
+ # Check
128
+ expectations.each_pair do |num, char|
129
+ filter_and_args = compiler.rules_collection.filter_for_layout(layouts[num])
130
+ refute_nil(filter_and_args)
131
+ assert_equal(char, filter_and_args[1][:char])
132
+ end
133
+ end
134
+
135
+ def test_compile_rep_should_write_proper_snapshots
136
+ # Mock rep
137
+ item = Nanoc::Item.new('<%= 1 %> <%%= 2 %> <%%%= 3 %>', {}, '/moo/')
138
+ rep = Nanoc::ItemRep.new(item, :blah)
139
+
140
+ # Set snapshot filenames
141
+ rep.raw_paths = {
142
+ :raw => 'raw.txt',
143
+ :pre => 'pre.txt',
144
+ :post => 'post.txt',
145
+ :last => 'last.txt'
146
+ }
147
+
148
+ # Create rule
149
+ rule_block = proc do
150
+ filter :erb
151
+ filter :erb
152
+ layout '/blah/'
153
+ filter :erb
154
+ end
155
+ rule = Nanoc::Rule.new(/blah/, :meh, rule_block)
156
+
157
+ # Create layout
158
+ layout = Nanoc::Layout.new('head <%= yield %> foot', {}, '/blah/')
159
+
160
+ # Create site
161
+ site = mock
162
+ site.stubs(:config).returns({})
163
+ site.stubs(:items).returns([])
164
+ site.stubs(:layouts).returns([ layout ])
165
+
166
+ # Create compiler
167
+ compiler = Nanoc::Compiler.new(site)
168
+ compiler.rules_collection.expects(:compilation_rule_for).times(2).with(rep).returns(rule)
169
+ compiler.rules_collection.layout_filter_mapping[%r{^/blah/$}] = [ :erb, {} ]
170
+ site.stubs(:compiler).returns(compiler)
171
+
172
+ # Compile
173
+ compiler.send(:compile_rep, rep)
174
+
175
+ # Test
176
+ assert File.file?('raw.txt')
177
+ assert File.file?('pre.txt')
178
+ assert File.file?('post.txt')
179
+ assert File.file?('last.txt')
180
+ assert_equal '<%= 1 %> <%%= 2 %> <%%%= 3 %>', File.read('raw.txt')
181
+ assert_equal '1 2 <%= 3 %>', File.read('pre.txt')
182
+ assert_equal 'head 1 2 3 foot', File.read('post.txt')
183
+ assert_equal 'head 1 2 3 foot', File.read('last.txt')
184
+ end
185
+
186
+ def test_compile_with_no_reps
187
+ with_site do |site|
188
+ site.compile
189
+
190
+ assert Dir['output/*'].empty?
191
+ end
192
+ end
193
+
194
+ def test_compile_with_one_rep
195
+ with_site do |site|
196
+ File.open('content/index.html', 'w') { |io| io.write('o hello') }
197
+
198
+ site.compile
199
+
200
+ assert Dir['output/*'].size == 1
201
+ assert File.file?('output/index.html')
202
+ assert File.read('output/index.html') == 'o hello'
203
+ end
204
+ end
205
+
206
+ def test_compile_with_two_independent_reps
207
+ with_site do |site|
208
+ File.open('content/foo.html', 'w') { |io| io.write('o hai') }
209
+ File.open('content/bar.html', 'w') { |io| io.write('o bai') }
210
+
211
+ site.compile
212
+
213
+ assert Dir['output/*'].size == 2
214
+ assert File.file?('output/foo/index.html')
215
+ assert File.file?('output/bar/index.html')
216
+ assert File.read('output/foo/index.html') == 'o hai'
217
+ assert File.read('output/bar/index.html') == 'o bai'
218
+ end
219
+ end
220
+
221
+ def test_compile_with_two_dependent_reps
222
+ with_site(:compilation_rule_content => 'filter :erb') do |site|
223
+ File.open('content/foo.html', 'w') do |io|
224
+ io.write('<%= @items.find { |i| i.identifier == "/bar/" }.compiled_content %>!!!')
225
+ end
226
+ File.open('content/bar.html', 'w') do |io|
227
+ io.write('manatee')
228
+ end
229
+
230
+ site.compile
231
+
232
+ assert Dir['output/*'].size == 2
233
+ assert File.file?('output/foo/index.html')
234
+ assert File.file?('output/bar/index.html')
235
+ assert File.read('output/foo/index.html') == 'manatee!!!'
236
+ assert File.read('output/bar/index.html') == 'manatee'
237
+ end
238
+ end
239
+
240
+ def test_compile_with_two_mutually_dependent_reps
241
+ with_site(:compilation_rule_content => 'filter :erb') do |site|
242
+ File.open('content/foo.html', 'w') do |io|
243
+ io.write('<%= @items.find { |i| i.identifier == "/bar/" }.compiled_content %>')
244
+ end
245
+ File.open('content/bar.html', 'w') do |io|
246
+ io.write('<%= @items.find { |i| i.identifier == "/foo/" }.compiled_content %>')
247
+ end
248
+
249
+ assert_raises Nanoc::Errors::RecursiveCompilation do
250
+ site.compile
251
+ end
252
+ end
253
+ end
254
+
255
+ def test_disallow_routes_not_starting_with_slash
256
+ # Create site
257
+ Nanoc::CLI.run %w( create_site bar)
258
+
259
+ FileUtils.cd('bar') do
260
+ # Create routes
261
+ File.open('Rules', 'w') do |io|
262
+ io.write "compile '*' do\n"
263
+ io.write " layout 'default'\n"
264
+ io.write "end\n"
265
+ io.write "\n"
266
+ io.write "route '*' do\n"
267
+ io.write " 'index.html'\n"
268
+ io.write "end\n"
269
+ io.write "\n"
270
+ io.write "layout '*', :erb\n"
271
+ end
272
+
273
+ # Create site
274
+ site = Nanoc::Site.new('.')
275
+ error = assert_raises(RuntimeError) do
276
+ site.compile
277
+ end
278
+ assert_match /^The path returned for the.*does not start with a slash. Please ensure that all routing rules return a path that starts with a slash./, error.message
279
+ end
280
+ end
281
+
282
+ def test_load_should_be_idempotent
283
+ # Create site
284
+ Nanoc::CLI.run %w( create_site bar)
285
+
286
+ FileUtils.cd('bar') do
287
+ site = Nanoc::Site.new('.')
288
+
289
+ compiler = Nanoc::Compiler.new(site)
290
+ def compiler.route_reps
291
+ raise 'oh my gosh it is borken'
292
+ end
293
+
294
+ assert site.instance_eval { !@loaded }
295
+ assert_raises(RuntimeError) { compiler.load }
296
+ assert site.instance_eval { !@loaded }
297
+ assert_raises(RuntimeError) { compiler.load }
298
+ end
299
+ end
300
+
301
+ def test_compile_should_recompile_all_reps
302
+ Nanoc::CLI.run %w( create_site bar )
303
+
304
+ FileUtils.cd('bar') do
305
+ Nanoc::CLI.run %w( compile )
306
+
307
+ site = Nanoc::Site.new('.')
308
+ site.compile
309
+
310
+ # At this point, even the already compiled items in the previous pass
311
+ # should have their compiled content assigned, so this should work:
312
+ site.items[0].reps[0].compiled_content
313
+ end
314
+ end
315
+
316
+ def test_disallow_multiple_snapshots_with_the_same_name
317
+ # Create site
318
+ Nanoc::CLI.run %w( create_site bar )
319
+
320
+ FileUtils.cd('bar') do
321
+ # Create routes
322
+ File.open('Rules', 'w') do |io|
323
+ io.write "compile '*' do\n"
324
+ io.write " snapshot :aaa\n"
325
+ io.write " snapshot :aaa\n"
326
+ io.write "end\n"
327
+ io.write "\n"
328
+ io.write "route '*' do\n"
329
+ io.write " '/index.html'\n"
330
+ io.write "end\n"
331
+ io.write "\n"
332
+ io.write "layout '*', :erb\n"
333
+ end
334
+
335
+ # Compile
336
+ site = Nanoc::Site.new('.')
337
+ assert_raises Nanoc::Errors::CannotCreateMultipleSnapshotsWithSameName do
338
+ site.compile
339
+ end
340
+ end
341
+ end
342
+
343
+ def test_include_compiled_content_of_active_item_at_previous_snapshot
344
+ with_site do |site|
345
+ # Create item
346
+ File.open('content/index.html', 'w') do |io|
347
+ io.write('[<%= @item.compiled_content(:snapshot => :aaa) %>]')
348
+ end
349
+
350
+ # Create routes
351
+ File.open('Rules', 'w') do |io|
352
+ io.write "compile '*' do\n"
353
+ io.write " snapshot :aaa\n"
354
+ io.write " filter :erb\n"
355
+ io.write " filter :erb\n"
356
+ io.write "end\n"
357
+ io.write "\n"
358
+ io.write "route '*' do\n"
359
+ io.write " '/index.html'\n"
360
+ io.write "end\n"
361
+ io.write "\n"
362
+ io.write "layout '*', :erb\n"
363
+ end
364
+
365
+ # Compile
366
+ site = Nanoc::Site.new('.')
367
+ site.compile
368
+
369
+ # Check
370
+ assert_equal '[[[<%= @item.compiled_content(:snapshot => :aaa) %>]]]', File.read('output/index.html')
371
+ end
372
+ end
373
+
374
+ def test_layout_with_extra_filter_args
375
+ with_site do |site|
376
+ # Create item
377
+ File.open('content/index.html', 'w') do |io|
378
+ io.write('This is <%= @foo %>.')
379
+ end
380
+
381
+ # Create routes
382
+ File.open('Rules', 'w') do |io|
383
+ io.write "compile '*' do\n"
384
+ io.write " filter :erb, :locals => { :foo => 123 }\n"
385
+ io.write "end\n"
386
+ io.write "\n"
387
+ io.write "route '*' do\n"
388
+ io.write " '/index.html'\n"
389
+ io.write "end\n"
390
+ io.write "\n"
391
+ io.write "layout '*', :erb\n"
392
+ end
393
+
394
+ # Compile
395
+ site = Nanoc::Site.new('.')
396
+ site.compile
397
+
398
+ # Check
399
+ assert_equal 'This is 123.', File.read('output/index.html')
400
+ end
401
+ end
402
+
403
+ end