# file      : tests/rep-fetch.test
# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
# license   : MIT; see accompanying LICENSE file

.include common.test auth.test config.test remote.test remote-git.test

# Source repository:
#
# rep-fetch
# |-- bar
# |   |-- stable                  -> ../foo/stable (prerequisite)
# |   |   |-- libbar-1.0.0.tar.gz -> libfoo >= 1.0.0
# |   |   `-- repositories
# |   |-- testing                 -> stable (complement),
# |   |   |                          ../foo/testing (prerequisite)
# |   |   |-- libbar-1.1.0.tar.gz -> libfoo >= 1.1.0
# |   |   `-- repositories
# |   `-- unstable                -> testing (complement),
# |       |                          ../foo/testing (prerequisite)
# |       |-- libbar-1.1.1.tar.gz  -> libfoo >= 1.1.0
# |       `-- repositories
# |
# |-- foo
# |   |-- stable
# |   |   |-- libfoo-1.0.0.tar.gz
# |   |   `-- repositories
# |   `-- testing                 -> stable (complement)
# |       |-- libfoo-1.1.0.tar.gz
# |       `-- repositories
# |
# `-- hello
# |   |-- libhello-1.0.0.tar.gz
# |   `-- repositories
# |
# `-- git/* (see rep-fetch-git.test)

# Prepare repositories used by tests if running in the local mode.
#
+if ($remote != true)
  rep_create += 2>!

  # Create the signed 'hello' repository.
  #
  cp -r $src/hello $out/hello
  cat <<<$cert_manifest >+$out/hello/repositories
  $rep_create --key $key $out/hello &$out/hello/packages &$out/hello/signature

  # Create 'foo/*' repositories.
  #
  cp -r $src/foo $out/foo
  $rep_create $out/foo/stable  &$out/foo/stable/packages
  $rep_create $out/foo/testing &$out/foo/testing/packages

  # Create 'bar/*' repositories.
  #
  cp -r $src/bar $out/bar
  $rep_create $out/bar/stable   &$out/bar/stable/packages
  $rep_create $out/bar/testing  &$out/bar/testing/packages
  $rep_create $out/bar/unstable &$out/bar/unstable/packages

  # Create git repositories.
  #
  $git_extract $src/git/state0/libfoo.tar
  $git_extract $src/git/state0/libbar.tar
  $git_extract $src/git/state0/style.tar
  $git_extract $src/git/state0/style-basic.tar &$out_git/state0/***

  $git_extract $src/git/state1/libfoo.tar
  $git_extract $src/git/state1/libbaz.tar
  $git_extract $src/git/state1/style.tar
  $git_extract $src/git/state1/style-basic.tar &$out_git/state1/***
end

: no-repos
:
$clone_cfg;
$* 2>>/EOE != 0
  error: configuration cfg/ has no repositories
    info: use 'bpkg rep-add' to add a repository
  EOE

: bpkg-repos
:
{
  test.options += --auth all

  rep_add += -d cfg 2>!

  : hello
  :
  {
    $clone_root_cfg && $rep_add $rep/hello;

    $* --trust $cert_fp 2>>EOE &cfg/.bpkg/certificates/**;
      fetching bpkg:build2.org/rep-fetch/hello
      1 package(s) in 1 repository(s)
      EOE

    $* 2>>EOE
      fetching bpkg:build2.org/rep-fetch/hello
      1 package(s) in 1 repository(s)
      EOE
  }

  : bar-unstable
  :
  {
    $clone_root_cfg && $rep_add $rep/bar/unstable;

    $* --trust-yes 2>>EOE;
      fetching bpkg:build2.org/rep-fetch/bar/unstable
      fetching bpkg:build2.org/rep-fetch/foo/testing (prerequisite of bpkg:build2.org/rep-fetch/bar/unstable)
      fetching bpkg:build2.org/rep-fetch/foo/stable (complements bpkg:build2.org/rep-fetch/foo/testing)
      fetching bpkg:build2.org/rep-fetch/bar/testing (complements bpkg:build2.org/rep-fetch/bar/unstable)
      fetching bpkg:build2.org/rep-fetch/bar/stable (complements bpkg:build2.org/rep-fetch/bar/testing)
      5 package(s) in 5 repository(s)
      EOE

    $* 2>>EOE
      fetching bpkg:build2.org/rep-fetch/bar/unstable
      fetching bpkg:build2.org/rep-fetch/foo/testing (prerequisite of bpkg:build2.org/rep-fetch/bar/unstable)
      fetching bpkg:build2.org/rep-fetch/foo/stable (complements bpkg:build2.org/rep-fetch/foo/testing)
      fetching bpkg:build2.org/rep-fetch/bar/testing (complements bpkg:build2.org/rep-fetch/bar/unstable)
      fetching bpkg:build2.org/rep-fetch/bar/stable (complements bpkg:build2.org/rep-fetch/bar/testing)
      5 package(s) in 5 repository(s)
      EOE
  }

  : both
  :
  {
    $clone_root_cfg && $rep_add $rep/hello && $rep_add $rep/bar/unstable;

    $* --trust-yes 2>>EOE &cfg/.bpkg/certificates/**;
      fetching bpkg:build2.org/rep-fetch/bar/unstable
      fetching bpkg:build2.org/rep-fetch/foo/testing (prerequisite of bpkg:build2.org/rep-fetch/bar/unstable)
      fetching bpkg:build2.org/rep-fetch/foo/stable (complements bpkg:build2.org/rep-fetch/foo/testing)
      fetching bpkg:build2.org/rep-fetch/bar/testing (complements bpkg:build2.org/rep-fetch/bar/unstable)
      fetching bpkg:build2.org/rep-fetch/bar/stable (complements bpkg:build2.org/rep-fetch/bar/testing)
      fetching bpkg:build2.org/rep-fetch/hello
      6 package(s) in 6 repository(s)
      EOE

    $* 2>>EOE
      fetching bpkg:build2.org/rep-fetch/bar/unstable
      fetching bpkg:build2.org/rep-fetch/foo/testing (prerequisite of bpkg:build2.org/rep-fetch/bar/unstable)
      fetching bpkg:build2.org/rep-fetch/foo/stable (complements bpkg:build2.org/rep-fetch/foo/testing)
      fetching bpkg:build2.org/rep-fetch/bar/testing (complements bpkg:build2.org/rep-fetch/bar/unstable)
      fetching bpkg:build2.org/rep-fetch/bar/stable (complements bpkg:build2.org/rep-fetch/bar/testing)
      fetching bpkg:build2.org/rep-fetch/hello
      6 package(s) in 6 repository(s)
      EOE
  }
}

: git-repos
:
if ($git_supported != true)
{
  # Skip git repository tests.
  #
}
elif ($remote != true)
{
  git_protocol = 'local'
  .include rep-fetch-git.test
}
else
{
  : https-dumb
  :
  {
    git_protocol = 'https-dumb'
    .include rep-fetch-git.test
  }

  : https-smart
  :
  {
    git_protocol = 'https-smart'
    .include rep-fetch-git.test
  }

  : https-smart-unadv
  :
  {
    git_protocol = 'https-smart-unadv'
    .include rep-fetch-git.test
  }

  : git
  :
  {
    git_protocol = 'git'
    .include rep-fetch-git.test
  }
}