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

Add method add_local_file_paths to config class #347

Merged
merged 1 commit into from
Jul 6, 2017
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
24 changes: 24 additions & 0 deletions lib/asset_sync/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def initialize
self.cdn_distribution_id = nil
self.invalidate = []
self.cache_asset_regexps = []
@additional_local_file_paths_procs = []

load_yml! if defined?(::Rails) && yml_exists?
end

Expand Down Expand Up @@ -220,8 +222,30 @@ def fog_options
return options
end

# @api
def add_local_file_paths(&block)
@additional_local_file_paths_procs =
additional_local_file_paths_procs + [block]
end

# @api private
# This is to be called in Storage
# Not to be called by user
def additional_local_file_paths
return [] if additional_local_file_paths_procs.empty?

# Using `Array()` to ensure it works when single value is returned
additional_local_file_paths_procs.each_with_object([]) do |proc, paths|
paths.concat(Array(proc.call))
end
end

private

# This is a proc to get additional local files paths
# Since this is a proc it won't be able to be configured by a YAML file
attr_reader :additional_local_file_paths_procs

def default_manifest_directory
File.join(::Rails.public_path, assets_prefix)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/asset_sync/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def ignored_files
end

def local_files
@local_files ||= get_local_files.uniq
@local_files ||=
(get_local_files + config.additional_local_file_paths).uniq
end

def always_upload_files
Expand Down
87 changes: 79 additions & 8 deletions spec/unit/storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
it 'should overwrite all remote files if set to ignore' do
@config.existing_remote_files = 'ignore'
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:local_files).and_return(@local_files)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist

@local_files.each do |file|
Expand All @@ -26,7 +26,7 @@
@config.always_upload = ['local_image.jpg', /local_image\d\.svg/]

storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:local_files).and_return(@local_files)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist

Expand All @@ -40,7 +40,7 @@
@config.ignored_files = ['local_image1.jpg', /local_stylesheet\d\.css/]

storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:local_files).and_return(@local_files)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist

Expand Down Expand Up @@ -68,7 +68,78 @@
]

storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:local_files).and_return(@local_files)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist

updated_nonfingerprinted_files = [
'public/image.png',
'public/application.js',
]
(@local_files - @remote_files + updated_nonfingerprinted_files).each do |file|
expect(storage).to receive(:upload_file).with(file)
end
storage.upload_files
end

context "when config #add_local_file_paths is called" do
let(:additional_local_file_paths) do
["webpack/example_asset.jpg"]
end

before(:each) do
@config.add_local_file_paths do
additional_local_file_paths
end
end

let(:storage) do
AssetSync::Storage.new(@config)
end

let(:file_paths_should_be_uploaded) do
@local_files -
@remote_files -
storage.ignored_files +
storage.always_upload_files +
additional_local_file_paths
end

before do
# Stubbing
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
# Pretend the files all exist
allow(File).to receive(:file?).and_return(true)
end

it "uploads additional files in additional to local files" do
file_paths_should_be_uploaded.each do |file|
expect(storage).to receive(:upload_file).with(file)
end
storage.upload_files
end
end

it 'should upload additonal files' do
@local_files = [
'public/image.png',
'public/image-82389298328.png',
'public/image-a8389f9h324.png',
'public/application.js',
'public/application-b3389d983k1.js',
'public/application-ac387d53f31.js',
'public',
]
@remote_files = [
'public/image.png',
'public/image-a8389f9h324.png',
'public/application.js',
'public/application-b3389d983k1.js',
]

storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:file?).and_return(true) # Pretend they all exist

Expand Down Expand Up @@ -110,7 +181,7 @@
remote_files = []
@config.cache_asset_regexps = [/\.[a-f0-9]{6}$/i, /\.[a-f0-9]{8}$/i]
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:local_files).and_return(local_files)
allow(storage).to receive(:get_local_files).and_return(local_files)
allow(storage).to receive(:get_remote_files).and_return(remote_files)
allow(File).to receive(:file?).and_return(true)
allow(File).to receive(:open).and_return(nil)
Expand Down Expand Up @@ -155,7 +226,7 @@ def check_file(file)
@config.fog_provider = 'AWS'

storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:local_files).and_return(@local_files)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(storage).to receive(:upload_file).and_return(true)

Expand Down Expand Up @@ -193,7 +264,7 @@ def check_file(file)
}
}
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:local_files).and_return(@local_files)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:open).and_return('file') # Pretend they all exist

Expand All @@ -216,7 +287,7 @@ def check_file(file)
}
}
storage = AssetSync::Storage.new(@config)
allow(storage).to receive(:local_files).and_return(@local_files)
allow(storage).to receive(:get_local_files).and_return(@local_files)
allow(storage).to receive(:get_remote_files).and_return(@remote_files)
allow(File).to receive(:open).and_return('file') # Pretend they all exist
bucket = double
Expand Down