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`tokarchuk/v1.17
parent
d4f84f1c93
commit
b877504b03
@ -0,0 +1,40 @@ |
|||||||
|
// Copyright 2017 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 TestRunWithContextNoTimeout(t *testing.T) { |
||||||
|
maxLoops := 10 |
||||||
|
|
||||||
|
// 'git --version' does not block so it must be finished before the timeout triggered.
|
||||||
|
cmd := NewCommand(context.Background(), "--version") |
||||||
|
for i := 0; i < maxLoops; i++ { |
||||||
|
if err := cmd.RunWithContext(&RunContext{}); err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestRunWithContextTimeout(t *testing.T) { |
||||||
|
maxLoops := 10 |
||||||
|
|
||||||
|
// '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.RunWithContext(&RunContext{Timeout: 1 * time.Millisecond}); err != nil { |
||||||
|
if err != context.DeadlineExceeded { |
||||||
|
t.Fatalf("Testing %d/%d: %v", i, maxLoops, err) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -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
|
// Use of this source code is governed by a MIT-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
//go:build race
|
|
||||||
// +build race
|
|
||||||
|
|
||||||
package git |
package git |
||||||
|
|
||||||
import ( |
import ( |
||||||
"context" |
"context" |
||||||
"testing" |
"testing" |
||||||
"time" |
|
||||||
) |
|
||||||
|
|
||||||
func TestRunInDirTimeoutPipelineNoTimeout(t *testing.T) { |
"github.com/stretchr/testify/assert" |
||||||
maxLoops := 1000 |
) |
||||||
|
|
||||||
// 'git --version' does not block so it must be finished before the timeout triggered.
|
func TestRunWithContextStd(t *testing.T) { |
||||||
cmd := NewCommand(context.Background(), "--version") |
cmd := NewCommand(context.Background(), "--version") |
||||||
for i := 0; i < maxLoops; i++ { |
stdout, stderr, err := cmd.RunWithContextString(&RunContext{}) |
||||||
if err := cmd.RunInDirTimeoutPipeline(-1, "", nil, nil); err != nil { |
assert.NoError(t, err) |
||||||
t.Fatal(err) |
assert.Empty(t, stderr) |
||||||
} |
assert.Contains(t, stdout, "git version") |
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func TestRunInDirTimeoutPipelineAlwaysTimeout(t *testing.T) { |
|
||||||
maxLoops := 1000 |
|
||||||
|
|
||||||
// 'git hash-object --stdin' blocks on stdin so we can have the timeout triggered.
|
cmd = NewCommand(context.Background(), "--no-such-arg") |
||||||
cmd := NewCommand(context.Background(), "hash-object", "--stdin") |
stdout, stderr, err = cmd.RunWithContextString(&RunContext{}) |
||||||
for i := 0; i < maxLoops; i++ { |
if assert.Error(t, err) { |
||||||
if err := cmd.RunInDirTimeoutPipeline(1*time.Microsecond, "", nil, nil); err != nil { |
assert.Equal(t, stderr, err.Stderr()) |
||||||
if err != context.DeadlineExceeded { |
assert.Contains(t, err.Stderr(), "unknown option:") |
||||||
t.Fatalf("Testing %d/%d: %v", i, maxLoops, err) |
assert.Contains(t, err.Error(), "exit status 129 - unknown option:") |
||||||
} |
assert.Empty(t, stdout) |
||||||
} |
|
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue