Exemple #1
0
func testShortRefResolvesSymbolic(t *testing.T, repo Repository, spec string, tget string, oid *objects.ObjectId) {
	symbolicRef, err := RefFromSpec(repo, spec)
	util.AssertNoErr(t, err)
	assertSymbolicRef(t, symbolicRef, tget)

	peeledRef, err := PeeledRefFromSpec(repo, spec)
	util.AssertNoErr(t, err)
	assertPeeledRef(t, peeledRef, oid)
}
Exemple #2
0
func testShortRefResolvesPeeled(t *testing.T, repo Repository, spec string, oid *objects.ObjectId) {
	peeledRef, err := RefFromSpec(repo, spec)
	util.AssertNoErr(t, err)
	assertPeeledRef(t, peeledRef, oid)

	peeledRef, err = PeeledRefFromSpec(repo, spec)
	util.AssertNoErr(t, err)
	assertPeeledRef(t, peeledRef, oid)
}
Exemple #3
0
// Test_readCommits will compare the commit output of
// git and ggit for a string of commits.
func Test_readTree(t *testing.T) {
	testCase := test.TreeDiff
	repo := api.Open(testCase.Repo())
	info, _ := testCase.Info().(*test.InfoTreeDiff)

	oid := objects.OidNow(info.TreeOid1)
	o, err := repo.ObjectFromOid(oid)
	util.AssertNoErr(t, err)
	// get the tree
	tree1, _ := o.(*objects.Tree)

	oid = objects.OidNow(info.TreeOid2)
	o, err = repo.ObjectFromOid(oid)
	util.AssertNoErr(t, err)
	// get the tree
	tree2, _ := o.(*objects.Tree)

	diff, err := NewTreeDiffer(repo, NewBlobComparator()).Diff(tree1, tree2)
	modified := diff.Modified()

	if len(modified) != 1 {
		t.Errorf("expected 1 modification in the tree diff. got %d", len(modified))
	}

	edit := modified[0]
	before, after := edit.Before, edit.After
	beforeOid, afterOid := before.ObjectId(), after.ObjectId()
	beforeName := before.Name()

	if beforeName != info.ModifiedFileName {
		t.Errorf("expected modified file name to be [%s], but got [%s]", info.ModifiedFileName, beforeName)
	}

	if beforeOid.String() != info.ModifiedFileBeforeOid {
		t.Errorf("expected modified file's initial id to be [%s], but got [%s]", info.ModifiedFileBeforeOid, beforeOid)
	}

	if afterOid.String() != info.ModifiedFileAfterOid {
		t.Errorf("expected modified file's initial id to be [%s], but got [%s]", info.ModifiedFileAfterOid, afterOid)
	}

	deleted := diff.Deleted()

	util.Assert(t, len(deleted) == 1, fmt.Sprintf("expected 1 file to be deleted, got [%d]", len(deleted)))
	entry := deleted[0]
	deletedName, deletedOid := entry.Name(), entry.ObjectId().String()
	if deletedName != info.RemovedFileName {
		t.Errorf("expected deleted file's name to be [%s], but got [%s]", info.RemovedFileName, deletedName)
	}
	if deletedOid != info.RemovedFileOid {
		t.Errorf("expected deleted file's id to be [%s], but got [%s]", info.RemovedFileOid, deletedOid)
	}
}
Exemple #4
0
func testPeelRef(t *testing.T, repo Repository, spec string, oid *objects.ObjectId) {
	// first peel it manually
	ref, err := repo.Ref(spec)
	util.AssertNoErr(t, err)
	util.AssertEqualString(t, ref.Name(), spec)
	peeledRef, err := PeelRef(repo, ref)
	util.AssertNoErr(t, err)
	assertPeeledRef(t, peeledRef, oid)

	// now, peel it automatically
	ref, err = PeeledRefFromSpec(repo, spec)
	util.AssertNoErr(t, err)
	peeledRef, err = PeelRef(repo, ref)
	util.AssertNoErr(t, err)
	assertPeeledRef(t, peeledRef, oid)
}
Exemple #5
0
func testRefPathSymbolic(t *testing.T, repo Repository, spec string, tget string) {
	symbolicRef, err := repo.Ref(spec)
	util.AssertNoErr(t, err)
	util.AssertEqualString(t, symbolicRef.Name(), spec)
	// make sure target is symbolic and matches
	assertSymbolicRef(t, symbolicRef, tget)
}
Exemple #6
0
// parseAndCompareTree will take a string tree representation and compare
// it with that tree from the provided repository. A tree representation
// has the following format:
//
//     <oid_of_this_tree><LF>
//     <size><LF>
//     <output_of_cat_file_p>
//
// Example:
//
// `dc5c0113b3d246da13b9d4b54861d2894da45e5a
// 348
// 100644 blob 022f0ad6583f99c3c01d0245db452e58bbb8fc8a	.gitignore
// 100644 blob 4760db634060e0f5c76eddb18031d53bff882a23	README
// 040000 tree 888fd8c9a6612c01d8f7c4e71fef114c3d3386d5	api
// 040000 tree 7ba590c4aa02ba2268f732f70b6143cd60457782	builtin
// 040000 tree bd52eecc257922f4c9008d080ad7dd5f9ac3444e	dotfiles
// 040000 tree 9ee2de084ec91660183740dacd8988c8170537e3	ggcase
// 100644 blob 82a1896a6d17e56220681fb66f94ac8a7e1d9bdc	ggit.go
// 100644 blob 3b20433fdf8544b2cff26989c5fc63327137e809	templates.go
// 040000 tree d42d8e7cf260b951e17687404031b1975ffa0b38	test
// 100644 blob 0c01aeb4856324d8487a252e32e04961bf603fad	version.go
// `
func parseAndCompareTree(t *testing.T, repo Repository, treeRepr string) {
	p := parse.ObjectParserForString(treeRepr)

	oid := p.ParseOid()
	p.ConsumeByte(token.LF)
	size := p.ParseInt(token.LF, 10, 32)

	o, err := repo.ObjectFromOid(oid)
	util.AssertNoErr(t, err)

	hdr := o.Header()
	util.Assert(t, hdr.Type() == objects.ObjectTree)
	util.Assert(t, hdr.Size() == size)

	tree := o.(*objects.Tree)
	entries := tree.Entries()

	for !p.EOF() {
		mode := p.ParseFileMode(token.SP)
		otype := objects.ObjectType(p.ConsumeStrings(token.ObjectTypes))
		p.ConsumeByte(token.SP)
		oidStr := p.ParseOid().String()
		p.ConsumeByte(token.TAB)
		name := p.ReadString(token.LF)
		util.Assert(t, len(entries) > 0)
		entry := entries[0]
		util.Assert(t, mode == entry.Mode())
		util.Assertf(t, name == entry.Name(), "expecting: `%s` got: `%s", name, entry.Name())
		util.Assert(t, otype == entry.ObjectType())
		util.Assertf(t, oidStr == entry.ObjectId().String(), "expecting: `%s` got: `%s", oidStr, entry.ObjectId())
		entries = entries[1:]
	}
	util.Assert(t, len(entries) == 0)
}
Exemple #7
0
// testShortOid retrives the object by all possible combinations of
// shortening its id.
func testShortOid(t *testing.T, repo Repository, oid *objects.ObjectId) {
	rev := oid.String()
	for i := 4; i <= 40; i++ {
		o, err := ObjectFromRevision(repo, rev[:i])
		util.AssertNoErr(t, err)
		util.AssertEqualString(t, o.ObjectId().String(), oid.String())
	}
}
Exemple #8
0
func testRefRetrieval(t *testing.T, repo Repository, f func() ([]objects.Ref, error), expected []string) {
	refs, err := f()
	util.AssertNoErr(t, err)
	util.AssertEqualInt(t, len(expected), len(refs))
	for i, r := range expected {
		util.AssertEqualString(t, r, refs[i].Name())
	}
}
Exemple #9
0
func Test_OidFromString(t *testing.T) {
	ids := getTestOids(numTests)
	for _, id := range ids {
		oid, err := OidFromString(id)
		util.AssertNoErr(t, err)
		util.AssertEqualString(t, oid.String(), id)
	}
}
Exemple #10
0
func testPackedTagDerefInfo(t *testing.T, repo Repository, spec string, oid *objects.ObjectId) {
	ref, err := repo.Ref(spec)
	util.AssertNoErr(t, err)
	util.AssertEqualString(t, ref.Name(), spec)
	// make sure target is symbolic and matches
	symbolic, target := ref.Target()
	util.Assert(t, !symbolic)
	if target == nil || ref.Commit() == nil {
		t.Fatalf("nil target or commit")
	}
	util.AssertEqualString(t, ref.Commit().String(), oid.String())
}
Exemple #11
0
func Test_parseValidBlob(t *testing.T) {
	for _, v := range testCasesBlobs {
		tb := makeTestBlob(v)
		p := ObjectParserForString(tb)
		hdr, err := p.ParseHeader()
		util.AssertNoErr(t, err)
		util.Assert(t, hdr.Type() == objects.ObjectBlob)
		util.Assert(t, hdr.Size() == int64(len(v)))

		o, err := p.ParsePayload()
		util.AssertNoErr(t, err)
		util.Assert(t, o.Header().Type() == objects.ObjectBlob)
		util.Assert(t, o.Header().Size() == int64(len(v)))
		util.Assert(t, o.ObjectId() == nil) // wasn't set in the test scenario

		var b *objects.Blob
		util.AssertPanicFree(t, func() {
			b = o.(*objects.Blob)
		})
		util.AssertEqualString(t, string(b.Data()), v)
	}
}
Exemple #12
0
// Test_readCommits will compare the commit output of
// git and ggit for a string of commits.
func Test_readTree(t *testing.T) {
	testCase := test.Tree
	repo := Open(testCase.Repo())
	info := testCase.Info().(*test.InfoTree)
	f := format.NewStrFormat()

	var (
		oid = objects.OidNow(info.TreeOid)
	)
	o, err := repo.ObjectFromOid(oid)
	util.AssertNoErr(t, err)

	// check the id
	util.Assert(t, o.ObjectId().String() == info.TreeOid)

	// check the header
	util.Assert(t, o.Header().Type() == objects.ObjectTree)
	util.AssertEqualInt(t, int(o.Header().Size()), info.TreeSize)

	// get the tree
	// now convert to a tag and check the fields
	var tree *objects.Tree
	util.AssertPanicFree(t, func() {
		tree = o.(*objects.Tree)
	})

	// check entries
	entries := tree.Entries()
	util.AssertEqualInt(t, info.N, len(entries))
	util.Assert(t, info.N > 2)

	// check a file
	file := entries[0]
	util.AssertEqualString(t, info.File1Oid, file.ObjectId().String())
	util.Assert(t, file.Mode() == objects.ModeBlob)
	// TODO: add checks for type, etc.

	// check the output
	// check the whole representation, which will catch
	// most of the other stuff
	f.Reset()
	f.Object(o)
	util.AssertEqualString(t, info.TreeRepr, f.String())
}
Exemple #13
0
func Test_parseBlobBadSize(t *testing.T) {
	test := func(contents string) {
		l := len(contents)
		size := rand.Intn(l+1) - 1 + 2*rand.Intn(2)*l // never equal to l
		tb := makeTestBlobWithSize(size, contents)
		p := ObjectParserForString(tb)
		hdr, e := p.ParseHeader()
		util.AssertNoErr(t, e)
		util.Assert(t, hdr.Type() == objects.ObjectBlob)
		util.Assert(t, hdr.Size() == int64(size))

		// should not be able to parse
		_, e = p.ParsePayload()
		util.Assert(t, e != nil)
	}
	for _, v := range testCasesBlobs {
		test(v)
	}
}
Exemple #14
0
// this test implements basic blob reading from
// a git repository.
func Test_readBlobs(t *testing.T) {
	testRepo := test.Blobs

	repo := Open(testRepo.Repo())
	info := testRepo.Info().(*test.InfoBlobs)

	if len(info.Blobs) < 1 {
		fmt.Println("warning: no blobs to test")
	}

	for _, detail := range info.Blobs {
		// read the blob
		oid := objects.OidNow(detail.Oid)
		o, err := repo.ObjectFromOid(oid)
		util.AssertNoErr(t, err)
		util.Assert(t, o.Header().Type() == objects.ObjectBlob)
		b := o.(*objects.Blob)
		util.AssertEqualString(t, string(b.Data()), detail.Contents)
		util.AssertEqualInt(t, int(b.Header().Size()), len(detail.Contents))
	}

}
Exemple #15
0
// testObjectExpected retrieves the commit with the given revision specification
// from the given repository and ensures that this operation went well and the
// returned object in fact has the expected oid.
func testObjectExpected(t *testing.T, repo Repository, rev string, expOid *objects.ObjectId, expType objects.ObjectType) {
	parent, err := ObjectFromRevision(repo, rev)
	util.AssertNoErr(t, err)
	util.Assert(t, parent.Header().Type() == expType)
	util.AssertEqualString(t, parent.ObjectId().String(), expOid.String())
}
Exemple #16
0
func testRefPathPeeled(t *testing.T, repo Repository, spec string, oid *objects.ObjectId) {
	ref, err := repo.Ref(spec)
	util.AssertNoErr(t, err)
	util.AssertEqualString(t, ref.Name(), spec)
	assertPeeledRef(t, ref, oid)
}