|
| 1 | +#!/usr/bin/env bats |
| 2 | +## |
| 3 | +# Unit tests for .circleci/post-coverage-comment.sh |
| 4 | +# |
| 5 | +# shellcheck disable=SC2030,SC2031,SC2034 |
| 6 | + |
| 7 | +load ../_helper.bash |
| 8 | + |
| 9 | +@test "post-coverage-comment: missing coverage file" { |
| 10 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 11 | + |
| 12 | + export CIRCLE_PULL_REQUEST="https://github.com/myorg/myrepo/pull/123" |
| 13 | + export GITHUB_TOKEN="token12345" |
| 14 | + export CIRCLE_PROJECT_USERNAME="myorg" |
| 15 | + export CIRCLE_PROJECT_REPONAME="myrepo" |
| 16 | + |
| 17 | + run .circleci/post-coverage-comment.sh /nonexistent/file.txt |
| 18 | + assert_failure |
| 19 | + assert_output_contains "ERROR: Coverage file not found" |
| 20 | + |
| 21 | + popd >/dev/null || exit 1 |
| 22 | +} |
| 23 | + |
| 24 | +@test "post-coverage-comment: no arguments" { |
| 25 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 26 | + |
| 27 | + export CIRCLE_PULL_REQUEST="https://github.com/myorg/myrepo/pull/123" |
| 28 | + export GITHUB_TOKEN="token12345" |
| 29 | + |
| 30 | + run .circleci/post-coverage-comment.sh |
| 31 | + assert_failure |
| 32 | + assert_output_contains "ERROR: Coverage file not found" |
| 33 | + |
| 34 | + popd >/dev/null || exit 1 |
| 35 | +} |
| 36 | + |
| 37 | +@test "post-coverage-comment: skip when not a pull request" { |
| 38 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 39 | + |
| 40 | + mkdir -p .logs/coverage/phpunit |
| 41 | + echo "Lines: 100.00%" >.logs/coverage/phpunit/coverage.txt |
| 42 | + |
| 43 | + unset CIRCLE_PULL_REQUEST |
| 44 | + export GITHUB_TOKEN="token12345" |
| 45 | + |
| 46 | + run .circleci/post-coverage-comment.sh .logs/coverage/phpunit/coverage.txt |
| 47 | + assert_success |
| 48 | + assert_output_contains "Not a pull request. Skipping." |
| 49 | + |
| 50 | + popd >/dev/null || exit 1 |
| 51 | +} |
| 52 | + |
| 53 | +@test "post-coverage-comment: skip when no GITHUB_TOKEN" { |
| 54 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 55 | + |
| 56 | + mkdir -p .logs/coverage/phpunit |
| 57 | + echo "Lines: 100.00%" >.logs/coverage/phpunit/coverage.txt |
| 58 | + |
| 59 | + export CIRCLE_PULL_REQUEST="https://github.com/myorg/myrepo/pull/123" |
| 60 | + unset GITHUB_TOKEN |
| 61 | + |
| 62 | + run .circleci/post-coverage-comment.sh .logs/coverage/phpunit/coverage.txt |
| 63 | + assert_success |
| 64 | + assert_output_contains "GITHUB_TOKEN is not set. Skipping." |
| 65 | + |
| 66 | + popd >/dev/null || exit 1 |
| 67 | +} |
| 68 | + |
| 69 | +@test "post-coverage-comment: post with no existing comments" { |
| 70 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 71 | + |
| 72 | + mkdir -p .logs/coverage/phpunit |
| 73 | + printf "Code Coverage Report:\n Lines: 100.00%%\n" >.logs/coverage/phpunit/coverage.txt |
| 74 | + |
| 75 | + declare -a STEPS=( |
| 76 | + # GET existing comments - return empty array. |
| 77 | + "@curl -s -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json https://api.github.com/repos/myorg/myrepo/issues/123/comments?per_page=100 # []" |
| 78 | + # POST new comment. |
| 79 | + '@curl -s -X POST -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json https://api.github.com/repos/myorg/myrepo/issues/123/comments * # {"id": 1}' |
| 80 | + ) |
| 81 | + |
| 82 | + mocks="$(run_steps "setup")" |
| 83 | + |
| 84 | + export CIRCLE_PULL_REQUEST="https://github.com/myorg/myrepo/pull/123" |
| 85 | + export GITHUB_TOKEN="token12345" |
| 86 | + export CIRCLE_PROJECT_USERNAME="myorg" |
| 87 | + export CIRCLE_PROJECT_REPONAME="myrepo" |
| 88 | + |
| 89 | + run .circleci/post-coverage-comment.sh .logs/coverage/phpunit/coverage.txt |
| 90 | + assert_success |
| 91 | + |
| 92 | + run_steps "assert" "${mocks[@]}" |
| 93 | + |
| 94 | + popd >/dev/null || exit 1 |
| 95 | +} |
| 96 | + |
| 97 | +@test "post-coverage-comment: minimize existing comments before posting" { |
| 98 | + pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1 |
| 99 | + |
| 100 | + mkdir -p .logs/coverage/phpunit |
| 101 | + printf "Code Coverage Report:\n Lines: 95.00%%\n" >.logs/coverage/phpunit/coverage.txt |
| 102 | + |
| 103 | + declare -a STEPS=( |
| 104 | + # GET existing comments - return one with marker. |
| 105 | + '@curl -s -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json https://api.github.com/repos/myorg/myrepo/issues/456/comments?per_page=100 # [{"node_id": "MDEyOklzc3VlQ29tbWVudDE=", "body": "old coverage <!-- coverage-circleci -->"}]' |
| 106 | + # POST GraphQL to minimize existing comment. |
| 107 | + '@curl -s -X POST -H Authorization: bearer token12345 -H Content-Type: application/json https://api.github.com/graphql * # {"data":{"minimizeComment":{"minimizedComment":{"isMinimized":true}}}}' |
| 108 | + # POST new comment. |
| 109 | + '@curl -s -X POST -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json https://api.github.com/repos/myorg/myrepo/issues/456/comments * # {"id": 2}' |
| 110 | + ) |
| 111 | + |
| 112 | + mocks="$(run_steps "setup")" |
| 113 | + |
| 114 | + export CIRCLE_PULL_REQUEST="https://github.com/myorg/myrepo/pull/456" |
| 115 | + export GITHUB_TOKEN="token12345" |
| 116 | + export CIRCLE_PROJECT_USERNAME="myorg" |
| 117 | + export CIRCLE_PROJECT_REPONAME="myrepo" |
| 118 | + |
| 119 | + run .circleci/post-coverage-comment.sh .logs/coverage/phpunit/coverage.txt |
| 120 | + assert_success |
| 121 | + |
| 122 | + run_steps "assert" "${mocks[@]}" |
| 123 | + |
| 124 | + popd >/dev/null || exit 1 |
| 125 | +} |
0 commit comments