# file      : tests/rep-add.testscript
# license   : MIT; see accompanying LICENSE file

.include common.testscript config.testscript

rep_list += -d cfg

: location
:
{
  +$clone_cfg

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

  : empty
  :
  {
    $clone_cfg;

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

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

  : no-version
  :
  {
    $clone_cfg;

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

  : git-invalid-fragment
  :
  {
    $clone_cfg;

    $* 'git://example.org/repo#' 2>>EOE != 0
      error: invalid git repository location 'git://example.org/repo#': missing refname or commit id for git repository
      EOE
  }

  : git-scheme-pkg-type
  :
  {
    $clone_cfg;

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

  : git-pkg-types
  :
  {
    $clone_cfg;

    $* 'git+https://example.org/repo' --type pkg 2>>EOE != 0
      error: pkg repository type mismatch for location 'git+https://example.org/repo'
      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"

    $clone_cfg;

    $* "$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 git:example.org/repo#master
        EOE
    }

    : http-git
    :
    {
      $clone_cfg;

      $* 'http://example.org/repo.git#master' 2>>EOE
        added git:example.org/repo#master
        EOE
    }

    : http-pkg
    :
    {
      $clone_cfg;

      $* 'http://example.org/1/repo' 2>>EOE
        added pkg:example.org/repo
        EOE
    }

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

      $* 'repo' 2>>/~%EOE%
        %added git:.+/repo%
        EOE
    }

    : file-pkg
    :
    {
      $clone_cfg;

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

: relative-path
:
{
  $clone_cfg;

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

  $* ./1/../1/bar/stable 2>>/~%EOE%
    %unchanged pkg:.+/relative-path/bar/stable%
    EOE
}

: absolute-path
:
{
  $clone_cfg;

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

  $* $~/1/../1/foo/stable 2>>/~%EOE%
    %unchanged pkg:.+/absolute-path/foo/stable%
    EOE
}

: remote-url
:
{
  +$clone_cfg

  : pkg
  :
  {
    $clone_cfg;

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

    $* 'https://www.example.org/1/testing' 2>>EOE;
      updated pkg:example.org/testing
      EOE

    $rep_list >>EOO
      pkg:example.org/testing https://www.example.org/1/testing
      EOO
  }

  : git
  :
  {
    $clone_cfg;

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

    $* 'https://www.example.org/testing.git#master' 2>>EOE;
      updated git:example.org/testing#master
      EOE

    $rep_list >>EOO
      git:example.org/testing#master https://www.example.org/testing.git#master
      EOO
  }
}

: typed-url
:
{
  $clone_cfg;

  $* 'git+https://example.org/repo' 2>>EOE;
    added git:example.org/repo
    EOE

  $rep_list >>EOO
    git:example.org/repo git+https://example.org/repo
    EOO
}

: multiple-locations
:
{
  $clone_cfg;

  $* 'http://pkg.example.org/1/alpha' 'http://pkg.example.org/1/beta' 2>>EOE;
    added pkg:example.org/alpha
    added pkg:example.org/beta
    EOE

  $rep_list >>EOO
    pkg:example.org/alpha http://pkg.example.org/1/alpha
    pkg:example.org/beta http://pkg.example.org/1/beta
    EOO
}
