github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/lintcmd/cache/hash_test.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cache 6 7 import ( 8 "fmt" 9 "os" 10 "testing" 11 ) 12 13 func TestHash(t *testing.T) { 14 c := &Cache{} 15 h := c.NewHash("alice") 16 h.Write([]byte("hello world")) 17 sum := fmt.Sprintf("%x", h.Sum()) 18 want := "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" 19 if sum != want { 20 t.Errorf("hash(hello world) = %v, want %v", sum, want) 21 } 22 } 23 24 func TestHashFile(t *testing.T) { 25 f, err := os.CreateTemp("", "cmd-go-test-") 26 if err != nil { 27 t.Fatal(err) 28 } 29 name := f.Name() 30 fmt.Fprintf(f, "hello world") 31 defer os.Remove(name) 32 if err := f.Close(); err != nil { 33 t.Fatal(err) 34 } 35 36 var h ActionID // make sure hash result is assignable to ActionID 37 h, err = FileHash(name) 38 if err != nil { 39 t.Fatal(err) 40 } 41 sum := fmt.Sprintf("%x", h) 42 want := "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" 43 if sum != want { 44 t.Errorf("hash(hello world) = %v, want %v", sum, want) 45 } 46 }