Skip to content

Commit

Permalink
Merge pull request #118 from rails/schneems/fix-windows-4.x
Browse files Browse the repository at this point in the history
[close #115] Support Windows filesystem
  • Loading branch information
schneems committed Aug 26, 2015
2 parents 5def4d7 + 573754b commit fce29dc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/sprockets/uri_tar.rb
Expand Up @@ -35,6 +35,15 @@ def compress
scheme + compressed_path
end

# Internal: Tells us if we are using an absolute path
#
# Nix* systems start with a `/` like /Users/schneems.
# Windows systems start with a drive letter than colon and slash
# like C:/Schneems.
def absolute_path?
path.start_with?("/".freeze) || path =~ /\A[a-zA-Z]:\// # windows
end

# Internal: Convert a "compressed" uri to an absolute path
#
# If a uri is inside of the environment's root it will not
Expand All @@ -48,7 +57,7 @@ def compress
#
# Returns String
def expand
if path.start_with?("/".freeze)
if absolute_path?
# Stored path was absolute, don't add root
scheme + path
else
Expand All @@ -67,7 +76,14 @@ def expand
#
# Returns String
def compressed_path
if compressed_path = @env.split_subpath(root, path)
# windows
if !@root.start_with?("/".freeze) && path.start_with?("/".freeze)
consistent_root = "/".freeze + @root
else
consistent_root = @root
end

if compressed_path = @env.split_subpath(consistent_root, path)
compressed_path
else
path
Expand Down
34 changes: 34 additions & 0 deletions test/test_uri_tar.rb
@@ -0,0 +1,34 @@
require 'sprockets_test'

class TestURITar < Sprockets::TestCase
test "works with windows" do
fake_env = Class.new do
include Sprockets::PathUtils
attr_accessor :root
end.new

uri = "C:/Sites/sprockets/test/fixtures/paths/application.css?type=text/css"
fake_env.root = "C:/Different/path"
tar = Sprockets::URITar.new(uri, fake_env)
assert_equal uri, tar.expand
assert_equal uri, tar.compress
assert_equal uri, tar.compressed_path

uri = "file:///C:/Sites/sprockets/test/fixtures/paths/application.css?type=text/css"
fake_env.root = "C:/Sites/sprockets"
tar = Sprockets::URITar.new(uri, fake_env)
assert_equal uri, tar.expand
assert_equal Sprockets::URITar.new(tar.expand, fake_env).expand, tar.expand
assert_equal "test/fixtures/paths/application.css?type=text/css", tar.compressed_path
assert_equal "file://test/fixtures/paths/application.css?type=text/css", tar.compress
assert_equal Sprockets::URITar.new(tar.compress, fake_env).compress, tar.compress
assert_equal Sprockets::URITar.new(tar.expand, fake_env).compress, tar.compress

uri = "C:/Sites/sprockets/test/fixtures/paths/application.css?type=text/css"
fake_env.root = "C:/Sites/sprockets"
tar = Sprockets::URITar.new(uri, fake_env)
assert_equal uri, tar.expand
assert_equal "test/fixtures/paths/application.css?type=text/css", tar.compressed_path
assert_equal "test/fixtures/paths/application.css?type=text/css", tar.compress
end
end

0 comments on commit fce29dc

Please sign in to comment.