mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-08-11 17:50:58 +00:00
feat(tests): cmd executeCommand captures stderr & stdout (#769)
test only refactor that will be used by https://code.forgejo.org/forgejo/runner/pulls/757 <!--start release-notes-assistant--> <!--URL:https://code.forgejo.org/forgejo/runner--> - features - [PR](https://code.forgejo.org/forgejo/runner/pulls/769): <!--number 769 --><!--line 0 --><!--description ZmVhdCh0ZXN0cyk6IGNtZCBleGVjdXRlQ29tbWFuZCBjYXB0dXJlcyBzdGRlcnIgJiBzdGRvdXQ=-->feat(tests): cmd executeCommand captures stderr & stdout<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/769 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>
This commit is contained in:
parent
bc716490af
commit
cb8c3b0002
2 changed files with 54 additions and 15 deletions
|
@ -3,7 +3,6 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
@ -14,27 +13,15 @@ import (
|
|||
"code.forgejo.org/forgejo/runner/internal/pkg/ver"
|
||||
"connectrpc.com/connect"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func executeCommand(ctx context.Context, cmd *cobra.Command, args ...string) (string, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
cmd.SetOut(buf)
|
||||
cmd.SetErr(buf)
|
||||
cmd.SetArgs(args)
|
||||
|
||||
err := cmd.ExecuteContext(ctx)
|
||||
|
||||
return buf.String(), err
|
||||
}
|
||||
|
||||
func Test_createRunnerFileCmd(t *testing.T) {
|
||||
configFile := "config.yml"
|
||||
ctx := context.Background()
|
||||
cmd := createRunnerFileCmd(ctx, &configFile)
|
||||
output, err := executeCommand(ctx, cmd)
|
||||
output, _, _, err := executeCommand(ctx, t, cmd)
|
||||
assert.ErrorContains(t, err, `required flag(s) "instance", "secret" not set`)
|
||||
assert.Contains(t, output, "Usage:")
|
||||
}
|
||||
|
@ -89,7 +76,7 @@ func Test_runCreateRunnerFile(t *testing.T) {
|
|||
//
|
||||
ctx := context.Background()
|
||||
cmd := createRunnerFileCmd(ctx, &configFile)
|
||||
output, err := executeCommand(ctx, cmd, "--connect", "--secret", secret, "--instance", instance, "--name", name)
|
||||
output, _, _, err := executeCommand(ctx, t, cmd, "--connect", "--secret", secret, "--instance", instance, "--name", name)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "", output)
|
||||
|
||||
|
|
52
internal/app/cmd/main_test.go
Normal file
52
internal/app/cmd/main_test.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
// Copyright 2025 The Forgejo Authors
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"code.forgejo.org/forgejo/runner/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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue