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

.include common.testscript project.testscript

config_cxx = [cmdline] cc config.cxx=$quote($recall($cxx.path) $cxx.config.mode, true)

new    += 2>!
init   += $config_cxx -d prj 2>!
sync   += -d prj 2>!
deinit += -d prj
config += -d prj 2>!

: no-cfg
:
{
  : default
  :
  {
    $clone_root_prj;

    $* 2>>/"EOE" != 0;
      error: no default configuration in project $~/prj/
        info: use \(@<cfg-name> | --config|-c <cfg-dir> | --all|-a\) to specify configuration explicitly
      EOE

    $* --stdout-format 'json' 2>>/"EOE" != 0
      error: no default configuration in project $~/prj/
        info: use \(@<cfg-name> | --config|-c <cfg-dir> | --all|-a\) to specify configuration explicitly
      EOE
  }

  : all
  :
  {
    $clone_root_prj;

    $* -a 2>>EOE != 0;
      error: no existing configurations
      EOE

    $* -a --stdout-format 'json' >'[]'
  }
}

: single-cfg
:
{
  : single-project
  :
  {
    $clone_root_prj;

    $init -C @cfg &prj-cfg/***;

    $* >'prj configured 0.1.0-a.0.19700101000000';

    $* --stdout-format 'json' >>~%EOO%;
      [
        {
          "configuration": {
            "id": 1,
      %      "path": ".+prj-cfg",%
            "name": "cfg"
          },
          "packages": [
        {
      %.+
        }
      ]
        }
      ]
      EOO

    $deinit 2>>/"EOE"
      deinitializing in project $~/prj/
      synchronizing:
        drop prj
      EOE
  }

  : no-projects
  :
  {
    $new -t empty prj &prj/***;

    $config create @cfg -- &prj-cfg/***;

    $* 2>>~%EOE% != 0;
      %error: no packages in project .+%
      EOE

    $* --stdout-format 'json' >>~%EOO%;
    [
      {
        "configuration": {
          "id": 1,
    %      "path": ".+prj-cfg",%
          "name": "cfg"
        }
      }
    ]
    EOO

    $* libbar 2>>EOE;
      info: no packages initialized in configuration @cfg, skipping
      EOE

    $* --stdout-format 'json' libbar >>~%EOO%
    [
      {
        "configuration": {
          "id": 1,
    %      "path": ".+prj-cfg",%
          "name": "cfg"
        }
      }
    ]
    EOO
  }
}

: multi-cfg
:
{
  $clone_prj;

  $init -C @cfg1 &prj-cfg1/***;
  $init -C @cfg2 &prj-cfg2/***;

  $* @cfg2 >'prj configured 0.1.0-a.0.19700101000000';

  $* --all >>EOO;
    in configuration @cfg1:
    prj configured 0.1.0-a.0.19700101000000

    in configuration @cfg2:
    prj configured 0.1.0-a.0.19700101000000
    EOO

  $* --all --stdout-format 'json' >>~%EOO%;
    [
      {
        "configuration": {
          "id": 1,
    %      "path": ".+prj-cfg1",%
          "name": "cfg1"
        },
        "packages": [
      {
    %.+
      }
    ]
      },
      {
        "configuration": {
          "id": 2,
    %      "path": ".+prj-cfg2",%
          "name": "cfg2"
        },
        "packages": [
      {
    %.+
      }
    ]
      }
    ]
    EOO

  $deinit 2>>/"EOE"
    deinitializing in project $~/prj/
    synchronizing:
      drop prj
    EOE
}

: multi-prj
:
{
  $new -t empty prj &prj/***;
  $new -d prj --package pkg1;
  $new -d prj --package pkg2;

  $init -d prj/pkg1 -C @cfg &prj-cfg/***;

  # Initialized package is specified.
  #
  $* -d prj/pkg1 >>EOE;
    pkg1 configured 0.1.0-a.0.19700101000000
    EOE

  $* -d prj/pkg1 --stdout-format 'json' >>~%EOO%;
    [
      {
        "configuration": {
          "id": 1,
    %      "path": ".+prj-cfg",%
          "name": "cfg"
        },
        "packages": [
      {
        "name": "pkg1",
        "status": "configured",
        "version": "0.1.0-a.0.19700101000000",
        "hold_package": true,
        "hold_version": true
      }
    ]
      }
    ]
    EOO

  # Uninitialized package is specified.
  #
  $* -d prj/pkg2 2>>EOE != 0;
    error: package pkg2 is not initialized in any default configuration(s)
    EOE

  $* -d prj/pkg2 --stdout-format 'json' >>~%EOO%;
    [
      {
        "configuration": {
          "id": 1,
    %      "path": ".+prj-cfg",%
          "name": "cfg"
        },
        "packages": [
      {
        "name": "pkg2",
        "status": "uninitialized"
      }
    ]
      }
    ]
    EOO

  # No package is specified.
  #
  $* >>EOE;
    pkg1 configured 0.1.0-a.0.19700101000000
    EOE

  $* --stdout-format 'json' >>~%EOO%
    [
      {
        "configuration": {
          "id": 1,
    %      "path": ".+prj-cfg",%
          "name": "cfg"
        },
        "packages": [
      {
        "name": "pkg1",
        "status": "configured",
        "version": "0.1.0-a.0.19700101000000",
        "hold_package": true,
        "hold_version": true
      },
      {
        "name": "pkg2",
        "status": "uninitialized"
      }
    ]
      }
    ]
    EOO
}

: dependency
:
{
  $clone_prj;

  $init -C @cfg &prj-cfg/***;

  $new -t lib libprj &libprj/***;

  cat <<EOI >+prj/repositories.manifest;
    :
    role: prerequisite
    location: ../libprj
    type: dir
    EOI

  cat <<EOI >+prj/manifest;
    depends: libprj
    EOI

  $* --recursive >>EOO 2>>/~%EOE%; # Note: implicitly fetches in cfg.
    prj configured 0.1.0-a.0.19700101000000 available 0.1.0-a.0.19700101000000#1
    EOO
    %fetching dir:.+/libprj \(prerequisite of dir:.+/prj\)%
    EOE

  $sync;

  $* --recursive >>~%EOO%;
    prj configured 0.1.0-a.0.19700101000000#1
    %  libprj configured 0.+%
    EOO

  $* --recursive --stdout-format 'json' >>~%EOO%;
    [
      {
        "configuration": {
          "id": 1,
    %      "path": ".+prj-cfg",%
          "name": "cfg"
        },
        "packages": [
      {
    %.+
      }
    ]
      }
    ]
    EOO

  $deinit 2>>/"EOE"
    deinitializing in project $~/prj/
    synchronizing:
      drop prj
      drop libprj
    EOE
}
