-
Notifications
You must be signed in to change notification settings - Fork 44
/
utils.go
56 lines (49 loc) · 1.29 KB
/
utils.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cmd
import (
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
)
type FileCheck struct {
Expected, NotExpected []string
ExpectedContent map[string]string
}
func (fc FileCheck) Cleanup(t *testing.T) {
for _, file := range fc.ExpectedFiles() {
t.Logf("removing %v", file)
if err := os.Remove(file); err != nil {
t.Errorf("failed to remove file %s: %v", file, err)
}
}
}
func (fc FileCheck) ExpectedFiles() []string {
expected := fc.Expected
for file := range fc.ExpectedContent {
expected = append(expected, file)
}
return expected
}
func (fc FileCheck) Verify(t *testing.T, dir string) {
for _, f := range fc.ExpectedFiles() {
if _, err := os.Stat(filepath.Join(dir, f)); err != nil {
t.Errorf("Expected to find %s: %v", f, err)
}
}
for _, f := range fc.NotExpected {
if _, err := os.Stat(filepath.Join(dir, f)); err == nil {
t.Errorf("Expected to not find %s", f)
}
}
for file, expectedContent := range fc.ExpectedContent {
content, err := os.ReadFile(filepath.Join(dir, file))
if err != nil {
t.Fatalf("ioutil.ReadFile: %v", err)
}
if diff := cmp.Diff(expectedContent, string(content)); diff != "" {
t.Errorf("content of %s differs: %s", file, diff)
}
}
}