xref: /aosp_15_r20/external/shflags/shflags_issue_57.sh (revision 63d4e48fb639f6414be0db9d718e3be2667e4fed)
1#! /bin/sh
2# vim:et:ft=sh:sts=2:sw=2
3#
4# shFlags unit test for Issue #57.
5# https://github.com/kward/shflags/issues/57
6#
7# Copyright 2023 Kate Ward. All Rights Reserved.
8# Released under the Apache 2.0 license.
9#
10# Author: [email protected] (Kate Ward)
11# https://github.com/kward/shflags
12#
13### ShellCheck (http://www.shellcheck.net/)
14# Disable source following.
15#   shellcheck disable=SC1090,SC1091
16# $() are not fully portable (POSIX != portable).
17#   shellcheck disable=SC2006
18
19# These variables will be overridden by the test helpers.
20returnF="${TMPDIR:-/tmp}/return"
21stdoutF="${TMPDIR:-/tmp}/STDOUT"
22stderrF="${TMPDIR:-/tmp}/STDERR"
23
24# Load test helpers.
25. ./shflags_test_helpers
26
27# Test proper functionality with 'set -o pipefail' enabled.
28testIssue57() {
29  # shellcheck disable=SC3040
30  set -o pipefail
31
32  th_clearReturn
33  (
34    FLAGS -h >"${stdoutF}" 2>"${stderrF}"
35    echo $? >"${returnF}"
36  )
37
38  assertFalse \
39      'short help request should have returned a false exit code.' \
40      "$(th_queryReturn)"
41  ( grep 'show this help' "${stderrF}" >/dev/null )
42  r3turn=$?
43  assertTrue \
44      'short request for help should have produced some help output.' \
45      ${r3turn}
46  [ ${r3turn} -eq "${FLAGS_TRUE}" ] || th_showOutput
47}
48
49oneTimeSetUp() {
50  th_oneTimeSetUp
51
52  if flags_getoptIsStd; then
53    th_warn 'Standard version of getopt found. Enhanced tests will be skipped.'
54    return
55  fi
56  th_warn 'Enhanced version of getopt found. Standard tests will be skipped.'
57}
58
59setUp() {
60  flags_reset
61}
62
63# Load and run shUnit2.
64# shellcheck disable=SC2034
65[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
66. "${TH_SHUNIT}"
67