File: file_spec.rb

package info (click to toggle)
ruby-carrierwave 1.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,692 kB
  • sloc: ruby: 9,881; sh: 26; makefile: 5
file content (98 lines) | stat: -rw-r--r-- 3,163 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'spec_helper'
require 'support/file_utils_helper'
require 'tempfile'

describe CarrierWave::Storage::File do
  include FileUtilsHelper

  subject(:storage) { described_class.new(uploader) }

  let(:uploader_class) { Class.new(CarrierWave::Uploader::Base) }
  let(:tempfile) { Tempfile.new("foo") }
  let(:sanitized_temp_file) { CarrierWave::SanitizedFile.new(tempfile) }
  let(:uploader) { uploader_class.new }

  after { FileUtils.rm_rf(public_path) }

  describe '#delete_dir!' do
    let(:file) { File.open(file_path("test.jpg")) }

    context "when the directory is not empty" do
      let(:cache_id_dir) { File.dirname(cache_path) }
      let(:cache_path) { File.expand_path(File.join(uploader.cache_dir, uploader.cache_name), uploader.root) }
      let(:existing_file) { File.join(cache_id_dir, "exsting_file.txt") }

      before do
        uploader.cache!(file)
        File.open(existing_file, "wb"){|f| f << "I exist"}
        uploader.store!
      end

      it "doesn't delete the old cache_id" do
        expect(File).to be_directory(cache_id_dir)
      end

      it "doesn't delete other existing files in old cache_id dir" do
        expect(File).to exist existing_file
      end
    end
  end

  describe '#cache!' do
    context "when FileUtils.mkdir_p raises Errno::EMLINK" do
      before { fake_failed_mkdir_p(Errno::EMLINK) }
      after { storage.cache!(sanitized_temp_file) }

      it { is_expected.to receive(:clean_cache!).with(600) }
    end

    context "when FileUtils.mkdir_p raises Errno::ENOSPC" do
      before { fake_failed_mkdir_p(Errno::ENOSPC) }
      after { storage.cache!(sanitized_temp_file) }

      it { is_expected.to receive(:clean_cache!).with(600) }
    end
  end

  describe '#clean_cache!' do
    let(:today) { '2016/10/09 10:00:00'.to_time }
    let(:five_days_ago) { today.ago(5.days) }
    let(:three_days_ago) { today.ago(3.days) }
    let(:yesterday) { today.yesterday }
    let(:cache_dir) { File.expand_path(uploader_class.cache_dir, CarrierWave.root) }

    before do
      [five_days_ago, three_days_ago, yesterday, (today - 1.minute)].each do |created_date|
        Timecop.freeze (created_date) do
          FileUtils.mkdir_p File.expand_path(CarrierWave.generate_cache_id, cache_dir)
        end
      end
    end

    after { FileUtils.rm_rf(cache_dir) }

    it "clears all files older than now in the default cache directory" do
      Timecop.freeze(today) { uploader_class.clean_cached_files!(0) }

      expect(Dir.glob("#{cache_dir}/*").size).to eq(0)
    end

    it "clears all files older than, by default, 24 hours in the default cache directory" do
      Timecop.freeze(today) { uploader_class.clean_cached_files! }

      expect(Dir.glob("#{cache_dir}/*").size).to eq(2)
    end

    it "allows to set since how many seconds delete the cached files" do
      Timecop.freeze(today) { uploader_class.clean_cached_files!(4.days) }

      expect(Dir.glob("#{cache_dir}/*").size).to eq(3)
    end

    it "'s aliased on the CarrierWave module" do
      Timecop.freeze(today) { CarrierWave.clean_cached_files! }

      expect(Dir.glob("#{cache_dir}/*").size).to eq(2)
    end
  end
end