# file      : tests/test/script/runner/env.testscript
# license   : MIT; see accompanying LICENSE file

.include ../common.testscript

: cwd
:
{{
  : not-exist
  :
  $c <<EOI && $b 2>>/~%EOE% != 0
  env -c a -- $* -w
  EOI
  %testscript:1:1: error: specified working directory .+/a/ does not exist%
    info: test id: 1
  EOE

  : process
  :
  $c <<EOI && $b
  mkdir a;
  env -c a -- $* -w >/~%.+/a%
  EOI

  : builtin
  :
  $c <<EOI && $b
  mkdir a;
  env -c a -- touch b;
  test -f a/b
  EOI

  : absolute
  :
  $c <<EOI && $b
  mkdir a;
  env -c $~/a -- touch b;
  test -f a/b
  EOI
}}

: variables
:
{{
  : set
  :
  $c <<EOI && $b
  env abc=xyz -- $* -v abc >xyz
  EOI

  : unset
  :
  $c <<EOI && env abc=xyz -- $b
  env --unset=abc -- $* -v abc >"<none>"
  EOI
}}

: timeout
:
{{
  : expired
  :
  $c <<EOI && $b 2>>~%EOE% != 0
  env --timeout 1 -- $* -l 5
  EOI
  %testscript:1:1: error: .+ terminated: execution timeout expired%
    info: test id: 1
  EOE

  : not-expired
  :
  $c <<EOI && $b
  env --timeout 5 -- $* -l 1
  EOI

  : invalid
  :
  $c <<EOI && $b 2>>EOE != 0
  env --timeout a -- $*
  EOI
  testscript:1:15: error: env: invalid value 'a' for option '--timeout'
  EOE
}}

: syntax-1
:
{{
  b = [cmdline] $regex.apply($b, '(.+testscript.syntax).+', "\1 = 1'")

  : cwd
  :
  {{
    : not-exist
    :
    $c <'env -c a -- $* -w' && $b 2>>/~%EOE% != 0
    %testscript:1:1: error: specified working directory .+/a/ does not exist%
      info: test id: 1
    EOE

    : process
    :
    $c <<EOI && $b
    mkdir a;
    env -c a -- $* -w >/~%.+/a%
    EOI

    : builtin
    :
    $c <<EOI && $b
    mkdir a;
    env -c a -- touch b;
    test -f a/b
    EOI

    : absolute
    :
    $c <<EOI && $b
    mkdir a;
    env -c $~/a -- touch b;
    test -f a/b
    EOI
  }}

  : variables
  :
  {{
    $c <'env abc=xyz -- $* -v abc >xyz' && $b                         : set
    $c <'env --unset=abc -- $* -v abc >"<none>"' && env abc=xyz -- $b : unset
  }}

  : timeout
  :
  {{
    : expired
    :
    $c <'env --timeout 1 -- $* -l 5' && $b 2>>~%EOE% != 0
    %testscript:1:1: error: .+ terminated: execution timeout expired%
      info: test id: 1
    EOE

    : not-expired
    :
    $c <'env --timeout 5 -- $* -l 1' && $b

    : invalid
    :
    $c <'env --timeout a -- $*' && $b 2>>EOE != 0
    testscript:1:15: error: env: invalid value 'a' for option '--timeout'
    EOE
  }}
}}
