dir-feat: Make loading from directory recursively possible

The commit adds the following modifications:
 * Possibility to load hooks recursively from a directory
 * Extraction of the loadinghook method in webhook.go
 * Tests done on a fairly complex tree of directory

Allowing hooks to be loaded from a directory is a nice feature, as it
allows a better organisation/readibility of the hooks that we have.

The LoadFromDir method rely on LoadFromFile and is meant to be very
permissive, and will return errors only when no hooks have been loaded.
Otherwise it will simply return 'nil' and the potential issues/warnings
that happen during the loading of hooks (invalid file, invalid path,
issue opening, etc).
This commit is contained in:
Hugo Rosnet 2016-06-13 15:32:19 +02:00
parent 18b0573bc4
commit 1db3be532b
7 changed files with 345 additions and 18 deletions

View file

@ -3,6 +3,7 @@ package hook
import (
"reflect"
"testing"
"strings"
)
var checkPayloadSignatureTests = []struct {
@ -217,6 +218,39 @@ func TestHooksLoadFromFile(t *testing.T) {
}
}
var hooksLoadFromDirTests = []struct {
path string
warn string
ok bool
}{
// ok - because at least one hook is loaded - exception for "" path
{"", "path '' is unspecified", true},
{"../test/hooks_dir.test", "../test/hooks_dir.test/depth1/depth2/depth3/depth4/empty_file.example (unexpected end of JSON input),../test/hooks_dir.test/depth1/depth2/depth3b is empty,../test/hooks_dir.test/depth1b/hooks-invalid.json.example (invalid character ':' after array element)", true},
{"../test/hooks_dir.test/depth1", "../test/hooks_dir.test/depth1/depth2/depth3/depth4/empty_file.example (unexpected end of JSON input),../test/hooks_dir.test/depth1/depth2/depth3b is empty", true},
{"../test/hooks_dir.test//depth1/depth2/", "../test/hooks_dir.test//depth1/depth2/depth3/depth4/empty_file.example (unexpected end of JSON input),../test/hooks_dir.test//depth1/depth2/depth3b is empty", true},
// failures - because no hook has been loaded
{"../test/hooks_dir.test///depth1b//", "../test/hooks_dir.test///depth1b//hooks-invalid.json.example (invalid character ':' after array element)", false},
{"../test/hooks_dir.test//depth1/depth2///depth3", "../test/hooks_dir.test//depth1/depth2///depth3/depth4/empty_file.example (unexpected end of JSON input)", false},
{"../test/hooks_dir.test/depth1/depth2/depth3b////", "../test/hooks_dir.test/depth1/depth2/depth3b//// is empty", false},
{"../test/hooks_dir.test/depth1/depth2/depth3/depth4/", "../test/hooks_dir.test/depth1/depth2/depth3/depth4/empty_file.example (unexpected end of JSON input)", false},
{"../test/hooks_dir.test/non-existing-dir", "path '../test/hooks_dir.test/non-existing-dir' is invalid", false},
}
func TestHooksLoadFromDir(t *testing.T) {
for _, tt := range hooksLoadFromDirTests {
h := &Hooks{}
warnings, err := h.LoadFromDir(tt.path)
// to simplify the comparison from the slice we received
warnings_string := strings.Join(warnings, ",")
if (err == nil) != tt.ok {
t.Errorf(err.Error())
}
if warnings_string != tt.warn {
t.Errorf("recevied: [%s]\nexpected [%s]", warnings_string, tt.warn)
}
}
}
var hooksMatchTests = []struct {
id string
hooks Hooks