Skip to content

Always initialize assets environment in rake task #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/sprockets/rails/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def initialize(app = nil)

def environment
if app
app.assets
# Use initialized app.assets or force build an environment if
# config.assets.compile is disabled
app.assets || Sprockets::Railtie.build_environment(app)
else
super
end
Expand Down
9 changes: 7 additions & 2 deletions lib/sprockets/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ def configure(&block)
Sprockets::Rails::Task.new(app)
end

def self.build_environment(app)
def build_environment(app, initialized = nil)
initialized = app.initialized? if initialized.nil?
unless initialized
::Rails.logger.warn "Application uninitialized: Try calling YourApp::Application.initialize!"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafaelfranca does this error make sense?

Also, I was wondering why app.logger doesn't exist.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, it seems we are talking about an internal thing of Rails. Users never had to worry about initializing an application. Do you know when the application is not initialized at this point?

Also, I was wondering why app.logger doesn't exist.

The application doesn't have logger. It is a global thing in the Rails module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know when the application is not initialized at this point?

If you override the assets:environment task, which is permitted, you could forget or screw up initializing the app on your own which could lead to an uninitialized app instance here. Yeah, this won't affect people on the golden path.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. So I think the message is good.

end

config = app.config
env = Sprockets::Environment.new(app.root.to_s)

Expand Down Expand Up @@ -126,7 +131,7 @@ def self.build_manifest(app)
config = app.config

if config.assets.compile
app.assets = self.build_environment(app)
app.assets = self.build_environment(app, true)
app.routes.prepend do
mount app.assets => config.assets.prefix
end
Expand Down
88 changes: 87 additions & 1 deletion test/test_railtie.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
require 'minitest/autorun'
require 'active_support'
require 'active_support/testing/isolation'
require 'minitest/autorun'

Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

def silence_stderr
orig_stderr = $stderr.clone
$stderr.reopen File.new('/dev/null', 'w')
yield
ensure
$stderr.reopen orig_stderr
end

class TestBoot < Minitest::Test
include ActiveSupport::Testing::Isolation

Expand Down Expand Up @@ -264,4 +272,82 @@ def test_manifest_path_respects_rails_public_path
assert_match %r{test_public/assets/manifest-.*\.json$}, manifest.path
assert_match %r{test_public/assets$}, manifest.dir
end

def test_load_tasks
app.initialize!
app.load_tasks

assert Rake.application['assets:environment']
assert Rake.application['assets:precompile']
assert Rake.application['assets:clean']
assert Rake.application['assets:clobber']
end

def test_task_precompile
app.configure do
config.assets.paths << FIXTURES_PATH
config.assets.precompile += ["foo.js"]
end
app.initialize!
app.load_tasks

path = "#{app.assets_manifest.dir}/foo-4ef5541f349f7ed5a0d6b71f2fa4c82745ca106ae02f212aea5129726ac6f6ab.js"

silence_stderr do
Rake.application['assets:clobber'].execute
end
refute File.exist?(path)

silence_stderr do
Rake.application['assets:precompile'].execute
end
assert File.exist?(path)

silence_stderr do
Rake.application['assets:clobber'].execute
end
refute File.exist?(path)
end

def test_task_precompile_compile_false
app.configure do
config.assets.compile = false
config.assets.paths << FIXTURES_PATH
config.assets.precompile += ["foo.js"]
end
app.initialize!
app.load_tasks

path = "#{app.assets_manifest.dir}/foo-4ef5541f349f7ed5a0d6b71f2fa4c82745ca106ae02f212aea5129726ac6f6ab.js"

silence_stderr do
Rake.application['assets:clobber'].execute
end
refute File.exist?(path)

silence_stderr do
Rake.application['assets:precompile'].execute
end
assert File.exist?(path)

silence_stderr do
Rake.application['assets:clobber'].execute
end
refute File.exist?(path)
end

def test_direct_build_environment_call
app.configure do
config.assets.paths << "javascripts"
config.assets.paths << "stylesheets"
end
app.initialize!

assert env = Sprockets::Railtie.build_environment(app)
assert_kind_of Sprockets::Environment, env

assert_equal ROOT, env.root
assert_equal ["#{ROOT}/javascripts", "#{ROOT}/stylesheets"],
env.paths.sort
end
end