From 1d7f9f1cb204d4a7a7941bddc8a0c53494fdc35d Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Wed, 11 Dec 2024 18:22:48 +0300 Subject: [PATCH] chore: add unit tests --- internal/config/load.go | 27 +++++----- internal/config/load_test.go | 102 ++++++++++++++++++++++++++++++++++- 2 files changed, 116 insertions(+), 13 deletions(-) diff --git a/internal/config/load.go b/internal/config/load.go index 48027fbe..251ea411 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -44,7 +44,8 @@ var ( ".json": json.Parser(), ".toml": toml.Parser(), } - withActionsMerge = koanf.WithMergeFunc(mergeActions) + + mergeActionsOption = koanf.WithMergeFunc(mergeActions) ) // ConfigNotFoundError. @@ -64,7 +65,7 @@ func loadOne(k *koanf.Koanf, filesystem afero.Fs, root string, names []string) e continue } - if err := k.Load(kfs.Provider(newIOFS(filesystem), config), parsers[extension], withActionsMerge); err != nil { + if err := k.Load(kfs.Provider(newIOFS(filesystem), config), parsers[extension], mergeActionsOption); err != nil { return err } @@ -176,7 +177,7 @@ func loadRemotes(k *koanf.Koanf, filesystem afero.Fs, repo *git.Repository, remo panic("TODO: unknown extension to parse") } - if err := k.Load(kfs.Provider(newIOFS(filesystem), configPath), parser, withActionsMerge); err != nil { + if err := k.Load(kfs.Provider(newIOFS(filesystem), configPath), parser, mergeActionsOption); err != nil { return err } @@ -224,7 +225,7 @@ func extendRecursive(k *koanf.Koanf, filesystem afero.Fs, root string, extends [ if !ok { panic("TODO: unknown extension for extent " + path) } - if err := extent.Load(kfs.Provider(newIOFS(filesystem), path), parser, withActionsMerge); err != nil { + if err := extent.Load(kfs.Provider(newIOFS(filesystem), path), parser, mergeActionsOption); err != nil { return err } @@ -232,7 +233,7 @@ func extendRecursive(k *koanf.Koanf, filesystem afero.Fs, root string, extends [ return err } - if err := k.Load(koanfProvider{extent}, nil, withActionsMerge); err != nil { + if err := k.Load(koanfProvider{extent}, nil, mergeActionsOption); err != nil { return err } } @@ -361,6 +362,7 @@ func addHook(name string, main, secondary *koanf.Koanf, c *Config) error { default: } } + if len(destActions) > 0 { dest["actions"] = destActions } @@ -417,6 +419,7 @@ func (k koanfProvider) ReadBytes() ([]byte, error) { func mergeActions(src, dest map[string]interface{}) error { srcActions := make(map[string][]interface{}) + for name, maybeHook := range src { switch hook := maybeHook.(type) { case map[string]interface{}: @@ -473,19 +476,19 @@ func mergeActions(src, dest map[string]interface{}) error { } func mergeActionsSlice(src, dest []interface{}) []interface{} { - namedActions := make(map[string]map[string]interface{}) - resultActions := make([]interface{}, 0, len(dest)) + mergable := make(map[string]map[string]interface{}) + result := make([]interface{}, 0, len(dest)) for _, maybeAction := range dest { switch destAction := maybeAction.(type) { case map[string]interface{}: switch name := destAction["name"].(type) { case string: - namedActions[name] = destAction + mergable[name] = destAction default: } - resultActions = append(resultActions, maybeAction) + result = append(result, maybeAction) default: } } @@ -495,7 +498,7 @@ func mergeActionsSlice(src, dest []interface{}) []interface{} { case map[string]interface{}: switch name := srcAction["name"].(type) { case string: - destAction, ok := namedActions[name] + destAction, ok := mergable[name] if ok { var srcSubActions []interface{} var destSubActions []interface{} @@ -541,10 +544,10 @@ func mergeActionsSlice(src, dest []interface{}) []interface{} { default: } - resultActions = append(resultActions, maybeAction) + result = append(result, maybeAction) default: } } - return resultActions + return result } diff --git a/internal/config/load_test.go b/internal/config/load_test.go index 99005845..53a6a250 100644 --- a/internal/config/load_test.go +++ b/internal/config/load_test.go @@ -709,6 +709,106 @@ pre-commit: }, }, }, + "with actions": { + files: map[string]string{ + "lefthook.yml": ` +pre-commit: + actions: + - run: 1 + - run: 2 + name: second +`, + "lefthook-local.yml": ` +pre-commit: + actions: + - run: 3 + - run: local 2 + name: second +`, + }, + result: &Config{ + SourceDir: ".lefthook", + SourceDirLocal: ".lefthook-local", + Hooks: map[string]*Hook{ + "pre-commit": { + Actions: []*Action{ + &Action{Run: "1"}, + &Action{Run: "local 2", Name: "second"}, + &Action{Run: "3"}, + }, + }, + }, + }, + }, + "with nested actions": { + files: map[string]string{ + "lefthook.yml": ` +pre-commit: + actions: + - name: group 1 + group: + actions: + - run: 1.1 + - run: 1.2 + - name: nested + group: + actions: + - run: 1.nested.1 + - run: 1.nested.2 + name: nested 2 +`, + "lefthook-local.yml": ` +pre-commit: + actions: + - name: group 1 + glob: "*.rb" + group: + parallel: true + actions: + - name: nested + group: + actions: + - run: 1.nested.2 local + name: nested 2 + - run: 1.nested.3 + - run: 1.3 + - run: 1.4 +`, + }, + result: &Config{ + SourceDir: ".lefthook", + SourceDirLocal: ".lefthook-local", + Hooks: map[string]*Hook{ + "pre-commit": { + Actions: []*Action{ + &Action{ + Name: "group 1", + Glob: "*.rb", + Group: &Group{ + Parallel: true, + Actions: []*Action{ + &Action{Run: "1.1"}, + &Action{Run: "1.2"}, + &Action{ + Name: "nested", + Group: &Group{ + Actions: []*Action{ + &Action{Run: "1.nested.1"}, + &Action{Run: "1.nested.2 local", Name: "nested 2"}, + &Action{Run: "1.nested.3"}, + }, + }, + }, + &Action{Run: "1.3"}, + &Action{Run: "1.4"}, + }, + }, + }, + }, + }, + }, + }, + }, } { fs := afero.Afero{Fs: afero.NewMemMapFs()} repo := &git.Repository{ @@ -738,7 +838,7 @@ pre-commit: result, err := Load(fs.Fs, repo) assert.NoError(err) - assert.Equal(result, tt.result) + assert.Equal(tt.result, result) }) }