summaryrefslogtreecommitdiff
path: root/spec/migrations/move_system_upload_folder_spec.rb
blob: b622b4e9536b1c79fbad129d049e60c205df43e2 (plain)
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
require 'spec_helper'
require Rails.root.join("db", "migrate", "20170717074009_move_system_upload_folder.rb")

describe MoveSystemUploadFolder do
  let(:migration) { described_class.new }
  let(:test_base) { File.join(Rails.root, 'tmp', 'tests', 'move-system-upload-folder') }

  before do
    allow(migration).to receive(:base_directory).and_return(test_base)
    FileUtils.rm_rf(test_base)
    FileUtils.mkdir_p(test_base)
    allow(migration).to receive(:say)
  end

  describe '#up' do
    let(:test_folder) { File.join(test_base, 'system') }
    let(:test_file) { File.join(test_folder, 'file') }

    before do
      FileUtils.mkdir_p(test_folder)
      FileUtils.touch(test_file)
    end

    it 'moves the related folder' do
      migration.up

      expect(File.exist?(File.join(test_base, '-', 'system', 'file'))).to be_truthy
    end

    it 'creates a symlink linking making the new folder available on the old path' do
      migration.up

      expect(File.symlink?(File.join(test_base, 'system'))).to be_truthy
      expect(File.exist?(File.join(test_base, 'system', 'file'))).to be_truthy
    end
  end

  describe '#down' do
    let(:test_folder) { File.join(test_base, '-', 'system') }
    let(:test_file) { File.join(test_folder, 'file') }

    before do
      FileUtils.mkdir_p(test_folder)
      FileUtils.touch(test_file)
    end

    it 'moves the system folder back to the old location' do
      migration.down

      expect(File.exist?(File.join(test_base, 'system', 'file'))).to be_truthy
    end

    it 'removes the symlink if it existed' do
      FileUtils.ln_s(test_folder, File.join(test_base, 'system'))

      migration.down

      expect(File.directory?(File.join(test_base, 'system'))).to be_truthy
      expect(File.symlink?(File.join(test_base, 'system'))).to be_falsey
    end
  end
end