# file      : tests/builtin/mv.testscript
# license   : MIT; see accompanying LICENSE file

test.arguments = "mv"
test.options += -c

: unknown-option
:
$* -u >'option -u' 2>"mv: unknown option '-u'" == 1

: args
:
{
  : none
  :
  $* 2>"mv: missing arguments" == 1

  : no-source
  :
  $* a 2>"mv: missing source path" == 1

  : no-trailing-sep
  :
  $* a b c 2>"mv: multiple source paths without trailing separator for destination directory" == 1

  : empty
  :
  {
    : dest
    :
    $* '' 2>"mv: invalid path ''" == 1

    : src1
    :
    $* '' a 2>"mv: invalid path ''" == 1

    : src2
    :
    $* '' a b/ 2>"mv: invalid path ''" == 1
  }
}

: synopsis-1
:
: Move an entity to the specified path.
:
{
  : file
  :
  {
    : existing
    :
    {
      : to-non-existing
      :
      {
        touch a &!a;

        $* a b >>/~%EOO% &b;
          %move .+/a .+/b false true%
          %move .+/a .+/b false false%
          EOO

        test -f b && test -f a == 1
      }

      : to-existing
      :
      {
        touch a b &!a;

        $* a b >>/~%EOO%;
          %move .+/a .+/b false true%
          %move .+/a .+/b false false%
          EOO

        test -f b && test -f a == 1
      }

      : to-self
      :
      {
        touch a;

        $* a a >/~'%move .+/a .+/a false true%' 2>>/~%EOE% != 0
          %mv: unable to move entry '.+/a' to itself%
          EOE
      }

      : to-dir
      :
      {
        touch a;
        mkdir b;

        $* a b >/~'%move .+/a .+/b false true%' 2>>/~%EOE% != 0
          %mv: unable to move entry '.+/a' to '.+/b': .+%
          EOE
      }
    }
  }

  : dir
  :
  {
    : existing
    :
    {
      : to-non-existing
      :
      {
        mkdir a &!a/;

        $* a b &b/ >>/~%EOO%;
          %move .+/a .+/b false true%
          %move .+/a .+/b false false%
          EOO

        test -d b && test -d a == 1
      }

      : to-non-empty
      :
      {
        mkdir a b;
        touch b/c;

        $* a b >/~'%move .+/a .+/b false true%' 2>>/~%EOE% != 0
          %mv: unable to move entry '.+/a' to '.+/b': .+%
          EOE
      }

      : to-non-dir
      :
      {
        mkdir a;
        touch b;

        $* a b >/~'%move .+/a .+/b false true%' 2>>/~%EOE% != 0
          %mv: unable to move entry '.+/a' to '.+/b': .+%
          EOE
      }
    }

    : overlap
    :
    {
      mkdir a;

      $* a a/b >/~'%move .+/a .+/a/b false true%' 2>>/~%EOE% != 0
        %mv: unable to move entry '.+/a' to '.+/a/b': .+%
        EOE
    }
  }

  : non-existing
  :
  {
    $* a b >/~'%move .+/a .+/b false true%' 2>>/~%EOE% != 0
      %mv: unable to move entry '.+/a' to '.+/b': .+%
      EOE
  }
}

: synopsis-2
:
: Move entities into the specified directory.
:
{
  mkdir a c &!a/;
  touch a/b b &!a/b &!b;

  $* a b c/ >>/~%EOO% &c/a/ &c/a/b &c/b;
    %move .+/a .+/c/a false true%
    %move .+/a .+/c/a false false%
    %move .+/b .+/c/b false true%
    %move .+/b .+/c/b false false%
    EOO

  test -d c/a && test -f c/a/b && test -f c/b
}