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

# Commonly-used variables setup and driver command line.
#

# Use the same build system driver as the one running the tests (as opposed
# to one that may or may not be found via PATH). Note that this implies that
# we don't support cross-testing.
#
# A common approach will be to run build2 as a sanity check in a directory
# produced or updated by a command being tested.
#
build = $recall($build.path)

# Disable loading the user's default options files (that may affect the test
# commands execution) for bdep, bpkg, and build2.
#
# Note that this works without quoting on Windows because of the effective
# escaping during the command line re-parse.
#
options_guard = $~/.build2
+mkdir $options_guard

+echo '--no-default-options' >=$options_guard/b.options
+echo '--no-default-options' >=$options_guard/bpkg.options
+echo '--no-default-options' >=$options_guard/bdep.options

test.options += --default-options $options_guard \
--build $build --build-option "--default-options=$options_guard" \
--bpkg-option "--default-options=$options_guard" \
--bpkg-option "--build-option=--default-options=$options_guard"

build = [cmdline] $build --default-options $options_guard

+cat <<"EOI" >=$options_guard/bdep-sync-implicit.options
--build-option "--default-options=$options_guard"
--bpkg-option "--default-options=$options_guard"
--bpkg-option "--build-option=--default-options=$options_guard"
EOI

# Check that git version is the minimum supported one or above. The lowest
# common denominator for bdep commands is 2.1.0.
#
+git --version | set git_version_out

+echo "$git_version_out" | sed -n -e 's/git version (\d+\.\d+\.\d+).*/\1/p' | \
 set git_version

+if ("$git_version" == "")
  exit "unable to obtain git version from '$git_version_out'"
end

+echo "$git_version" | sed -e 's/(\d+).*/\1/'      | set git_version_major
+echo "$git_version" | sed -e 's/\d+\.(\d+).*/\1/' | set git_version_minor

+if! ($git_version_major >  2 || \
      $git_version_major == 2 && $git_version_minor >= 1)
  exit "minimum supported git version is 2.1.0"
end

# Helper commands that can be used by tests to prepare the testing environment
# or validate an outcome of the command being tested. They are likely to get
# additional options and redirects appended prior to use. A common approach
# will be to redirect output to the null device for commands that are used for
# test setup, and to match it for commands being tested or performing teardown
# (for example, to make sure that configuration post-test state is valid and is
# as expected).
#
clean   = [cmdline] $* clean
deinit  = [cmdline] $* deinit
init    = [cmdline] $* init
fetch   = [cmdline] $* fetch
new     = [cmdline] $* new --no-checks
status  = [cmdline] $* status
sync    = [cmdline] $* sync
update  = [cmdline] $* update
config  = [cmdline] $* config
publish = [cmdline] $* publish
release = [cmdline] $* release

# All testscripts are named after bdep commands, for example sync.testscript.
# So the testscript scope id is a name of the command being tested.
#
cmd = [string] $@
test.arguments = $cmd
