-
Notifications
You must be signed in to change notification settings - Fork 50
/
helpers.go
56 lines (49 loc) · 1.79 KB
/
helpers.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
package testhelmfile
import (
"os"
"path/filepath"
"testing"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/testhelpers"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
// AssertHelmfiles asserts the helmfiles in the given directory versus the output dir
func AssertHelmfiles(t *testing.T, expectedDir, outDir string, generateTestOutput bool) {
m := map[string]bool{}
FindAllHelmfiles(t, m, outDir)
FindAllHelmfiles(t, m, expectedDir)
require.NotEmpty(t, m, "failed to find helmfile.yaml files")
for f := range m {
outFile := filepath.Join(outDir, f)
expectedFile := filepath.Join(expectedDir, f)
if generateTestOutput {
dir := filepath.Dir(expectedFile)
err := os.MkdirAll(dir, files.DefaultDirWritePermissions)
require.NoError(t, err, "failed to make dir %s", dir)
data, err := os.ReadFile(outFile)
require.NoError(t, err, "failed to load %s", outFile)
err = os.WriteFile(expectedFile, data, 0o600)
require.NoError(t, err, "failed to save file %s", expectedFile)
t.Logf("saved %s\n", expectedFile)
} else {
t.Logf("verified %s\n", outFile)
_ = testhelpers.AssertEqualFileText(t, expectedFile, outFile)
}
}
}
// FindAllHelmfiles finds all the relative paths of the helmfiles in the given directory
func FindAllHelmfiles(t *testing.T, m map[string]bool, dir string) {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { //nolint:staticcheck
if info == nil || info.IsDir() || info.Name() != "helmfile.yaml" {
return nil
}
rel, err := filepath.Rel(dir, path) //nolint:staticcheck
if err != nil {
return errors.Wrapf(err, "failed to get relative path of %s from %s", path, dir)
}
m[rel] = true
return nil
})
require.NoError(t, err, "failed to walk dir %s", dir)
}