summaryrefslogtreecommitdiffstats
path: root/fluent-bit/tests/lib/shunit2/shunit2_args_test.sh
blob: fc252232b02c11ce09f57653a35361c0ac50ad07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/sh
#
# shunit2 unit test for running subset(s) of tests based upon command line arguments.
# Also shows how non-default tests or a arbitrary subset of tests can be run.
#
# Disable source following.
#   shellcheck disable=SC1090,SC1091

# Load test helpers.
. ./shunit2_test_helpers

# This test does not nomrally run because it does not begin "test*"
# Will be run by settting the arguments to the script to include the name of this test.
non_default_test() {
  # arbitrary assert
  assertTrue 0
  # true intent is to set this variable, which will be tested below
  NON_DEFAULT_TEST_RAN="yup, we ran"
}

# Test that the "non_default_test" ran, otherwise fail
test_non_default_ran() {
  assertNotNull "'non_default_test' did not run" "$NON_DEFAULT_TEST_RAN"
}

# fail if this test runs, which is shouldn't if args are set correctly.
test_will_fail() {
  fail "test_will_fail should not be run if arg-parsing works"
}

oneTimeSetUp() {
  th_oneTimeSetUp
  # prime with "null" value
  NON_DEFAULT_TEST_RAN=""
}

# Load and run shunit2.
# shellcheck disable=SC2034
[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0

# If zero/one argument(s) are provided, this test is being run in it's
# entirety, and therefore we want to set the arguments to the script
# to (simulate and) test the processing of command-line specified
# tests.  If we don't, then the "test_will_fail" test will run (by
# default) and the overall test will fail.
#
# However, if two or more arguments are provided, then assume this
# test script is being run by hand to experiment with command-line
# test specification, and then don't override the user provided
# arguments.
if command [ "$#" -le 1 ]; then
  # We set the arguments in a POSIX way, inasmuch as we can;
  # helpful tip:
  #   https://unix.stackexchange.com/questions/258512/how-to-remove-a-positional-parameter-from
  set -- "--" "non_default_test" "test_non_default_ran"
fi

# Load and run tests, but only if running as a script, not if being sourced by shunit2
command [ -z "${SHUNIT_VERSION:-}" ] && . "${TH_SHUNIT}"