Skip to content

Commit

Permalink
add conditional cleanup of repodata directory
Browse files Browse the repository at this point in the history
  • Loading branch information
paragjain0910 committed Dec 18, 2024
1 parent 3b38a78 commit 5976796
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rmt/mirror/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}') % {
Expand Down
25 changes: 25 additions & 0 deletions spec/lib/rmt/mirror/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5976796

Please sign in to comment.