set -u

. $(dirname $0)/test_helper.sh


test_fails_gracefully_on_uneexisting_directory() {
  output=$(debci test --run-id 1 --backend null foo/bar 2>&1)
  assertFalse "[ -d $debci_data_basedir/autopkgtest-incoming/unstable/${debci_arch}/t/test/1 ]"
}

test_fails_gracefully_on_existing_directory_that_is_not_a_package() {
  mkdir -p $__tmpdir/test
  output=$(cd $__tmpdir && debci test --run-id 1 --backend null test/)
  assertEquals "" "$output"
}

test_package="${0%/*}/test-package-large-logs"
test_truncates_large_logs() {
  output="$__tmpdir/debci-test-output"
  (cd $test_package && debci test --output-directory="${output}" --backend null .) # >/dev/null 2>&1)
  log="${output}/log"
  assertTrue "[ -f ${log}.gz ]"
  gunzip "${log}.gz"
  size=$(stat --format=%s "${log}")
  upper_limit=$((21*1024*1024))
  assertTrue "log is smaller than 21MB" "[ $size -lt $upper_limit ]"
}

test_package_large_artifacts_dir="${0%/*}/test-package-large-artifacts"
test_limits_size_of_artifacts_directory() {
  output="$__tmpdir/debci-test-output"
  (cd $test_package_large_artifacts_dir && debci test --output-directory="${output}" --backend null .) # >/dev/null 2>&1)
  size=$(du -sm "${output}"/artifacts | awk '{print($1)}')
  assertTrue "there are artifacts" "[ $size -gt 1 ]"
  assertTrue "artifacts are less than 100MB" "[ $size -le 100 ]"
  n_files_removed=$(find ${output}/artifacts -name '*.removed-by-debci' | wc -l)
  assertTrue "some files were removed" "[ $n_files_removed -gt 0 ]"
  assertTrue "message left about file removal" "grep 'directory was too large.' ${output}/artifacts.removed.txt"
}

. shunit2
