diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2020-10-26 16:53:18 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2020-10-26 17:26:30 +0300 |
commit | a191b92a692619a93b22e09c3bf211a993da0aab (patch) | |
tree | 63d770a4a34436d2aab03ab4dba98b7ba3958261 | |
parent | 2257d9448da5a0fd566843c1b56ac22657dc5ebd (diff) |
gather test coverage from gitaly-git2gosmh-git2go-test-coverage
Gathers test coverage from gitaly-git2go by running functionality
in-process as well as in a subprocess.
-rw-r--r-- | cmd/gitaly-git2go/command/commit.go (renamed from cmd/gitaly-git2go/commit.go) | 4 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/commit/change_file_mode.go (renamed from cmd/gitaly-git2go/commit/change_file_mode.go) | 0 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/commit/commit.go (renamed from cmd/gitaly-git2go/commit/commit.go) | 9 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/commit/create_directory.go (renamed from cmd/gitaly-git2go/commit/create_directory.go) | 0 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/commit/create_file.go (renamed from cmd/gitaly-git2go/commit/create_file.go) | 0 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/commit/delete_file.go (renamed from cmd/gitaly-git2go/commit/delete_file.go) | 0 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/commit/move_file.go (renamed from cmd/gitaly-git2go/commit/move_file.go) | 0 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/commit/update_file.go (renamed from cmd/gitaly-git2go/commit/update_file.go) | 0 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/commit/validate.go (renamed from cmd/gitaly-git2go/commit/validate.go) | 0 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/conflicts.go (renamed from cmd/gitaly-git2go/conflicts.go) | 2 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/conflicts_test.go (renamed from cmd/gitaly-git2go/conflicts_test.go) | 2 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/main.go | 69 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/main_test.go (renamed from cmd/gitaly-git2go/main_test.go) | 2 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/merge.go (renamed from cmd/gitaly-git2go/merge.go) | 2 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/merge_test.go (renamed from cmd/gitaly-git2go/merge_test.go) | 2 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/revert.go (renamed from cmd/gitaly-git2go/revert.go) | 2 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/revert_test.go (renamed from cmd/gitaly-git2go/revert_test.go) | 2 | ||||
-rw-r--r-- | cmd/gitaly-git2go/command/util.go (renamed from cmd/gitaly-git2go/util.go) | 2 | ||||
-rw-r--r-- | cmd/gitaly-git2go/main.go | 54 | ||||
-rw-r--r-- | internal/git2go/commit.go | 2 | ||||
-rw-r--r-- | internal/git2go/commit_test.go | 222 | ||||
-rw-r--r-- | internal/git2go/executor.go | 20 | ||||
-rw-r--r-- | internal/git2go/executor_in_process_test.go | 42 | ||||
-rw-r--r-- | internal/git2go/executor_test.go | 29 |
24 files changed, 294 insertions, 173 deletions
diff --git a/cmd/gitaly-git2go/commit.go b/cmd/gitaly-git2go/command/commit.go index 3d8ac4c7f..82c388e20 100644 --- a/cmd/gitaly-git2go/commit.go +++ b/cmd/gitaly-git2go/command/commit.go @@ -1,13 +1,13 @@ // +build static,system_libgit2 -package main +package command import ( "context" "flag" "io" - "gitlab.com/gitlab-org/gitaly/cmd/gitaly-git2go/commit" + "gitlab.com/gitlab-org/gitaly/cmd/gitaly-git2go/command/commit" ) type commitSubcommand struct{} diff --git a/cmd/gitaly-git2go/commit/change_file_mode.go b/cmd/gitaly-git2go/command/commit/change_file_mode.go index 17200ee37..17200ee37 100644 --- a/cmd/gitaly-git2go/commit/change_file_mode.go +++ b/cmd/gitaly-git2go/command/commit/change_file_mode.go diff --git a/cmd/gitaly-git2go/commit/commit.go b/cmd/gitaly-git2go/command/commit/commit.go index 90862e1a7..8d00b7d04 100644 --- a/cmd/gitaly-git2go/commit/commit.go +++ b/cmd/gitaly-git2go/command/commit/commit.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "io" + "log" git "github.com/libgit2/git2go/v30" "gitlab.com/gitlab-org/gitaly/internal/git2go" @@ -62,6 +63,14 @@ func commit(ctx context.Context, params git2go.CommitParams) (string, error) { } } + if false { + log.Printf("never covered") + } + + if true { + log.Printf("covered") + } + for _, action := range params.Actions { if err := apply(action, repo, index); err != nil { return "", fmt.Errorf("apply action %T: %w", action, err) diff --git a/cmd/gitaly-git2go/commit/create_directory.go b/cmd/gitaly-git2go/command/commit/create_directory.go index c7059c0a3..c7059c0a3 100644 --- a/cmd/gitaly-git2go/commit/create_directory.go +++ b/cmd/gitaly-git2go/command/commit/create_directory.go diff --git a/cmd/gitaly-git2go/commit/create_file.go b/cmd/gitaly-git2go/command/commit/create_file.go index 62bc6f61b..62bc6f61b 100644 --- a/cmd/gitaly-git2go/commit/create_file.go +++ b/cmd/gitaly-git2go/command/commit/create_file.go diff --git a/cmd/gitaly-git2go/commit/delete_file.go b/cmd/gitaly-git2go/command/commit/delete_file.go index 0c9df3be6..0c9df3be6 100644 --- a/cmd/gitaly-git2go/commit/delete_file.go +++ b/cmd/gitaly-git2go/command/commit/delete_file.go diff --git a/cmd/gitaly-git2go/commit/move_file.go b/cmd/gitaly-git2go/command/commit/move_file.go index e9c115083..e9c115083 100644 --- a/cmd/gitaly-git2go/commit/move_file.go +++ b/cmd/gitaly-git2go/command/commit/move_file.go diff --git a/cmd/gitaly-git2go/commit/update_file.go b/cmd/gitaly-git2go/command/commit/update_file.go index 0bf6e7514..0bf6e7514 100644 --- a/cmd/gitaly-git2go/commit/update_file.go +++ b/cmd/gitaly-git2go/command/commit/update_file.go diff --git a/cmd/gitaly-git2go/commit/validate.go b/cmd/gitaly-git2go/command/commit/validate.go index 8854cdbbe..8854cdbbe 100644 --- a/cmd/gitaly-git2go/commit/validate.go +++ b/cmd/gitaly-git2go/command/commit/validate.go diff --git a/cmd/gitaly-git2go/conflicts.go b/cmd/gitaly-git2go/command/conflicts.go index 85f3c5a9c..5ae3fb71e 100644 --- a/cmd/gitaly-git2go/conflicts.go +++ b/cmd/gitaly-git2go/command/conflicts.go @@ -1,6 +1,6 @@ // +build static,system_libgit2 -package main +package command import ( "context" diff --git a/cmd/gitaly-git2go/conflicts_test.go b/cmd/gitaly-git2go/command/conflicts_test.go index 4cef16c12..5d81dc2b7 100644 --- a/cmd/gitaly-git2go/conflicts_test.go +++ b/cmd/gitaly-git2go/command/conflicts_test.go @@ -1,6 +1,6 @@ // +build static,system_libgit2 -package main +package command import ( "testing" diff --git a/cmd/gitaly-git2go/command/main.go b/cmd/gitaly-git2go/command/main.go new file mode 100644 index 000000000..15a07851b --- /dev/null +++ b/cmd/gitaly-git2go/command/main.go @@ -0,0 +1,69 @@ +// +build static,system_libgit2 + +package command + +import ( + "context" + "errors" + "flag" + "fmt" + "io" +) + +// Process encapsulates process global state. +type Process struct { + Args []string + Stdin io.Reader + Stdout io.Writer + Stderr io.Writer +} + +type subcmd interface { + Flags() *flag.FlagSet + Run(ctx context.Context, stdin io.Reader, stdout io.Writer) error +} + +var subcommands = map[string]subcmd{ + "conflicts": &conflictsSubcommand{}, + "commit": commitSubcommand{}, + "merge": &mergeSubcommand{}, + "revert": &revertSubcommand{}, +} + +const programName = "gitaly-git2go" + +func Main(p Process) int { + if err := main(p); err != nil { + fmt.Fprintf(p.Stderr, err.Error()+"\n") + return 1 + } + + return 0 +} + +func main(p Process) error { + flags := flag.NewFlagSet(programName, flag.ExitOnError) + flags.Parse(p.Args) + + if flags.NArg() < 2 { + return errors.New("missing subcommand") + } + + subcmd, ok := subcommands[flags.Arg(1)] + if !ok { + return fmt.Errorf("unknown subcommand: %q", flags.Arg(1)) + } + + subcmdFlags := subcmd.Flags() + subcmdFlags.Parse(flags.Args()[2:]) + + if subcmdFlags.NArg() != 0 { + return fmt.Errorf("%s: trailing arguments", subcmdFlags.Name()) + } + + if err := subcmd.Run(context.Background(), p.Stdin, p.Stdout); err != nil { + return fmt.Errorf("%s: %s", subcmdFlags.Name(), err) + } + + return nil +} diff --git a/cmd/gitaly-git2go/main_test.go b/cmd/gitaly-git2go/command/main_test.go index e9552633b..9b366a67e 100644 --- a/cmd/gitaly-git2go/main_test.go +++ b/cmd/gitaly-git2go/command/main_test.go @@ -1,6 +1,6 @@ // +build static,system_libgit2 -package main +package command import ( "os" diff --git a/cmd/gitaly-git2go/merge.go b/cmd/gitaly-git2go/command/merge.go index 5a9f10a73..c0d9b7b32 100644 --- a/cmd/gitaly-git2go/merge.go +++ b/cmd/gitaly-git2go/command/merge.go @@ -1,6 +1,6 @@ // +build static,system_libgit2 -package main +package command import ( "context" diff --git a/cmd/gitaly-git2go/merge_test.go b/cmd/gitaly-git2go/command/merge_test.go index c201560a0..c77d4cb0f 100644 --- a/cmd/gitaly-git2go/merge_test.go +++ b/cmd/gitaly-git2go/command/merge_test.go @@ -1,6 +1,6 @@ // +build static,system_libgit2 -package main +package command import ( "testing" diff --git a/cmd/gitaly-git2go/revert.go b/cmd/gitaly-git2go/command/revert.go index 110d50309..c18b147af 100644 --- a/cmd/gitaly-git2go/revert.go +++ b/cmd/gitaly-git2go/command/revert.go @@ -1,6 +1,6 @@ // +build static,system_libgit2 -package main +package command import ( "context" diff --git a/cmd/gitaly-git2go/revert_test.go b/cmd/gitaly-git2go/command/revert_test.go index 6c62b9f93..555d2fa25 100644 --- a/cmd/gitaly-git2go/revert_test.go +++ b/cmd/gitaly-git2go/command/revert_test.go @@ -1,6 +1,6 @@ // +build static,system_libgit2 -package main +package command import ( "testing" diff --git a/cmd/gitaly-git2go/util.go b/cmd/gitaly-git2go/command/util.go index a315c5f3a..f5a8937dd 100644 --- a/cmd/gitaly-git2go/util.go +++ b/cmd/gitaly-git2go/command/util.go @@ -1,6 +1,6 @@ // +build static,system_libgit2 -package main +package command import ( "fmt" diff --git a/cmd/gitaly-git2go/main.go b/cmd/gitaly-git2go/main.go index cfe5e5cde..aa13c1506 100644 --- a/cmd/gitaly-git2go/main.go +++ b/cmd/gitaly-git2go/main.go @@ -1,55 +1,17 @@ // +build static,system_libgit2 - package main import ( - "context" - "flag" - "fmt" - "io" "os" -) - -type subcmd interface { - Flags() *flag.FlagSet - Run(ctx context.Context, stdin io.Reader, stdout io.Writer) error -} - -var subcommands = map[string]subcmd{ - "conflicts": &conflictsSubcommand{}, - "commit": commitSubcommand{}, - "merge": &mergeSubcommand{}, - "revert": &revertSubcommand{}, -} - -const programName = "gitaly-git2go" -func fatalf(format string, args ...interface{}) { - fmt.Fprintf(os.Stderr, format+"\n", args...) - os.Exit(1) -} + "gitlab.com/gitlab-org/gitaly/cmd/gitaly-git2go/command" +) func main() { - flags := flag.NewFlagSet(programName, flag.ExitOnError) - flags.Parse(os.Args) - - if flags.NArg() < 2 { - fatalf("missing subcommand") - } - - subcmd, ok := subcommands[flags.Arg(1)] - if !ok { - fatalf("unknown subcommand: %q", flags.Arg(1)) - } - - subcmdFlags := subcmd.Flags() - subcmdFlags.Parse(flags.Args()[2:]) - - if subcmdFlags.NArg() != 0 { - fatalf("%s: trailing arguments", subcmdFlags.Name()) - } - - if err := subcmd.Run(context.Background(), os.Stdin, os.Stdout); err != nil { - fatalf("%s: %s", subcmdFlags.Name(), err) - } + os.Exit(command.Main(command.Process{ + Args: os.Args, + Stdin: os.Stdin, + Stdout: os.Stdout, + Stderr: os.Stderr, + })) } diff --git a/internal/git2go/commit.go b/internal/git2go/commit.go index ee24862d9..5b8fc430e 100644 --- a/internal/git2go/commit.go +++ b/internal/git2go/commit.go @@ -55,7 +55,7 @@ func (b Executor) Commit(ctx context.Context, params CommitParams) (string, erro return "", err } - output, err := run(ctx, b.binaryPath, input, "commit") + output, err := b.run(ctx, b.binaryPath, input, "commit") if err != nil { return "", err } diff --git a/internal/git2go/commit_test.go b/internal/git2go/commit_test.go index 6a1eea7a0..85df5e689 100644 --- a/internal/git2go/commit_test.go +++ b/internal/git2go/commit_test.go @@ -1,12 +1,10 @@ -package git2go +package git2go_test import ( "bytes" "context" "errors" "fmt" - "os" - "path/filepath" "strconv" "strings" "testing" @@ -14,31 +12,27 @@ import ( "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/git" - "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" + "gitlab.com/gitlab-org/gitaly/internal/git2go" "gitlab.com/gitlab-org/gitaly/internal/testhelper" ) -func TestMain(m *testing.M) { - testhelper.Configure() - testhelper.ConfigureGitalyGit2Go() - os.Exit(m.Run()) -} - type commit struct { Parent string - Author Signature - Committer Signature + Author git2go.Signature + Committer git2go.Signature Message string } -func TestExecutor_Commit(t *testing.T) { +func TestExecutor_Commit(t *testing.T) { testExecutors(t, testExecutor_Commit) } + +func testExecutor_Commit(t *testing.T, executor git2go.Executor) { const ( DefaultMode = "100644" ExecutableMode = "100755" ) type step struct { - actions []Action + actions []git2go.Action error error treeEntries []testhelper.TreeEntry } @@ -57,8 +51,6 @@ func TestExecutor_Commit(t *testing.T) { updatedFile, err := repo.WriteBlob(ctx, "file", bytes.NewBufferString("updated")) require.NoError(t, err) - executor := New(filepath.Join(config.Config.BinDir, "gitaly-git2go")) - for _, tc := range []struct { desc string steps []step @@ -67,8 +59,8 @@ func TestExecutor_Commit(t *testing.T) { desc: "create directory", steps: []step{ { - actions: []Action{ - CreateDirectory{Path: "directory"}, + actions: []git2go.Action{ + git2go.CreateDirectory{Path: "directory"}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "directory/.gitkeep"}, @@ -80,11 +72,11 @@ func TestExecutor_Commit(t *testing.T) { desc: "create directory created duplicate", steps: []step{ { - actions: []Action{ - CreateDirectory{Path: "directory"}, - CreateDirectory{Path: "directory"}, + actions: []git2go.Action{ + git2go.CreateDirectory{Path: "directory"}, + git2go.CreateDirectory{Path: "directory"}, }, - error: DirectoryExistsError("directory"), + error: git2go.DirectoryExistsError("directory"), }, }, }, @@ -92,18 +84,18 @@ func TestExecutor_Commit(t *testing.T) { desc: "create directory existing duplicate", steps: []step{ { - actions: []Action{ - CreateDirectory{Path: "directory"}, + actions: []git2go.Action{ + git2go.CreateDirectory{Path: "directory"}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "directory/.gitkeep"}, }, }, { - actions: []Action{ - CreateDirectory{Path: "directory"}, + actions: []git2go.Action{ + git2go.CreateDirectory{Path: "directory"}, }, - error: DirectoryExistsError("directory"), + error: git2go.DirectoryExistsError("directory"), }, }, }, @@ -111,18 +103,18 @@ func TestExecutor_Commit(t *testing.T) { desc: "create directory with a files name", steps: []step{ { - actions: []Action{ - CreateFile{Path: "file", OID: originalFile}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "file", OID: originalFile}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "file", Content: "original"}, }, }, { - actions: []Action{ - CreateDirectory{Path: "file"}, + actions: []git2go.Action{ + git2go.CreateDirectory{Path: "file"}, }, - error: FileExistsError("file"), + error: git2go.FileExistsError("file"), }, }, }, @@ -130,8 +122,8 @@ func TestExecutor_Commit(t *testing.T) { desc: "create file", steps: []step{ { - actions: []Action{ - CreateFile{Path: "file", OID: originalFile}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "file", OID: originalFile}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "file", Content: "original"}, @@ -143,11 +135,11 @@ func TestExecutor_Commit(t *testing.T) { desc: "create duplicate file", steps: []step{ { - actions: []Action{ - CreateFile{Path: "file", OID: originalFile}, - CreateFile{Path: "file", OID: updatedFile}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "file", OID: originalFile}, + git2go.CreateFile{Path: "file", OID: updatedFile}, }, - error: FileExistsError("file"), + error: git2go.FileExistsError("file"), }, }, }, @@ -155,9 +147,9 @@ func TestExecutor_Commit(t *testing.T) { desc: "create file overwrites directory", steps: []step{ { - actions: []Action{ - CreateDirectory{Path: "directory"}, - CreateFile{Path: "directory", OID: originalFile}, + actions: []git2go.Action{ + git2go.CreateDirectory{Path: "directory"}, + git2go.CreateFile{Path: "directory", OID: originalFile}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "directory", Content: "original"}, @@ -169,9 +161,9 @@ func TestExecutor_Commit(t *testing.T) { desc: "update created file", steps: []step{ { - actions: []Action{ - CreateFile{Path: "file", OID: originalFile}, - UpdateFile{Path: "file", OID: updatedFile}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "file", OID: originalFile}, + git2go.UpdateFile{Path: "file", OID: updatedFile}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "file", Content: "updated"}, @@ -183,16 +175,16 @@ func TestExecutor_Commit(t *testing.T) { desc: "update existing file", steps: []step{ { - actions: []Action{ - CreateFile{Path: "file", OID: originalFile}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "file", OID: originalFile}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "file", Content: "original"}, }, }, { - actions: []Action{ - UpdateFile{Path: "file", OID: updatedFile}, + actions: []git2go.Action{ + git2go.UpdateFile{Path: "file", OID: updatedFile}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "file", Content: "updated"}, @@ -204,10 +196,10 @@ func TestExecutor_Commit(t *testing.T) { desc: "update non-existing file", steps: []step{ { - actions: []Action{ - UpdateFile{Path: "non-existing", OID: updatedFile}, + actions: []git2go.Action{ + git2go.UpdateFile{Path: "non-existing", OID: updatedFile}, }, - error: FileNotFoundError("non-existing"), + error: git2go.FileNotFoundError("non-existing"), }, }, }, @@ -215,9 +207,9 @@ func TestExecutor_Commit(t *testing.T) { desc: "move created file", steps: []step{ { - actions: []Action{ - CreateFile{Path: "original-file", OID: originalFile}, - MoveFile{Path: "original-file", NewPath: "moved-file", OID: originalFile}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "original-file", OID: originalFile}, + git2go.MoveFile{Path: "original-file", NewPath: "moved-file", OID: originalFile}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "moved-file", Content: "original"}, @@ -229,11 +221,11 @@ func TestExecutor_Commit(t *testing.T) { desc: "moving directory fails", steps: []step{ { - actions: []Action{ - CreateDirectory{Path: "directory"}, - MoveFile{Path: "directory", NewPath: "moved-directory"}, + actions: []git2go.Action{ + git2go.CreateDirectory{Path: "directory"}, + git2go.MoveFile{Path: "directory", NewPath: "moved-directory"}, }, - error: FileNotFoundError("directory"), + error: git2go.FileNotFoundError("directory"), }, }, }, @@ -241,16 +233,16 @@ func TestExecutor_Commit(t *testing.T) { desc: "move file inferring content", steps: []step{ { - actions: []Action{ - CreateFile{Path: "original-file", OID: originalFile}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "original-file", OID: originalFile}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "original-file", Content: "original"}, }, }, { - actions: []Action{ - MoveFile{Path: "original-file", NewPath: "moved-file"}, + actions: []git2go.Action{ + git2go.MoveFile{Path: "original-file", NewPath: "moved-file"}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "moved-file", Content: "original"}, @@ -262,10 +254,10 @@ func TestExecutor_Commit(t *testing.T) { desc: "move file with non-existing source", steps: []step{ { - actions: []Action{ - MoveFile{Path: "non-existing", NewPath: "destination-file"}, + actions: []git2go.Action{ + git2go.MoveFile{Path: "non-existing", NewPath: "destination-file"}, }, - error: FileNotFoundError("non-existing"), + error: git2go.FileNotFoundError("non-existing"), }, }, }, @@ -273,12 +265,12 @@ func TestExecutor_Commit(t *testing.T) { desc: "move file with already existing destination file", steps: []step{ { - actions: []Action{ - CreateFile{Path: "source-file", OID: originalFile}, - CreateFile{Path: "already-existing", OID: updatedFile}, - MoveFile{Path: "source-file", NewPath: "already-existing"}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "source-file", OID: originalFile}, + git2go.CreateFile{Path: "already-existing", OID: updatedFile}, + git2go.MoveFile{Path: "source-file", NewPath: "already-existing"}, }, - error: FileExistsError("already-existing"), + error: git2go.FileExistsError("already-existing"), }, }, }, @@ -288,10 +280,10 @@ func TestExecutor_Commit(t *testing.T) { desc: "move file with already existing destination directory", steps: []step{ { - actions: []Action{ - CreateFile{Path: "file", OID: originalFile}, - CreateDirectory{Path: "already-existing"}, - MoveFile{Path: "file", NewPath: "already-existing"}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "file", OID: originalFile}, + git2go.CreateDirectory{Path: "already-existing"}, + git2go.MoveFile{Path: "file", NewPath: "already-existing"}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "already-existing", Content: "original"}, @@ -303,16 +295,16 @@ func TestExecutor_Commit(t *testing.T) { desc: "move file providing content", steps: []step{ { - actions: []Action{ - CreateFile{Path: "original-file", OID: originalFile}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "original-file", OID: originalFile}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "original-file", Content: "original"}, }, }, { - actions: []Action{ - MoveFile{Path: "original-file", NewPath: "moved-file", OID: updatedFile}, + actions: []git2go.Action{ + git2go.MoveFile{Path: "original-file", NewPath: "moved-file", OID: updatedFile}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "moved-file", Content: "updated"}, @@ -324,10 +316,10 @@ func TestExecutor_Commit(t *testing.T) { desc: "mark non-existing file executable", steps: []step{ { - actions: []Action{ - ChangeFileMode{Path: "non-existing"}, + actions: []git2go.Action{ + git2go.ChangeFileMode{Path: "non-existing"}, }, - error: FileNotFoundError("non-existing"), + error: git2go.FileNotFoundError("non-existing"), }, }, }, @@ -335,17 +327,17 @@ func TestExecutor_Commit(t *testing.T) { desc: "mark executable file executable", steps: []step{ { - actions: []Action{ - CreateFile{Path: "file-1", OID: originalFile}, - ChangeFileMode{Path: "file-1", ExecutableMode: true}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "file-1", OID: originalFile}, + git2go.ChangeFileMode{Path: "file-1", ExecutableMode: true}, }, treeEntries: []testhelper.TreeEntry{ {Mode: ExecutableMode, Path: "file-1", Content: "original"}, }, }, { - actions: []Action{ - ChangeFileMode{Path: "file-1", ExecutableMode: true}, + actions: []git2go.Action{ + git2go.ChangeFileMode{Path: "file-1", ExecutableMode: true}, }, treeEntries: []testhelper.TreeEntry{ {Mode: ExecutableMode, Path: "file-1", Content: "original"}, @@ -357,9 +349,9 @@ func TestExecutor_Commit(t *testing.T) { desc: "mark created file executable", steps: []step{ { - actions: []Action{ - CreateFile{Path: "file-1", OID: originalFile}, - ChangeFileMode{Path: "file-1", ExecutableMode: true}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "file-1", OID: originalFile}, + git2go.ChangeFileMode{Path: "file-1", ExecutableMode: true}, }, treeEntries: []testhelper.TreeEntry{ {Mode: ExecutableMode, Path: "file-1", Content: "original"}, @@ -371,16 +363,16 @@ func TestExecutor_Commit(t *testing.T) { desc: "mark existing file executable", steps: []step{ { - actions: []Action{ - CreateFile{Path: "file-1", OID: originalFile}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "file-1", OID: originalFile}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "file-1", Content: "original"}, }, }, { - actions: []Action{ - ChangeFileMode{Path: "file-1", ExecutableMode: true}, + actions: []git2go.Action{ + git2go.ChangeFileMode{Path: "file-1", ExecutableMode: true}, }, treeEntries: []testhelper.TreeEntry{ {Mode: ExecutableMode, Path: "file-1", Content: "original"}, @@ -392,10 +384,10 @@ func TestExecutor_Commit(t *testing.T) { desc: "move non-existing file", steps: []step{ { - actions: []Action{ - MoveFile{Path: "non-existing", NewPath: "destination"}, + actions: []git2go.Action{ + git2go.MoveFile{Path: "non-existing", NewPath: "destination"}, }, - error: FileNotFoundError("non-existing"), + error: git2go.FileNotFoundError("non-existing"), }, }, }, @@ -403,12 +395,12 @@ func TestExecutor_Commit(t *testing.T) { desc: "move doesn't overwrite a file", steps: []step{ { - actions: []Action{ - CreateFile{Path: "file-1", OID: originalFile}, - CreateFile{Path: "file-2", OID: updatedFile}, - MoveFile{Path: "file-1", NewPath: "file-2"}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "file-1", OID: originalFile}, + git2go.CreateFile{Path: "file-2", OID: updatedFile}, + git2go.MoveFile{Path: "file-1", NewPath: "file-2"}, }, - error: FileExistsError("file-2"), + error: git2go.FileExistsError("file-2"), }, }, }, @@ -416,10 +408,10 @@ func TestExecutor_Commit(t *testing.T) { desc: "delete non-existing file", steps: []step{ { - actions: []Action{ - DeleteFile{Path: "non-existing"}, + actions: []git2go.Action{ + git2go.DeleteFile{Path: "non-existing"}, }, - error: FileNotFoundError("non-existing"), + error: git2go.FileNotFoundError("non-existing"), }, }, }, @@ -427,9 +419,9 @@ func TestExecutor_Commit(t *testing.T) { desc: "delete created file", steps: []step{ { - actions: []Action{ - CreateFile{Path: "file-1", OID: originalFile}, - DeleteFile{Path: "file-1"}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "file-1", OID: originalFile}, + git2go.DeleteFile{Path: "file-1"}, }, }, }, @@ -438,27 +430,27 @@ func TestExecutor_Commit(t *testing.T) { desc: "delete existing file", steps: []step{ { - actions: []Action{ - CreateFile{Path: "file-1", OID: originalFile}, + actions: []git2go.Action{ + git2go.CreateFile{Path: "file-1", OID: originalFile}, }, treeEntries: []testhelper.TreeEntry{ {Mode: DefaultMode, Path: "file-1", Content: "original"}, }, }, { - actions: []Action{ - DeleteFile{Path: "file-1"}, + actions: []git2go.Action{ + git2go.DeleteFile{Path: "file-1"}, }, }, }, }, } { t.Run(tc.desc, func(t *testing.T) { - author := NewSignature("Author Name", "author.email@example.com", time.Now()) + author := git2go.NewSignature("Author Name", "author.email@example.com", time.Now()) var parentCommit string for i, step := range tc.steps { message := fmt.Sprintf("commit %d", i+1) - commitID, err := executor.Commit(ctx, CommitParams{ + commitID, err := executor.Commit(ctx, git2go.CommitParams{ Repository: repoPath, Author: author, Message: message, @@ -522,7 +514,7 @@ func getCommit(t testing.TB, ctx context.Context, repo git.Repository, oid strin return commit } -func unmarshalSignature(t testing.TB, data string) Signature { +func unmarshalSignature(t testing.TB, data string) git2go.Signature { t.Helper() // Format: NAME <EMAIL> DATE_UNIX DATE_TIMEZONE @@ -538,7 +530,7 @@ func unmarshalSignature(t testing.TB, data string) Signature { timestamp, err := strconv.ParseInt(split3[0], 10, 64) require.NoError(t, err) - return Signature{ + return git2go.Signature{ Name: split1[0], Email: split2[0], When: time.Unix(timestamp, 0), diff --git a/internal/git2go/executor.go b/internal/git2go/executor.go index 0007ab7e5..f6578cbc8 100644 --- a/internal/git2go/executor.go +++ b/internal/git2go/executor.go @@ -1,11 +1,29 @@ package git2go +import ( + "bytes" + "context" + "io" +) + +// RunFunc runns a command. Used for testing. +type RunFunc func(context.Context, string, io.Reader, ...string) (*bytes.Buffer, error) + // Executor executes gitaly-git2go. type Executor struct { binaryPath string + run RunFunc } // New returns a new gitaly-git2go executor using the provided binary. func New(binaryPath string) Executor { - return Executor{binaryPath: binaryPath} + return Executor{ + binaryPath: binaryPath, + run: run, + } +} + +// NewWithRunner returns an Executor using the given run method. Used for testing. +func NewWithRunner(run RunFunc) Executor { + return Executor{run: run} } diff --git a/internal/git2go/executor_in_process_test.go b/internal/git2go/executor_in_process_test.go new file mode 100644 index 000000000..73b0258f9 --- /dev/null +++ b/internal/git2go/executor_in_process_test.go @@ -0,0 +1,42 @@ +// +build static,system_libgit2 + +package git2go_test + +import ( + "bytes" + "context" + "fmt" + "io" + + "gitlab.com/gitlab-org/gitaly/cmd/gitaly-git2go/command" + "gitlab.com/gitlab-org/gitaly/internal/git2go" +) + +func init() { + // When the test binary is compiled with libgit2, we'll run + // the tests additionally by making direct calls to gitaly-git2go + // without using a subprocess. This makes it possible to gather + // test coverage for the code in the subprocess as well. + bridge := git2go.NewWithRunner(func(ctx context.Context, binaryPath string, stdin io.Reader, args ...string) (*bytes.Buffer, error) { + if stdin == nil { + stdin = bytes.NewReader(nil) + } + + stderr := &bytes.Buffer{} + stdout := &bytes.Buffer{} + errcode := command.Main(command.Process{ + Args: append([]string{"in-process"}, args...), + Stdin: stdin, + Stdout: stdout, + Stderr: stderr, + }) + + if errcode != 0 { + return nil, fmt.Errorf("code: %d, stderr: %q", errcode, stderr) + } + + return stdout, nil + }) + + executors["in-process"] = bridge +} diff --git a/internal/git2go/executor_test.go b/internal/git2go/executor_test.go new file mode 100644 index 000000000..c345f7c36 --- /dev/null +++ b/internal/git2go/executor_test.go @@ -0,0 +1,29 @@ +package git2go_test + +import ( + "os" + "path/filepath" + "testing" + + "gitlab.com/gitlab-org/gitaly/internal/git2go" + "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" + "gitlab.com/gitlab-org/gitaly/internal/testhelper" +) + +var executors = map[string]git2go.Executor{} + +func TestMain(m *testing.M) { + testhelper.Configure() + testhelper.ConfigureGitalyGit2Go() + + executors["subprocess"] = git2go.New(filepath.Join(config.Config.BinDir, "gitaly-git2go")) + + os.Exit(m.Run()) +} + +// testExecutor is a helper for running a test with a subprocess and in-process. +func testExecutors(t *testing.T, test func(*testing.T, git2go.Executor)) { + for name, executor := range executors { + t.Run(name, func(t *testing.T) { test(t, executor) }) + } +} |