Skip to content

Commit

Permalink
tests: parsing dir should be equivalent to parsing individual files
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmood Ali committed Jun 12, 2019
1 parent 093a760 commit 56bdf0c
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions command/agent/config_parse_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package agent

import (
"fmt"
"path/filepath"
"sort"
"testing"
"time"

Expand Down Expand Up @@ -670,3 +672,68 @@ func TestConfig_ParseDir(t *testing.T) {

require.EqualValues(t, sample1, c)
}

// TestConfig_ParseDir_Matches_IndividualParsing asserts
// that parsing a directory config is the equivalent of
// parsing individual files in any order
func TestConfig_ParseDir_Matches_IndividualParsing(t *testing.T) {
dirConfig, err := LoadConfig("./testdata/sample1")
require.NoError(t, err)

dirConfig = DefaultConfig().Merge(dirConfig)

files := []string{
"testdata/sample1/sample0.json",
"testdata/sample1/sample1.json",
"testdata/sample1/sample2.hcl",
}

for _, perm := range permutations(files) {
t.Run(fmt.Sprintf("permutation %v", perm), func(t *testing.T) {
config := DefaultConfig()

for _, f := range perm {
fc, err := LoadConfig(f)
require.NoError(t, err)

config = config.Merge(fc)
}

// sort files to get stable view
sort.Strings(config.Files)
sort.Strings(dirConfig.Files)

require.EqualValues(t, dirConfig, config)
})
}

}

// https://stackoverflow.com/a/30226442
func permutations(arr []string) [][]string {
var helper func([]string, int)
res := [][]string{}

helper = func(arr []string, n int) {
if n == 1 {
tmp := make([]string, len(arr))
copy(tmp, arr)
res = append(res, tmp)
} else {
for i := 0; i < n; i++ {
helper(arr, n-1)
if n%2 == 1 {
tmp := arr[i]
arr[i] = arr[n-1]
arr[n-1] = tmp
} else {
tmp := arr[0]
arr[0] = arr[n-1]
arr[n-1] = tmp
}
}
}
}
helper(arr, len(arr))
return res
}

0 comments on commit 56bdf0c

Please sign in to comment.