test(logging): add logtest.NewLogger (#1300)

Return a `Logger` and its recording `Handler` together, so tests no longer need the two-line setup.

Stacked on https://gitea.com/gitea/runner/pulls/1295.

Assisted-by: Codet
Co-authored-by: bircni <[email protected]>
Reviewed-on: https://gitea.com/gitea/runner/pulls/1300
Reviewed-by: bircni <[email protected]>
This commit is contained in:
Lunny Xiaoandbircni committed 2026-10-11 23:47:50 +00:00
1 parent de961b91eb
commit 3bc5cc03f8
7 files changed
+21 -32

No files matched your search

+5 -10
View File
@@ -354,8 +354,7 @@ func TestRemoveIgnoresAutoRemoveRace(t *testing.T) {
{name: "real failure", err: removeFailure, wantErr: removeFailure},
} {
t.Run(tc.name, func(t *testing.T) {
hook := logtest.New()
logger := logging.New(hook)
logger, hook := logtest.NewLogger()
ctx := common.WithLogger(context.Background(), logger)
client := &mockDockerClient{}
client.On("ContainerKill", ctx, "abc", killOpts).Return(mobyclient.ContainerKillResult{}, nil)
@@ -616,8 +615,7 @@ func TestCheckVolumes(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
hook := logtest.New()
logger := logging.New(hook)
logger, hook := logtest.NewLogger()
ctx := common.WithLogger(context.Background(), logger)
cr := &containerReference{
input: &NewContainerInput{
@@ -716,8 +714,7 @@ func TestSanitizeOptionsHostConfig(t *testing.T) {
// mergeOptions merges both option sources into a bare container, returning the result and its log.
func mergeOptions(t *testing.T, runnerOptions, workflowOptions string, privileged bool) (*container.HostConfig, *logtest.Handler) {
t.Helper()
hook := logtest.New()
logger := logging.New(hook)
logger, hook := logtest.NewLogger()
cr := &containerReference{input: &NewContainerInput{
RunnerOptions: runnerOptions,
WorkflowOptions: workflowOptions,
@@ -810,8 +807,7 @@ func TestSanitizeConfigKeepsTmpfsMounts(t *testing.T) {
require.Len(t, hostConfig.Mounts, 1)
for _, validVolumes := range [][]string{nil, {"data"}, {"**:ro"}} {
hook := logtest.New()
logger := logging.New(hook)
logger, hook := logtest.NewLogger()
_, sanitized := (&containerReference{input: &NewContainerInput{ValidVolumes: validVolumes}}).sanitizeConfig(common.WithLogger(context.Background(), logger), &container.Config{}, &container.HostConfig{Mounts: []mount.Mount{
hostConfig.Mounts[0],
{Type: mount.TypeVolume, Target: "/anonymous"},
@@ -936,8 +932,7 @@ func TestMergeContainerConfigsWarnsOnlyAboutOptionsThatWereGiven(t *testing.T) {
func TestDockerCreateKeepsInputEntrypoint(t *testing.T) {
for _, entrypoint := range [][]string{nil, {"sleep", "3600"}} {
for _, optionEntrypoint := range []string{"/sbin/dumb-init", ""} {
hook := logtest.New()
logger := logging.New(hook)
logger, hook := logtest.NewLogger()
cr := &containerReference{
input: &NewContainerInput{
NetworkMode: "bridge",