middleman-sprockets-cj 3.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +2 -0
  4. data/.simplecov +9 -0
  5. data/.travis.yml +15 -0
  6. data/CHANGELOG.md +71 -0
  7. data/CONTRIBUTING.md +43 -0
  8. data/Gemfile +32 -0
  9. data/LICENSE.md +20 -0
  10. data/README.md +104 -0
  11. data/Rakefile +45 -0
  12. data/features/asset_hash-3.0.feature +105 -0
  13. data/features/asset_hash.feature +107 -0
  14. data/features/bower.feature +37 -0
  15. data/features/jst.feature +14 -0
  16. data/features/long_filenames.feature +17 -0
  17. data/features/sass_globs.feature +9 -0
  18. data/features/sass_partials.feature +36 -0
  19. data/features/sprockets.feature +129 -0
  20. data/features/sprockets_gems.feature +67 -0
  21. data/features/step_definitions/server_steps.rb +7 -0
  22. data/features/support/env.rb +18 -0
  23. data/fixtures/asset-hash-app/config.rb +7 -0
  24. data/fixtures/asset-hash-app/source/images/100px.gif +0 -0
  25. data/fixtures/asset-hash-app/source/images/100px.jpg +0 -0
  26. data/fixtures/asset-hash-app/source/images/100px.png +0 -0
  27. data/fixtures/asset-hash-app/source/index.html.erb +308 -0
  28. data/fixtures/asset-hash-app/source/javascripts/application.js +8 -0
  29. data/fixtures/asset-hash-app/source/javascripts/sprockets_base.js +5 -0
  30. data/fixtures/asset-hash-app/source/javascripts/sprockets_sub.js +3 -0
  31. data/fixtures/asset-hash-app/source/layout.erb +19 -0
  32. data/fixtures/asset-hash-app/source/other.html.erb +2 -0
  33. data/fixtures/asset-hash-app/source/partials.html.erb +2 -0
  34. data/fixtures/asset-hash-app/source/stylesheets/_partial.sass +2 -0
  35. data/fixtures/asset-hash-app/source/stylesheets/jquery-mobile.css.scss +1 -0
  36. data/fixtures/asset-hash-app/source/stylesheets/site.css.scss +4 -0
  37. data/fixtures/asset-hash-app/source/stylesheets/uses_partials.css.sass +4 -0
  38. data/fixtures/asset-hash-app/source/subdir/index.html.erb +308 -0
  39. data/fixtures/asset-hash-host-app/config.rb +6 -0
  40. data/fixtures/asset-hash-host-app/source/images/100px.gif +0 -0
  41. data/fixtures/asset-hash-host-app/source/images/100px.jpg +0 -0
  42. data/fixtures/asset-hash-host-app/source/images/100px.png +0 -0
  43. data/fixtures/asset-hash-host-app/source/index.html.erb +6 -0
  44. data/fixtures/asset-hash-host-app/source/layout.erb +17 -0
  45. data/fixtures/asset-hash-host-app/source/other.html.erb +2 -0
  46. data/fixtures/asset-hash-host-app/source/stylesheets/site.css.scss +4 -0
  47. data/fixtures/asset-hash-host-app/source/subdir/index.html.erb +6 -0
  48. data/fixtures/asset-paths-app/config.rb +4 -0
  49. data/fixtures/asset-paths-app/derp/javascripts/vendored_js.js +1 -0
  50. data/fixtures/asset-paths-app/source/javascripts/vendored_include.js +1 -0
  51. data/fixtures/bower-app/bower.json +7 -0
  52. data/fixtures/bower-app/bower_components/underscore/bower.json +8 -0
  53. data/fixtures/bower-app/bower_components/underscore/underscore.js +1343 -0
  54. data/fixtures/bower-app/config.rb +3 -0
  55. data/fixtures/bower-app/source/javascripts/application.js +1 -0
  56. data/fixtures/bower-individual-outputdir-app/bower.json +7 -0
  57. data/fixtures/bower-individual-outputdir-app/bower_components/underscore/bower.json +8 -0
  58. data/fixtures/bower-individual-outputdir-app/bower_components/underscore/underscore.js +1343 -0
  59. data/fixtures/bower-individual-outputdir-app/config.rb +6 -0
  60. data/fixtures/bower-individual-outputdir-app/source/javascripts/application.js +1 -0
  61. data/fixtures/bower-individual-outputdir-app/vendor/assets/components/lightbox2/.bower.json +34 -0
  62. data/fixtures/bower-individual-outputdir-app/vendor/assets/components/lightbox2/bower.json +26 -0
  63. data/fixtures/bower-individual-outputdir-app/vendor/assets/components/lightbox2/img/close.png +0 -0
  64. data/fixtures/bower-individual-outputdir-app/vendor/assets/components/lightbox2/js/lightbox.js +2 -0
  65. data/fixtures/bower-individual-outputdir-app/vendor/assets/components/lightbox2/package.json +37 -0
  66. data/fixtures/bower-json-app/config.rb +1 -0
  67. data/fixtures/bower-json-app/source/javascripts/application.js.coffee +1 -0
  68. data/fixtures/bower-json-app/source/javascripts/bower.json +5 -0
  69. data/fixtures/bower-multiple-assets-app/.bowerrc +4 -0
  70. data/fixtures/bower-multiple-assets-app/bower.json +7 -0
  71. data/fixtures/bower-multiple-assets-app/config.rb +7 -0
  72. data/fixtures/bower-multiple-assets-app/vendor/assets/components/lightbox2/.bower.json +34 -0
  73. data/fixtures/bower-multiple-assets-app/vendor/assets/components/lightbox2/bower.json +26 -0
  74. data/fixtures/bower-multiple-assets-app/vendor/assets/components/lightbox2/img/close.png +0 -0
  75. data/fixtures/bower-multiple-assets-app/vendor/assets/components/lightbox2/js/lightbox.js +2 -0
  76. data/fixtures/bower-multiple-assets-app/vendor/assets/components/lightbox2/package.json +37 -0
  77. data/fixtures/glob-app/config.rb +0 -0
  78. data/fixtures/glob-app/source/stylesheets/main.css.scss +1 -0
  79. data/fixtures/glob-app/source/stylesheets/module1/_i-am-mod.scss +3 -0
  80. data/fixtures/glob-app/source/stylesheets/module2/_derp.sass +2 -0
  81. data/fixtures/glob-app/source/stylesheets/shared/3rd-party/bootstrap.sass +2 -0
  82. data/fixtures/glob-app/source/stylesheets/shared/shared.scss +3 -0
  83. data/fixtures/jquery-mobile-app/config.rb +0 -0
  84. data/fixtures/jquery-mobile-app/source/stylesheets/base.css.scss +2 -0
  85. data/fixtures/long-filenames-app/config.rb +0 -0
  86. data/fixtures/long-filenames-app/source/images/00000000-0000-0000-0000-000000.svg +3 -0
  87. data/fixtures/long-filenames-app/source/images/00000000-0000-0000-0000-0000001.svg +3 -0
  88. data/fixtures/preview-app/config.rb +0 -0
  89. data/fixtures/preview-app/source/content.html.erb +1 -0
  90. data/fixtures/preview-app/source/layout.erb +1 -0
  91. data/fixtures/preview-app/source/stylesheets/_partial.sass +2 -0
  92. data/fixtures/preview-app/source/stylesheets/_partial2.css.sass +2 -0
  93. data/fixtures/preview-app/source/stylesheets/main.css.sass +4 -0
  94. data/fixtures/preview-app/source/stylesheets/main2.css.sass +4 -0
  95. data/fixtures/preview-app/source/stylesheets/plain.css.sass +2 -0
  96. data/fixtures/sprockets-app-debug-assets/config.rb +1 -0
  97. data/fixtures/sprockets-app-debug-assets/source/index.html.erb +8 -0
  98. data/fixtures/sprockets-app-debug-assets/source/javascripts/dependency1.js +3 -0
  99. data/fixtures/sprockets-app-debug-assets/source/javascripts/dependency2.js +1 -0
  100. data/fixtures/sprockets-app-debug-assets/source/javascripts/main.js +4 -0
  101. data/fixtures/sprockets-app-debug-assets/source/stylesheets/app.css.scss +4 -0
  102. data/fixtures/sprockets-app-debug-assets/source/stylesheets/dependency1.css +0 -0
  103. data/fixtures/sprockets-app-debug-assets/source/stylesheets/dependency2.css.scss +0 -0
  104. data/fixtures/sprockets-app/config.rb +7 -0
  105. data/fixtures/sprockets-app/source/index.html.erb +6 -0
  106. data/fixtures/sprockets-app/source/library/css/bootstrap_include.css.scss +1 -0
  107. data/fixtures/sprockets-app/source/library/css/plain.css +3 -0
  108. data/fixtures/sprockets-app/source/library/css/sprockets_base1.css.scss +1 -0
  109. data/fixtures/sprockets-app/source/library/css/sprockets_base2.css.scss +1 -0
  110. data/fixtures/sprockets-app/source/library/css/sprockets_sub.css.scss +1 -0
  111. data/fixtures/sprockets-app/source/library/js/jquery_include.js +1 -0
  112. data/fixtures/sprockets-app/source/library/js/plain.js +3 -0
  113. data/fixtures/sprockets-app/source/library/js/sprockets_base.js +5 -0
  114. data/fixtures/sprockets-app/source/library/js/sprockets_sub.js +3 -0
  115. data/fixtures/sprockets-app/source/library/js/vendored_include.js +1 -0
  116. data/fixtures/sprockets-app/vendor/assets/javascripts/coffee.js.coffee +2 -0
  117. data/fixtures/sprockets-app/vendor/assets/javascripts/vendored_js.js +1 -0
  118. data/fixtures/sprockets-app/vendor/assets/stylesheets/vendored.css.scss +4 -0
  119. data/fixtures/sprockets-app2/config.rb +0 -0
  120. data/fixtures/sprockets-app2/data/test.yml +4 -0
  121. data/fixtures/sprockets-app2/data/test2.json +4 -0
  122. data/fixtures/sprockets-app2/source/javascripts/_templates/test.jst.ejs +1 -0
  123. data/fixtures/sprockets-app2/source/javascripts/_templates/test2.jst.eco +1 -0
  124. data/fixtures/sprockets-app2/source/javascripts/asset_path.js.erb +3 -0
  125. data/fixtures/sprockets-app2/source/javascripts/multiple_engines.js.coffee.erb +1 -0
  126. data/fixtures/sprockets-app2/source/javascripts/sprockets_base.js +5 -0
  127. data/fixtures/sprockets-app2/source/javascripts/sprockets_sub.js +3 -0
  128. data/fixtures/sprockets-app2/source/javascripts/templates.js +1 -0
  129. data/fixtures/sprockets-app2/source/stylesheets/sprockets_base1.css.scss +1 -0
  130. data/fixtures/sprockets-app2/source/stylesheets/sprockets_base2.css.scss +1 -0
  131. data/fixtures/sprockets-app2/source/stylesheets/sprockets_sub.css.scss +1 -0
  132. data/fixtures/sprockets-images-app/config.rb +1 -0
  133. data/fixtures/sprockets-images-app/source/index.html.erb +10 -0
  134. data/fixtures/sprockets-images-app/source/library/images/cat.jpg +0 -0
  135. data/fixtures/sprockets-images-app/vendor/assets/images/cat-2.jpg +0 -0
  136. data/lib/middleman-sprockets.rb +6 -0
  137. data/lib/middleman-sprockets/asset.rb +185 -0
  138. data/lib/middleman-sprockets/asset_list.rb +41 -0
  139. data/lib/middleman-sprockets/asset_tag_helpers.rb +54 -0
  140. data/lib/middleman-sprockets/config_only_environment.rb +50 -0
  141. data/lib/middleman-sprockets/environment.rb +228 -0
  142. data/lib/middleman-sprockets/extension.rb +145 -0
  143. data/lib/middleman-sprockets/imported_asset.rb +57 -0
  144. data/lib/middleman-sprockets/pathname_extensions.rb +10 -0
  145. data/lib/middleman-sprockets/sass_function_hack.rb +9 -0
  146. data/lib/middleman-sprockets/version.rb +5 -0
  147. data/middleman-sprockets.gemspec +22 -0
  148. data/spec/asset_list_spec.rb +38 -0
  149. data/spec/asset_spec.rb +116 -0
  150. data/spec/imported_asset_spec.rb +72 -0
  151. data/spec/spec_helper.rb +16 -0
  152. data/spec/support/aruba.rb +18 -0
  153. data/spec/support/rspec.rb +17 -0
  154. metadata +257 -0
