Welcome to mirror list, hosted at ThFree Co, Russian Federation.

submodule_test.go « gitaly-git2go-v15 « cmd - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e6c2a5bdf4ca5605f333519b1078b4aeafa401b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//go:build static && system_libgit2 && !gitaly_test_sha256

package main

import (
	"bytes"
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v15/internal/git"
	"gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest"
	"gitlab.com/gitlab-org/gitaly/v15/internal/git/localrepo"
	"gitlab.com/gitlab-org/gitaly/v15/internal/git/lstree"
	"gitlab.com/gitlab-org/gitaly/v15/internal/git2go"
	"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper"
	"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg"
)

func TestSubmodule(t *testing.T) {
	commitMessage := []byte("Update Submodule message")

	testCases := []struct {
		desc           string
		command        git2go.SubmoduleCommand
		expectedStderr string
	}{
		{
			desc: "Update submodule",
			command: git2go.SubmoduleCommand{
				AuthorName: string(gittest.TestUser.Name),
				AuthorMail: string(gittest.TestUser.Email),
				Message:    string(commitMessage),
				CommitSHA:  "41fa1bc9e0f0630ced6a8a211d60c2af425ecc2d",
				Submodule:  "gitlab-grack",
				Branch:     "master",
			},
		},
		{
			desc: "Update submodule inside folder",
			command: git2go.SubmoduleCommand{
				AuthorName: string(gittest.TestUser.Name),
				AuthorMail: string(gittest.TestUser.Email),
				Message:    string(commitMessage),
				CommitSHA:  "e25eda1fece24ac7a03624ed1320f82396f35bd8",
				Submodule:  "test_inside_folder/another_folder/six",
				Branch:     "submodule_inside_folder",
			},
		},
		{
			desc: "Invalid branch",
			command: git2go.SubmoduleCommand{
				AuthorName: string(gittest.TestUser.Name),
				AuthorMail: string(gittest.TestUser.Email),
				Message:    string(commitMessage),
				CommitSHA:  "e25eda1fece24ac7a03624ed1320f82396f35bd8",
				Submodule:  "test_inside_folder/another_folder/six",
				Branch:     "non/existent",
			},
			expectedStderr: "Invalid branch",
		},
		{
			desc: "Invalid submodule",
			command: git2go.SubmoduleCommand{
				AuthorName: string(gittest.TestUser.Name),
				AuthorMail: string(gittest.TestUser.Email),
				Message:    string(commitMessage),
				CommitSHA:  "e25eda1fece24ac7a03624ed1320f82396f35bd8",
				Submodule:  "non-existent-submodule",
				Branch:     "master",
			},
			expectedStderr: "Invalid submodule path",
		},
		{
			desc: "Duplicate reference",
			command: git2go.SubmoduleCommand{
				AuthorName: string(gittest.TestUser.Name),
				AuthorMail: string(gittest.TestUser.Email),
				Message:    string(commitMessage),
				CommitSHA:  "409f37c4f05865e4fb208c771485f211a22c4c2d",
				Submodule:  "six",
				Branch:     "master",
			},
			expectedStderr: "The submodule six is already at 409f37c4f",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			cfg, repoProto, repoPath := testcfg.BuildWithRepo(t)
			testcfg.BuildGitalyGit2Go(t, cfg)
			repo := localrepo.NewTestRepo(t, cfg, repoProto)
			executor := buildExecutor(t, cfg)

			tc.command.Repository = repoPath
			ctx := testhelper.Context(t)

			response, err := executor.Submodule(ctx, repo, tc.command)
			if tc.expectedStderr != "" {
				require.Error(t, err)
				require.Contains(t, err.Error(), tc.expectedStderr)
				return
			}
			require.NoError(t, err)

			commit, err := repo.ReadCommit(ctx, git.Revision(response.CommitID))
			require.NoError(t, err)
			require.Equal(t, commit.Author.Email, gittest.TestUser.Email)
			require.Equal(t, commit.Committer.Email, gittest.TestUser.Email)
			require.Equal(t, commit.Subject, commitMessage)

			entry := gittest.Exec(
				t,
				cfg,
				"-C",
				repoPath,
				"ls-tree",
				"-z",
				fmt.Sprintf("%s^{tree}:", response.CommitID),
				tc.command.Submodule,
			)
			parser := lstree.NewParser(bytes.NewReader(entry))
			parsedEntry, err := parser.NextEntry()
			require.NoError(t, err)
			require.Equal(t, tc.command.Submodule, parsedEntry.Path)
			require.Equal(t, tc.command.CommitSHA, parsedEntry.ObjectID.String())
		})
	}
}