require 'fileutils'
require 'test/unit'

#
# This test proves that JRuby's FileUtils#mv removes the destination file
# even in the case where the source file does not exist.
#

class FileUtilsMvTest < Test::Unit::TestCase

  def mv_with_options(mv_options = {})
    source      = "/tmp/FileUtilsMvTest_source"
    destination = "/tmp/FileUtilsMvTest_destination"

    FileUtils.rm(source, :force => true)
    FileUtils.rm(destination, :force => true)

    # Make sure the files don't exist before we get started
    assert !File.exists?(source)
    assert !File.exists?(destination)

    # Create the source file
    File.open(source,"w") { |file| file.write("test") }

    # Move source => destination and check they exist
    FileUtils.mv(source, destination, mv_options)
    assert !File.exists?(source)
    assert File.exists?(destination)

    # Move again and make sure the source is still missing but that destination is still there
    begin
      FileUtils.mv(source, destination, mv_options)
    rescue Errno::ENOENT => ex
      # Don't want this to blow up the test
    end
    assert !File.exists?(source)
    assert File.exists?(destination)
  end

  def test_without_force_true
    mv_with_options
  end

  def test_with_force_true
    mv_with_options(:force => true)
  end
end
