This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
forked from rails/sprockets
/
test_secretary.rb
89 lines (74 loc) · 3.89 KB
/
test_secretary.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require "test_helper"
class SecretaryTest < Test::Unit::TestCase
def test_load_locations_are_not_expanded_when_expand_paths_is_false
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH)
secretary.add_load_location("src/**/", :expand_paths => false)
assert_equal [File.join(FIXTURES_PATH, "src/**"), FIXTURES_PATH],
secretary.environment.load_path.map { |pathname| pathname.absolute_location }
end
def test_load_locations_are_expanded_when_expand_paths_is_true
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH)
secretary.add_load_location("src/**/", :expand_paths => true)
assert_equal [File.join(FIXTURES_PATH, "src", "foo"), File.join(FIXTURES_PATH, "src"), FIXTURES_PATH],
secretary.environment.load_path.map { |pathname| pathname.absolute_location }
end
def test_source_files_are_not_expanded_when_expand_paths_is_false
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH)
assert_raises(Sprockets::LoadError) do
secretary.add_source_file("src/f*.js", :expand_paths => false)
end
end
def test_source_files_are_expanded_when_expand_paths_is_true
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH)
secretary.add_source_file("src/f*.js", :expand_paths => true)
assert_equal [File.join(FIXTURES_PATH, "src", "foo.js")],
secretary.preprocessor.source_files.map { |source_file| source_file.pathname.absolute_location }
end
def test_install_assets_into_empty_directory
with_temporary_directory do |temp|
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH, :asset_root => temp)
secretary.add_source_file("src/script_with_assets.js")
assert_equal [], Dir[File.join(temp, "**", "*")]
secretary.install_assets
assert_equal paths_relative_to(temp,
"images", "images/script_with_assets", "images/script_with_assets/one.png",
"images/script_with_assets/two.png", "stylesheets", "stylesheets/script_with_assets.css"),
Dir[File.join(temp, "**", "*")].sort
end
end
def test_install_assets_into_nonexistent_directory
with_temporary_directory do |temp|
temp = File.join(temp, "assets")
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH, :asset_root => temp)
secretary.add_source_file("src/script_with_assets.js")
assert_equal [], Dir[File.join(temp, "**", "*")]
secretary.install_assets
assert_equal paths_relative_to(temp,
"images", "images/script_with_assets", "images/script_with_assets/one.png",
"images/script_with_assets/two.png", "stylesheets", "stylesheets/script_with_assets.css"),
Dir[File.join(temp, "**", "*")].sort
end
end
def test_install_assets_into_subdirectories_that_already_exist
with_temporary_directory do |temp|
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH, :asset_root => temp)
secretary.add_source_file("src/script_with_assets.js")
FileUtils.mkdir_p(File.join(temp, "images", "script_with_assets"))
assert_equal paths_relative_to(temp, "images", "images/script_with_assets"), Dir[File.join(temp, "**", "*")]
secretary.install_assets
assert_equal paths_relative_to(temp,
"images", "images/script_with_assets", "images/script_with_assets/one.png",
"images/script_with_assets/two.png", "stylesheets", "stylesheets/script_with_assets.css"),
Dir[File.join(temp, "**", "*")].sort
end
end
def test_secretary_passes_strip_comments_option_through_to_preprocessor
secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH, :strip_comments => false)
secretary.add_source_file("src/script_with_comments.js")
assert_equal content_of_fixture("src/script_with_comments.js"), secretary.concatenation.to_s
end
protected
def paths_relative_to(root, *paths)
paths.map { |path| File.join(root, path) }
end
end