Skip to content
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

Allow age to be altered in clean_assets rake task #677

Merged
merged 2 commits into from
Jun 23, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprocket

## Master

- Allow age to be altered in asset:clean rake task.
- Fix `Sprockets::Server` to return lower-cased response headers to comply with Rack::Lint 3.0. [#744](https://github.com/rails/sprockets/pull/744)
- Adding new directive `depend_on_directory` [#668](https://github.com/rails/sprockets/pull/668)
- Fix `application/js-sourcemap+json` charset [#669](https://github.com/rails/sprockets/pull/669)
Expand Down
8 changes: 6 additions & 2 deletions lib/rake/sprocketstask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ def manifest
#
attr_accessor :assets

# Number of old assets to keep.
# Minimum number of old assets to keep. See Sprockets::Manifest#clean for more information.
attr_accessor :keep

# Assets created within this age will be kept. See Sprockets::Manifest#clean for more information.
attr_accessor :age

# Logger to use during rake tasks. Defaults to using stderr.
#
# t.logger = Logger.new($stdout)
Expand Down Expand Up @@ -103,6 +106,7 @@ def initialize(name = :assets)
@logger = Logger.new($stderr)
@logger.level = Logger::INFO
@keep = 2
@age = 3600

yield self if block_given?

Expand Down Expand Up @@ -130,7 +134,7 @@ def define
desc name == :assets ? "Clean old assets" : "Clean old #{name} assets"
task "clean_#{name}" do
with_logger do
manifest.clean(keep)
manifest.clean(keep, age)
end
end

Expand Down
20 changes: 20 additions & 0 deletions test/test_rake_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ def teardown

end

test "clean" do
digest_path = @env['application.js'].digest_path
filename = fixture_path('default/application.coffee')

sandbox filename do
@rake[:assets].invoke
assert File.exist?("#{@dir}/#{digest_path}")

File.open(filename, 'w') { |f| f.write "change;" }
changed_digest_path = @env['application.js'].digest_path

@rake[:assets].invoke

@rake[:clean_assets].invoke(1, 0)

assert File.exist?("#{@dir}/#{digest_path}")
refute File.exist?("#{@dir}/#{changed_digest_path}")
end
end

test "custom manifest directory" do
Rake::SprocketsTask.new do |t|
t.environment = nil
Expand Down