# file      : tests/test/config-test/testscript
# license   : MIT; see accompanying LICENSE file

# Setup a realistic test project that we will then exercise.
#

test.options   = --no-default-options --serial-stop --quiet
test.arguments = 'test(../proj/@./)' # Test out-of-src (for parallel).
test.cleanups  = &?**/               # Cleanup out directory structure.

+mkdir proj
+mkdir proj/build
+cat <<EOI >=proj/build/bootstrap.build
project = proj
amalgamation =

using test
EOI

+cat <<EOI >=proj/buildfile
d = tests/ units/
./: $d
#include $d
EOI

# tests/ - as a subproject
#
+mkdir proj/tests
+mkdir proj/tests/build
+cat <<EOI >=proj/tests/build/bootstrap.build
project =

using test
EOI

+cat <<EOI >=proj/tests/buildfile
d = script/
./: $d
#include $d
EOI

# tests/script - scripted test
#
+mkdir proj/tests/script
+cat <<EOI >=proj/tests/script/buildfile
./: testscript{basics.testscript}
EOI
+cat <<EOI >=proj/tests/script/basics.testscript
echo 'tests/script/basics/foo' >| : foo
echo 'tests/script/basics/bar' >| : bar

: baz
{{
  echo 'tests/script/basics/baz/foo' >| : foo
  echo 'tests/script/basics/baz/bar' >| : bar
}}
EOI

# units/ - as a subdirectory
#
+mkdir proj/units

# This one is "dual": test and sub-test alias.
#
+cat <<EOI >=proj/units/buildfile
d = simple/ script/
./: $d testscript
#include $d
EOI
+cat <<EOI >=proj/units/testscript
echo 'units' >|
EOI

# units/simple - simple (non-scripted) test
#
# This one is a bit tricky since we need an executable to run. We don't want
# to be building anything as part of our test project so what we do is test a
# dummy file target with an overridden test target (note that $src_root here
# refers to the root of the project being tested).
#
+mkdir proj/units/simple
+touch proj/units/simple/driver
+cat <<EOI >=proj/units/simple/buildfile
driver = $src_root/../../exe{driver}
file{driver}@./: $driver
file{driver}@./: test = $driver
file{driver}@./: test.arguments = units/simple
EOI

# units/script - scripted test
#
+mkdir proj/units/script
+cat <<EOI >=proj/units/script/buildfile
./: testscript
EOI
+cat <<EOI >=proj/units/script/testscript
echo 'units/script/foo' >| : foo
echo 'units/script/bar' >| : bar
EOI

# @@ TMP Drop this when support for syntax 1 is dropped (see
#        libbuild2/script/parser.hxx for details).
#
# Prepare a project for running the syntax 1 testscripts. Also specify syntax
# version explicitly for the original syntax 2 testscripts.
#
+cp -p -R proj proj-s1
+for f [path]: tests/script/basics.testscript units/testscript units/script/testscript
{
  # Prepare syntax 2 testscripts.
  #
  t = [path] "proj/$f"

  mv $t tmp
  echo 'testscript.syntax = 2' >=$t
  cat tmp                      >+$t

  # Prepare syntax 1 testscripts.
  #
  t = [path] "proj-s1/$f"

  sed -e 's/^\{\{$/{/' -e 's/^\}\}$/}/' $t >=tmp

  echo 'testscript.syntax = 1' >=$t
  cat tmp                      >+$t
}

# Now the tests. Should all be top-level, no groups (or set test.arguments).
#

: all
:
$* >>EOO
tests/script/basics/foo
tests/script/basics/bar
tests/script/basics/baz/foo
tests/script/basics/baz/bar
units/simple
units/script/foo
units/script/bar
units
EOO

: alias-pass
: Test lead-up alias pass-through (but not test)
:
$* config.test=units/simple/file{driver} >>EOO
units/simple
EOO

: alias-test
: Test lead-up alias test (but not pass-through)
:
$* config.test=dir{units/} >>EOO
units
EOO

: alias-pass-test
: Test lead-up alias pass-through and test
:
$* config.test=units/ >>EOO
units/simple
units/script/foo
units/script/bar
units
EOO

: alias-pass-id-only
: Test lead-up alias pass-through (ids only)
:
$* config.test=bogus >>EOO
units/simple
EOO

: target-simple
:
$* config.test=units/simple/file{driver} >>EOO
units/simple
EOO

: target-script
:
$* config.test=dir{units/script/} >>EOO
units/script/foo
units/script/bar
EOO

: id
:
$* config.test=foo >>EOO
units/simple
units/script/foo
EOO

: target-id
:
$* config.test=dir{units/script/}@foo >>EOO
units/script/foo
EOO

: target-ids
:
$* 'config.test=dir{units/script/}@{foo bar}' >>EOO
units/script/foo
units/script/bar
EOO

: id-group
:
$* config.test=tests/@{basics/baz} >>EOO
tests/script/basics/baz/foo
tests/script/basics/baz/bar
EOO

: id-in-group
:
$* config.test=tests/@{basics/baz/bar} >>EOO
tests/script/basics/baz/bar
EOO

: syntax-1
:
{{
  test.arguments = $regex.apply($test.arguments, \
                                '^test\(.+\)$',  \
                                'test(../../proj-s1/@./)')

  : all
  :
  $* >>EOO
  tests/script/basics/foo
  tests/script/basics/bar
  tests/script/basics/baz/foo
  tests/script/basics/baz/bar
  units/simple
  units/script/foo
  units/script/bar
  units
  EOO

  : alias-pass
  : Test lead-up alias pass-through (but not test)
  :
  $* config.test=units/simple/file{driver} >>EOO
  units/simple
  EOO

  : alias-test
  : Test lead-up alias test (but not pass-through)
  :
  $* config.test=dir{units/} >>EOO
  units
  EOO

  : alias-pass-test
  : Test lead-up alias pass-through and test
  :
  $* config.test=units/ >>EOO
  units/simple
  units/script/foo
  units/script/bar
  units
  EOO

  : alias-pass-id-only
  : Test lead-up alias pass-through (ids only)
  :
  $* config.test=bogus >>EOO
  units/simple
  EOO

  : target-simple
  :
  $* config.test=units/simple/file{driver} >>EOO
  units/simple
  EOO

  : target-script
  :
  $* config.test=dir{units/script/} >>EOO
  units/script/foo
  units/script/bar
  EOO

  : id
  :
  $* config.test=foo >>EOO
  units/simple
  units/script/foo
  EOO

  : target-id
  :
  $* config.test=dir{units/script/}@foo >>EOO
  units/script/foo
  EOO

  : target-ids
  :
  $* 'config.test=dir{units/script/}@{foo bar}' >>EOO
  units/script/foo
  units/script/bar
  EOO

  : id-group
  :
  $* config.test=tests/@{basics/baz} >>EOO
  tests/script/basics/baz/foo
  tests/script/basics/baz/bar
  EOO

  : id-in-group
  :
  $* config.test=tests/@{basics/baz/bar} >>EOO
  tests/script/basics/baz/bar
  EOO
}}
