Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Provide environment file enumerators
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Aug 21, 2011
1 parent 4de91c1 commit d13401a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 76 deletions.
21 changes: 21 additions & 0 deletions lib/sprockets/base.rb
Expand Up @@ -111,6 +111,27 @@ def [](*args)
find_asset(*args)
end

def each_file
return to_enum(__method__) unless block_given?
paths.each do |base_path|
Dir["#{base_path}/**/*"].each do |filename|
yield filename
end
end
nil
end

def each_logical_path
return to_enum(__method__) unless block_given?
files = {}
each_file do |filename|
logical_path = attributes_for(filename).logical_path
yield logical_path unless files[logical_path]
files[logical_path] = true
end
nil
end

# Pretty inspect
def inspect
"#<#{self.class}:0x#{object_id.to_s(16)} " +
Expand Down
14 changes: 1 addition & 13 deletions lib/sprockets/static_compilation.rb
Expand Up @@ -47,7 +47,7 @@ def precompile(*paths)

manifest = {}
paths.each do |path|
files.each do |logical_path|
each_logical_path do |logical_path|
if path.is_a?(Regexp)
# Match path against `Regexp`
next unless path.match(logical_path)
Expand All @@ -74,17 +74,5 @@ def precompile(*paths)
end
manifest
end

private
# Get all reachable files in environment path
def files
files = Set.new
paths.each do |base_path|
Dir["#{base_path}/**/*"].each do |filename|
files << attributes_for(filename).logical_path
end
end
files
end
end
end
86 changes: 23 additions & 63 deletions test/test_environment.rb
Expand Up @@ -166,77 +166,37 @@ def self.test(name, &block)
@env[fixture_path("default/mobile/a.js")].logical_path
end

test "precompile" do
digest = @env["gallery.js"].digest
filename = fixture_path("public/gallery-#{digest}.js")
filename_gz = "#{filename}.gz"

sandbox filename, filename_gz do
assert !File.exist?(filename)
manifest = @env.precompile("gallery.js", :to => fixture_path("public"))
assert File.exist?(filename)
assert File.exist?(filename_gz)
assert_equal "gallery-#{digest}.js", manifest["gallery.js"]
test "iterate over each file" do
files = []
@env.each_file do |filename|
files << filename
end
assert_equal 23, files.length
end

test "precompile glob" do
dirname = fixture_path("public/mobile")

a_digest = @env["mobile/a.js"].digest
b_digest = @env["mobile/b.js"].digest
c_digest = @env["mobile/c.css"].digest

sandbox dirname do
assert !File.exist?(dirname)
manifest = @env.precompile("mobile/*", :to => fixture_path("public"))

assert File.exist?(dirname)
[nil, '.gz'].each do |gzipped|
assert File.exist?(File.join(dirname, "a-#{a_digest}.js#{gzipped}"))
assert File.exist?(File.join(dirname, "b-#{b_digest}.js#{gzipped}"))
assert File.exist?(File.join(dirname, "c-#{c_digest}.css#{gzipped}"))
end
assert_equal "mobile/a-#{a_digest}.js", manifest["mobile/a.js"]
assert_equal "mobile/b-#{b_digest}.js", manifest["mobile/b.js"]
assert_equal "mobile/c-#{c_digest}.css", manifest["mobile/c.css"]
end
test "each file enumerator" do
files = []
enum = @env.each_file
assert_equal 23, enum.to_a.length
end

test "precompile regexp" do
dirname = fixture_path("public/mobile")

a_digest = @env["mobile/a.js"].digest
b_digest = @env["mobile/b.js"].digest
c_digest = @env["mobile/c.css"].digest

sandbox dirname do
assert !File.exist?(dirname)
manifest = @env.precompile(/mobile\/.*/, :to => fixture_path("public"))

assert File.exist?(dirname)
[nil, '.gz'].each do |gzipped|
assert File.exist?(File.join(dirname, "a-#{a_digest}.js#{gzipped}"))
assert File.exist?(File.join(dirname, "b-#{b_digest}.js#{gzipped}"))
assert File.exist?(File.join(dirname, "c-#{c_digest}.css#{gzipped}"))
end
assert_equal "mobile/a-#{a_digest}.js", manifest["mobile/a.js"]
assert_equal "mobile/b-#{b_digest}.js", manifest["mobile/b.js"]
assert_equal "mobile/c-#{c_digest}.css", manifest["mobile/c.css"]
test "iterate over each logical path" do
paths = []
@env.each_logical_path do |logical_path|
paths << logical_path
end
end
assert_equal 23, paths.length
assert_equal paths.size, paths.uniq.size, "has duplicates"

test "precompile static asset" do
digest = @env["hello.txt"].digest
filename = fixture_path("public/hello-#{digest}.txt")
assert paths.include?("application.js")
assert paths.include?("coffee/foo.js")
assert paths.include?("coffee/index.js")
end

sandbox filename do
assert !File.exist?(filename)
manifest = @env.precompile("hello.txt", :to => fixture_path("public"))
assert File.exist?(filename)
assert !File.exist?("#{filename}.gz")
assert_equal "hello-#{digest}.txt", manifest["hello.txt"]
end
test "each logical path enumerator" do
files = []
enum = @env.each_logical_path
assert_equal 23, enum.to_a.length
end

test "CoffeeScript files are compiled in a closure" do
Expand Down

0 comments on commit d13401a

Please sign in to comment.