|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# tests for pr-should-link-jira.t |
| 4 | +# |
| 5 | + |
| 6 | +ME=$(basename $0) |
| 7 | + |
| 8 | +# Our test script queries github, for which we need token |
| 9 | +if [[ -z "$CIRRUS_REPO_CLONE_TOKEN" ]]; then |
| 10 | + if [[ -n "$GITHUB_TOKEN" ]]; then |
| 11 | + export CIRRUS_REPO_CLONE_TOKEN="$GITHUB_TOKEN" |
| 12 | + else |
| 13 | + echo "$ME: Please set \$CIRRUS_REPO_CLONE_TOKEN" >&2 |
| 14 | + exit 1 |
| 15 | + fi |
| 16 | +fi |
| 17 | + |
| 18 | +############################################################################### |
| 19 | +# BEGIN test cases |
| 20 | +# |
| 21 | + |
| 22 | +read -d '\n' msg_no_jira << EndOfText |
| 23 | +This is some text |
| 24 | +without a jira |
| 25 | +EndOfText |
| 26 | + |
| 27 | +read -d '\n' msg_invalid << EndOfText |
| 28 | +This is some text |
| 29 | +without a jira |
| 30 | +Fixes #42 |
| 31 | +More text... |
| 32 | +EndOfText |
| 33 | + |
| 34 | +read -d '\n' msg_jira << EndOfText |
| 35 | +This is some text |
| 36 | +with a jira |
| 37 | +Fixes https://issues.redhat.com/browse/RHEL-50507 |
| 38 | +More text... |
| 39 | +EndOfText |
| 40 | + |
| 41 | +read -d '\n' msg_jira2 << EndOfText |
| 42 | +This is some text |
| 43 | +with a jira |
| 44 | +Fixes: https://issues.redhat.com/browse/RHEL-50507 |
| 45 | +More text... |
| 46 | +EndOfText |
| 47 | + |
| 48 | +read -d '\n' msg_multiple << EndOfText |
| 49 | +This is some text |
| 50 | +with multiple jira lines |
| 51 | +Fixes https://issues.redhat.com/browse/RHEL-50507 |
| 52 | +More text... |
| 53 | +Fixes: https://issues.redhat.com/browse/RHEL-50506 |
| 54 | +More text... |
| 55 | +EndOfText |
| 56 | + |
| 57 | +# Feel free to add as needed. Syntax is: |
| 58 | +# <exit status> <pr> <commit message> <dest branch> # comments |
| 59 | +# |
| 60 | +# Where: |
| 61 | +# exit status is the expected exit status of the script |
| 62 | +# pr pr number (only used to get tag, 0000 if doesn't matter) |
| 63 | +# commit message commit message |
| 64 | +# dest branch name of branch |
| 65 | +# |
| 66 | + |
| 67 | +tests=" |
| 68 | +0 0000 msg_no_jira main not rhel branch, no link, should pass |
| 69 | +0 0000 msg_jira main not rhel branch, link, should pass |
| 70 | +0 0000 msg_invalid main not rhel branch, invalid link, should pass |
| 71 | +0 0000 msg_no_jira v4.9 not rhel branch, no link, should pass |
| 72 | +1 23514 msg_no_jira v4.9-rhel no link, no tag, should fail |
| 73 | +0 8890 msg_no_jira v4.9-rhel no link, tag, should work |
| 74 | +1 23514 msg_invalid v4.9-rhel invalid link, no tag, should fail |
| 75 | +0 0000 msg_jira v4.9-rhel link, should work |
| 76 | +0 0000 msg_jira2 v4.9-rhel link with colon, should work |
| 77 | +0 0000 msg_multiple v4.9-rhel multiple links, should work |
| 78 | +" |
| 79 | + |
| 80 | +# The script we're testing |
| 81 | +test_script=$(dirname $0)/$(basename $0 .t) |
| 82 | + |
| 83 | +# END test cases |
| 84 | +############################################################################### |
| 85 | +# BEGIN test-script runner and status checker |
| 86 | + |
| 87 | +function run_test_script() { |
| 88 | + local expected_rc=$1 |
| 89 | + local testname=$2 |
| 90 | + |
| 91 | + testnum=$(( testnum + 1 )) |
| 92 | + |
| 93 | + # DO NOT COMBINE 'local output=...' INTO ONE LINE. If you do, you lose $? |
| 94 | + local output |
| 95 | + output=$( $test_script ) |
| 96 | + local actual_rc=$? |
| 97 | + |
| 98 | + if [[ $actual_rc != $expected_rc ]]; then |
| 99 | + echo "not ok $testnum $testname" |
| 100 | + echo "# expected rc $expected_rc" |
| 101 | + echo "# actual rc $actual_rc" |
| 102 | + if [[ -n "$output" ]]; then |
| 103 | + echo "# script output: $output" |
| 104 | + fi |
| 105 | + rc=1 |
| 106 | + else |
| 107 | + if [[ $expected_rc == 1 ]]; then |
| 108 | + # Confirm we get an error message |
| 109 | + if [[ ! "$output" =~ "Please add a reference" ]]; then |
| 110 | + echo "not ok $testnum $testname" |
| 111 | + echo "# Expected: ~ 'Please add a reference'" |
| 112 | + echo "# Actual: $output" |
| 113 | + rc=1 |
| 114 | + else |
| 115 | + echo "ok $testnum $testname - rc=$expected_rc" |
| 116 | + fi |
| 117 | + else |
| 118 | + echo "ok $testnum $testname - rc=$expected_rc" |
| 119 | + fi |
| 120 | + fi |
| 121 | +} |
| 122 | + |
| 123 | +# END test-script runner and status checker |
| 124 | +############################################################################### |
| 125 | +# BEGIN test-case parsing |
| 126 | + |
| 127 | +rc=0 |
| 128 | +testnum=0 |
| 129 | +tested_override= |
| 130 | + |
| 131 | +while read expected_rc pr msg branch rest; do |
| 132 | + # Skip blank lines |
| 133 | + test -z "$expected_rc" && continue |
| 134 | + |
| 135 | + export DEST_BRANCH=$branch |
| 136 | + export CIRRUS_CHANGE_MESSAGE="${!msg}" |
| 137 | + export CIRRUS_PR=$pr |
| 138 | + |
| 139 | + run_test_script $expected_rc "PR $pr $msg $branch - $rest" |
| 140 | +done <<<"$tests" |
| 141 | + |
| 142 | +echo "1..$testnum" |
| 143 | +exit $rc |
| 144 | + |
| 145 | +# END Test-case parsing |
| 146 | +############################################################################### |
0 commit comments