@@ -0,0 +1,145 @@
1
+ require "sprockets"
2
+ require "sprockets-sass"
3
+ require "middleman-sprockets/pathname_extensions"
4
+ require "middleman-sprockets/asset"
5
+ require "middleman-sprockets/imported_asset"
6
+ require "middleman-sprockets/asset_list"
7
+ require "middleman-sprockets/config_only_environment"
8
+ require "middleman-sprockets/environment"
9
+ require "middleman-sprockets/asset_tag_helpers"
10
+
11
+ # Sprockets extension
12
+ module Middleman
13
+ class SprocketsExtension < Extension
14
+ option :debug_assets, false, 'Split up each required asset into its own script/style tag instead of combining them (development only)'
15
+
16
+ attr_reader :environment
17
+
18
+ # This module gets mixed into both the Middleman instance and the Middleman class,
19
+ # so that it's available in config.rb
20
+ module SprocketsAccessor
21
+ # The sprockets environment
22
+ # @return [Middleman::MiddlemanSprocketsEnvironment]
23
+ def sprockets
24
+ extensions[:sprockets].environment
25
+ end
26
+ end
27
+
28
+ def initialize(app, options_hash={}, &block)
29
+ require "middleman-sprockets/sass_function_hack"
30
+
31
+ super
32
+
33
+ # Start out with a stub environment that can only be configured (paths and such)
34
+ @environment = ::Middleman::Sprockets::ConfigOnlyEnvironment.new
35
+
36
+ app.send :include, SprocketsAccessor
37
+ end
38
+
39
+ helpers do
40
+ include SprocketsAccessor
41
+ include ::Middleman::Sprockets::AssetTagHelpers
42
+ end
43
+
44
+ def after_configuration
45
+ ::Tilt.register ::Sprockets::EjsTemplate, 'ejs'
46
+ ::Tilt.register ::Sprockets::EcoTemplate, 'eco'
47
+ ::Tilt.register ::Sprockets::JstProcessor, 'jst'
48
+
49
+ app.template_extensions :jst => :js, :eco => :js, :ejs => :js
50
+
51
+ if app.config.defines_setting?(:debug_assets) && !options.setting(:debug_assets).value_set?
52
+ options[:debug_assets] = app.config[:debug_assets]
53
+ end
54
+
55
+ config_environment = @environment
56
+ debug_assets = !app.build? && options[:debug_assets]
57
+ @environment = ::Middleman::Sprockets::Environment.new(app, :debug_assets => debug_assets)
58
+ config_environment.apply_to_environment(@environment)
59
+
60
+ append_paths_from_gems
61
+
62
+ # Setup Sprockets Sass options
63
+ if app.config.defines_setting?(:sass)
64
+ app.config[:sass].each { |k, v| ::Sprockets::Sass.options[k] = v }
65
+ end
66
+
67
+ # Intercept requests to /javascripts and /stylesheets and pass to sprockets
68
+ our_sprockets = self.environment
69
+
70
+ [app.config[:js_dir], app.config[:css_dir], app.config[:images_dir], app.config[:fonts_dir]].each do |dir|
71
+ app.map("/#{dir}") { run our_sprockets }
72
+ end
73
+ end
74
+
75
+ # Add sitemap resource for every image in the sprockets load path
76
+ def manipulate_resource_list(resources)
77
+ imported_assets = Middleman::Sprockets::AssetList.new
78
+
79
+ environment.imported_assets.each do |asset|
80
+ asset.resolve_path_with environment
81
+ @app.logger.debug "== Importing Sprockets asset #{asset.real_path}"
82
+
83
+ imported_assets << asset
84
+ end
85
+
86
+ resources_list = []
87
+ environment.paths.each do |load_path|
88
+ environment.each_entry(load_path) do |path|
89
+ asset = Middleman::Sprockets::Asset.new(path, source_directory: load_path)
90
+
91
+ imported_assets.lookup(asset) do |candidate, found_asset|
92
+ candidate.destination_path = found_asset.output_path if found_asset.output_path
93
+ candidate.import_it
94
+ end
95
+
96
+ next unless asset.import?
97
+
98
+ if asset.has_type? :image
99
+ asset.destination_directory = @app.config[:images_dir]
100
+ elsif asset.has_type? :script
101
+ asset.destination_directory = @app.config[:js_dir]
102
+ elsif asset.has_type? :font
103
+ asset.destination_directory = @app.config[:fonts_dir]
104
+ elsif asset.has_type? :stylesheet
105
+ asset.destination_directory = @app.config[:css_dir]
106
+ end
107
+
108
+ new_path = @app.sitemap.extensionless_path(asset.destination_path.to_s)
109
+
110
+ next if @app.sitemap.find_resource_by_destination_path(new_path.to_s)
111
+ resources_list << ::Middleman::Sitemap::Resource.new(@app.sitemap, new_path.to_s, path.to_s)
112
+ end
113
+ end
114
+ resources + resources_list
115
+ end
116
+
117
+ private
118
+
119
+ # Add any directories from gems with Rails-like paths to sprockets load path
120
+ def append_paths_from_gems
121
+ try_paths = [
122
+ %w{ assets },
123
+ %w{ app },
124
+ %w{ app assets },
125
+ %w{ vendor },
126
+ %w{ vendor assets },
127
+ %w{ lib },
128
+ %w{ lib assets }
129
+ ].inject([]) do |sum, v|
130
+ sum + [
131
+ File.join(v, 'javascripts'),
132
+ File.join(v, 'stylesheets'),
133
+ File.join(v, 'images'),
134
+ File.join(v, 'fonts')
135
+ ]
136
+ end
137
+
138
+ ([app.root] + ::Middleman.rubygems_latest_specs.map(&:full_gem_path)).each do |root_path|
139
+ try_paths.map {|p| File.join(root_path, p) }.
140
+ select {|p| File.directory?(p) }.
141
+ each {|path| self.environment.append_path(path) }
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+ module Middleman
3
+ module Sprockets
4
+ # ImportedAsset
5
+ class ImportedAsset
6
+ attr_reader :logical_path, :output_path, :real_path
7
+
8
+ # Create instance
9
+ #
10
+ # @param [Pathname] logical_path
11
+ # The logical path to the asset given in config.rb
12
+ #
13
+ # @param [proc] output_dir
14
+ # An individual output directory for that particular asset
15
+ def initialize(logical_path, determine_output_path = proc { nil })
16
+ @logical_path = Pathname.new(logical_path)
17
+ @output_path = if output_path = determine_output_path.call(@logical_path)
18
+ Pathname.new(output_path)
19
+ else
20
+ nil
21
+ end
22
+ end
23
+
24
+ # Resolve logical path to real path
25
+ #
26
+ # @param [#resolve] resolver
27
+ # The objects which is able to resolve a logical path
28
+ def resolve_path_with(resolver)
29
+ @real_path = resolver.resolve(logical_path)
30
+
31
+ raise ::Sprockets::FileNotFound, "Couldn't find asset '#{logical_path}'" if real_path == nil || real_path == ''
32
+ end
33
+
34
+ # String representation of asset
35
+ #
36
+ # @return [String]
37
+ # The logical path as string
38
+ def to_s
39
+ logical_path.to_s
40
+ end
41
+
42
+ # Does the given patch matches asset
43
+ #
44
+ # @param [Pathname] path
45
+ # The path to be checked
46
+ def match?(path)
47
+ has_real_path? path
48
+ end
49
+
50
+ private
51
+
52
+ def has_real_path?(path)
53
+ real_path == path
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ class Pathname
3
+ def start_with?(*paths)
4
+ to_s.start_with?(*paths)
5
+ end
6
+
7
+ def end_with?(*paths)
8
+ to_s.end_with?(*paths)
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ if defined?(Sass)
2
+ module Sass::Script::Functions
3
+ # Override generated_image_url to use Sprockets
4
+ # a la https://github.com/Compass/compass-rails/blob/master/lib/compass-rails/patches/4_0.rb
5
+ def generated_image_url(path, only_path = nil)
6
+ asset_url(path, Sass::Script::String.new("image"))
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Middleman
2
+ module Sprockets
3
+ VERSION = "3.3.6"
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "middleman-sprockets/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "middleman-sprockets-cj"
7
+ s.version = Middleman::Sprockets::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Thomas Reynolds', 'Ben Hollis', 'Karl Freeman']
10
+ s.email = ['me@tdreyno.com', 'ben@benhollis.net', 'karlfreeman@gmail.com']
11
+ s.homepage = "https://github.com/middleman/middleman-sprockets"
12
+ s.summary = %q{Sprockets support for Middleman}
13
+ s.description = %q{Sprockets support for Middleman}
14
+ s.license = "MIT"
15
+ s.files = `git ls-files -z`.split("\0")
16
+ s.test_files = `git ls-files -z -- {fixtures,features}/*`.split("\0")
17
+ s.require_paths = ["lib"]
18
+ s.add_dependency("middleman-core-cj", ["~> 3.3"])
19
+ s.add_dependency("sprockets", ["~> 2.12.1"])
20
+ s.add_dependency("sprockets-sass", ["~> 1.2.0"])
21
+ s.add_dependency("sprockets-helpers", ["~> 1.1.0"])
22
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ RSpec.describe AssetList do
4
+ context '#add' do
5
+ it 'adds an asset to the list' do
6
+ asset = instance_double 'Middleman::Sprockets::Asset'
7
+ list = AssetList.new
8
+
9
+ expect {
10
+ list << asset
11
+ }.not_to raise_error
12
+ end
13
+ end
14
+ context '#lookup' do
15
+ it 'finds an asset in list' do
16
+ asset = instance_double 'Middleman::Sprockets::Asset'
17
+ expect(asset).to receive(:source_path).and_return 'path/to/source'
18
+ expect(asset).to receive(:match?).and_return true
19
+
20
+ list = AssetList.new
21
+ list << asset
22
+
23
+ expect(list.lookup(asset)).to be asset
24
+ end
25
+
26
+ it 'supports a block which gets the found asset passed' do
27
+ asset = instance_double 'Middleman::Sprockets::Asset'
28
+ allow(asset).to receive(:source_path).and_return 'path/to/source'
29
+ expect(asset).to receive(:destination_path=).with 'path/to/source'
30
+ expect(asset).to receive(:match?).and_return true
31
+
32
+ list = AssetList.new
33
+ list << asset
34
+
35
+ list.lookup(asset) { |candidate, found_asset| found_asset.destination_path = found_asset.source_path }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,116 @@
1
+ # encoding: utf-8
2
+ RSpec.describe Asset do
3
+ context '#has_type?' do
4
+ it 'finds type by extension' do
5
+ asset = Asset.new('/source/path/to/image.png', source_directory: '/source/path/to')
6
+ expect(asset).to have_type :image
7
+ end
8
+
9
+ it 'finds type by path' do
10
+ asset = Asset.new('/source/path/to/images/image.xz', source_directory: '/source/path/to/images')
11
+ expect(asset).to have_type :image
12
+ end
13
+ end
14
+
15
+ context '#import?, #import_it' do
16
+ it 'fails if file does not exist' do
17
+ in_current_dir do
18
+ base_path = File.expand_path('source/path/to')
19
+ file_path = File.expand_path('source/path/to/image.xz')
20
+
21
+ asset = Asset.new(file_path, source_directory: base_path)
22
+
23
+ expect(asset).not_to be_import
24
+ end
25
+ end
26
+
27
+ it 'succeeds if import it is set' do
28
+ in_current_dir do
29
+ base_path = File.expand_path('source/path/to')
30
+ file_path = File.expand_path('source/path/to/image.xz')
31
+ write_file file_path, 'asdf'
32
+
33
+ asset = Asset.new(file_path, source_directory: base_path)
34
+ asset.import_it
35
+
36
+ expect(asset).to be_import
37
+ end
38
+ end
39
+
40
+ it 'succeeds if is in trusted directory images' do
41
+ in_current_dir do
42
+ base_path = File.expand_path('source/path/to/images')
43
+ file_path = File.expand_path('source/path/to/images/image.xz')
44
+ write_file file_path, 'asdf'
45
+
46
+ asset = Asset.new(file_path, source_directory: base_path)
47
+
48
+ expect(asset).to be_import
49
+ end
50
+ end
51
+ end
52
+
53
+ context '#destination_path, #destination_path=, #destination_directory' do
54
+ it 'returns @destination_path if set' do
55
+ in_current_dir do
56
+ base_path = File.expand_path('source/path/to/images')
57
+ file_path = File.expand_path('source/path/to/images/image.xz')
58
+
59
+ asset = Asset.new(file_path, source_directory: base_path)
60
+ asset.destination_path = 'asdf/image.xz'
61
+
62
+ expect(asset.destination_path).to eq Pathname.new('asdf/image.xz')
63
+ end
64
+ end
65
+
66
+ it 'builds path based on destination_directory and relative file path' do
67
+ in_current_dir do
68
+ base_path = File.expand_path('source/path/to/images')
69
+ file_path = File.expand_path('source/path/to/images/image.xz')
70
+
71
+ asset = Asset.new(file_path, source_directory: base_path)
72
+ asset.destination_directory = '/images'
73
+
74
+ expect(asset.destination_path.to_s).to eq '/images/image.xz'
75
+ end
76
+ end
77
+
78
+ it 'fails if destination_directory and @destination_path are not set' do
79
+ in_current_dir do
80
+ base_path = File.expand_path('source/path/to/images')
81
+ file_path = File.expand_path('source/path/to/images/image.xz')
82
+
83
+ asset = Asset.new(file_path, source_directory: base_path)
84
+
85
+ expect {
86
+ asset.destination_path
87
+ }.to raise_error ::Sprockets::FileNotFound
88
+ end
89
+ end
90
+ end
91
+
92
+ context '#match?' do
93
+ it 'success if source path is equal' do
94
+ in_current_dir do
95
+ base_path = File.expand_path('source/path/to')
96
+ file_path = File.expand_path('source/path/to/images/image.xz')
97
+
98
+ asset1 = Asset.new(file_path, source_directory: base_path)
99
+
100
+ expect(asset1).to be_match file_path
101
+ end
102
+ end
103
+
104
+ it 'fails if source path is not equal' do
105
+ in_current_dir do
106
+ base_path = File.expand_path('source/path/to')
107
+ file_path = File.expand_path('source/path/to/images/image.xz')
108
+
109
+ asset1 = Asset.new(file_path, source_directory: base_path)
110
+
111
+ expect(asset1).not_to be_match 'asdf'
112
+ end
113
+ end
114
+ end
115
+
116
+ end
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+ RSpec.describe ImportedAsset do
3
+ context '#output_path' do
4
+ it 'uses block as second argument on initialize to get path' do
5
+ asset = ImportedAsset.new 'source/to/asset/image.png', proc { 'hello/world.png' }
6
+
7
+ expect(asset.output_path.to_s).to eq 'hello/world.png'
8
+ end
9
+ end
10
+
11
+ context '#resolve_path_with' do
12
+ it 'resolves path' do
13
+ in_current_dir do
14
+ relative_path = 'source/path/to/image.xz'
15
+ file_path = File.expand_path(relative_path)
16
+
17
+ resolver = double('Environment')
18
+ expect(resolver).to receive(:resolve).with(Pathname.new(relative_path)).and_return file_path
19
+
20
+ asset = ImportedAsset.new relative_path
21
+ asset.resolve_path_with resolver
22
+ end
23
+ end
24
+
25
+ it 'raises an error if path could not be resolved' do
26
+ in_current_dir do
27
+ relative_path = 'source/path/to/image.xz'
28
+
29
+ resolver = double('Environment')
30
+ allow(resolver).to receive(:resolve).with(Pathname.new(relative_path)).and_return nil
31
+
32
+ asset = ImportedAsset.new relative_path
33
+
34
+ expect {
35
+ asset.resolve_path_with resolver
36
+ }.to raise_error ::Sprockets::FileNotFound
37
+ end
38
+ end
39
+ end
40
+
41
+ context '#match?' do
42
+ it 'succeeds if real path matches' do
43
+ in_current_dir do
44
+ relative_path = 'source/path/to/image.xz'
45
+ file_path = File.expand_path(relative_path)
46
+
47
+ resolver = double('Environment')
48
+ allow(resolver).to receive(:resolve).and_return file_path
49
+
50
+ asset = ImportedAsset.new relative_path
51
+ asset.resolve_path_with resolver
52
+
53
+ expect(asset).to be_match file_path
54
+ end
55
+ end
56
+
57
+ it 'fails if does not match' do
58
+ in_current_dir do
59
+ relative_path = 'source/path/to/image.xz'
60
+ file_path = File.expand_path(relative_path)
61
+
62
+ resolver = double('Environment')
63
+ allow(resolver).to receive(:resolve).and_return file_path + 'fail'
64
+
65
+ asset = ImportedAsset.new relative_path
66
+ asset.resolve_path_with resolver
67
+
68
+ expect(asset).not_to be_match file_path
69
+ end
70
+ end
71
+ end
72
+ end