| 1 | #!/bin/bash |
| 2 | # |
| 3 | # SPDX-License-Identifier: GPL-2.0-only |
| 4 | # |
| 5 | # Copyright (C) 2020 Geneviève Bastien <gbastien@versatic.net> |
| 6 | # |
| 7 | # This file tests pretty printing in details some event classes that are |
| 8 | # not all covered by the main babeltrace tests with traces. |
| 9 | SH_TAP=1 |
| 10 | |
| 11 | if [ "x${BT_TESTS_SRCDIR:-}" != "x" ]; then |
| 12 | UTILSSH="$BT_TESTS_SRCDIR/utils/utils.sh" |
| 13 | else |
| 14 | UTILSSH="$(dirname "$0")/../../utils/utils.sh" |
| 15 | fi |
| 16 | |
| 17 | # shellcheck source=../../utils/utils.sh |
| 18 | source "$UTILSSH" |
| 19 | |
| 20 | data_dir="$BT_TESTS_DATADIR/plugins/sink.text.pretty" |
| 21 | temp_stdout_expected_file=$(mktemp -t test_pretty_expected_stdout.XXXXXX) |
| 22 | temp_stderr_expected="/dev/null" |
| 23 | |
| 24 | plan_tests 14 |
| 25 | |
| 26 | function compare_enum_sorted |
| 27 | { |
| 28 | local expected_file="$1" |
| 29 | local actual_file="$2" |
| 30 | |
| 31 | # The order in which enum labels are printed by a `sink.text.pretty` |
| 32 | # component directly depends on the order in which mappings were added |
| 33 | # to the enum field class in the source component. This order should |
| 34 | # not be relied on when testing. Relying on it caused problems with |
| 35 | # Python component classes because different versions of Python sort |
| 36 | # data structures differently (e.g. dictionaries are insertion sorted |
| 37 | # since Python 3.7). |
| 38 | |
| 39 | run_python_bt2 python3 "${BT_TESTS_SRCDIR}/utils/python/split_sort_compare.py" \ |
| 40 | "$(cat $expected_file)" "$(cat $actual_file)" |
| 41 | } |
| 42 | |
| 43 | function run_test |
| 44 | { |
| 45 | local test_name=$1 |
| 46 | local expected_to_fail="$2" |
| 47 | local value="$3" |
| 48 | local expected_stdout_file="$4" |
| 49 | local test_text= |
| 50 | local actual_stdout_file |
| 51 | local actual_stderr_file |
| 52 | local ret=0 |
| 53 | local local_args=( |
| 54 | "--plugin-path" "$data_dir" |
| 55 | "-c" "src.test-pretty.TheSourceOfProblems" |
| 56 | "-p" "enum-values=\"$enum_values\"" |
| 57 | "-p" "value=$value" |
| 58 | "-p" "enum-signed=$enum_signed" |
| 59 | "-c" "sink.text.pretty" |
| 60 | ) |
| 61 | |
| 62 | actual_stdout_file="$(mktemp -t actual_pretty_stdout.XXXXXX)" |
| 63 | actual_stderr_file="$(mktemp -t actual_pretty_stderr.XXXXXX)" |
| 64 | |
| 65 | bt_cli "$actual_stdout_file" "$actual_stderr_file" "${local_args[@]}" |
| 66 | |
| 67 | compare_enum_sorted "$expected_stdout_file" "$actual_stdout_file" |
| 68 | ret_stdout=$? |
| 69 | |
| 70 | bt_diff /dev/null "$actual_stderr_file" |
| 71 | ret_stderr=$? |
| 72 | |
| 73 | if ((ret_stdout != 0 || ret_stderr != 0)); then |
| 74 | ret=1 |
| 75 | fi |
| 76 | |
| 77 | rm -f "$actual_stdout_file" "$actual_stderr_file" |
| 78 | |
| 79 | if (($expected_to_fail)); then |
| 80 | isnt $ret 0 "$test_name signed=$enum_signed with value=$value doesn't match as expected" |
| 81 | else |
| 82 | ok $ret "$test_name signed=$enum_signed with value=$value matches" |
| 83 | fi |
| 84 | |
| 85 | } |
| 86 | |
| 87 | function test_normal_enum { |
| 88 | test_name="Normal enum" |
| 89 | enum_signed="$1" |
| 90 | enum_values="single,1,1 single2,2,2 single3,4,4 range,4,8 range2,15,20" |
| 91 | |
| 92 | # Hit a single value |
| 93 | cat <<- 'END' > "$temp_stdout_expected_file" |
| 94 | with_enum: { enum_field = ( "single" : container = 1 ) } |
| 95 | END |
| 96 | run_test "$test_name" 0 1 "$temp_stdout_expected_file" |
| 97 | |
| 98 | # Hit a single range |
| 99 | cat <<- 'END' > "$temp_stdout_expected_file" |
| 100 | with_enum: { enum_field = ( "range" : container = 7 ) } |
| 101 | END |
| 102 | run_test "$test_name" 0 7 "$temp_stdout_expected_file" |
| 103 | |
| 104 | # Unknown |
| 105 | cat <<- 'END' > "$temp_stdout_expected_file" |
| 106 | with_enum: { enum_field = ( <unknown> : container = 21 ) } |
| 107 | END |
| 108 | run_test "$test_name" 0 21 "$temp_stdout_expected_file" |
| 109 | |
| 110 | # Unknown but with bits with a value, but range larger than 1 element |
| 111 | cat <<- 'END' > "$temp_stdout_expected_file" |
| 112 | with_enum: { enum_field = ( <unknown> : container = 12 ) } |
| 113 | END |
| 114 | run_test "$test_name" 0 12 "$temp_stdout_expected_file" |
| 115 | |
| 116 | # Unknown value of 0 |
| 117 | cat <<- 'END' > "$temp_stdout_expected_file" |
| 118 | with_enum: { enum_field = ( <unknown> : container = 0 ) } |
| 119 | END |
| 120 | run_test "$test_name" 0 0 "$temp_stdout_expected_file" |
| 121 | } |
| 122 | |
| 123 | function test_normal_enum_negative { |
| 124 | test_name="Normal enum with negative value" |
| 125 | enum_signed="true" |
| 126 | enum_values="zero,0,0 single,1,1 single2,2,2 single3,4,4 range,4,8 negative,-1,-1 rangeNegative,-6,-2" |
| 127 | |
| 128 | # Hit a single negative value |
| 129 | cat <<- 'END' > "$temp_stdout_expected_file" |
| 130 | with_enum: { enum_field = ( "negative" : container = -1 ) } |
| 131 | END |
| 132 | run_test "$test_name" 0 -1 "$temp_stdout_expected_file" |
| 133 | |
| 134 | # Hit a single negative range |
| 135 | cat <<- 'END' > "$temp_stdout_expected_file" |
| 136 | with_enum: { enum_field = ( "rangeNegative" : container = -6 ) } |
| 137 | END |
| 138 | run_test "$test_name" 0 -6 "$temp_stdout_expected_file" |
| 139 | |
| 140 | # Unknown |
| 141 | cat <<- 'END' > "$temp_stdout_expected_file" |
| 142 | with_enum: { enum_field = ( <unknown> : container = -7 ) } |
| 143 | END |
| 144 | run_test "$test_name" 0 -7 "$temp_stdout_expected_file" |
| 145 | |
| 146 | # value of 0 |
| 147 | cat <<- 'END' > "$temp_stdout_expected_file" |
| 148 | with_enum: { enum_field = ( "zero" : container = 0 ) } |
| 149 | END |
| 150 | run_test "$test_name" 0 0 "$temp_stdout_expected_file" |
| 151 | } |
| 152 | |
| 153 | # Enumerations tests |
| 154 | test_normal_enum "false" |
| 155 | test_normal_enum "true" |
| 156 | test_normal_enum_negative |
| 157 | |
| 158 | # Do not `rm` $temp_stderr_expected because it's set to `/dev/null` right now |
| 159 | # and that would print an error. |
| 160 | rm -f "$temp_stdout_expected_file" |