diff --git a/lib/rmt/mirror/base.rb b/lib/rmt/mirror/base.rb index 43bb3023c..bacc5e15a 100644 --- a/lib/rmt/mirror/base.rb +++ b/lib/rmt/mirror/base.rb @@ -155,9 +155,9 @@ def cleanup_stale_metadata logger.debug("Can not remove stale metadata directory: #{e}") end - def move_files(glob:, destination:) + def move_files(glob:, destination:, clean_before: true) FileUtils.mkpath(destination) unless Dir.exist?(destination) - FileUtils.rm_rf(Dir.glob(File.join(destination, '*'))) + FileUtils.rm_rf(Dir.glob(File.join(destination, '*'))) if clean_before FileUtils.mv(Dir.glob(glob), destination, force: true) rescue StandardError => e raise RMT::Mirror::Exception.new(_('Error while moving files %{glob} to %{dest}: %{error}') % { diff --git a/spec/lib/rmt/mirror/base_spec.rb b/spec/lib/rmt/mirror/base_spec.rb index 3a38371a4..021ae161b 100644 --- a/spec/lib/rmt/mirror/base_spec.rb +++ b/spec/lib/rmt/mirror/base_spec.rb @@ -264,6 +264,31 @@ base.move_files(glob: src, destination: dest) end.to raise_exception(/Error while moving files/) end + + it 'does not remove existing files when clean_before is false' do + allow(Dir).to receive(:exist?).with(dest).and_return(true) + allow(Dir).to receive(:glob).with(src).and_return(%w[/source/path/newfile1.txt /source/path/newfile2.txt]) + allow(Dir).to receive(:glob).with(File.join(dest, '*')).and_return(%w[/destination/path/existing1.txt /destination/path/existing2.txt]) + + expect(FileUtils).not_to receive(:rm_rf) + expect(FileUtils).to receive(:mv).with(%w[/source/path/newfile1.txt /source/path/newfile2.txt], dest, force: true) + + base.move_files(glob: src, destination: dest, clean_before: false) + end + + it 'removes existing files when clean_before is true' do + allow(Dir).to receive(:exist?).with(dest).and_return(true) + existing_files = %w[/destination/path/file1.txt /destination/path/file2.txt] + source_files = %w[/source/path/newfile1.txt /source/path/newfile2.txt] + + allow(Dir).to receive(:glob).with(File.join(dest, '*')).and_return(existing_files) + allow(Dir).to receive(:glob).with(src).and_return(source_files) + + expect(FileUtils).to receive(:rm_rf).with(existing_files) + expect(FileUtils).to receive(:mv).with(source_files, dest, force: true) + + base.move_files(glob: src, destination: dest, clean_before: true) + end end describe '#need_to_download?' do