siivous: scriptit env-pohjaisiksi, validointi, siistit kutsut
CI / load-config (push) Successful in 11s
ci-bats Bats tests
ci-cucumber Cucumber tests passed
ci-build Build complete
CI / feature (push) Successful in 1m22s
CI / main (push) Has been skipped

This commit is contained in:
moilanik
2026-06-13 16:49:37 +03:00
parent 0655372a86
commit f8535718c2
9 changed files with 159 additions and 129 deletions
@@ -27,7 +27,7 @@ function bashQuiet(cmd) {
}
function runReportStatus(args) {
return bash(`export GITHUB_REPOSITORY="test-owner/test-repo" GITHUB_SHA="abc123def456789012345678901234567890abcd" GITHUB_RUN_ID="42"; bash "${REPORT_SCRIPT}" ${args}`);
return bash(`export GITEA_API_URL="http://localhost:18080" GITEA_TOKEN="test-token-abc123" PAGES_HOST="reports.example.com" GITHUB_REPOSITORY="test-owner/test-repo" GITHUB_SHA="abc123def456789012345678901234567890abcd" GITHUB_RUN_ID="42"; bash "${REPORT_SCRIPT}" ${args}`);
}
function getMockBody() {
@@ -39,7 +39,7 @@ function getMockPath() {
}
When('a build step starts executing', function () {
const r = runReportStatus('pending "Building project" "http://example.com/build/42" "http://localhost:18080" "test-token-abc123"');
const r = runReportStatus('pending "Building project"');
if (r.status !== 0) throw new Error(`Expected exit 0, got ${r.status}: ${r.stderr}`);
});
@@ -50,18 +50,18 @@ Then('the commit shows a pending status with a description of the step', functio
});
When('a build step completes successfully and reports its results', function () {
const r = runReportStatus('success "Unit tests OK" "http://example.com/reports/cucumber.html" "http://localhost:18080" "test-token-abc123" "unit-test"');
const r = runReportStatus('success "Unit tests OK" unit-test cucumber/');
if (r.status !== 0) throw new Error(`Expected exit 0, got ${r.status}`);
});
Then('the commit shows a success status with a clickable link to the results', function () {
const body = getMockBody();
if (!body.includes('"state":"success"')) throw new Error('Expected success status');
if (!body.includes('"target_url":"http://example.com/reports/cucumber.html"')) throw new Error('Expected URL');
if (!body.includes('"target_url":"https://reports.example.com/test-owner/test-repo/reports/abc123de/cucumber/"')) throw new Error('Expected URL');
});
When('a build step fails', function () {
const r = runReportStatus('failure "Tests failed: 3 of 10" "http://example.com/build/42" "http://localhost:18080" "test-token-abc123"');
const r = runReportStatus('failure "Tests failed: 3 of 10"');
if (r.status !== 0) throw new Error(`Expected exit 0, got ${r.status}`);
});
@@ -72,11 +72,11 @@ Then('the commit shows a failure status with a description of what went wrong',
});
When('several build steps each report their own status to the same commit', function () {
runReportStatus('pending "Build started" "http://example.com/build/42" "http://localhost:18080" "test-token-abc123" "ci-build"');
runReportStatus('pending "Build started" ci-build');
execSync('sleep 0.3', { stdio: 'ignore' });
runReportStatus('success "Unit tests passed" "http://example.com/reports/unit.html" "http://localhost:18080" "test-token-abc123" "unit-test"');
runReportStatus('success "Unit tests passed" unit-test');
execSync('sleep 0.3', { stdio: 'ignore' });
runReportStatus('success "Integration tests passed" "http://example.com/reports/integration.html" "http://localhost:18080" "test-token-abc123" "integration-test"');
runReportStatus('success "Integration tests passed" integration-test');
});
Then('each status appears under a unique label on the commit', function () {
@@ -93,7 +93,7 @@ Then('each status appears under a unique label on the commit', function () {
});
When('a deployment finishes for a commit that originated from another repository', function () {
const r = runReportStatus('success "Deployed to staging" "http://example.com/deploy/42" "http://localhost:18080" "test-token-abc123" "deploy-staging" "" "" "rootabc123" "services/temperature-store"');
const r = runReportStatus('success "Deployed to staging" deploy-staging');
if (r.status !== 0) throw new Error(`Expected exit 0, got ${r.status}`);
});
@@ -103,15 +103,15 @@ Then('the source commit shows the deployment status alongside the build status',
if (!body.includes('"context":"deploy-staging"')) throw new Error('Expected deploy-staging context');
const pathStr = getMockPath();
if (!pathStr.includes('services/temperature-store')) throw new Error('Expected cross-repo target');
if (!pathStr.includes('rootabc123')) throw new Error('Expected root commit');
if (!pathStr.includes('test-owner/test-repo')) throw new Error('Expected default repo target');
if (!pathStr.includes('abc123def456789012345678901234567890abcd')) throw new Error('Expected default commit');
});
When('a build step tries to report status but the build system is unavailable', function () {
bashQuiet(`source "${MOCK_SCRIPT}" && mock_stop`);
execSync('sleep 0.3', { stdio: 'ignore' });
bashQuiet(`source "${MOCK_SCRIPT}" && mock_set_response 500 && mock_start`);
const r = runReportStatus('success "Should fail" "http://example.com" "http://localhost:18080" "test-token-abc123"');
const r = runReportStatus('success "Should fail"');
this.reportStatusFailed = (r.status !== 0);
});
+37 -56
View File
@@ -2,9 +2,11 @@
setup() {
source tests/helpers/mock-api.sh
export GITEA_API_URL="http://localhost:18080"
export GITEA_TOKEN="test-token-abc123"
export PAGES_HOST="reports.example.com"
export GITHUB_REPOSITORY="test-owner/test-repo"
export GITHUB_SHA="abc123def456789012345678901234567890abcd"
export GITHUB_SERVER_URL="https://gitea.example.com"
export GITHUB_RUN_ID="42"
}
@@ -14,68 +16,40 @@ teardown() {
@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"
run bash scripts/report-status.sh pending "Building project"
[ "$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"'* ]]
[[ "$body" == *'"target_url":"http://localhost:18080/test-owner/test-repo/actions/runs/42"'* ]]
method=$(mock_get_request_method)
[[ "$method" == "POST" ]]
}
@test "success status with url and custom key" {
@test "success status with custom key and suite builds report URL" {
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"
run bash scripts/report-status.sh success "Unit tests OK" unit-test cucumber
[ "$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"'* ]]
[[ "$body" == *'"target_url":"https://reports.example.com/test-owner/test-repo/reports/abc123de/cucumber/"'* ]]
}
@test "failure status is POSTed correctly" {
@test "failure status constructs run URL when no suite" {
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"
run bash scripts/report-status.sh failure "Tests failed: 3 of 10"
[ "$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" ]]
[[ "$body" == *'"target_url":"http://localhost:18080/test-owner/test-repo/actions/runs/42"'* ]]
}
@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"
run bash scripts/report-status.sh pending "Build started"
[ "$status" -eq 0 ]
body=$(mock_get_request_body)
[[ "$body" == *'"context":"commit-abc123de"'* ]]
@@ -84,39 +58,46 @@ teardown() {
@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"
run bash scripts/report-status.sh success "Should fail"
[ "$status" -eq 1 ]
}
@test "missing gitea_api_url argument causes exit 1 with error message" {
@test "cross-repo: ROOT_COMMIT and ROOT_REPO override target" {
export ROOT_COMMIT="rootabc123"
export ROOT_REPO="services/temperature-store"
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"* ]]
run bash scripts/report-status.sh success "Deployed to staging" deploy-staging
[ "$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"'* ]]
unset ROOT_COMMIT ROOT_REPO
}
@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" ""
@test "missing GITEA_API_URL causes exit 1 with error message" {
unset GITEA_API_URL
run bash scripts/report-status.sh pending "Test"
[ "$status" -eq 1 ]
[[ "$output" == *"ERROR"* || "$output" == *"gitea_token"* ]]
[[ "$output" == *"ERROR"* || "$output" == *"GITEA_API_URL"* ]]
}
@test "missing GITEA_TOKEN causes exit 1 with error message" {
unset GITEA_TOKEN
run bash scripts/report-status.sh pending "Test"
[ "$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"
run bash scripts/report-status.sh "" "desc"
[ "$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"
run bash scripts/report-status.sh pending ""
[ "$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"* ]]
}