diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2022-03-31 19:56:22 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-31 13:56:22 +0200 |
commit | b877504b03a43dc5ed19109f1a00f08a343ebaba (patch) | |
tree | e1769eab011325f444b07fd388a7f7430abc85c6 /modules/git/command_test.go | |
parent | d4f84f1c937e24e71aa5f05c58d440cde741450f (diff) | |
download | gitea-b877504b03a43dc5ed19109f1a00f08a343ebaba.tar.gz gitea-b877504b03a43dc5ed19109f1a00f08a343ebaba.zip |
Refactor `git.Command.Run*`, introduce `RunWithContextString` and `RunWithContextBytes` (#19266)
This follows
* https://github.com/go-gitea/gitea/issues/18553
Introduce `RunWithContextString` and `RunWithContextBytes` to help the refactoring. Add related unit tests. They keep the same behavior to save stderr into err.Error() as `RunInXxx` before.
Remove `RunInDirTimeoutPipeline` `RunInDirTimeoutFullPipeline` `RunInDirTimeout` `RunInDirTimeoutEnv` `RunInDirPipeline` `RunInDirFullPipeline` `RunTimeout`, `RunInDirTimeoutEnvPipeline`, `RunInDirTimeoutEnvFullPipeline`, `RunInDirTimeoutEnvFullPipelineFunc`.
Then remaining `RunInDir` `RunInDirBytes` `RunInDirWithEnv` can be easily refactored in next PR with a simple search & replace:
* before: `stdout, err := RunInDir(path)`
* next: `stdout, _, err := RunWithContextString(&git.RunContext{Dir:path})`
Other changes:
1. When `timeout <= 0`, use default. Because `timeout==0` is meaningless and could cause bugs. And now many functions becomes more simple, eg: `GitGcRepos` 9 lines to 1 line. `Fsck` 6 lines to 1 line.
2. Only set defaultCommandExecutionTimeout when the option `setting.Git.Timeout.Default > 0`
Diffstat (limited to 'modules/git/command_test.go')
-rw-r--r-- | modules/git/command_test.go | 41 |
1 files changed, 15 insertions, 26 deletions
diff --git a/modules/git/command_test.go b/modules/git/command_test.go index f92f526d2d..33a6661d45 100644 --- a/modules/git/command_test.go +++ b/modules/git/command_test.go @@ -1,40 +1,29 @@ -// Copyright 2017 The Gitea Authors. All rights reserved. +// Copyright 2022 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -//go:build race -// +build race - package git import ( "context" "testing" - "time" -) -func TestRunInDirTimeoutPipelineNoTimeout(t *testing.T) { - maxLoops := 1000 + "github.com/stretchr/testify/assert" +) - // 'git --version' does not block so it must be finished before the timeout triggered. +func TestRunWithContextStd(t *testing.T) { cmd := NewCommand(context.Background(), "--version") - for i := 0; i < maxLoops; i++ { - if err := cmd.RunInDirTimeoutPipeline(-1, "", nil, nil); err != nil { - t.Fatal(err) - } - } -} - -func TestRunInDirTimeoutPipelineAlwaysTimeout(t *testing.T) { - maxLoops := 1000 + stdout, stderr, err := cmd.RunWithContextString(&RunContext{}) + assert.NoError(t, err) + assert.Empty(t, stderr) + assert.Contains(t, stdout, "git version") - // 'git hash-object --stdin' blocks on stdin so we can have the timeout triggered. - cmd := NewCommand(context.Background(), "hash-object", "--stdin") - for i := 0; i < maxLoops; i++ { - if err := cmd.RunInDirTimeoutPipeline(1*time.Microsecond, "", nil, nil); err != nil { - if err != context.DeadlineExceeded { - t.Fatalf("Testing %d/%d: %v", i, maxLoops, err) - } - } + cmd = NewCommand(context.Background(), "--no-such-arg") + stdout, stderr, err = cmd.RunWithContextString(&RunContext{}) + 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) } } |