|
4 | 4 | package cmd
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "strings" |
7 | 10 | "testing"
|
8 | 11 |
|
9 | 12 | "code.gitea.io/gitea/models/unittest"
|
| 13 | + "code.gitea.io/gitea/modules/test" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/urfave/cli" |
10 | 17 | )
|
11 | 18 |
|
12 | 19 | func TestMain(m *testing.M) {
|
13 | 20 | unittest.MainTest(m, &unittest.TestOptions{
|
14 | 21 | GiteaRootPath: "..",
|
15 | 22 | })
|
16 | 23 | }
|
| 24 | + |
| 25 | +func newTestApp(testCmdAction func(ctx *cli.Context) error) *cli.App { |
| 26 | + app := cli.NewApp() |
| 27 | + app.HelpName = "gitea" |
| 28 | + testCmd := cli.Command{Name: "test-cmd", Action: testCmdAction} |
| 29 | + app.Commands = append(app.Commands, testCmd) |
| 30 | + return app |
| 31 | +} |
| 32 | + |
| 33 | +type runResult struct { |
| 34 | + Stdout string |
| 35 | + Stderr string |
| 36 | + ExitCode int |
| 37 | +} |
| 38 | + |
| 39 | +func runTestApp(app *cli.App, args ...string) (runResult, error) { |
| 40 | + outBuf := new(strings.Builder) |
| 41 | + errBuf := new(strings.Builder) |
| 42 | + app.Writer = outBuf |
| 43 | + app.ErrWriter = errBuf |
| 44 | + exitCode := -1 |
| 45 | + defer test.MockVariableValue(&cli.ErrWriter, app.ErrWriter)() |
| 46 | + defer test.MockVariableValue(&cli.OsExiter, func(code int) { |
| 47 | + if exitCode == -1 { |
| 48 | + exitCode = code // save the exit code once and then reset the writer (to simulate the exit) |
| 49 | + app.Writer, app.ErrWriter, cli.ErrWriter = io.Discard, io.Discard, io.Discard |
| 50 | + } |
| 51 | + })() |
| 52 | + err := RunMainApp(app, args...) |
| 53 | + return runResult{outBuf.String(), errBuf.String(), exitCode}, err |
| 54 | +} |
| 55 | + |
| 56 | +func TestCliCmdError(t *testing.T) { |
| 57 | + app := newTestApp(func(ctx *cli.Context) error { return fmt.Errorf("normal error") }) |
| 58 | + r, err := runTestApp(app, "./gitea", "test-cmd") |
| 59 | + assert.Error(t, err) |
| 60 | + assert.Equal(t, 1, r.ExitCode) |
| 61 | + assert.Equal(t, "", r.Stdout) |
| 62 | + assert.Equal(t, "Command error: normal error\n", r.Stderr) |
| 63 | + |
| 64 | + app = newTestApp(func(ctx *cli.Context) error { return cli.NewExitError("exit error", 2) }) |
| 65 | + r, err = runTestApp(app, "./gitea", "test-cmd") |
| 66 | + assert.Error(t, err) |
| 67 | + assert.Equal(t, 2, r.ExitCode) |
| 68 | + assert.Equal(t, "", r.Stdout) |
| 69 | + assert.Equal(t, "exit error\n", r.Stderr) |
| 70 | + |
| 71 | + app = newTestApp(func(ctx *cli.Context) error { return nil }) |
| 72 | + r, err = runTestApp(app, "./gitea", "test-cmd", "--no-such") |
| 73 | + assert.Error(t, err) |
| 74 | + assert.Equal(t, 1, r.ExitCode) |
| 75 | + assert.EqualValues(t, "Incorrect Usage: flag provided but not defined: -no-such\n\nNAME:\n gitea test-cmd - \n\nUSAGE:\n gitea test-cmd [arguments...]\n", r.Stdout) |
| 76 | + assert.Equal(t, "", r.Stderr) // the cli package's strange behavior, the error message is not in stderr .... |
| 77 | + |
| 78 | + app = newTestApp(func(ctx *cli.Context) error { return nil }) |
| 79 | + r, err = runTestApp(app, "./gitea", "test-cmd") |
| 80 | + assert.NoError(t, err) |
| 81 | + assert.Equal(t, -1, r.ExitCode) // the cli.OsExiter is not called |
| 82 | + assert.Equal(t, "", r.Stdout) |
| 83 | + assert.Equal(t, "", r.Stderr) |
| 84 | +} |
0 commit comments