Permalink
Browse files

Fixed AssetLocation to only consider / separated tokens when determin…

…ing prefix matches. This avoids locations like 'foo_a' from matching virtual paths ending in 'foo'
  • Loading branch information...
Grockit
Grockit committed Sep 8, 2009
1 parent efbf242 commit 784d7162106caaaa0b10481bf31cac705ccc0a81
View
@@ -1,4 +1,4 @@
- ScrewUnit.configure do
+ScrewUnit.configure do
port 8181
add_js_location "/under_test", "client/lib"
add_js_location "/specs", "client/spec"
View
@@ -6,6 +6,8 @@
require "#{dir}/screw_unit/resources"
require "#{dir}/screw_unit/configuration"
require "#{dir}/screw_unit/asset_manager"
+require "#{dir}/screw_unit/string_extension"
+require "#{dir}/screw_unit/array_extension"
require "#{dir}/screw_unit/asset_location"
require "#{dir}/screw_unit/js_file"
@@ -0,0 +1,10 @@
+class Array
+ def starts_with?(prefix)
+ return true if prefix.empty?
+ return false if prefix.size > size
+ prefix.each_with_index do |element, index|
+ return false unless self[index] == element
+ end
+ true
+ end
+end
@@ -3,6 +3,9 @@ class AssetLocation
attr_reader :virtual_prefix, :physical_prefix
def initialize(virtual_prefix, physical_prefix)
+
+ raise "No directory exists at #{physical_prefix}" unless File.exist?(physical_prefix) && File.directory?(physical_prefix)
+
@virtual_prefix, @physical_prefix = virtual_prefix, physical_prefix
end
@@ -21,11 +24,11 @@ def physicalize_path(virtual_path)
end
def matches_physical_path?(physical_path)
- physical_path.starts_with?(physical_prefix)
+ physical_path.path_starts_with?(physical_prefix)
end
def matches_virtual_path?(virtual_path)
- virtual_path.starts_with?(virtual_prefix)
+ virtual_path.path_starts_with?(virtual_prefix)
end
def matches_virtual_glob_pattern?(virtual_glob_pattern)
@@ -34,11 +37,11 @@ def matches_virtual_glob_pattern?(virtual_glob_pattern)
def is_subsumed_by_virtual_glob_pattern?(pattern)
glob_pattern_without_wildcards = pattern.gsub(/\/\*.*/, "")
- virtual_prefix.starts_with?(glob_pattern_without_wildcards)
+ virtual_prefix.path_starts_with?(glob_pattern_without_wildcards)
end
def subsumes_virtual_glob_pattern?(pattern)
- pattern.starts_with?(virtual_prefix)
+ pattern.path_starts_with?(virtual_prefix)
end
def virtual_glob(virtual_glob_pattern)
@@ -1,9 +1,3 @@
-class String
- def starts_with?(prefix)
- index(prefix) == 0
- end
-end
-
module ScrewUnit
class AssetManager
attr_reader :locations, :js_locations
@@ -6,6 +6,8 @@ class JsFile
attr_reader :physical_path, :containing_dir, :asset_manager
def initialize(physical_path, asset_manager)
+ raise "JsFile created with nil physical_path" if physical_path.nil?
+
@physical_path, @asset_manager = physical_path, asset_manager
@containing_dir = File.dirname(physical_path)
end
@@ -49,7 +51,9 @@ def initialize(parent_dir, relative_path, asset_manager)
class GlobalRequireDeclaration
attr_reader :js_file
def initialize(global_path, asset_manager)
- @js_file = JsFile.new(asset_manager.physicalize_path_from_js_load_path(global_path + ".js"), asset_manager)
+ physical_path = asset_manager.physicalize_path_from_js_load_path(global_path + ".js")
+ raise "No file '#{global_path}.js' found on load path" unless physical_path
+ @js_file = JsFile.new(physical_path, asset_manager)
end
end
end
@@ -0,0 +1,9 @@
+class String
+ def starts_with?(prefix)
+ index(prefix) == 0
+ end
+
+ def path_starts_with?(prefix)
+ split('/').starts_with?(prefix.split('/'))
+ end
+end
@@ -0,0 +1,15 @@
+require "#{File.dirname(__FILE__)}/../screw_unit_spec_helper"
+
+describe Array do
+ describe "#starts_with?" do
+ it "returns true only for arrays that share a common prefix with the receiver" do
+ a = ["foo", "bar", "baz"]
+ a.starts_with?(["foo"]).should be_true
+ a.starts_with?(["foo", "bar"]).should be_true
+ a.starts_with?(["foo", "bar", "baz"]).should be_true
+ a.starts_with?(["foo", "bar", "baz", "quux"]).should_not be_true
+ a.starts_with?(["quux"]).should_not be_true
+ a.starts_with?([]).should be_true
+ end
+ end
+end
@@ -0,0 +1,31 @@
+require "#{File.dirname(__FILE__)}/../screw_unit_spec_helper"
+
+module ScrewUnit
+ describe AssetLocation do
+ attr_reader :dir
+
+ before do
+ @dir = File.dirname(__FILE__)
+ end
+
+ describe "#subsumes_virtual_glob_pattern?" do
+ it "returns true only if the glob pattern is within the #virtual_prefix" do
+ location = AssetLocation.new("/foo/bar", "#{dir}/file_system_fixtures_for_asset_manager_specs/dir_1")
+ location.subsumes_virtual_glob_pattern?("/foo/bar/**/*.js").should be_true
+ location.subsumes_virtual_glob_pattern?("/foo/bar/quux/**/*.js").should be_true
+ location.subsumes_virtual_glob_pattern?("/quux/bar/**/*.js").should be_false
+ location.subsumes_virtual_glob_pattern?("/foo/bar_x/**/*.js").should be_false
+ end
+ end
+
+ describe "#is_subsumed_by_virtual_glob_pattern?" do
+ it "returns true only if the glob pattern contains the #virtual_prefix" do
+ location = AssetLocation.new("/foo/bar", "#{dir}/file_system_fixtures_for_asset_manager_specs/dir_1")
+ location.is_subsumed_by_virtual_glob_pattern?("/foo/**/*.js").should be_true
+ location.is_subsumed_by_virtual_glob_pattern?("/foo/bar/**/*.js").should be_true
+ location.is_subsumed_by_virtual_glob_pattern?("/foo/ba/**/*.js").should be_false
+ location.is_subsumed_by_virtual_glob_pattern?("/foo/bar/quux/**/*.js").should be_false
+ end
+ end
+ end
+end
@@ -25,6 +25,11 @@ module ScrewUnit
manager.add_location('/', "#{dir}/file_system_fixtures_for_asset_manager_specs/dir_4")
manager.virtualize_path("#{dir}/file_system_fixtures_for_asset_manager_specs/dir_4/1.css").should == "/1.css"
end
+
+ it "is not confused by multiple physical paths with the same partial prefix" do
+ manager.add_location("/specs/b", "#{dir}/file_system_fixtures_for_asset_manager_specs/dir")
+ manager.virtualize_path("#{dir}/file_system_fixtures_for_asset_manager_specs/dir_1/1.js").should == "/specs/1/1.js"
+ end
end
describe "#physicalize_path" do
@@ -38,6 +43,12 @@ module ScrewUnit
manager.add_location('/', "#{dir}/file_system_fixtures_for_asset_manager_specs/dir_4")
manager.physicalize_path('/1.css').should == "#{dir}/file_system_fixtures_for_asset_manager_specs/dir_4/1.css"
end
+
+ it "is not confused by multiple virtual paths with the same partial prefix" do
+ manager.add_location("/foo_prime", "#{dir}/file_system_fixtures_for_asset_manager_specs/dir_1")
+ manager.add_location("/foo", "#{dir}/file_system_fixtures_for_asset_manager_specs/dir_2")
+ manager.physicalize_path("/foo_prime/1.js").should == "#{dir}/file_system_fixtures_for_asset_manager_specs/dir_1/1.js"
+ end
end
describe "#glob_virtual_paths" do

0 comments on commit 784d716

Please sign in to comment.