1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-08-06 17:40:58 +00:00
forgejo-runner/internal/app/cmd/main_test.go
Earl Warren ec99579451
chore: to allow the runner to be imported, v9 needs to be in the go module (#777)
It will be imported by Forgejo.

<!--start release-notes-assistant-->
<!--URL:https://code.forgejo.org/forgejo/runner-->
- other
  - [PR](https://code.forgejo.org/forgejo/runner/pulls/777): <!--number 777 --><!--line 0 --><!--description Y2hvcmU6IHRvIGFsbG93IHRoZSBydW5uZXIgdG8gYmUgaW1wb3J0ZWQsIHY5IG5lZWRzIHRvIGJlIGluIHRoZSBnbyBtb2R1bGU=-->chore: to allow the runner to be imported, v9 needs to be in the go module<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/777
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
2025-07-31 10:35:11 +00:00

52 lines
1.1 KiB
Go

// Copyright 2025 The Forgejo Authors
// SPDX-License-Identifier: MIT
package cmd
import (
"bytes"
"context"
"io"
"os"
"testing"
"code.forgejo.org/forgejo/runner/v9/testutils"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)
// Capture what's being written into a standard file descriptor.
func captureOutput(t *testing.T, stdFD *os.File) (finish func() (output string)) {
t.Helper()
r, w, err := os.Pipe()
require.NoError(t, err)
resetStdout := testutils.MockVariable(stdFD, *w)
return func() (output string) {
w.Close()
resetStdout()
out, err := io.ReadAll(r)
require.NoError(t, err)
return string(out)
}
}
func executeCommand(ctx context.Context, t *testing.T, cmd *cobra.Command, args ...string) (cmdOut, stdOut, stdErr string, err error) {
t.Helper()
finishStdout := captureOutput(t, os.Stdout)
finishStderr := captureOutput(t, os.Stderr)
buf := new(bytes.Buffer)
cmd.SetOut(buf)
cmd.SetErr(buf)
cmd.SetArgs(args)
err = cmd.ExecuteContext(ctx)
cmdOut = buf.String()
stdOut = finishStdout()
stdErr = finishStderr()
return cmdOut, stdOut, stdErr, err
}