-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Improve testing init, clean up webhook tests #37412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
66e0e1e
models/fixtures: remove test data from webhook.yml, use code to inser…
Copilot 8bb79e7
models/webhook: use db.Insert in tests, merge fixture setup into prep…
Copilot d3341cf
models/webhook: address review feedback - add RepoID to hookOwner3, u…
Copilot 502c76e
fix
wxiaoguang e07cf5b
fix lint
wxiaoguang 9147c35
fine tune
wxiaoguang a6bfda0
fix test
wxiaoguang 9553fae
clean up
wxiaoguang 17cca5c
fix
wxiaoguang 7a1f463
don't send request to example.com
wxiaoguang 363daae
unify path init
wxiaoguang 9abb18c
fix test
wxiaoguang 3359227
fix test
wxiaoguang 5204ef9
fine tune
wxiaoguang 54d3e43
fix test
wxiaoguang 398b47f
debug
wxiaoguang a973269
debug
wxiaoguang 11f910c
debug
wxiaoguang 1591c06
debug
wxiaoguang 18795df
debug
wxiaoguang 888f4da
debug
wxiaoguang f310b1c
debug
wxiaoguang 81fcfab
fix
wxiaoguang 0b7aee8
fix
wxiaoguang e86d62e
clean up
wxiaoguang 9294e96
fix test
wxiaoguang 1d6a95f
fix test
wxiaoguang 09cf004
clean up paths
wxiaoguang b6831e0
fine tune comments
wxiaoguang 632765f
Apply suggestion from @silverwind
silverwind 6dd7f76
Apply suggestion from @silverwind
silverwind 675ad20
Apply suggestion from @silverwind
silverwind aee8bfc
Apply suggestion from @silverwind
silverwind adb8895
Merge branch 'main' into copilot/remove-text-fixtures-webhook-yml
silverwind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| // Copyright 2026 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| // Tests here reload the config system multiple times with uncontrollable details. | ||
| // So they must be in a separate package, to avoid affecting other tests | ||
|
|
||
| package cmdtest | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "code.gitea.io/gitea/cmd" | ||
| "code.gitea.io/gitea/models/unittest" | ||
| "code.gitea.io/gitea/modules/setting" | ||
| "code.gitea.io/gitea/modules/test" | ||
| "code.gitea.io/gitea/modules/util" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| func TestMain(m *testing.M) { | ||
| unittest.MainTest(m) | ||
| } | ||
|
|
||
| func makePathOutput(workPath, customPath, customConf string) string { | ||
| return fmt.Sprintf("WorkPath=%s\nCustomPath=%s\nCustomConf=%s", workPath, customPath, customConf) | ||
| } | ||
|
|
||
| func newTestApp(testCmd cli.Command) *cli.Command { | ||
| app := cmd.NewMainApp(cmd.AppVersion{}) | ||
| testCmd.Name = util.IfZero(testCmd.Name, "test-cmd") | ||
| cmd.PrepareSubcommandWithGlobalFlags(&testCmd) | ||
| app.Commands = append(app.Commands, &testCmd) | ||
| app.DefaultCommand = testCmd.Name | ||
| return app | ||
| } | ||
|
|
||
| type runResult struct { | ||
| Stdout string | ||
| Stderr string | ||
| ExitCode int | ||
| } | ||
|
|
||
| func runTestApp(app *cli.Command, args ...string) (runResult, error) { | ||
| outBuf := new(strings.Builder) | ||
| errBuf := new(strings.Builder) | ||
| app.Writer = outBuf | ||
| app.ErrWriter = errBuf | ||
| exitCode := -1 | ||
| defer test.MockVariableValue(&cli.ErrWriter, app.ErrWriter)() | ||
| defer test.MockVariableValue(&cli.OsExiter, func(code int) { | ||
| if exitCode == -1 { | ||
| exitCode = code // save the exit code once and then reset the writer (to simulate the exit) | ||
| app.Writer, app.ErrWriter, cli.ErrWriter = io.Discard, io.Discard, io.Discard | ||
| } | ||
| })() | ||
| err := cmd.RunMainApp(app, args...) | ||
| return runResult{outBuf.String(), errBuf.String(), exitCode}, err | ||
| } | ||
|
|
||
| func TestCliCmd(t *testing.T) { | ||
| defaultWorkPath := filepath.FromSlash("/tmp/mocked-work-path") | ||
| defaultCustomPath := filepath.Join(defaultWorkPath, "custom") | ||
| defaultCustomConf := filepath.Join(defaultCustomPath, "conf/app.ini") | ||
| defer setting.MockBuiltinPaths(defaultWorkPath, "", "")() | ||
|
|
||
| cli.CommandHelpTemplate = "(command help template)" | ||
| cli.RootCommandHelpTemplate = "(app help template)" | ||
| cli.SubcommandHelpTemplate = "(subcommand help template)" | ||
|
|
||
| cases := []struct { | ||
| env map[string]string | ||
| cmd string | ||
| exp string | ||
| }{ | ||
| // help commands | ||
| { | ||
| cmd: "./gitea -h", | ||
| exp: "DEFAULT CONFIGURATION:", | ||
| }, | ||
| { | ||
| cmd: "./gitea help", | ||
| exp: "DEFAULT CONFIGURATION:", | ||
| }, | ||
|
|
||
| { | ||
| cmd: "./gitea -c /dev/null -h", | ||
| exp: "ConfigFile: /dev/null", | ||
| }, | ||
|
|
||
| { | ||
| cmd: "./gitea -c /dev/null help", | ||
| exp: "ConfigFile: /dev/null", | ||
| }, | ||
| { | ||
| cmd: "./gitea help -c /dev/null", | ||
| exp: "ConfigFile: /dev/null", | ||
| }, | ||
|
|
||
| { | ||
| cmd: "./gitea -c /dev/null test-cmd -h", | ||
| exp: "ConfigFile: /dev/null", | ||
| }, | ||
| { | ||
| cmd: "./gitea test-cmd -c /dev/null -h", | ||
| exp: "ConfigFile: /dev/null", | ||
| }, | ||
| { | ||
| cmd: "./gitea test-cmd -h -c /dev/null", | ||
| exp: "ConfigFile: /dev/null", | ||
| }, | ||
|
|
||
| { | ||
| cmd: "./gitea -c /dev/null test-cmd help", | ||
| exp: "ConfigFile: /dev/null", | ||
| }, | ||
| { | ||
| cmd: "./gitea test-cmd -c /dev/null help", | ||
| exp: "ConfigFile: /dev/null", | ||
| }, | ||
| { | ||
| cmd: "./gitea test-cmd help -c /dev/null", | ||
| exp: "ConfigFile: /dev/null", | ||
| }, | ||
|
|
||
| // parse paths | ||
| { | ||
| cmd: "./gitea test-cmd", | ||
| exp: makePathOutput(defaultWorkPath, defaultCustomPath, defaultCustomConf), | ||
| }, | ||
| { | ||
| cmd: "./gitea -c /tmp/app.ini test-cmd", | ||
| exp: makePathOutput(defaultWorkPath, defaultCustomPath, "/tmp/app.ini"), | ||
| }, | ||
| { | ||
| cmd: "./gitea test-cmd -c /tmp/app.ini", | ||
| exp: makePathOutput(defaultWorkPath, defaultCustomPath, "/tmp/app.ini"), | ||
| }, | ||
| { | ||
| env: map[string]string{"GITEA_WORK_DIR": "/tmp"}, | ||
| cmd: "./gitea test-cmd", | ||
| exp: makePathOutput("/tmp", "/tmp/custom", "/tmp/custom/conf/app.ini"), | ||
| }, | ||
| { | ||
| env: map[string]string{"GITEA_WORK_DIR": "/tmp"}, | ||
| cmd: "./gitea test-cmd --work-path /tmp/other", | ||
| exp: makePathOutput("/tmp/other", "/tmp/other/custom", "/tmp/other/custom/conf/app.ini"), | ||
| }, | ||
| { | ||
| env: map[string]string{"GITEA_WORK_DIR": "/tmp"}, | ||
| cmd: "./gitea test-cmd --config /tmp/app-other.ini", | ||
| exp: makePathOutput("/tmp", "/tmp/custom", "/tmp/app-other.ini"), | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.cmd, func(t *testing.T) { | ||
| app := newTestApp(cli.Command{ | ||
| Action: func(ctx context.Context, cmd *cli.Command) error { | ||
| _, _ = fmt.Fprint(cmd.Root().Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf)) | ||
| return nil | ||
| }, | ||
| }) | ||
| for k, v := range c.env { | ||
| t.Setenv(k, v) | ||
| } | ||
| args := strings.Split(c.cmd, " ") // for test only, "split" is good enough | ||
| r, err := runTestApp(app, args...) | ||
| assert.NoError(t, err, c.cmd) | ||
| assert.NotEmpty(t, c.exp, c.cmd) | ||
| if !assert.Contains(t, r.Stdout, c.exp, c.cmd) { | ||
| t.Log("Full output:\n" + r.Stdout) | ||
| t.Log("Expected:\n" + c.exp) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCliCmdError(t *testing.T) { | ||
| app := newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return errors.New("normal error") }}) | ||
| r, err := runTestApp(app, "./gitea", "test-cmd") | ||
| assert.Error(t, err) | ||
| assert.Equal(t, 1, r.ExitCode) | ||
| assert.Empty(t, r.Stdout) | ||
| assert.Equal(t, "Command error: normal error\n", r.Stderr) | ||
|
|
||
| app = newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return cli.Exit("exit error", 2) }}) | ||
| r, err = runTestApp(app, "./gitea", "test-cmd") | ||
| assert.Error(t, err) | ||
| assert.Equal(t, 2, r.ExitCode) | ||
| assert.Empty(t, r.Stdout) | ||
| assert.Equal(t, "exit error\n", r.Stderr) | ||
|
|
||
| app = newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return nil }}) | ||
| r, err = runTestApp(app, "./gitea", "test-cmd", "--no-such") | ||
| assert.Error(t, err) | ||
| assert.Equal(t, 1, r.ExitCode) | ||
| assert.Empty(t, r.Stdout) | ||
| assert.Equal(t, "Incorrect Usage: flag provided but not defined: -no-such\n\n", r.Stderr) | ||
|
|
||
| app = newTestApp(cli.Command{Action: func(ctx context.Context, cmd *cli.Command) error { return nil }}) | ||
| r, err = runTestApp(app, "./gitea", "test-cmd") | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, -1, r.ExitCode) // the cli.OsExiter is not called | ||
| assert.Empty(t, r.Stdout) | ||
| assert.Empty(t, r.Stderr) | ||
| } | ||
|
|
||
| func TestCliCmdBefore(t *testing.T) { | ||
| ctxNew := context.WithValue(context.Background(), any("key"), "value") | ||
| configValues := map[string]string{} | ||
| setting.CustomConf = "/tmp/any.ini" | ||
| var actionCtx context.Context | ||
| app := newTestApp(cli.Command{ | ||
| Before: func(context.Context, *cli.Command) (context.Context, error) { | ||
| configValues["before"] = setting.CustomConf | ||
| return ctxNew, nil | ||
| }, | ||
| Action: func(ctx context.Context, cmd *cli.Command) error { | ||
| configValues["action"] = setting.CustomConf | ||
| actionCtx = ctx | ||
| return nil | ||
| }, | ||
| }) | ||
| _, err := runTestApp(app, "./gitea", "--config", "/dev/null", "test-cmd") | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, ctxNew, actionCtx) | ||
| assert.Equal(t, "/tmp/any.ini", configValues["before"], "BeforeFunc must be called before preparing config") | ||
| assert.Equal(t, "/dev/null", configValues["action"]) | ||
| } |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.