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

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2022-08-05 10:57:26 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2022-08-05 11:06:54 +0300
commitd40efdcbdc0180e0d86681fe7ce4ca8fdec6767f (patch)
treeea5ef85b7d9ca810d9ca4cba8bfd85cd22fefcfe
parent50a9249550320cd41ef3fa75fd317df1e087e6a3 (diff)
lstree: Refactor parser test to generate data at runtime
The lstree's parser tests use static data of git-ls-tree(1) that has been written into a set of files. This makes it hard to test this package with SHA256 given that the output of course contains SHA1 object hashes. Refactor the tests to instead generate data at runtime. Note that we're removing one of the two testcases in this process: both testcases do in fact exercise the exact same logic, only that one of both has a space in one of its tree entries. So we just retain the test for the tree entry with the space and discard the other one given that they don't really test different things anyway.
-rw-r--r--internal/git/lstree/parser_test.go98
-rw-r--r--internal/git/lstree/testdata/z-lstree-irregular.txtbin265 -> 0 bytes
-rw-r--r--internal/git/lstree/testdata/z-lstree.txtbin260 -> 0 bytes
3 files changed, 37 insertions, 61 deletions
diff --git a/internal/git/lstree/parser_test.go b/internal/git/lstree/parser_test.go
index 6f8bb1912..3eb9a9111 100644
--- a/internal/git/lstree/parser_test.go
+++ b/internal/git/lstree/parser_test.go
@@ -3,91 +3,74 @@
package lstree
import (
+ "bytes"
"io"
- "os"
"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/testhelper/testcfg"
)
func TestParser(t *testing.T) {
- testCases := []struct {
- desc string
- filename string
- entries Entries
+ t.Parallel()
+
+ cfg := testcfg.Build(t)
+ _, repoPath := gittest.InitRepo(t, cfg, cfg.Storages[0])
+
+ gitignoreBlobID := gittest.WriteBlob(t, cfg, repoPath, []byte("gitignore"))
+ gitmodulesBlobID := gittest.WriteBlob(t, cfg, repoPath, []byte("gitmodules"))
+ submoduleCommitID := gittest.WriteCommit(t, cfg, repoPath)
+
+ regularEntriesTreeID := gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{
+ {Path: ".gitignore", Mode: "100644", OID: gitignoreBlobID},
+ {Path: ".gitmodules", Mode: "100644", OID: gitmodulesBlobID},
+ {Path: "entry with space", Mode: "040000", OID: gittest.DefaultObjectHash.EmptyTreeOID},
+ {Path: "gitlab-shell", Mode: "160000", OID: submoduleCommitID},
+ })
+
+ for _, tc := range []struct {
+ desc string
+ treeID git.ObjectID
+ expectedEntries Entries
}{
{
- desc: "regular entries",
- filename: "testdata/z-lstree.txt",
- entries: Entries{
+ desc: "regular entries",
+ treeID: regularEntriesTreeID,
+ expectedEntries: Entries{
{
Mode: []byte("100644"),
Type: Blob,
- ObjectID: "dfaa3f97ca337e20154a98ac9d0be76ddd1fcc82",
+ ObjectID: gitignoreBlobID,
Path: ".gitignore",
},
{
Mode: []byte("100644"),
Type: Blob,
- ObjectID: "0792c58905eff3432b721f8c4a64363d8e28d9ae",
+ ObjectID: gitmodulesBlobID,
Path: ".gitmodules",
},
{
Mode: []byte("040000"),
Type: Tree,
- ObjectID: "3c122d2b7830eca25235131070602575cf8b41a1",
- Path: "encoding",
+ ObjectID: gittest.DefaultObjectHash.EmptyTreeOID,
+ Path: "entry with space",
},
{
Mode: []byte("160000"),
Type: Submodule,
- ObjectID: "79bceae69cb5750d6567b223597999bfa91cb3b9",
+ ObjectID: submoduleCommitID,
Path: "gitlab-shell",
},
},
},
- {
- desc: "irregular path",
- filename: "testdata/z-lstree-irregular.txt",
- entries: Entries{
- {
- Mode: []byte("100644"),
- Type: Blob,
- ObjectID: "dfaa3f97ca337e20154a98ac9d0be76ddd1fcc82",
- Path: ".gitignore",
- },
- {
- Mode: []byte("100644"),
- Type: Blob,
- ObjectID: "0792c58905eff3432b721f8c4a64363d8e28d9ae",
- Path: ".gitmodules",
- },
- {
- Mode: []byte("040000"),
- Type: Tree,
- ObjectID: "3c122d2b7830eca25235131070602575cf8b41a1",
- Path: "some encoding",
- },
- {
- Mode: []byte("160000"),
- Type: Submodule,
- ObjectID: "79bceae69cb5750d6567b223597999bfa91cb3b9",
- Path: "gitlab-shell",
- },
- },
- },
- }
-
- for _, testCase := range testCases {
- t.Run(testCase.desc, func(t *testing.T) {
- file, err := os.Open(testCase.filename)
-
- require.NoError(t, err)
- defer file.Close()
+ } {
+ t.Run(tc.desc, func(t *testing.T) {
+ treeData := gittest.Exec(t, cfg, "-C", repoPath, "ls-tree", "-z", tc.treeID.String())
+ parser := NewParser(bytes.NewReader(treeData))
parsedEntries := Entries{}
-
- parser := NewParser(file)
for {
entry, err := parser.NextEntry()
if err == io.EOF {
@@ -98,14 +81,7 @@ func TestParser(t *testing.T) {
parsedEntries = append(parsedEntries, *entry)
}
- expectedEntries := testCase.entries
- require.Len(t, expectedEntries, len(parsedEntries))
-
- for index, parsedEntry := range parsedEntries {
- expectedEntry := expectedEntries[index]
-
- require.Equal(t, expectedEntry, parsedEntry)
- }
+ require.Equal(t, tc.expectedEntries, parsedEntries)
})
}
}
diff --git a/internal/git/lstree/testdata/z-lstree-irregular.txt b/internal/git/lstree/testdata/z-lstree-irregular.txt
deleted file mode 100644
index ed55df3a4..000000000
--- a/internal/git/lstree/testdata/z-lstree-irregular.txt
+++ /dev/null
Binary files differ
diff --git a/internal/git/lstree/testdata/z-lstree.txt b/internal/git/lstree/testdata/z-lstree.txt
deleted file mode 100644
index 653a2f8b1..000000000
--- a/internal/git/lstree/testdata/z-lstree.txt
+++ /dev/null
Binary files differ