Skip to content

Commit

Permalink
add TestIsRegisteredAsDirectory (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 22, 2023
1 parent 78da3a0 commit b818695
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions internal/store/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,104 @@ func TestGetEntry(t *testing.T) {
}
}

func TestIsRegisteredAsDirectory(t *testing.T) {
type args struct {
dirName string
}
type fields struct {
entries []*Entry
}
type test struct {
name string
args args
fields fields
want bool
}
tests := []*test{
func() *test {
hash, _ := hex.DecodeString("87f3c49bccf2597484ece08746d3ee5defaba335")
hash = sha.SHA1(hash)
return &test{
name: "true",
args: args{
dirName: "dir",
},
fields: fields{
entries: []*Entry{
NewEntry(hash, []byte("dir/dir2/test.txt")),
NewEntry(hash, []byte("dir/test.txt")),
NewEntry(hash, []byte("test.txt")),
},
},
want: true,
}
}(),
func() *test {
hash, _ := hex.DecodeString("87f3c49bccf2597484ece08746d3ee5defaba335")
hash = sha.SHA1(hash)
return &test{
name: "true: sub directory",
args: args{
dirName: "dir/dir2",
},
fields: fields{
entries: []*Entry{
NewEntry(hash, []byte("dir/dir2/test.txt")),
NewEntry(hash, []byte("dir/test.txt")),
NewEntry(hash, []byte("test.txt")),
},
},
want: true,
}
}(),
func() *test {
hash, _ := hex.DecodeString("87f3c49bccf2597484ece08746d3ee5defaba335")
hash = sha.SHA1(hash)
return &test{
name: "false",
args: args{
dirName: "sample",
},
fields: fields{
entries: []*Entry{
NewEntry(hash, []byte("dir/dir2/test.txt")),
NewEntry(hash, []byte("dir/test.txt")),
NewEntry(hash, []byte("test.txt")),
},
},
want: false,
}
}(),
}
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)
}

index, err := NewIndex(goitDir)
if err != nil {
t.Log(err)
}

for _, entry := range tt.fields.entries {
_, err = index.Update(goitDir, entry.Hash, entry.Path)
if err != nil {
t.Log(err)
}
}

got := index.IsRegisteredAsDirectory(tt.args.dirName)
if got != tt.want {
t.Errorf("got = %v, want = %v", got, tt.want)
}
})
}
}

func TestDeleteEntry(t *testing.T) {
type fields struct {
hash sha.SHA1
Expand Down

0 comments on commit b818695

Please sign in to comment.