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

.include common.test config.test

: location
:
{
  +$clone_cfg

  : none
  :
  $* 2>>EOE != 0
  error: repository location argument expected
    info: run 'bpkg help rep-add' for more information
  EOE

  : empty
  :
  $* '' 2>>EOE != 0
  error: empty repository location
  EOE

  : unknown-type
  :
  $* 'repo' --type unknown 2>>EOE != 0
  error: invalid value 'unknown' for option '--type'
  EOE

  : no-version
  :
  $* 'stable' 2>>/~%EOE% != 0
  %error: invalid bpkg repository location '.+/no-version/stable': missing repository version%
    info: consider using --type to specify repository type
  EOE

  : git-no-branch
  :
  $* 'git://example.org/repo' 2>>EOE != 0
  error: invalid git repository location 'git://example.org/repo': missing branch/tag or commit id for git repository
  EOE

  : bpkg-git-scheme
  :
  $* 'git://example.org/repo' --type bpkg 2>>EOE != 0
  error: invalid bpkg repository location 'git://example.org/repo': unsupported scheme for bpkg repository
  EOE

  : invalid-path
  :
  {
    s="../../../../../../../../../../../../../../../../../../../../../../../"
    s="$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s"
    $* "$s" 2>>~%EOE% != 0
    %error: invalid repository path '.+/': invalid filesystem path%
    EOE
  }

  : type-detection
  :
  {
    +$clone_cfg

    : git-scheme
    :
    $clone_cfg;
    $* 'git://example.org/repo#master' 2>>EOE
      added repository git:example.org/repo
      EOE

    : http-git
    :
    $clone_cfg;
    $* 'http://example.org/repo.git#master' 2>>EOE
      added repository git:example.org/repo
      EOE

    : http-bpkg
    :
    $clone_cfg;
    $* 'http://example.org/1/repo' 2>>EOE
      added repository bpkg:example.org/repo
      EOE

    : file-git
    :
    $clone_cfg && mkdir -p repo/.git;

    $* 'repo' 2>>~%EOE% != 0
      %error: invalid git repository location '.+repo': missing branch/tag or commit id for git repository%
      EOE

    : file-bpkg
    :
    $clone_cfg;

    $* '1/repo' 2>>/~%EOE%
      %added repository .+/repo%
      EOE
  }
}

: relative-path
:
{
  $clone_cfg;

  $* ./1/bar/stable 2>>/~%EOE%;
    %added repository bpkg:.+/relative-path/bar/stable%
    EOE

  $* ./1/../1/bar/stable 2>>/~%EOE% != 0
    %error: bpkg:.+/relative-path/bar/stable is already a repository of this configuration%
    EOE
}

: absolute-path
:
{
  $clone_cfg;

  $* $~/1/foo/stable 2>>/~%EOE%;
    %added repository bpkg:.+/absolute-path/foo/stable%
    EOE

  $* $~/1/../1/foo/stable 2>>/~%EOE% != 0
    %error: bpkg:.+/absolute-path/foo/stable is already a repository of this configuration%
    EOE
}

: remote-url
:
{
  +$clone_cfg

  : bpkg
  :
  $clone_cfg;

  $* 'http://pkg.example.org/1/testing' 2>>~%EOE%;
    %added repository bpkg:example.org/testing%
    EOE

  $* 'https://www.example.org/1/testing' 2>>~%EOE% != 0
    %error: bpkg:example.org/testing is already a repository of this configuration%
    EOE

  : git
  :
  $clone_cfg;

  $* 'git://example.org/testing#master' 2>>~%EOE%;
    %added repository git:example.org/testing%
    EOE

  $* 'git://www.example.org/testing#master' 2>>~%EOE% != 0
    %error: git:example.org/testing is already a repository of this configuration%
    EOE
}