|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# shellcheck disable=SC2155 |
| 4 | + |
| 5 | +# @data_provider provide_boolean_flags_true |
| 6 | +function test_env_flag_returns_success_when_true() { |
| 7 | + local var_name="$1" |
| 8 | + local fn_name="$2" |
| 9 | + |
| 10 | + local original_value="${!var_name}" |
| 11 | + eval "export $var_name=true" |
| 12 | + |
| 13 | + "$fn_name" |
| 14 | + assert_successful_code "$?" |
| 15 | + |
| 16 | + eval "export $var_name='$original_value'" |
| 17 | +} |
| 18 | + |
| 19 | +function provide_boolean_flags_true() { |
| 20 | + bashunit::data_set "BASHUNIT_PARALLEL_RUN" "bashunit::env::is_parallel_run_enabled" |
| 21 | + bashunit::data_set "BASHUNIT_SHOW_HEADER" "bashunit::env::is_show_header_enabled" |
| 22 | + bashunit::data_set "BASHUNIT_HEADER_ASCII_ART" "bashunit::env::is_header_ascii_art_enabled" |
| 23 | + bashunit::data_set "BASHUNIT_SIMPLE_OUTPUT" "bashunit::env::is_simple_output_enabled" |
| 24 | + bashunit::data_set "BASHUNIT_STOP_ON_FAILURE" "bashunit::env::is_stop_on_failure_enabled" |
| 25 | + bashunit::data_set "BASHUNIT_SHOW_EXECUTION_TIME" "bashunit::env::is_show_execution_time_enabled" |
| 26 | + bashunit::data_set "BASHUNIT_INTERNAL_LOG" "bashunit::env::is_internal_log_enabled" |
| 27 | + bashunit::data_set "BASHUNIT_VERBOSE" "bashunit::env::is_verbose_enabled" |
| 28 | + bashunit::data_set "BASHUNIT_BENCH_MODE" "bashunit::env::is_bench_mode_enabled" |
| 29 | + bashunit::data_set "BASHUNIT_NO_OUTPUT" "bashunit::env::is_no_output_enabled" |
| 30 | + bashunit::data_set "BASHUNIT_SHOW_SKIPPED" "bashunit::env::is_show_skipped_enabled" |
| 31 | + bashunit::data_set "BASHUNIT_SHOW_INCOMPLETE" "bashunit::env::is_show_incomplete_enabled" |
| 32 | + bashunit::data_set "BASHUNIT_STRICT_MODE" "bashunit::env::is_strict_mode_enabled" |
| 33 | + bashunit::data_set "BASHUNIT_STOP_ON_ASSERTION_FAILURE" "bashunit::env::is_stop_on_assertion_failure_enabled" |
| 34 | + bashunit::data_set "BASHUNIT_SKIP_ENV_FILE" "bashunit::env::is_skip_env_file_enabled" |
| 35 | + bashunit::data_set "BASHUNIT_LOGIN_SHELL" "bashunit::env::is_login_shell_enabled" |
| 36 | + bashunit::data_set "BASHUNIT_FAILURES_ONLY" "bashunit::env::is_failures_only_enabled" |
| 37 | + bashunit::data_set "BASHUNIT_SHOW_OUTPUT_ON_FAILURE" "bashunit::env::is_show_output_on_failure_enabled" |
| 38 | + bashunit::data_set "BASHUNIT_NO_PROGRESS" "bashunit::env::is_no_progress_enabled" |
| 39 | + bashunit::data_set "BASHUNIT_NO_COLOR" "bashunit::env::is_no_color_enabled" |
| 40 | + bashunit::data_set "BASHUNIT_COVERAGE" "bashunit::env::is_coverage_enabled" |
| 41 | +} |
| 42 | + |
| 43 | +# @data_provider provide_boolean_flags_false |
| 44 | +function test_env_flag_returns_failure_when_false() { |
| 45 | + local var_name="$1" |
| 46 | + local fn_name="$2" |
| 47 | + |
| 48 | + local original_value="${!var_name}" |
| 49 | + eval "export $var_name=false" |
| 50 | + |
| 51 | + if "$fn_name"; then |
| 52 | + eval "export $var_name='$original_value'" |
| 53 | + fail "Expected $fn_name to return failure when $var_name=false" |
| 54 | + return |
| 55 | + fi |
| 56 | + |
| 57 | + eval "export $var_name='$original_value'" |
| 58 | +} |
| 59 | + |
| 60 | +function provide_boolean_flags_false() { |
| 61 | + bashunit::data_set "BASHUNIT_PARALLEL_RUN" "bashunit::env::is_parallel_run_enabled" |
| 62 | + bashunit::data_set "BASHUNIT_SHOW_HEADER" "bashunit::env::is_show_header_enabled" |
| 63 | + bashunit::data_set "BASHUNIT_SIMPLE_OUTPUT" "bashunit::env::is_simple_output_enabled" |
| 64 | + bashunit::data_set "BASHUNIT_STOP_ON_FAILURE" "bashunit::env::is_stop_on_failure_enabled" |
| 65 | + bashunit::data_set "BASHUNIT_VERBOSE" "bashunit::env::is_verbose_enabled" |
| 66 | + bashunit::data_set "BASHUNIT_NO_OUTPUT" "bashunit::env::is_no_output_enabled" |
| 67 | + bashunit::data_set "BASHUNIT_STRICT_MODE" "bashunit::env::is_strict_mode_enabled" |
| 68 | + bashunit::data_set "BASHUNIT_NO_COLOR" "bashunit::env::is_no_color_enabled" |
| 69 | + bashunit::data_set "BASHUNIT_COVERAGE" "bashunit::env::is_coverage_enabled" |
| 70 | +} |
| 71 | + |
| 72 | +function test_is_dev_mode_enabled_when_dev_log_set() { |
| 73 | + local original="$BASHUNIT_DEV_LOG" |
| 74 | + export BASHUNIT_DEV_LOG="/tmp/dev.log" |
| 75 | + |
| 76 | + bashunit::env::is_dev_mode_enabled |
| 77 | + assert_successful_code "$?" |
| 78 | + |
| 79 | + export BASHUNIT_DEV_LOG="$original" |
| 80 | +} |
| 81 | + |
| 82 | +function test_is_dev_mode_disabled_when_dev_log_empty() { |
| 83 | + local original="$BASHUNIT_DEV_LOG" |
| 84 | + export BASHUNIT_DEV_LOG="" |
| 85 | + |
| 86 | + if bashunit::env::is_dev_mode_enabled; then |
| 87 | + export BASHUNIT_DEV_LOG="$original" |
| 88 | + fail "Expected is_dev_mode_enabled to return failure when BASHUNIT_DEV_LOG is empty" |
| 89 | + return |
| 90 | + fi |
| 91 | + |
| 92 | + export BASHUNIT_DEV_LOG="$original" |
| 93 | +} |
| 94 | + |
| 95 | +function test_is_tap_output_enabled_when_format_is_tap() { |
| 96 | + local original="$BASHUNIT_OUTPUT_FORMAT" |
| 97 | + export BASHUNIT_OUTPUT_FORMAT="tap" |
| 98 | + |
| 99 | + bashunit::env::is_tap_output_enabled |
| 100 | + assert_successful_code "$?" |
| 101 | + |
| 102 | + export BASHUNIT_OUTPUT_FORMAT="$original" |
| 103 | +} |
| 104 | + |
| 105 | +function test_is_tap_output_disabled_when_format_is_not_tap() { |
| 106 | + local original="$BASHUNIT_OUTPUT_FORMAT" |
| 107 | + export BASHUNIT_OUTPUT_FORMAT="" |
| 108 | + |
| 109 | + if bashunit::env::is_tap_output_enabled; then |
| 110 | + export BASHUNIT_OUTPUT_FORMAT="$original" |
| 111 | + fail "Expected is_tap_output_enabled to return failure when format is empty" |
| 112 | + return |
| 113 | + fi |
| 114 | + |
| 115 | + export BASHUNIT_OUTPUT_FORMAT="$original" |
| 116 | +} |
| 117 | + |
| 118 | +function test_active_internet_connection_returns_failure_when_no_network() { |
| 119 | + local original="${BASHUNIT_NO_NETWORK:-}" |
| 120 | + export BASHUNIT_NO_NETWORK="true" |
| 121 | + |
| 122 | + if bashunit::env::active_internet_connection; then |
| 123 | + export BASHUNIT_NO_NETWORK="$original" |
| 124 | + fail "Expected active_internet_connection to fail when BASHUNIT_NO_NETWORK=true" |
| 125 | + return |
| 126 | + fi |
| 127 | + |
| 128 | + export BASHUNIT_NO_NETWORK="$original" |
| 129 | +} |
| 130 | + |
| 131 | +function test_find_terminal_width_returns_a_number() { |
| 132 | + local result |
| 133 | + result=$(bashunit::env::find_terminal_width) |
| 134 | + |
| 135 | + assert_matches "^[0-9]+$" "$result" |
| 136 | +} |
| 137 | + |
| 138 | +function test_find_terminal_width_fallback_returns_100() { |
| 139 | + bashunit::mock tput true |
| 140 | + bashunit::mock stty true |
| 141 | + |
| 142 | + local result |
| 143 | + result=$(bashunit::env::find_terminal_width) |
| 144 | + |
| 145 | + assert_equals "100" "$result" |
| 146 | +} |
| 147 | + |
| 148 | +function test_print_verbose_outputs_env_var_names() { |
| 149 | + local original="$BASHUNIT_VERBOSE" |
| 150 | + export BASHUNIT_VERBOSE="true" |
| 151 | + |
| 152 | + local output |
| 153 | + output=$(bashunit::env::print_verbose) |
| 154 | + |
| 155 | + assert_contains "BASHUNIT_DEFAULT_PATH" "$output" |
| 156 | + assert_contains "BASHUNIT_PARALLEL_RUN" "$output" |
| 157 | + assert_contains "BASHUNIT_VERBOSE" "$output" |
| 158 | + assert_contains "BASHUNIT_COVERAGE" "$output" |
| 159 | + |
| 160 | + export BASHUNIT_VERBOSE="$original" |
| 161 | +} |
0 commit comments