#!/usr/bin/env bash
set -xeuo pipefail

source mapping/artifact/tools/test_utilities.sh

wrapper_tool=mapping/artifact/tools/layer

function setup() {
  make_test_case_environment "$wrapper_tool"
}

function test_no_args() {
  (
    builtin cd "$(setup)"

    assert_failure \
      2 "usage: layer\.py .*layer\.py: error: the following arguments are required: subcommand" \
      "$wrapper_tool"
  )
}

function test_unknown_verb() {
  (
    builtin cd "$(setup)"

    assert_failure \
      2 "usage: layer\.py .*layer\.py: error: argument subcommand: invalid choice: 'unknown_verb'" \
      "$wrapper_tool" unknown_verb
  )
}

function test_help() {
  (
    builtin cd "$(setup)"

    [[ $("$wrapper_tool" -h) == "usage: layer.py"* ]]
    [[ $("$wrapper_tool" --help) == "usage: layer.py"* ]]
  )
}

function test_hash_verb() {
  local wrapped_tool=mapping/artifact/tools/layer_tools/hash
  (
    builtin cd "$(setup)"
    make_echo_script "$wrapped_tool"

    assert_failure \
      2 "usage: layer\.py hash .*layer\.py hash: error: the following arguments are required: path" \
      "$wrapper_tool" hash

    [ "$("$wrapper_tool" hash arg1)" == ""$wrapped_tool" -path=arg1" ]

    assert_failure \
      2 "usage: layer\.py .*layer\.py: error: unrecognized arguments: arg2" \
      "$wrapper_tool" hash arg1 arg2
  )
}

function test_info_verb() {
  local wrapped_tool=mapping/artifact/tools/layer_tools/info
  (
    builtin cd "$(setup)"
    make_echo_script "$wrapped_tool"

    assert_failure \
      2 "usage: layer\.py info .*layer\.py info: error: the following arguments are required: path" \
      "$wrapper_tool" info

    [ "$("$wrapper_tool" info arg1)" == ""$wrapped_tool" -path=arg1" ]

    assert_failure \
      2 "usage: layer\.py .*layer\.py: error: unrecognized arguments: arg2" \
      "$wrapper_tool" info arg1 arg2
  )
}

function main() {
  test_no_args
  test_unknown_verb
  test_help
  test_hash_verb
  test_info_verb
}

main
