Skip to content

Commit

Permalink
Merge pull request #856 from jxs1211/feat-pkg-util-md5
Browse files Browse the repository at this point in the history
🍀 Proposal: pkg/util/md5 Enhancement (sub task of pkg/util Enhancement #833)
  • Loading branch information
xavier-hou authored Jul 18, 2022
2 parents a2db298 + a419c77 commit d6875bb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/util/md5/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package md5

import (
"os"
"path/filepath"
"testing"
)

func TestCalcFileMD5(t *testing.T) {
testDir := t.TempDir()
bFile, _ := os.Create(filepath.Join(testDir, "b.txt"))
cFile, _ := os.Create(filepath.Join(testDir, "c.txt"))
_, err := cFile.WriteString("test")
if err != nil {
t.Error(err)
}
defer bFile.Close()
defer cFile.Close()
tests := []struct {
name string
filename string
want string
wantErr bool
}{
// TODO: Add test cases.
{"base not exist", "a.txt", "", true},
{"base empty file", bFile.Name(), "d41d8cd98f00b204e9800998ecf8427e", false},
{"base contented file", cFile.Name(), "098f6bcd4621d373cade4e832627b4f6", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CalcFileMD5(tt.filename)
if (err != nil) != tt.wantErr {
t.Errorf("CalcFileMD5() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CalcFileMD5() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions pkg/util/md5/md5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func createFileAndMD5File(fileName string) (string, error) {
if err != nil {
return "", err
}
defer md5File.Close()

_, err = md5File.Write([]byte(md5))
if err != nil {
return "", err
Expand Down

0 comments on commit d6875bb

Please sign in to comment.