mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-08-06 17:40: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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue