Files
runner/internal/pkg/report/globalmask_test.go
T
silverwindandbircni 430f7bb2cf fix: handle workflow commands like GitHub's runner (#1333)
Commands the runner applies itself, like the `::set-output::` older actions still print, showed up raw in the job log. The reporter also re-parsed every line with one job-wide stop-commands and echo state, while act keeps one command handler per step, stage, composite child and hook, as GitHub does. A `::stop-commands::` left active in one of them kept the reporter suppressed for the others, so their `::add-mask::` values were never registered, and post steps logged command lines twice.

- act's command handler decides what reaches the log, as GitHub's runner does: applied commands only under `ACTIONS_STEP_DEBUG` or `::echo::on`, `::stop-commands::` and its resume line shown with long tokens masked
- Masks go straight to the reporter, which no longer parses command lines
- One parser follows GitHub's rules: leading whitespace, `##[...]` anywhere in a line, registered commands only
- Service logs honour only `::add-mask::`

---------

Co-authored-by: bircni <[email protected]>
Reviewed-on: https://gitea.com/gitea/runner/pulls/1333
Reviewed-by: bircni <[email protected]>
Co-authored-by: silverwind <[email protected]>
2026-10-21 16:03:40 +00:00

65 lines
1.9 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package report
import (
"strings"
"sync"
"testing"
"gitea.com/gitea/runner/act/common/logging"
"github.com/stretchr/testify/assert"
)
func TestGlobalMasks(t *testing.T) {
prevOut := logging.Output()
t.Cleanup(func() {
logging.SetOutput(prevOut)
logging.SetTextFormat(logging.TextOptions{})
})
var out strings.Builder
logging.SetOutput(&out)
logging.SetTextFormat(logging.TextOptions{DisableTimestamp: true, Filter: MaskGlobal})
format := func(job string) string {
out.Reset()
logging.With("job", job).Info("line")
return out.String()
}
register := func(oldnew ...string) *Reporter {
r := &Reporter{oldnew: oldnew}
registerGlobalMasks(r)
t.Cleanup(func() { deregisterGlobalMasks(r) })
return r
}
assert.Contains(t, format("build s3cr3t"), "s3cr3t")
first := register("s3cr3t", "***")
second := register("other", "***", "otherlonger", "***")
line := format("build s3cr3t and other")
assert.NotContains(t, line, "s3cr3t") // masked though it rode a field, not the message
assert.NotContains(t, line, "other")
assert.NotContains(t, format("otherlonger"), "longer") // longest first, so not "***longer"
deregisterGlobalMasks(first)
line = format("build s3cr3t and other")
assert.Contains(t, line, "s3cr3t")
assert.NotContains(t, line, "other") // the task still running keeps its own
deregisterGlobalMasks(second)
assert.Nil(t, globalReplacer.Load())
// A workflow could otherwise mask "error" here and rewrite every other task's log.
third := register("s3cr3t", "***")
var writers sync.WaitGroup
for _, mask := range []string{"runtime-secret", "another-secret"} {
writers.Go(func() { third.AddMask(mask) })
}
writers.Wait()
assert.Contains(t, format("saw runtime-secret"), "runtime-secret")
assert.Equal(t, "saw *** ***", third.mask("saw runtime-secret another-secret"))
}