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

Issue 338 #393

Merged
merged 5 commits into from
Jul 11, 2011
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/carrierwave/uploader/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,16 @@ def store!(new_file=nil)
#
def delete_cache_id
if @cache_id
path = File.join(cache_dir, @cache_id)
FileUtils.rm_rf(path) if File.exists?(path) && File.directory?(path)
path = File.expand_path(File.join(cache_dir, @cache_id), CarrierWave.root)
begin
Dir.rmdir(path)
rescue Errno::ENOENT
# Ignore: path does not exist
rescue Errno::ENOTDIR
# Ignore: path is not a dir
rescue Errno::ENOTEMPTY, Errno::EEXIST
# Ignore: dir is not empty
end
end
end

Expand Down
40 changes: 38 additions & 2 deletions spec/uploader/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,16 @@

it "should delete the old cache_id" do
@uploader.cache!(@file)
@uploader.should_receive(:delete_cache_id)
cache_path = @uploader.send(:cache_path) # WARNING: violating private
cache_id_dir = File.dirname(cache_path)
cache_parent_dir = File.split(cache_id_dir).first
File.should be_directory(cache_parent_dir)
File.should be_directory(cache_id_dir)

@uploader.store!

File.should be_directory(cache_parent_dir)
File.should_not be_directory(cache_id_dir)
end

context "with the delete_cache_id_after_storage option set to false" do
Expand All @@ -119,8 +127,36 @@

it "should not delete the old cache_id" do
@uploader.cache!(@file)
@uploader.should_not_receive(:delete_cache_id)
cache_path = @uploader.send(:cache_path) # WARNING: violating private
cache_id_dir = File.dirname(cache_path)
cache_parent_dir = File.split(cache_id_dir).first
File.should be_directory(cache_parent_dir)
File.should be_directory(cache_id_dir)

@uploader.store!

File.should be_directory(cache_parent_dir)
File.should be_directory(cache_id_dir)
end
end

context "when the old cache_id directory is not empty" do
before do
@uploader.cache!(@file)
cache_path = @uploader.send(:cache_path) # WARNING: violating private
@cache_id_dir = File.dirname(cache_path)
@existing_file = File.join(@cache_id_dir, "exsting_file.txt")
File.open(@existing_file, "wb"){|f| f << "I exist"}
end

it "should not delete the old cache_id" do
@uploader.store!
File.should be_directory(@cache_id_dir)
end

it "should not delete other existing files in old cache_id dir" do
@uploader.store!
File.should exist @existing_file
end
end

Expand Down
4 changes: 3 additions & 1 deletion spec/uploader/versions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
version :micro
end
end
@uploader_class.versions.keys.should == [:large, :thumb]
@uploader_class.versions.should have(2).versions
@uploader_class.versions.should include :large
@uploader_class.versions.should include :thumb
@uploader.large.versions.should be_empty
@uploader.thumb.versions.keys.should == [:mini]
@uploader.thumb.mini.versions.keys.should == [:micro]
Expand Down