2022-03-31 11:56:22 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-03-27 09:33:00 +00:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-31 11:56:22 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2019-03-27 09:33:00 +00:00
|
|
|
|
2022-03-31 11:56:22 +00:00
|
|
|
func TestRunWithContextStd(t *testing.T) {
|
2022-02-06 19:01:47 +00:00
|
|
|
cmd := NewCommand(context.Background(), "--version")
|
2022-04-01 02:55:30 +00:00
|
|
|
stdout, stderr, err := cmd.RunStdString(&RunOpts{})
|
2022-03-31 11:56:22 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, stderr)
|
|
|
|
assert.Contains(t, stdout, "git version")
|
2019-03-27 09:33:00 +00:00
|
|
|
|
2022-03-31 11:56:22 +00:00
|
|
|
cmd = NewCommand(context.Background(), "--no-such-arg")
|
2022-04-01 02:55:30 +00:00
|
|
|
stdout, stderr, err = cmd.RunStdString(&RunOpts{})
|
2022-03-31 11:56:22 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Equal(t, stderr, err.Stderr())
|
|
|
|
assert.Contains(t, err.Stderr(), "unknown option:")
|
|
|
|
assert.Contains(t, err.Error(), "exit status 129 - unknown option:")
|
|
|
|
assert.Empty(t, stdout)
|
2019-03-27 09:33:00 +00:00
|
|
|
}
|
2022-10-15 10:49:26 +00:00
|
|
|
|
2022-10-15 12:18:31 +00:00
|
|
|
cmd = NewCommand(context.Background())
|
|
|
|
cmd.AddDynamicArguments("-test")
|
|
|
|
assert.ErrorIs(t, cmd.Run(&RunOpts{}), ErrBrokenCommand)
|
|
|
|
|
|
|
|
cmd = NewCommand(context.Background())
|
|
|
|
cmd.AddDynamicArguments("--test")
|
|
|
|
assert.ErrorIs(t, cmd.Run(&RunOpts{}), ErrBrokenCommand)
|
2022-10-15 10:49:26 +00:00
|
|
|
|
|
|
|
subCmd := "version"
|
|
|
|
cmd = NewCommand(context.Background()).AddDynamicArguments(subCmd) // for test purpose only, the sub-command should never be dynamic for production
|
|
|
|
stdout, stderr, err = cmd.RunStdString(&RunOpts{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, stderr)
|
|
|
|
assert.Contains(t, stdout, "git version")
|
2019-03-27 09:33:00 +00:00
|
|
|
}
|