#! /bin/sh # vim:et:ft=sh:sts=2:sw=2 # # shFlags unit test for the public functions. # # Copyright 2008-2017 Kate Ward. All Rights Reserved. # Released under the Apache 2.0 license. # # Author: kate.ward@forestent.com (Kate Ward) # https://github.com/kward/shflags # ### ShellCheck (http://www.shellcheck.net/) # Disable source following. # shellcheck disable=SC1090,SC1091 # These variables will be overridden by the test helpers. setglobal expectedF = ""$(TMPDIR:-/tmp)/expected"" setglobal returnF = ""$(TMPDIR:-/tmp)/return"" setglobal stdoutF = ""$(TMPDIR:-/tmp)/STDOUT"" setglobal stderrF = ""$(TMPDIR:-/tmp)/STDERR"" # Load test helpers. source ./shflags_test_helpers proc testHelp { _testHelp '-h' flags_getoptIsEnh || return _testHelp '--help' } proc _testHelp { setglobal flag = $1 # Test default help output. th_clearReturn shell { FLAGS $(flag) >$(stdoutF) !2 >$(stderrF) echo $Status >$(returnF) } assertFalse \ 'a call for help should return a non-zero exit code.' \ $[th_queryReturn] grep 'show this help' $(stderrF) >/dev/null setglobal grepped = $Status assertTrue \ 'short request for help should have produced some help output.' \ $(grepped) test $(grepped) -ne $(FLAGS_TRUE) && th_showOutput # Test proper output when FLAGS_HELP set. shell { setglobal FLAGS_HELP = ''this is a test'' FLAGS $(flag) >$(stdoutF) !2 >$(stderrF) } grep 'this is a test' $(stderrF) >/dev/null setglobal grepped = $Status assertTrue 'setting FLAGS_HELP did not produce expected result' $(grepped) test $(grepped) -ne $(FLAGS_TRUE) && th_showOutput # test that "'" chars work in help string shell { DEFINE_boolean b false "help string containing a ' char" b FLAGS $(flag) >$(stdoutF) !2 >$(stderrF) } grep "help string containing a ' char" $(stderrF) >/dev/null setglobal grepped = $Status assertTrue "help strings containing apostrophes don't work" $(grepped) test $(grepped) -ne $(FLAGS_TRUE) && th_showOutput } proc mock_flags_columns { echo 80 } proc testStandardHelpOutput { flags_getoptIsStd || startSkipping DEFINE_boolean test_bool false 'test boolean' b DEFINE_integer test_int 0 'test integer' i DEFINE_string test_str '' 'test string' s DEFINE_string long_desc 'blah' \ 'testing of a long description to force wrap of default value' D DEFINE_string long_default \ 'this_is_a_long_default_value_to_force_alternate_indentation' \ 'testing of long default value' F setglobal help = ''USAGE: standard [flags] args'' cat >$(expectedF) << """ $(help) flags: -b test boolean (default: false) -i test integer (default: 0) -s test string (default: '') -D testing of a long description to force wrap of default value (default: 'blah') -F testing of long default value (default: 'this_is_a_long_default_value_to_force_alternate_indentation') -h show this help (default: false) """ shell { proc _flags_columns { mock_flags_columns; } setglobal FLAGS_HELP = $(help); FLAGS -h >$(stdoutF) !2 >$(stderrF) echo $Status >$(returnF) } assertFalse \ 'a call for help should return a non-zero exit code.' \ $[th_queryReturn] diff $(expectedF) $(stderrF) >/dev/null setglobal r3turn = $Status assertTrue 'unexpected help output' $(r3turn) th_showOutput $(r3turn) $(stdoutF) $(stderrF) } proc testEnhancedHelpOutput { flags_getoptIsEnh || startSkipping DEFINE_boolean test_bool false 'test boolean' b DEFINE_integer test_int 0 'test integer' i DEFINE_string test_str '' 'test string' s DEFINE_string long_desc 'blah' \ 'testing of a long description to force wrap of default value' D DEFINE_string long_default \ 'this_is_a_long_default_value_to_force_alternate_indentation' \ 'testing of long default value' F setglobal help = ''USAGE: enhanced [flags] args'' cat >$(expectedF) << """ $(help) flags: -b,--[no]test_bool: test boolean (default: false) -i,--test_int: test integer (default: 0) -s,--test_str: test string (default: '') -D,--long_desc: testing of a long description to force wrap of default value (default: 'blah') -F,--long_default: testing of long default value (default: 'this_is_a_long_default_value_to_force_alternate_indentation') -h,--help: show this help (default: false) """ shell { proc _flags_columns { mock_flags_columns; } # shellcheck disable=SC2034 setglobal FLAGS_HELP = $(help); FLAGS -h >$(stdoutF) !2 >$(stderrF) echo $Status >$(returnF) } assertFalse \ 'a call for help should return a non-zero exit code.' \ $[th_queryReturn] diff $(expectedF) $(stderrF) >/dev/null setglobal differed = $Status assertTrue 'unexpected help output' $(differed) th_showOutput $(differed) $(stdoutF) $(stderrF) } proc testNoHelp { flags_getoptIsEnh || startSkipping shell { FLAGS --nohelp >$(stdoutF) !2 >$(stderrF) } setglobal r3turn = $Status assertTrue "FLAGS returned a non-zero result ($(r3turn))" $(r3turn) assertFalse 'expected no output to STDOUT' "[ -s '$(stdoutF)' ]" assertFalse 'expected no output to STDERR' "[ -s '$(stderrF)' ]" } proc testLoggingLevel { # Check that the default logging level is set properly. setglobal got = $[flags_loggingLevel], want = $(__FLAGS_LEVEL_DEFAULT) assertTrue "Unexpected default logging level = $(got), want $(want)" "[ $(got) -eq $(want) ]" # Override the logging level, and check again. flags_setLoggingLevel $(FLAGS_LEVEL_FATAL) flags_setLoggingLevel $(FLAGS_LEVEL_INFO) setglobal got = $[flags_loggingLevel], want = $(FLAGS_LEVEL_INFO) assertTrue "Unexpected configured logging level = $(got), want $(want)" "[ $(got) -eq $(want) ]" } proc oneTimeSetUp { th_oneTimeSetUp if flags_getoptIsStd { th_warn 'Standard version of getopt found. Enhanced tests will be skipped.' return } th_warn 'Enhanced version of getopt found. Standard tests will be skipped.' } proc setUp { flags_reset } # Load and run shUnit2. # shellcheck disable=SC2034 test -n $(ZSH_VERSION:-) && setglobal SHUNIT_PARENT = $0 source "${TH_SHUNIT}"