Skip to content

Commit

Permalink
Drop trailing slash of prefix in #get_local_files (#425)
Browse files Browse the repository at this point in the history
The local filesystem glob in `AssetSync::Storage#get_local_files` uses
fuzzy matching when `config.prefix` is present. This can present a
problem in some cases, as it doesn't allow for distinguishing between
(e.g.) a folder called `assets/` and another folder called
`assets-temp/`. A situation could arise where the latter folder has
thousands/millions of files and we mistakenly publish local-only files,
or worse we could clobber another directory in the bucket managed in a
completely different context.

This change allows developers to be more specific in their
`config.prefix` by using a trailing slash for their folder name.
  • Loading branch information
PikachuEXE authored Aug 29, 2024
2 parents 4deb6ff + 923417f commit 9531c16
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/asset_sync/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def get_local_files

log "Using: Directory Search of #{path}/#{self.config.assets_prefix}"
Dir.chdir(path) do
to_load = self.config.assets_prefix.present? ? "#{self.config.assets_prefix}/**/**" : '**/**'
to_load = self.config.assets_prefix.present? ? File.join(self.config.assets_prefix, '/**/**') : '**/**'
Dir[to_load]
end
end
Expand Down
49 changes: 49 additions & 0 deletions spec/unit/storage_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/../spec_helper'
require 'fileutils'

describe AssetSync::Storage do
include_context "mock Rails without_yml"
Expand Down Expand Up @@ -490,4 +491,52 @@ def check_file(file)
end
end
end

describe '#get_local_files' do
around(:each) do |example|
Dir.mktmpdir do |public_path|
@public_path = public_path
example.call
end
end

before(:each) do
@config = AssetSync::Config.new
@config.public_path = @public_path
@config.prefix = 'assets'
@storage = AssetSync::Storage.new(@config)

Dir.mkdir("#{@public_path}/assets")
end

context 'with empty directory' do
it 'has no files' do
expect(@storage.get_local_files).to eq([])
end
end

context 'with non-empty directory' do
before(:each) do
FileUtils.touch("#{@public_path}/assets/application.js")
end

it 'lists available files' do
expect(@storage.get_local_files).to eq([
'assets/application.js'
])
end

context 'with trailing slash on asset prefix' do
before(:each) do
@config.prefix = 'assets/'
end

it 'lists available files with single slashes' do
expect(@storage.get_local_files).to eq([
'assets/application.js'
])
end
end
end
end
end

0 comments on commit 9531c16

Please sign in to comment.