From e300945a3d456af1b619447347cd19a779b7a8f5 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Fri, 24 Apr 2015 12:59:29 +0200 Subject: tests: always clean up temporary repository dirs Some test repositories are not correctly removed after the tests did run. Fix by introducing a function that is to be used for cleaning up temporary test repositories. --- index_test.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'index_test.go') diff --git a/index_test.go b/index_test.go index 98d9a31..647a0b8 100644 --- a/index_test.go +++ b/index_test.go @@ -2,14 +2,13 @@ package git import ( "io/ioutil" - "os" "runtime" "testing" ) func TestCreateRepoAndStage(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) idx, err := repo.Index() checkFatal(t, err) @@ -25,10 +24,10 @@ func TestCreateRepoAndStage(t *testing.T) { func TestIndexWriteTreeTo(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) repo2 := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo2) idx, err := repo.Index() checkFatal(t, err) @@ -44,7 +43,7 @@ func TestIndexWriteTreeTo(t *testing.T) { func TestIndexAddAndWriteTreeTo(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) odb, err := repo.Odb() checkFatal(t, err) @@ -74,7 +73,7 @@ func TestIndexAddAndWriteTreeTo(t *testing.T) { func TestIndexAddAllNoCallback(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) err := ioutil.WriteFile(repo.Workdir()+"/README", []byte("foo\n"), 0644) checkFatal(t, err) @@ -95,7 +94,7 @@ func TestIndexAddAllNoCallback(t *testing.T) { func TestIndexAddAllCallback(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) err := ioutil.WriteFile(repo.Workdir()+"/README", []byte("foo\n"), 0644) checkFatal(t, err) -- cgit v1.2.3 From a8ad0d204052d8bd9c4d0093cce62c54afa67188 Mon Sep 17 00:00:00 2001 From: Carlos Martín Nieto Date: Tue, 19 May 2015 14:33:30 +0200 Subject: Index: Add ReadTree() --- index.go | 14 ++++++++++++++ index_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) (limited to 'index_test.go') diff --git a/index.go b/index.go index 9f37f33..009aeb6 100644 --- a/index.go +++ b/index.go @@ -240,6 +240,20 @@ func (v *Index) WriteTreeTo(repo *Repository) (*Oid, error) { return oid, nil } +// ReadTree replaces the contents of the index with those of the given +// tree +func (v *Index) ReadTree(tree *Tree) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ret := C.git_index_read_tree(v.ptr, tree.cast_ptr); + if ret < 0 { + return MakeGitError(ret) + } + + return nil +} + func (v *Index) WriteTree() (*Oid, error) { oid := new(Oid) diff --git a/index_test.go b/index_test.go index 647a0b8..a1f0c9c 100644 --- a/index_test.go +++ b/index_test.go @@ -22,6 +22,34 @@ func TestCreateRepoAndStage(t *testing.T) { } } +func TestIndexReadTree(t *testing.T) { + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + _, _ = seedTestRepo(t, repo) + + ref, err := repo.Head() + checkFatal(t, err) + + obj, err := ref.Peel(ObjectTree); + checkFatal(t, err) + + tree := obj.(*Tree) + + idx, err := NewIndex() + checkFatal(t, err) + + err = idx.ReadTree(tree) + checkFatal(t, err) + + id, err := idx.WriteTreeTo(repo) + checkFatal(t, err) + + if tree.Id().Cmp(id) != 0 { + t.Fatalf("Read and written trees are not the same") + } +} + func TestIndexWriteTreeTo(t *testing.T) { repo := createTestRepo(t) defer cleanupTestRepo(t, repo) -- cgit v1.2.3 From d7a0495000e35d06993605a9a31a5f9823292f8a Mon Sep 17 00:00:00 2001 From: Carlos Martín Nieto Date: Tue, 19 May 2015 14:56:01 +0200 Subject: Index: Add OpenIndex This lets you persist an index at an arbitrary location. --- index.go | 18 ++++++++++++++++++ index_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) (limited to 'index_test.go') diff --git a/index.go b/index.go index 009aeb6..2082ebd 100644 --- a/index.go +++ b/index.go @@ -96,6 +96,24 @@ func NewIndex() (*Index, error) { return &Index{ptr: ptr}, nil } +// OpenIndex creates a new index at the given path. If the file does +// not exist it will be created when Write() is called. +func OpenIndex(path string) (*Index, error) { + var ptr *C.git_index + + var cpath = C.CString(path) + defer C.free(unsafe.Pointer(cpath)) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + if err := C.git_index_open(&ptr, cpath); err < 0 { + return nil, MakeGitError(err) + } + + return &Index{ptr: ptr}, nil +} + // Add adds or replaces the given entry to the index, making a copy of // the data func (v *Index) Add(entry *IndexEntry) error { diff --git a/index_test.go b/index_test.go index a1f0c9c..9b54945 100644 --- a/index_test.go +++ b/index_test.go @@ -2,6 +2,7 @@ package git import ( "io/ioutil" + "os" "runtime" "testing" ) @@ -148,6 +149,29 @@ func TestIndexAddAllCallback(t *testing.T) { } } +func TestIndexOpen(t *testing.T) { + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + path := repo.Workdir() + "/heyindex" + + _, err := os.Stat(path) + if !os.IsNotExist(err) { + t.Fatal("new index file already exists") + } + + idx, err := OpenIndex(path) + checkFatal(t, err) + + err = idx.Write() + checkFatal(t, err) + + _, err = os.Stat(path) + if os.IsNotExist(err) { + t.Fatal("new index file did not get written") + } +} + func checkFatal(t *testing.T, err error) { if err == nil { return -- cgit v1.2.3 From 72c19f73c9170720780839cd1561486e075d35a8 Mon Sep 17 00:00:00 2001 From: Carlos Martín Nieto Date: Tue, 19 May 2015 15:05:00 +0200 Subject: Index: Add Path() accessor --- index.go | 6 ++++++ index_test.go | 8 ++++++++ 2 files changed, 14 insertions(+) (limited to 'index_test.go') diff --git a/index.go b/index.go index 2082ebd..1a899f1 100644 --- a/index.go +++ b/index.go @@ -114,6 +114,12 @@ func OpenIndex(path string) (*Index, error) { return &Index{ptr: ptr}, nil } +// Path returns the index' path on disk or an empty string if it +// exists only in memory. +func (v *Index) Path() string { + return C.GoString(C.git_index_path(v.ptr)) +} + // Add adds or replaces the given entry to the index, making a copy of // the data func (v *Index) Add(entry *IndexEntry) error { diff --git a/index_test.go b/index_test.go index 9b54945..9283b83 100644 --- a/index_test.go +++ b/index_test.go @@ -83,6 +83,10 @@ func TestIndexAddAndWriteTreeTo(t *testing.T) { idx, err := NewIndex() checkFatal(t, err) + if idx.Path() != "" { + t.Fatal("in-memory repo has a path") + } + entry := IndexEntry{ Path: "README", Id: blobID, @@ -163,6 +167,10 @@ func TestIndexOpen(t *testing.T) { idx, err := OpenIndex(path) checkFatal(t, err) + if path != idx.Path() { + t.Fatalf("mismatched index paths, expected %v, got %v", path, idx.Path()) + } + err = idx.Write() checkFatal(t, err) -- cgit v1.2.3 From 53c158fbd7e5f4dac787f5c3a7107fcb4116f676 Mon Sep 17 00:00:00 2001 From: taylorchu Date: Sat, 30 May 2015 22:27:08 +0200 Subject: Fix test error messages --- index_test.go | 3 +-- reference_test.go | 3 +-- submodule_test.go | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) (limited to 'index_test.go') diff --git a/index_test.go b/index_test.go index 9283b83..7c65f4f 100644 --- a/index_test.go +++ b/index_test.go @@ -188,8 +188,7 @@ func checkFatal(t *testing.T, err error) { // The failure happens at wherever we were called, not here _, file, line, ok := runtime.Caller(1) if !ok { - t.Fatal() + t.Fatalf("Unable to get caller") } - t.Fatalf("Fail at %v:%v; %v", file, line, err) } diff --git a/reference_test.go b/reference_test.go index d6b5f22..5720a66 100644 --- a/reference_test.go +++ b/reference_test.go @@ -199,8 +199,7 @@ func checkRefType(t *testing.T, ref *Reference, kind ReferenceType) { // The failure happens at wherever we were called, not here _, file, line, ok := runtime.Caller(1) if !ok { - t.Fatal() + t.Fatalf("Unable to get caller") } - t.Fatalf("Wrong ref type at %v:%v; have %v, expected %v", file, line, ref.Type(), kind) } diff --git a/submodule_test.go b/submodule_test.go index ee75d53..27bc193 100644 --- a/submodule_test.go +++ b/submodule_test.go @@ -21,6 +21,6 @@ func TestSubmoduleForeach(t *testing.T) { checkFatal(t, err) if i != 1 { - t.Fatalf("expected one submodule found but got %i", i) + t.Fatalf("expected one submodule found but got %d", i) } } -- cgit v1.2.3