Files
gitea-ci-library/tests/report-status.bats
T
moilanik 0655372a86
CI / load-config (push) Successful in 12s
ci-bats Bats tests
ci-cucumber Cucumber tests passed
ci-build Build complete
CI / feature (push) Successful in 1m24s
CI / main (push) Has been skipped
piilloparametrit eksplisiittisiksi
2026-06-13 16:21:47 +03:00

123 lines
4.6 KiB
Bash

#!/usr/bin/env bats
setup() {
source tests/helpers/mock-api.sh
export GITHUB_REPOSITORY="test-owner/test-repo"
export GITHUB_SHA="abc123def456789012345678901234567890abcd"
export GITHUB_SERVER_URL="https://gitea.example.com"
export GITHUB_RUN_ID="42"
}
teardown() {
mock_stop
}
@test "pending status is POSTed with correct payload" {
mock_start
run bash scripts/report-status.sh pending "Building project" "http://example.com/build/42" "http://localhost:18080" "test-token-abc123"
[ "$status" -eq 0 ]
path=$(mock_get_request_path)
[[ "$path" == "/api/v1/repos/test-owner/test-repo/statuses/abc123def456789012345678901234567890abcd" ]]
body=$(mock_get_request_body)
[[ "$body" == *'"state":"pending"'* ]]
[[ "$body" == *'"description":"Building project"'* ]]
[[ "$body" == *'"target_url":"http://example.com/build/42"'* ]]
method=$(mock_get_request_method)
[[ "$method" == "POST" ]]
}
@test "success status with url and custom key" {
mock_start
run bash scripts/report-status.sh success "Unit tests OK" "http://example.com/reports/cucumber.html" "http://localhost:18080" "test-token-abc123" "unit-test"
[ "$status" -eq 0 ]
body=$(mock_get_request_body)
[[ "$body" == *'"state":"success"'* ]]
[[ "$body" == *'"description":"Unit tests OK"'* ]]
[[ "$body" == *'"target_url":"http://example.com/reports/cucumber.html"'* ]]
[[ "$body" == *'"context":"unit-test"'* ]]
}
@test "failure status is POSTed correctly" {
mock_start
run bash scripts/report-status.sh failure "Tests failed: 3 of 10" "http://example.com/build/42" "http://localhost:18080" "test-token-abc123"
[ "$status" -eq 0 ]
body=$(mock_get_request_body)
[[ "$body" == *'"state":"failure"'* ]]
[[ "$body" == *'"description":"Tests failed: 3 of 10"'* ]]
}
@test "error status is POSTed correctly" {
mock_start
run bash scripts/report-status.sh error "Build timed out" "http://example.com/build/42" "http://localhost:18080" "test-token-abc123"
[ "$status" -eq 0 ]
body=$(mock_get_request_body)
[[ "$body" == *'"state":"error"'* ]]
}
@test "cross-repo: root_commit and root_repo override target" {
mock_start
run bash scripts/report-status.sh success "Deployed to staging" "http://example.com/deploy/42" "http://localhost:18080" "test-token-abc123" "deploy-staging" "" "" "rootabc123" "services/temperature-store"
[ "$status" -eq 0 ]
path=$(mock_get_request_path)
[[ "$path" == "/api/v1/repos/services/temperature-store/statuses/rootabc123" ]]
body=$(mock_get_request_body)
[[ "$body" == *'"state":"success"'* ]]
[[ "$body" == *'"context":"deploy-staging"'* ]]
}
@test "cross-repo: only root_commit without root_repo is ignored" {
mock_start
run bash scripts/report-status.sh success "Partial cross-repo" "http://example.com" "http://localhost:18080" "test-token-abc123" "my-key" "" "" "abc"
[ "$status" -eq 0 ]
path=$(mock_get_request_path)
[[ "$path" == "/api/v1/repos/test-owner/test-repo/statuses/abc123def456789012345678901234567890abcd" ]]
}
@test "default key when not provided" {
mock_start
run bash scripts/report-status.sh pending "Build started" "http://example.com/build/42" "http://localhost:18080" "test-token-abc123"
[ "$status" -eq 0 ]
body=$(mock_get_request_body)
[[ "$body" == *'"context":"commit-abc123de"'* ]]
}
@test "API returns 500 causes exit 1" {
mock_set_response 500
mock_start
run bash scripts/report-status.sh success "Should fail" "http://example.com" "http://localhost:18080" "test-token-abc123"
[ "$status" -eq 1 ]
}
@test "missing gitea_api_url argument causes exit 1 with error message" {
mock_start
run bash scripts/report-status.sh pending "Test" "http://example.com" "" "test-token-abc123"
[ "$status" -eq 1 ]
[[ "$output" == *"ERROR"* || "$output" == *"gitea_api_url"* ]]
}
@test "missing gitea_token argument causes exit 1 with error message" {
mock_start
run bash scripts/report-status.sh pending "Test" "http://example.com" "http://localhost:18080" ""
[ "$status" -eq 1 ]
[[ "$output" == *"ERROR"* || "$output" == *"gitea_token"* ]]
}
@test "missing required state argument causes exit 1" {
mock_start
run bash scripts/report-status.sh "" "desc" "http://example.com" "http://localhost:18080" "test-token-abc123"
[ "$status" -eq 1 ]
}
@test "missing required description argument causes exit 1" {
mock_start
run bash scripts/report-status.sh pending "" "http://example.com" "http://localhost:18080" "test-token-abc123"
[ "$status" -eq 1 ]
}
@test "missing required state argument is checked before gitea_api_url" {
mock_start
run bash scripts/report-status.sh "" "desc" "http://example.com" "" ""
[ "$status" -eq 1 ]
[[ "$output" == *"state"* ]]
}