Skip to content

Commit

Permalink
add TestWriteHEAD (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 13, 2023
1 parent 476a301 commit cba1f6b
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions internal/log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package log
import (
"encoding/hex"
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -129,3 +131,79 @@ func TestNewRecord(t *testing.T) {
})
}
}

func TestWriteHEAD(t *testing.T) {
type args struct {
rec *record
}
type test struct {
name string
args args
want string
wantErr bool
}
tests := []*test{
func() *test {
hash, _ := hex.DecodeString("87f3c49bccf2597484ece08746d3ee5defaba335")
now := time.Now()
rec := NewRecord(CommitRecord, hash, hash, "Test Taro", "test@example.com", now, "test")

return &test{
name: "success: commit record",
args: args{
rec: rec,
},
want: fmt.Sprintf("%s %s %s <%s> %s %s\t%s: %s\n", rec.from, rec.to, rec.name, rec.email, rec.unixtime, rec.timeDiff, rec.recType, rec.message),
wantErr: false,
}
}(),
func() *test {
hash, _ := hex.DecodeString("87f3c49bccf2597484ece08746d3ee5defaba335")
now := time.Now()
rec := NewRecord(BranchRecord, hash, hash, "Test Taro", "test@example.com", now, "test")

return &test{
name: "success: branch record",
args: args{
rec: rec,
},
want: fmt.Sprintf("%s %s %s <%s> %s %s\t%s: %s\n", rec.from, rec.to, rec.name, rec.email, rec.unixtime, rec.timeDiff, rec.recType, rec.message),
wantErr: false,
}
}(),
func() *test {
hash, _ := hex.DecodeString("87f3c49bccf2597484ece08746d3ee5defaba335")
now := time.Now()
rec := NewRecord(CheckoutRecord, hash, hash, "Test Taro", "test@example.com", now, "test")

return &test{
name: "success: checkout record",
args: args{
rec: rec,
},
want: fmt.Sprintf("%s %s %s <%s> %s %s\t%s: %s\n", rec.from, rec.to, rec.name, rec.email, rec.unixtime, rec.timeDiff, rec.recType, rec.message),
wantErr: false,
}
}(),
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()

gLogger := NewGoitLogger(tmpDir)
if err := gLogger.WriteHEAD(tt.args.rec); (err != nil) != tt.wantErr {
t.Errorf("got = %v, want = %v", err, tt.wantErr)
}

headPath := filepath.Join(tmpDir, "logs", "HEAD")
b, err := os.ReadFile(headPath)
if err != nil {
t.Log(err)
}
got := string(b)
if got != tt.want {
t.Errorf("got = %s, want = %s", got, tt.want)
}
})
}
}

0 comments on commit cba1f6b

Please sign in to comment.