Debian Codegraph @

cache/hash_test.go

raw symbols SCIP
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 h := NewHash("alice")
15 h.Write([]byte("hello world"))
16 sum := fmt.Sprintf("%x", h.Sum())
17 want := "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
18 if sum != want {
19 t.Errorf("hash(hello world) = %v, want %v", sum, want)
20 }
21 }
22
23 func TestHashFile(t *testing.T) {
24 f, err := os.CreateTemp("", "cmd-go-test-")
25 if err != nil {
26 t.Fatal(err)
27 }
28 name := f.Name()
29 fmt.Fprintf(f, "hello world")
30 defer os.Remove(name)
31 if err := f.Close(); err != nil {
32 t.Fatal(err)
33 }
34
35 var h ActionID // make sure hash result is assignable to ActionID
36 h, err = FileHash(name)
37 if err != nil {
38 t.Fatal(err)
39 }
40 sum := fmt.Sprintf("%x", h)
41 want := "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
42 if sum != want {
43 t.Errorf("hash(hello world) = %v, want %v", sum, want)
44 }
45 }