Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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