File: files_test.go

package info (click to toggle)
golang-github-biogo-biogo 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 5,332 kB
  • sloc: sh: 282; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,033 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright ©2011-2012 The bíogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package util

import (
	"bytes"
	"crypto/md5"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"

	"gopkg.in/check.v1"
)

// Tests
func (s *S) TestHash(c *check.C) {
	// FIXME: This will not work with MacOS.
	_, err := exec.LookPath("md5sum")
	if err != nil {
		c.Skip(err.Error())
	}
	md5sum := exec.Command("md5sum", "./files_test.go")
	b := &bytes.Buffer{}
	md5sum.Stdout = b
	err = md5sum.Run()
	if err != nil {
		c.Fatal(err)
	}
	f, err := os.Open("./files_test.go")
	if err != nil {
		c.Fatalf("%v %s", md5sum, err)
	}
	x, err := ioutil.ReadAll(f)
	if err != nil {
		c.Fatal(err)
	}
	f.Seek(0, 0)

	md5hash, err := Hash(md5.New(), f)
	if err != nil {
		c.Fatal(err)
	}
	md5string := fmt.Sprintf("%x .*\n", md5hash)

	c.Check(string(b.Bytes()), check.Matches, md5string)

	y, err := ioutil.ReadAll(f)
	if err != nil {
		c.Fatal(err)
	}
	c.Check(x, check.DeepEquals, y)
}