|
| 1 | +#! /usr/bin/env bats |
| 2 | + |
| 3 | +load ../../environment |
| 4 | + |
| 5 | +ASSERTION_SOURCE="$_GO_CORE_DIR/lib/testing/log" |
| 6 | +load "$_GO_CORE_DIR/lib/bats/assertion-test-helpers" |
| 7 | + |
| 8 | +setup() { |
| 9 | + test_filter |
| 10 | + mkdir -p "$TEST_GO_ROOTDIR" |
| 11 | +} |
| 12 | + |
| 13 | +teardown() { |
| 14 | + remove_test_go_rootdir |
| 15 | +} |
| 16 | + |
| 17 | +@test "$SUITE: assert_log_equals empty output" { |
| 18 | + expect_assertion_success ':' 'assert_log_equals' |
| 19 | +} |
| 20 | + |
| 21 | +@test "$SUITE: assert_log_equals single non-@go.log line" { |
| 22 | + expect_assertion_success 'printf "Hello, World!\n"' \ |
| 23 | + 'assert_log_equals "Hello, World!"' |
| 24 | +} |
| 25 | + |
| 26 | +@test "$SUITE: assert_log_equals single @go.log line" { |
| 27 | + create_log_script '@go.log INFO Hello, World!' |
| 28 | + expect_assertion_success "'$TEST_GO_SCRIPT'" \ |
| 29 | + 'assert_log_equals INFO "Hello, World!"' |
| 30 | +} |
| 31 | + |
| 32 | +@test "$SUITE: assert_log_equals single formatted @go.log line" { |
| 33 | + create_log_script '@go.log INFO Hello, World!' |
| 34 | + _GO_LOG_FORMATTING='true' expect_assertion_success "'$TEST_GO_SCRIPT'" \ |
| 35 | + "assert_log_equals \"\$(format_log_label INFO)\" 'Hello, World!'" |
| 36 | +} |
| 37 | + |
| 38 | +@test "$SUITE: assert_log_equals multiple mixed lines" { |
| 39 | + create_log_script '@go.log INFO Hello, World!' \ |
| 40 | + 'printf "%s\n" "Not a @go.log line."' \ |
| 41 | + '@go.log WARN Goodbye, World!' \ |
| 42 | + 'printf "%s\n" "Also not a @go.log line."' |
| 43 | + |
| 44 | + local args=("'INFO' 'Hello, World!'" |
| 45 | + "'Not a @go.log line.'" |
| 46 | + "'WARN' 'Goodbye, World!'" |
| 47 | + "'Also not a @go.log line.'") |
| 48 | + expect_assertion_success "'$TEST_GO_SCRIPT'" \ |
| 49 | + "assert_log_equals ${args[*]}" |
| 50 | +} |
| 51 | + |
| 52 | +@test "$SUITE: assert_log_equals multiple mixed lines with formatting" { |
| 53 | + create_log_script '@go.log INFO Hello, World!' \ |
| 54 | + 'printf "%s\n" "Not a @go.log line."' \ |
| 55 | + '@go.log WARN Goodbye, World!' \ |
| 56 | + 'printf "%s\n" "Also not a @go.log line."' |
| 57 | + |
| 58 | + local args=("\"\$(format_log_label INFO)\" 'Hello, World!'" |
| 59 | + "'Not a @go.log line.'" |
| 60 | + "\"\$(format_log_label WARN)\" 'Goodbye, World!'" |
| 61 | + "'Also not a @go.log line.'") |
| 62 | + _GO_LOG_FORMATTING='true' expect_assertion_success "'$TEST_GO_SCRIPT'" \ |
| 63 | + "assert_log_equals ${args[*]}" |
| 64 | +} |
| 65 | + |
| 66 | +@test "$SUITE: assert_log_equals error if wrong number of log line args" { |
| 67 | + create_log_script '@go.log INFO Hello, World!' |
| 68 | + expect_assertion_failure "'$TEST_GO_SCRIPT'" \ |
| 69 | + "assert_log_equals 'INFO' 'Hello, World!' 'INFO'" \ |
| 70 | + 'ERROR: Wrong number of arguments for log line 1.' |
| 71 | +} |
| 72 | + |
| 73 | +@test "$SUITE: assert_log_equals fails if line doesn't match" { |
| 74 | + local info_label="$(format_log_label INFO)" |
| 75 | + local end_format_code="$(printf '%b' '\e[0m')" |
| 76 | + |
| 77 | + create_log_script '@go.log INFO Goodbye, World!' |
| 78 | + _GO_LOG_FORMATTING='true' expect_assertion_failure "'$TEST_GO_SCRIPT'" \ |
| 79 | + "assert_log_equals \"\$(format_log_label INFO)\" 'Hello, World!'" \ |
| 80 | + 'line 0 not equal to expected value:' \ |
| 81 | + " expected: '$info_label Hello, World!$end_format_code'" \ |
| 82 | + " actual: '$info_label Goodbye, World!$end_format_code'" \ |
| 83 | + 'OUTPUT:' \ |
| 84 | + "$info_label Goodbye, World!$end_format_code" |
| 85 | +} |
| 86 | + |
| 87 | +@test "$SUITE: assert_log_file_equals returns error if file doesn't exist" { |
| 88 | + expect_assertion_failure ':' "assert_log_file_equals 'nonexistent.log'" \ |
| 89 | + "'nonexistent.log' doesn't exist or isn't a regular file." |
| 90 | +} |
| 91 | + |
| 92 | +@test "$SUITE: assert_log_file_equals empty output" { |
| 93 | + expect_assertion_success "printf '' >'$TEST_GO_ROOTDIR/empty.log'" \ |
| 94 | + "assert_log_file_equals '$TEST_GO_ROOTDIR/empty.log'" |
| 95 | +} |
| 96 | + |
| 97 | +@test "$SUITE: assert_log_file_equals multiple mixed lines with formatting" { |
| 98 | + export TEST_LOG_FILE="$TEST_GO_ROOTDIR/test.log" |
| 99 | + |
| 100 | + # We need to clear the `TEST_LOG_FILE` between the two runs performed by |
| 101 | + # `expect_assertion_success`, since `@go.log` will append to the file. |
| 102 | + create_log_script "printf '' >'$TEST_LOG_FILE'" \ |
| 103 | + '@go.log INFO Hello, World!' \ |
| 104 | + 'printf "%s\n" "Not a @go.log line."' \ |
| 105 | + '@go.log WARN Goodbye, World!' \ |
| 106 | + 'printf "%s\n" "Also not a @go.log line."' |
| 107 | + |
| 108 | + # Note that the file should only get the `@go.log` lines. |
| 109 | + local args=("\"\$(format_log_label INFO)\" 'Hello, World!'" |
| 110 | + "\"\$(format_log_label WARN)\" 'Goodbye, World!'") |
| 111 | + _GO_LOG_FORMATTING='true' expect_assertion_success "'$TEST_GO_SCRIPT'" \ |
| 112 | + "assert_log_file_equals \"\$TEST_LOG_FILE\" ${args[*]}" |
| 113 | +} |
0 commit comments