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

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

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

: args
:
{{
  : none
  :
  $* -s 2>"ln: missing arguments" == 1

  : no-target
  :
  $* -s a 2>"ln: missing target path" == 1

  : no-trailing-sep
  :
  $* -s a b c 2>"ln: multiple target paths with non-directory link path" == 1

  : empty
  :
  {{
    : link
    :
    $* -s '' 2>"ln: invalid path ''" == 1

    : target1
    :
    $* -s '' a 2>"ln: invalid path ''" == 1

    : target2
    :
    $* -s '' a b/ 2>"ln: invalid path ''" == 1
  }}
}}

: file-symlink
:
: Test creating a file symlink.
:
{{
  : non-existing-link-path
  :
  {
    touch a

    $* -s a b >>/~%EOO% &b
      %create .+/b true%
      %create .+/b false%
      EOO

    test -f b
  }

  : existing-link
  :
  {{
    : file
    :
    {
      touch a b

      $* -s a b >>/~%EOO% 2>>/~%EOE% != 0
        %create .+/b true%
        EOO
        %(
        %ln: unable to create .+link '.+/b' to '.+/a': .+%|
        %ln: unable to copy file '.+/a' to '.+/b': .+%
        %)
        EOE
    }

    : dir
    :
    {
      touch a
      mkdir b

      $* -s a b >>/~%EOO% 2>>/~%EOE% != 0
        %create .+/b true%
        EOO
        %(
        %ln: unable to create .+link '.+/b' to '.+/a': .+%|
        %ln: unable to copy file '.+/a' to '.+/b': .+%
        %)
        EOE
    }
  }}

  : non-existing
  :
  {{
    : target
    :
    {
      $* -s a b 2>>/~%EOE% != 0
        %ln: unable to create symlink to '.+/a': no such file or directory%
        EOE
    }

    : link-dir
    :
    {
      touch a

      $* -s $~/a b/c >>/~%EOO% 2>>/~%EOE% != 0
        %create .+/b/c true%
        EOO
        %(
        %ln: unable to create .+link '.+/b/c' to '.+/a': .+%|
        %ln: unable to copy file '.+/a' to '.+/b/c': .+%
        %)
        EOE
    }
  }}
}}

: file-hardlink
:
: Test creating a file hard link.
:
{{
  test.options = # Clear -c.

  : basics
  :
  {
    touch --no-cleanup a
    $* a b &b
    test -f b
    rm a
    test -f b
  }
}}

: dir-symlink
:
: Test creating a directory symlink.
:
{{
  : non-existing-link-path
  :
  {
    mkdir a
    touch a/b

    $* -s a c >>/~%EOO% &c
      %create .+/c true%
      %create .+/c false%
      EOO

    test -f c/b
  }

  : existing-link
  :
  {{
    : dir
    :
    {
      mkdir a b

      $* -s a b >>/~%EOO% 2>>/~%EOE% != 0
        %create .+/b true%
        EOO
        %(
        %ln: unable to create .+link '.+/b' to '.+/a': .+%|
        %ln: unable to copy directory '.+/a' to '.+/b': .+%
        %)
        EOE
    }

    : file
    :
    {
      mkdir a
      touch b

      $* -s a b >>/~%EOO% 2>>/~%EOE% != 0
        %create .+/b true%
        EOO
        %(
        %ln: unable to create .+link '.+/b' to '.+/a': .+%|
        %ln: unable to copy directory '.+/a' to '.+/b': .+%
        %)
        EOE
    }
  }}

  : non-existing
  :
  {{
    : link-dir
    :
    {
      mkdir a

      $* -s $~/a b/c >>/~%EOO% 2>>/~%EOE% != 0
        %create .+/b/c true%
        EOO
        %(
        %ln: unable to create .+link '.+/b/c' to '.+/a': .+%|
        %ln: unable to copy directory '.+/a' to '.+/b/c': .+%
        %)
        EOE
    }
  }}
}}

: multiple-targets
:
: Test creating links for multiple targets in the specified directory.
:
{
  touch a
  mkdir b c

  $* -s ../a ../b c/ >>/~%EOO% &c/a &c/b
    %create .+/c/a true%
    %create .+/c/a false%
    %create .+/c/b true%
    %create .+/c/b false%
    EOO

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