# file      : tests/cfg-link.testscript
# license   : MIT; see accompanying LICENSE file

.include common.testscript

cfg_create += 2>!
cfg_info   += --link

uuid1 = '18f48b4b-b5d9-4712-b98c-1930df1c4228'
uuid2 = '28f48b4b-b5d9-4712-b98c-1930df1c4228'
uuid3 = '38f48b4b-b5d9-4712-b98c-1930df1c4228'

+$cfg_create -d cfg1 --name 'main'   --uuid "$uuid1" &cfg1/***
+$cfg_create -d cfg2 --name 'shared' --uuid "$uuid2" &cfg2/***

clone_cfgs = cp -r ../cfg1 ../cfg2 ./

sp = ' '

: self
:
{
  $cfg_create -d cfg1 --uuid "$uuid1" &cfg1/***;

  $* -d cfg1 cfg1 2>>/"EOE" != 0
    error: linking configuration $~/cfg1/ with itself
      info: uuid: $uuid1
    EOE
}

: same-name
:
{
  $cfg_create -d cfg1 --name 'main'   &cfg1/***;
  $cfg_create -d cfg2 --name 'shared' &cfg2/***;

  $* -d cfg1 cfg2 --name 'main' 2>>/"EOE" != 0
    error: linking configuration $~/cfg2/ using current configuration name 'main'
      info: consider specifying alternative name with --name
    EOE
}

: basic
:
{
  $clone_cfgs;

  $* -d cfg1 cfg2 2>>/"EOE";
    linked with configuration $~/cfg2/
      uuid: $uuid2
      type: target
      name: shared
      id:   1
    EOE

  $cfg_info -d cfg1 >>/"EOO";
    path: $~/cfg1/
    uuid: $uuid1
    type: target
    name: main

    path: $~/cfg2/
    uuid: $uuid2
    type: target
    name: shared
    EOO

  $cfg_info -d cfg2 --backlink >>/"EOO";
    path: $~/cfg2/
    uuid: $uuid2
    type: target
    name: shared

    path: $~/cfg1/
    uuid: $uuid1
    type: target
    name: main
    EOO

  # While at it, test that an implicit link is unnamed.
  #
  mv cfg1 cfg1.tmp;

  $cfg_info -d cfg2 --dangling >>/"EOO"
    path: $~/cfg2/
    uuid: $uuid2
    type: target
    name: shared

    path: $~/cfg1/
    uuid: $uuid1
    type: target
    name:$sp
    EOO
}

: implicit-relink
:
{
  $clone_cfgs;

  $* -d cfg1 cfg2 2>!;

  rm -r cfg1/;
  $cfg_create -d cfg1 --name 'foo' --uuid "$uuid1";

  $* -d cfg1 cfg2 2>>/"EOE";
    warning: current configuration $~/cfg1/ is already implicitly linked with $~/cfg2/
    linked with configuration $~/cfg2/
      uuid: $uuid2
      type: target
      name: shared
      id:   1
    EOE

  $cfg_info -d cfg1 >>/"EOO"
    path: $~/cfg1/
    uuid: $uuid1
    type: target
    name: foo

    path: $~/cfg2/
    uuid: $uuid2
    type: target
    name: shared
    EOO
}

: turn-implicit-explicit
:
{
  $clone_cfgs;

  $* -d cfg1 cfg2 2>!;

  $* -d cfg2 cfg1 2>>/"EOE";
    linked with configuration $~/cfg1/
      uuid: $uuid1
      type: target
      name: main
      id:   1
    EOE

  $cfg_info -d cfg2 >>/"EOO";
    path: $~/cfg2/
    uuid: $uuid2
    type: target
    name: shared

    path: $~/cfg1/
    uuid: $uuid1
    type: target
    name: main
    EOO

  $cfg_info -d cfg1 >>/"EOO";
    path: $~/cfg1/
    uuid: $uuid1
    type: target
    name: main

    path: $~/cfg2/
    uuid: $uuid2
    type: target
    name: shared
    EOO

  # While at it, test that relink attempt is reported.
  #
  $* -d cfg2 cfg1 2>>/"EOE" != 0
    error: configuration with uuid $uuid1 is already linked as ../cfg1/
    EOE
}

: uuid-clash
:
{
  $clone_cfgs;

  $* -d cfg1 cfg2 2>!;

  $* -d cfg1 cfg2 2>>/"EOE" != 0
    error: configuration with uuid $uuid2 is already linked as ../cfg2/
    EOE
}

: path-clash
:
{
  $clone_cfgs;

  $* -d cfg1 cfg2 2>!;

  mv cfg2 cfg2.tmp;
  $cfg_create -d cfg2 --name 'shared' --uuid "$uuid3" &cfg2/***;

  $* -d cfg1 cfg2 2>>/"EOE" != 0
    error: configuration with path $~/cfg2/ is already linked
    EOE
}

: name-clash
:
{
  $clone_cfgs;

  $* -d cfg1 cfg2 2>!;

  $cfg_create -d cfg3 --name 'shared' --uuid "$uuid3" &cfg3/***;

  $* -d cfg1 cfg3 --name 'shared' 2>>/"EOE" != 0;
    error: configuration with name shared is already linked as ../cfg2/
      info: consider specifying alternative name with --name
    EOE

  $* -d cfg1 cfg3 2>>/"EOE"
    warning: configuration with name shared is already linked as ../cfg2/, linking as unnamed
    linked with configuration $~/cfg3/
      uuid: $uuid3
      type: target
      id:   2
    EOE
}

: implicit-link-path-clash
:
{
  $clone_cfgs;

  $* -d cfg1 cfg2 2>!;
  $* -d cfg2 cfg1 2>!;

  mv cfg1 cfg1.tmp;
  $cfg_create -d cfg1 --name 'main' --uuid "$uuid3" &cfg1/***;

  $* -d cfg1 cfg2 2>>/"EOE" != 0
    error: current configuration $~/cfg1/ is already linked with $~/cfg2/
    EOE
}

: uuid-mismatch
:
{
  $clone_cfgs;

  $* -d cfg1 cfg2 2>!;

  mv cfg2 cfg2.tmp;

  $cfg_create -d cfg2 --name 'shared' --uuid "$uuid3";

  $cfg_info -d cfg1 >- 2>>/"EOE" != 0;
    error: configuration cfg2/ uuid mismatch
      info: uuid $uuid3
      info: linked with cfg1/ as $uuid2
    EOE

  rm -r cfg2;
  mv cfg2.tmp cfg2;

  $cfg_info -d cfg1 >>/"EOO"
    path: $~/cfg1/
    uuid: $uuid1
    type: target
    name: main

    path: $~/cfg2/
    uuid: $uuid2
    type: target
    name: shared
    EOO
}

: type-mismatch
:
{
  $clone_cfgs;

  $* -d cfg1 cfg2 2>!;

  rm -r cfg2;

  $cfg_create -d cfg2 --name 'shared' --uuid "$uuid2" --type 'shared';

  $cfg_info -d cfg1 >- 2>>/"EOE" != 0
    error: configuration cfg2/ type mismatch
      info: type shared
      info: linked with cfg1/ as target
    EOE
}