aboutsummaryrefslogtreecommitdiffsponsor
path: root/installpkg_test.go
blob: dd1baaf8f14fd9462e17c72b904755ca74124010 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package pkgtools

import (
	"crypto/md5"
	"fmt"
	"io"
	"os"
	"path"
	"testing"

	"github.com/stretchr/testify/require"
)

func lstree(p string) ([]string, error) {
	entries, err := os.ReadDir(p)
	if err != nil {
		return nil, err
	}

	var files []string
	for _, e := range entries {
		fPath := path.Join(p, e.Name())
		if e.Type().IsDir() {
			tree, err := lstree(fPath)
			if err != nil {
				return nil, err
			}
			for _, f := range tree {
				files = append(files, path.Join(e.Name(), f))
			}
			continue
		}
		if e.Type().IsRegular() {
			files = append(files, e.Name())
		}
	}
	return files, nil
}

func shasum(path string) (string, error) {
	f, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer f.Close()

	hash := md5.New()
	_, err = io.Copy(hash, f)
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("%x", hash.Sum(nil)), nil
}

func mapKeys(arg map[string]string) []string {
	var ret []string
	for k := range arg {
		ret = append(ret, k)
	}
	return ret
}

func requireDirContents(
	t *testing.T,
	expected string,
	actual string,
	msgAndArgs ...interface{},
) {

	expTree := map[string]string{}
	files, err := lstree(expected)
	require.NoError(t, err)
	for _, file := range files {
		hash, err := shasum(path.Join(expected, file))
		require.NoError(t, err, "calculating hash for %s", file)
		expTree[file] = hash
	}

	actualTree := map[string]string{}
	files, err = lstree(actual)
	require.NoError(t, err)
	for _, file := range files {
		hash, err := shasum(path.Join(actual, file))
		require.NoError(t, err, "calculating hash for %s", file)
		actualTree[file] = hash
	}

	for k, v := range expTree {
		v2, ok := actualTree[k]
		if !ok {
			continue
		}

		require.Equal(t,
			string(v), string(v2),
			"file '%s' and '%s' differ",
			path.Join(expected, k), path.Join(actual, k),
		)

		delete(actualTree, k)
		delete(expTree, k)
	}

	files = mapKeys(actualTree)
	require.Empty(t, files, "unexpected files left behind")
	files = mapKeys(expTree)
	require.Empty(t, files, "expected to be written")
}