Skip to content

Commit

Permalink
chore: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Dec 11, 2024
1 parent c9d8ceb commit 1d7f9f1
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 13 deletions.
27 changes: 15 additions & 12 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ var (
".json": json.Parser(),
".toml": toml.Parser(),
}
withActionsMerge = koanf.WithMergeFunc(mergeActions)

mergeActionsOption = koanf.WithMergeFunc(mergeActions)
)

// ConfigNotFoundError.
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -224,15 +225,15 @@ 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
}

if err := extendRecursive(extent, filesystem, root, extent.Strings("extends"), visited); err != nil {
return err
}

if err := k.Load(koanfProvider{extent}, nil, withActionsMerge); err != nil {
if err := k.Load(koanfProvider{extent}, nil, mergeActionsOption); err != nil {
return err
}
}
Expand Down Expand Up @@ -361,6 +362,7 @@ func addHook(name string, main, secondary *koanf.Koanf, c *Config) error {
default:
}
}

if len(destActions) > 0 {
dest["actions"] = destActions
}
Expand Down Expand Up @@ -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{}:
Expand Down Expand Up @@ -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:
}
}
Expand All @@ -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{}
Expand Down Expand Up @@ -541,10 +544,10 @@ func mergeActionsSlice(src, dest []interface{}) []interface{} {
default:
}

resultActions = append(resultActions, maybeAction)
result = append(result, maybeAction)
default:
}
}

return resultActions
return result
}
102 changes: 101 additions & 1 deletion internal/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},

Check failure on line 735 in internal/config/load_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
&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{

Check failure on line 784 in internal/config/load_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
Name: "group 1",
Glob: "*.rb",
Group: &Group{
Parallel: true,
Actions: []*Action{
&Action{Run: "1.1"},

Check failure on line 790 in internal/config/load_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
&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{
Expand Down Expand Up @@ -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)
})
}

Expand Down

0 comments on commit 1d7f9f1

Please sign in to comment.