From f16fc2229e8ee8162269ed00ac8296643a32136e Mon Sep 17 00:00:00 2001 From: Steven Sloan Date: Fri, 13 Nov 2015 16:33:59 -0500 Subject: [PATCH 1/3] feat(testing): add a fixture/file helper for rspec tests --- middleman-core/spec/spec_helper.rb | 2 ++ middleman-core/spec/support/given.rb | 42 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 middleman-core/spec/support/given.rb diff --git a/middleman-core/spec/spec_helper.rb b/middleman-core/spec/spec_helper.rb index 4e363fc6..c7b3d141 100644 --- a/middleman-core/spec/spec_helper.rb +++ b/middleman-core/spec/spec_helper.rb @@ -12,6 +12,8 @@ RSpec.configure do |config| config.include Aruba::Api end +require_relative 'support/given' + # encoding: utf-8 RSpec.configure do |config| config.filter_run :focus diff --git a/middleman-core/spec/support/given.rb b/middleman-core/spec/support/given.rb new file mode 100644 index 00000000..499fcb78 --- /dev/null +++ b/middleman-core/spec/support/given.rb @@ -0,0 +1,42 @@ +module Given + ROOT = File.expand_path( '../..', File.dirname( File.realpath(__FILE__) ) ) + TMP = File.join( ROOT, 'tmp' ) + + class << self + + def fixture name + cleanup! + + `rsync -av #{File.join( ROOT, 'fixtures', name )}/ #{TMP}/` + Dir.chdir TMP + ENV['MM_ROOT'] = TMP + end + + def no_file name + FileUtils.rm name, force: true + end + + def symlink source, destination + no_file destination + FileUtils.symlink File.expand_path(source), + File.expand_path(destination), + force: true + end + + def file name, content + file_path = File.join( TMP, name ) + FileUtils.mkdir_p( File.dirname(file_path) ) + File.open( file_path, 'w' ) do |file| + file.write content + end + end + + def cleanup! + Dir.chdir ROOT + if File.exist? TMP + `rm -rf #{TMP}` + end + end + + end +end From 49a7435dc9d0fd13cd30ca686c5b589585d283af Mon Sep 17 00:00:00 2001 From: Steven Sloan Date: Fri, 13 Nov 2015 16:37:58 -0500 Subject: [PATCH 2/3] spec(Util::asset_url): add specs covering each conditional branch --- .../spec/middleman-core/util_spec.rb | 74 ++++++++++++++++++- 1 file changed, 71 insertions(+), 3 deletions(-) diff --git a/middleman-core/spec/middleman-core/util_spec.rb b/middleman-core/spec/middleman-core/util_spec.rb index 447d1abd..f50d7555 100644 --- a/middleman-core/spec/middleman-core/util_spec.rb +++ b/middleman-core/spec/middleman-core/util_spec.rb @@ -1,5 +1,5 @@ require 'spec_helper' -require 'middleman-core/util' +require 'middleman-core' describe Middleman::Util do @@ -57,7 +57,7 @@ describe Middleman::Util do it "returns Hashie extended Hash if given a hash" do input = {test: "subject"} subject = Middleman::Util.recursively_enhance input - + expect( subject.test ).to eq "subject" end @@ -73,4 +73,72 @@ describe Middleman::Util do end end -end \ No newline at end of file + describe "::asset_url" do + + after(:each) do + Given.cleanup! + end + + context "when http_prefix is activated" do + + before(:each) do + Given.fixture 'clean-dir-app' + Given.file 'source/images/blank.gif', '' + @mm = Middleman::Application.new do + config[:http_prefix] = 'http_prefix' + end + end + + it "returns path with http_prefix pre-pended if resource is found" do + expect( Middleman::Util.asset_url( @mm, 'blank.gif', 'images', http_prefix: 'http_prefix' ) ).to eq 'http_prefix/images/blank.gif' + end + + it "returns path with http_prefix pre-pended if resource is not found" do + expect( Middleman::Util.asset_url( @mm, 'missing.gif', 'images', http_prefix: 'http_prefix' ) ).to eq 'http_prefix/images/missing.gif' + end + end + + context "when relative is true" do + + before(:each) do + Given.fixture 'relative-assets-app' + @mm = Middleman::Application.new + end + + it "returns path relative to the provided current_resource" do + current_resource = instance_double("Middleman::Sitemap::Resource", destination_path: 'a-path/index.html') + expect( Middleman::Util.asset_url( @mm, 'blank.gif', 'images', current_resource: current_resource, + relative: true ) ).to eq '../images/blank.gif' + end + + it "raises error if not given a current_resource" do + expect{ + Middleman::Util.asset_url( @mm, 'blank.gif', 'images', relative: true ) + }.to raise_error ArgumentError + end + end + + it "returns path if it is already a full path" do + expect( Middleman::Util.asset_url( @mm, 'http://example.com' ) ).to eq 'http://example.com' + expect( Middleman::Util.asset_url( @mm, 'data:example' ) ).to eq 'data:example' + end + + it "returns a resource url if given a resource's destination path" do + Given.fixture 'clean-dir-app' # includes directory indexes extension + Given.file 'source/how/about/that.html', '' + @mm = Middleman::Application.new + + expect( Middleman::Util.asset_url( @mm, '/how/about/that/index.html' ) ).to eq '/how/about/that/' + end + + it "returns a resource url if given a resources path" do + Given.fixture 'clean-dir-app' # includes directory indexes extension + Given.file 'source/how/about/that.html', '' + @mm = Middleman::Application.new + + expect( Middleman::Util.asset_url( @mm, '/how/about/that.html' ) ).to eq '/how/about/that/' + end + + end + +end From 6ab6669456cf5ea67c68a71349903bf562db44ab Mon Sep 17 00:00:00 2001 From: Steven Sloan Date: Fri, 13 Nov 2015 16:42:08 -0500 Subject: [PATCH 3/3] fix(Util::asset_url): allow use without passing a current_resource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Most cases the current_resource isn’t actually needed, so calls could be made from extensions and still get the correct path back. An exception will be raised with a descriptive message if the current_resource is needed but not given. (for example, if relative is true — it needs a resource to base the url on) --- middleman-core/lib/middleman-core/util.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index 8e9c0c41..139bca08 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -201,7 +201,7 @@ module Middleman Contract IsA['Middleman::Application'], String, String, Hash => String def asset_url(app, path, prefix='', options={}) # Don't touch assets which already have a full path - if path.include?('//') || path.start_with?('data:') || !options[:current_resource] + if path.include?('//') || path.start_with?('data:') path else # rewrite paths to use their destination path result = if resource = app.sitemap.find_resource_by_destination_path(url_for(app, path)) @@ -218,6 +218,10 @@ module Middleman if options[:relative] != true result else + unless options[:current_resource] + raise ArgumentError, '#asset_url must be run in a context with current_resource if relative: true' + end + current_dir = Pathname('/' + options[:current_resource].destination_path) Pathname(result).relative_path_from(current_dir.dirname).to_s end