# file      : tests/mventry/testscript
# license   : MIT; see accompanying LICENSE file

: file
:
{
  : non-existing
  :
  $* a b 2>- == 1

  : over
  :
  {
    : non-existing
    :
    echo 'foo' >=a &!a;
    $* a b &b;
    cat b >'foo';
    test -f a == 1

    : existing-file
    :
    echo 'foo' >=a &!a;
    echo 'bar' >=b;
    $* a b;
    cat b >'foo';
    test -f a == 1

    : existing-dir
    :
    echo 'foo' >=a;
    mkdir b;
    $* a b 2>- == 1
  }

  : to-dir
  :
  echo 'foo' >=a &!a;
  mkdir b;
  $* a b/ &b/a;
  cat b/a >'foo';
  test -f a == 1
}

: dir
:
{
  : over
  {
    : non-existing
    :
    mkdir -p a/b &!a/b/ &!a/;
    echo 'foo' >=a/c &!a/c;
    $* a b &b/ &b/c &b/b/;
    cat b/c >'foo';
    test -d b/b;
    test -d a == 1

    : empty-dir
    :
    mkdir -p a/b &!a/b/ &!a/;
    echo 'foo' >=a/c &!a/c;
    mkdir b;
    $* a b &b/c &b/b/;
    cat b/c >'foo';
    test -d b/b;
    test -d a == 1

    : non-empty-dir
    :
    mkdir -p a/b;
    mkdir -p b/d;
    $* a b 2>- == 1

    : existing-file
    :
    mkdir a;
    touch b;
    $* a b 2>- == 1
  }

  : to-dir
  :
  mkdir a &!a/;
  mkdir b;
  $* a b/ &b/a/;
  test -d b/a;
  test -f a == 1
}

: symlink
:
: If we are not cross-testing let's test renaming symlynks from and over. On
: Windows that involves mklink command usability test. If we fail to create a
: trial link (say because we are not in the Developer Mode and are running
: non-elevated console), then the test group will be silently skipped.
:
if ($test.target == $build.host)
{
  +if ($cxx.target.class != 'windows')
    lnf = ^ln -s t l &l
    lnd = $lnf
  else
    echo 'yes' >=t
    if cmd /C 'mklink l t' >- 2>- &?l && cat l >'yes'
      lnf = cmd /C 'mklink l t' &l >-
      lnd = cmd /C 'mklink /D l t' &l >-
    end

    jnc = cmd /C 'mklink /J l t' &l >-
  end

  : symlink
  :
  if! $empty($lnf)
  {
    : file
    :
    {
      : from
      :
      : Make sure that if source is a symlink it refers the same target after
      : rename.
      :
      echo 'foo' >=t;
      $lnf;
      $* l ll &!l ≪
      test -f t;
      test -f l == 1;
      echo 'bar' >=t;
      cat ll >'bar'

      : to
      :
      : Make sure that if destination is a symlink it is get overwritten and
      : it's target stays intact.
      :
      echo 'foo' >=t;
      $lnf;
      echo 'bar' >=f;
      $* f l &!f;
      cat t >'foo';
      test -f f == 1;
      echo 'baz' >=t;
      cat l >'bar'

      : over-existing-dir
      :
      echo 'foo' >=t;
      $lnf;
      mkdir d;
      $* t d 2>- == 1
    }

    : dir
    :
    {
      : from
      :
      : Make sure that if source is a symlink it refers the same target after
      : rename.
      :
      mkdir -p t;
      $lnd;
      $* l ll &!l ≪
      touch t/f;
      test -f ll/f;
      test -f l == 1;
      test -d l == 1

      : to
      :
      : Make sure that if destination is a symlink it is get overwritten and
      : it's target stays intact.
      :
      mkdir -p t;
      $lnd;
      echo 'foo' >=f;
      $* f l &!f;
      cat l >'foo';
      test -d t;
      test -f f == 1

      : over-existing-dir
      :
      mkdir t;
      $lnd;
      mkdir d;
      $* l d 2>- == 1
    }
  }

  : junction
  :
  if! $empty($jnc)
  {
    : from
    :
    : Make sure that if source is a junction it refers the same target after
    : rename.
    :
    mkdir -p t;
    $jnc;
    $* l ll &!l ≪
    touch t/f;
    test -f ll/f;
    test -f l == 1;
    test -d l == 1

    : to
    :
    : Make sure that if destination is a junction it is get overwritten and
    : it's target stays intact.
    :
    mkdir -p t;
    $jnc;
    echo 'foo' >=f;
    $* f l &!f;
    cat l >'foo';
    test -d t;
    test -f f == 1

    : over-existing-dir
    :
    mkdir t;
    $jnc;
    mkdir d;
    $* l d 2>- == 1
  }
}

: different-fs
:
: Note that nested tests may fail for cross-testing as the directory path will
: unlikelly be usable on both host (build2 driver) and target (test driver)
: platforms.
:
if! $empty($config.libbutl.test.rename.dir)
{
  wd = $config.libbutl.test.rename.dir/libbutl-rename
  +rm -rf $wd
  +mkdir $wd

  : file
  :
  {
    : over-non-existing
    :
    {
      wd = "$wd/$@";
      mkdir -p $wd;
      b = $wd/b;

      echo 'foo' >=a &!a;
      $* a $b;
      cat $b >'foo';
      test -f a == 1
    }

    : over-file
    :
    {
      wd = "$wd/$@";
      mkdir -p $wd;
      b = $wd/b;

      touch $b;
      echo 'foo' >=a &!a;
      $* a $b;
      cat $b >'foo';
      test -f a == 1
    }
  }

  : dir
  :
  : Test that renaming directory to different fs/drive expectedly fails.
  :
  {
    wd = "$wd/$@";
    mkdir -p $wd;
    b = $wd/b;

    mkdir a;
    $* a $b 2>- == 1
  }

  -rm -rf $wd
}