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

Commit

Permalink
Add tests for Sprockets::Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Stephenson committed May 29, 2008
1 parent 0b427dc commit f1eeca3
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -4,6 +4,6 @@ task :default => :test

Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList["test/*_test.rb"]
t.test_files = FileList["test/test_*.rb"]
t.verbose = true
end
20 changes: 10 additions & 10 deletions lib/sprockets/environment.rb
Expand Up @@ -3,20 +3,13 @@ class Environment
attr_reader :root, :load_path

def initialize(root, load_path = [])
@root = pathname_from(File.expand_path(root))
@load_path = [@root]

load_path.each do |location|
@load_path = [@root = Pathname.new(self, root)]

load_path.reverse_each do |location|
register_load_location(location)
end
end

def absolute_location_from(location)
location = location.to_s
location = File.join(root.absolute_location, location) unless location[/^\//]
File.expand_path(location)
end

def pathname_from(location)
Pathname.new(self, absolute_location_from(location))
end
Expand All @@ -31,5 +24,12 @@ def register_load_location(location)
def find(location)
load_path.map { |pathname| pathname.find(location) }.compact.first
end

protected
def absolute_location_from(location)
location = location.to_s
location = File.join(root.absolute_location, location) unless location[/^\//]
File.expand_path(location)
end
end
end
1 change: 1 addition & 0 deletions test/fixtures/src/foo/foo.js
@@ -0,0 +1 @@
var FooFoo = { };
66 changes: 66 additions & 0 deletions test/test_environment.rb
@@ -0,0 +1,66 @@
require "test_helper"

class EnvironmentTest < Test::Unit::TestCase
def test_load_path_locations_become_pathnames_for_absolute_locations_from_the_root
environment = Sprockets::Environment.new("/root", ["/a", "b"])
assert_load_path_equals ["/a", "/root/b", "/root"], environment
end

def test_pathname_from_for_location_with_leading_slash_should_return_a_pathname_with_the_location_unchanged
environment = Sprockets::Environment.new("/root")
assert_absolute_location "/a", environment.pathname_from("/a")
end

def test_pathname_from_for_relative_location_should_return_a_pathname_for_the_expanded_absolute_location_from_root
environment = Sprockets::Environment.new("/root")
assert_absolute_location "/root/a", environment.pathname_from("a")
assert_absolute_location "/root/a", environment.pathname_from("./a")
assert_absolute_location "/a", environment.pathname_from("../a")
end

def test_register_load_location_should_unshift_the_location_onto_the_load_path
environment = Sprockets::Environment.new("/root")
environment.register_load_location("a")
assert_load_path_equals ["/root/a", "/root"], environment
environment.register_load_location("b")
assert_load_path_equals ["/root/b", "/root/a", "/root"], environment
end

def test_register_load_location_should_remove_already_existing_locations_before_unshifting
environment = Sprockets::Environment.new("/root")
environment.register_load_location("a")
environment.register_load_location("b")
assert_load_path_equals ["/root/b", "/root/a", "/root"], environment
environment.register_load_location("a")
assert_load_path_equals ["/root/a", "/root/b", "/root"], environment
end

def test_find_should_return_the_first_matching_pathname_in_the_load_path
environment = environment_for_fixtures
first_pathname = environment.find("foo.js")
assert_absolute_location_ends_with "src/foo.js", first_pathname

environment.register_load_location(File.join(FIXTURES_PATH, "src", "foo"))
second_pathname = environment.find("foo.js")
assert_not_equal first_pathname, second_pathname
assert_absolute_location_ends_with "foo/foo.js", second_pathname
end

def test_find_should_return_nil_when_no_matching_source_file_is_found
environment = environment_for_fixtures
assert_nil environment.find("nonexistent.js")
end

protected
def assert_absolute_location(location, pathname)
assert_equal location, pathname.absolute_location
end

def assert_load_path_equals(load_path_absolute_locations, environment)
assert load_path_absolute_locations.zip(environment.load_path).map { |location, pathname| location == pathname.absolute_location }.all?
end

def assert_absolute_location_ends_with(location_ending, pathname)
assert pathname.absolute_location[/#{Regexp.escape(location_ending)}$/]
end
end
11 changes: 10 additions & 1 deletion test/test_helper.rb
Expand Up @@ -2,5 +2,14 @@
require "test/unit"

class Test::Unit::TestCase
FIXTURES_PATH = File.expand_path(File.join(File.dirname(__FILE__), "fixtures"))
FIXTURES_PATH = File.expand_path(File.join(File.dirname(__FILE__), "fixtures")) unless defined?(FIXTURES_PATH)

protected
def environment_for_fixtures
Sprockets::Environment.new(FIXTURES_PATH, source_directories_in_fixtures_path)
end

def source_directories_in_fixtures_path
Dir[File.join(FIXTURES_PATH, "**", "src")]
end
end
6 changes: 1 addition & 5 deletions test/preprocessor_test.rb → test/test_preprocessor.rb
Expand Up @@ -2,7 +2,7 @@

class PreprocessorTest < Test::Unit::TestCase
def setup
@environment = Sprockets::Environment.new(FIXTURES_PATH, source_directories_in_fixtures_path)
@environment = environment_for_fixtures
@preprocessor = Sprockets::Preprocessor.new(@environment)
end

Expand Down Expand Up @@ -89,8 +89,4 @@ def assert_output_file_contains(indented_text)
unindented_text = lines.map { |line| line[initial_indent..-1] }.join($/)
assert output_text[unindented_text]
end

def source_directories_in_fixtures_path
Dir[File.join(FIXTURES_PATH, "**", "src")]
end
end

0 comments on commit f1eeca3

Please sign in to comment.