BigW Consortium Gitlab

move_system_upload_folder_spec.rb 2.36 KB
Newer Older
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
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
36 37 38 39 40 41 42 43 44

    it 'does not move if the target directory already exists' do
      FileUtils.mkdir_p(File.join(test_base, '-', 'system'))

      expect(FileUtils).not_to receive(:mv)
      expect(migration).to receive(:say).with(/already exists. No need to redo the move/)

      migration.up
    end
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
  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
70 71 72 73 74 75 76 77 78

    it 'does not move if the old directory already exists' do
      FileUtils.mkdir_p(File.join(test_base, 'system'))

      expect(FileUtils).not_to receive(:mv)
      expect(migration).to receive(:say).with(/already exists and is not a symlink, no need to revert/)

      migration.down
    end
79 80
  end
end