#!/usr/bin/env bash-shlib
# -*- mode: shell-script -*-

include shunit

depends sed grep git mkdir readlink

export -f matches
export grep

tmp=/tmp
tprog="../bin/compose-core"
tprog=$(readlink -f $tprog)


export PATH=".:$PATH"
short_tprog=$(basename "$tprog")


##
## Convenience function
##

init_test() {
    test_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
    cd "$test_tmpdir"
    export CACHEDIR="$test_tmpdir/.cache"
    export VARDIR="$test_tmpdir/.var"
    export COMPOSE_DISABLE_DOCKER_COMPOSE_STORE="1"
    mkdir -p "$CACHEDIR"
}



tear_test() {
    rm -rf "$test_tmpdir"
}


##
## Tests
##


function test_compose_run_args {

    init_test

    export CHARM_STORE=$test_tmpdir
    mkdir -p  $test_tmpdir/{www,mysql}
    cat <<EOF2 > $test_tmpdir/www/metadata.yml
EOF2

    cat <<EOF2 > $test_tmpdir/mysql/metadata.yml
EOF2

    cat <<EOF2 > $test_tmpdir/compose.yml
web_site:
  charm: www
EOF2

    export DISABLE_SYSTEM_CONFIG_FILE=true

    assert_list <<EOF

### Testing args passing to docker-compose

## -- simple action

cd "$test_tmpdir"
out=\$("$tprog" --dry-compose-run run web_site 2>&1 >/dev/null )
expected="docker-compose run web_site"

[ "\$out" == "\$expected" ] || {
    echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
    exit 1
}


## -- simple single dash arg

cd "$test_tmpdir"

out=\$("$tprog" --dry-compose-run run -T web_site 2>&1 >/dev/null )
expected="docker-compose run -T web_site"

[ "\$out" == "\$expected" ] || {
    echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
    exit 1
}


## -- desaggregation of combined single char args

cd "$test_tmpdir"

out=\$("$tprog" --dry-compose-run logs -ft --tail 20 web_site 2>&1 >/dev/null)
expected="docker-compose logs -f -t --tail 20 web_site"

[ "\$out" == "\$expected" ] || {
    echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
    exit 1
}


## -- desaggregation of combined single char option and valued option char

cd "$test_tmpdir"

out=\$("$tprog" --dry-compose-run run -Tv x:y web_site 2>&1 >/dev/null)
expected="docker-compose run -T -v x:y web_site"

[ "\$out" == "\$expected" ] || {
    echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
    exit 1
}


## -- simple unexpected single dash arg

cd "$test_tmpdir"

out=\$("$tprog" --dry-compose-run run -Z web_site 2>&1)
expected_reg="Unknown option '-Z'"

[[ "\$out" =~ \$expected_reg ]] || {
    echo -e "Can't find '\$expected_reg' in out:\n\$out"
    exit 1
}

## -- simple unexpected single dash arg after expected one

cd "$test_tmpdir"

out=\$("$tprog" --dry-compose-run run -T -Z web_site 2>&1)
expected_reg="Unknown option '-Z'"

[[ "\$out" =~ \$expected_reg ]] || {
    echo -e "Can't find '\$expected_reg' in out:\n\$out"
    exit 1
}

## -- simple unexpected single dash arg after expected aggregated one

cd "$test_tmpdir"

out=\$("$tprog" --dry-compose-run run -TZ web_site 2>&1)
expected_reg="Unknown option '-Z'"

[[ "\$out" =~ \$expected_reg ]] || {
    echo -e "Can't find '\$expected_reg' in out:\n\$out"
    exit 1
}


## -- multiple services

cd "$test_tmpdir"

out=\$("$tprog" --dry-compose-run logs --tail 15 web_site mysql 2>&1 >/dev/null)
expected="docker-compose logs --tail 15 web_site mysql"

[ "\$out" == "\$expected" ] || {
    echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
    exit 1
}

## -- single services

cd "$test_tmpdir"

out=\$("$tprog" --dry-compose-run run web_site mysql 2>&1 >/dev/null)
expected="docker-compose run web_site mysql"

[ "\$out" == "\$expected" ] || {
    echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
    exit 1
}


EOF

    tear_test
}


function test_filter_opts {

    src=$(cat <<'EOF'
-d, --detach
--name NAME
--entrypoint CMD
-e KEY=VAL
-l, --label KEY=VAL
-u, --user=""
--no-deps
--rm
-p, --publish=[]
--service-ports
--use-aliases
-v, --volume=[]
-T
-w, --workdir=""
EOF
       )

    export src

    assert_list <<EOF

### Testing filtering opts

## -- multi_opts_filter should find opts with args

. "$tprog"
out=\$(echo "\$src" | multi_opts_filter |  tr " " "\n") || exit 12
expected="--name
--entrypoint
-e
-l
--label
-u
--user
-p
--publish
-v
--volume
-w
--workdir"

[ "\$out" == "\$expected" ] || {
     echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
     exit 1
}


## -- single_opts_filter should find opts with args

. "$tprog"
out=\$(echo "\$src" | single_opts_filter |  tr " " "\n") || exit 12
expected="-d
--detach
--no-deps
--rm
--service-ports
--use-aliases
-T"

[ "\$out" == "\$expected" ] || {
     echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
     exit 1
}


EOF

}

continue_on_error="0" testbench $*