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

.include ../../common.testscript

: path-search
:
{{
  +mkdir -p a/b

  : pattern-path
  :
  : Test overloads for pattern being of the path type.
  :
  {{
    : start-abs-dir
    :
    $* <'print $path_search([path] "b**/", [dir_path] $src_base/../../a)' >/'b/'

    : start-relative-dir
    :
    $* <'print $path_search([path] "b**/", [dir_path] ../../a)' 2>>~%EOE% != 0
    %error: start directory '.+' is relative%
    %  info: pattern '.+' is relative%
      <stdin>:1:8: info: while calling path_search(path, dir_path)
    EOE

    : start-untyped
    :
    $* <'print $path_search([path] "b**/", $src_base/../../a)' >/'b/'

    : start-multiple-names
    :
    $* <'print $path_search([path] "b**/", a b)' 2>>EOE != 0
    error: invalid argument: invalid dir_path value: multiple names
      <stdin>:1:8: info: while calling path_search(path, <untyped>)
    EOE
  }}

  : pattern-untyped
  :
  : Test overloads for pattern being untyped.
  :
  {{
    : start-dir
    :
    $* <'print $path_search("b**/", [dir_path] $src_base/../../a)' >/'b/'

    : start-untyped
    :
    $* <'print $path_search("b**/", $src_base/../../a)' >/'b/'

    : abs-pattern
    :
    if ($test.target == $build.host)
    {
      touch b
      $* <'print $path_search("$src_base/b*")' >>/"EOO"
      $out_base/test/$@/b
      EOO
    }
  }}

  : pattern-multiple-names
  :
  {{
    : dir
    :
    touch b;
    $* <'print $path_search(a b, $src_base)' 2>>EOE != 0
    error: invalid argument: invalid path value: multiple names
      <stdin>:1:8: info: while calling path_search(<untyped>, dir_path)
    EOE
  }}
}}

: file_exists
:
{{
  : file
  :
  touch f;
  $* <'print $file_exists(f)' >'true'

  : symlink
  :
  touch f && ln -s f s;
  $* <'print $file_exists([path] s)' >'true'

  : directory
  :
  mkdir d;
  $* <'print $file_exists([dir_path] d)' >'false'

  : testscript
  :
  touch f;
  echo $file_exists(f) >'true'
}}

: directory_exists
:
{{
  : directory
  :
  mkdir d;
  $* <'print $directory_exists(d)' >'true'

  : symlink
  :
  mkdir d && ln -s d s;
  $* <'print $directory_exists([dir_path] d)' >'true'

  : file
  :
  touch f;
  $* <'print $directory_exists([path] f)' >'false'

  : testscript
  :
  mkdir d;
  echo $directory_exists(d) >'true'
}}
