Skip to content

Commit

Permalink
add TestLoadHash (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 6, 2023
1 parent 55cbc27 commit 21ca9c4
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions internal/store/refs_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package store

import (
"errors"
"os"
"path/filepath"
"reflect"
"testing"

Expand Down Expand Up @@ -38,3 +41,64 @@ func TestNewBanch(t *testing.T) {
})
}
}

func TestLoadHash(t *testing.T) {
type fields struct {
name string
hash sha.SHA1
}
tests := []struct {
name string
fields fields
want sha.SHA1
wantErr error
}{
{
name: "success",
fields: fields{
name: "main",
hash: sha.SHA1([]byte("87f3c49bccf2597484ece08746d3ee5defaba335")),
},
want: sha.SHA1([]byte("87f3c49bccf2597484ece08746d3ee5defaba335")),
wantErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
// .goit initialization
goitDir := filepath.Join(tmpDir, ".goit")
if err := os.Mkdir(goitDir, os.ModePerm); err != nil {
t.Logf("%v: %s", err, goitDir)
}
// make .goit/refs directory
refsDir := filepath.Join(goitDir, "refs")
if err := os.Mkdir(refsDir, os.ModePerm); err != nil {
t.Logf("%v: %s", err, refsDir)
}
// make .goit/refs/heads directory
headsDir := filepath.Join(refsDir, "heads")
if err := os.Mkdir(headsDir, os.ModePerm); err != nil {
t.Logf("%v: %s", err, headsDir)
}
// make main branch
mainBranchPath := filepath.Join(headsDir, tt.fields.name)
f, err := os.Create(mainBranchPath)
if err != nil {
t.Logf("%v: %s", err, mainBranchPath)
}
if _, err := f.WriteString(tt.fields.hash.String()); err != nil {
t.Log(err)
}
f.Close()

b := newBranch(tt.fields.name, nil)
if err := b.loadHash(goitDir); !errors.Is(err, tt.wantErr) {
t.Errorf("got = %v, want = %v", err, tt.wantErr)
}
if b.hash.String() != tt.want.String() {
t.Errorf("got = %s, want = %s", b.hash, tt.want)
}
})
}
}

0 comments on commit 21ca9c4

Please sign in to comment.