Debian Codegraph @

util/files_test.go

raw
1 // Copyright ©2011-2012 The bíogo 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 util
6
7 import (
8 "bytes"
9 "crypto/md5"
10 "fmt"
11 "io/ioutil"
12 "os"
13 "os/exec"
14
15 "gopkg.in/check.v1"
16 )
17
18 // Tests
19 func (s *S) TestHash(c *check.C) {
20 // FIXME: This will not work with MacOS.
21 _, err := exec.LookPath("md5sum")
22 if err != nil {
23 c.Skip(err.Error())
24 }
25 md5sum := exec.Command("md5sum", "./files_test.go")
26 b := &bytes.Buffer{}
27 md5sum.Stdout = b
28 err = md5sum.Run()
29 if err != nil {
30 c.Fatal(err)
31 }
32 f, err := os.Open("./files_test.go")
33 if err != nil {
34 c.Fatalf("%v %s", md5sum, err)
35 }
36 x, err := ioutil.ReadAll(f)
37 if err != nil {
38 c.Fatal(err)
39 }
40 f.Seek(0, 0)
41
42 md5hash, err := Hash(md5.New(), f)
43 if err != nil {
44 c.Fatal(err)
45 }
46 md5string := fmt.Sprintf("%x .*\n", md5hash)
47
48 c.Check(string(b.Bytes()), check.Matches, md5string)
49
50 y, err := ioutil.ReadAll(f)
51 if err != nil {
52 c.Fatal(err)
53 }
54 c.Check(x, check.DeepEquals, y)
55 }