Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support .lefthook.yml and .lefthook-local.yml #520

Merged
merged 6 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (err NotFoundError) Error() string {

// Loads configs from the given directory with extensions.
func Load(fs afero.Fs, repo *git.Repository) (*Config, error) {
global, err := readOne(fs, repo.RootPath, []string{".lefthook", "lefthook"})
global, err := readOne(fs, repo.RootPath, []string{"lefthook", ".lefthook"})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func readOne(fs afero.Fs, path string, names []string) (*viper.Viper, error) {
// mergeAll merges (.lefthook or lefthook) and (extended config) and (remote)
// and (.lefthook-local or .lefthook-local) configs.
func mergeAll(fs afero.Fs, repo *git.Repository) (*viper.Viper, error) {
extends, err := readOne(fs, repo.RootPath, []string{".lefthook", "lefthook"})
extends, err := readOne(fs, repo.RootPath, []string{"lefthook", ".lefthook"})
if err != nil {
return nil, err
}
Expand All @@ -111,7 +111,7 @@ func mergeAll(fs afero.Fs, repo *git.Repository) (*viper.Viper, error) {
return nil, err
}

if err := mergeOne([]string{".lefthook-local", "lefthook-local"}, "", extends); err == nil {
if err := mergeOne([]string{"lefthook-local", ".lefthook-local"}, "", extends); err == nil {
if err = extend(extends, repo.RootPath); err != nil {
return nil, err
}
Expand Down
20 changes: 12 additions & 8 deletions internal/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pre-commit:
},
},
{
name: "with global, dot has priority",
name: "with global, nodot has priority",
otherFiles: map[string]string{
".lefthook.yml": `
pre-commit:
Expand All @@ -103,7 +103,7 @@ pre-commit:
Parallel: false,
Commands: map[string]*Command{
"tests": {
Run: "yarn test1",
Run: "yarn test2",
},
},
},
Expand Down Expand Up @@ -297,7 +297,7 @@ pre-push:
},
},
{
name: "with overrides, dot has priority",
name: "with overrides, nodot has priority",
otherFiles: map[string]string{
"lefthook.yml": `
pre-push:
Expand Down Expand Up @@ -329,7 +329,7 @@ pre-push:
Runner: "bash",
},
"local-extend": {
Runner: "bash1",
Runner: "bash2",
},
},
},
Expand Down Expand Up @@ -614,12 +614,16 @@ pre-push:
}

t.Run(fmt.Sprintf("%d: %s", i, tt.name), func(t *testing.T) {
if err := fs.WriteFile(filepath.Join(root, "lefthook.yml"), []byte(tt.global), 0o644); err != nil {
t.Errorf("unexpected error: %s", err)
if tt.global != "" {
Copy link
Contributor Author

@hyperupcall hyperupcall Jul 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only write to the config file if we explicitly specify its content. That way, if .lefthook.yml is written to, an empty lefthook.yml doesn't always take precedence

if err := fs.WriteFile(filepath.Join(root, "lefthook.yml"), []byte(tt.global), 0o644); err != nil {
t.Errorf("unexpected error: %s", err)
}
}

if err := fs.WriteFile(filepath.Join(root, "lefthook-local.yml"), []byte(tt.local), 0o644); err != nil {
t.Errorf("unexpected error: %s", err)
if tt.local != "" {
if err := fs.WriteFile(filepath.Join(root, "lefthook-local.yml"), []byte(tt.local), 0o644); err != nil {
t.Errorf("unexpected error: %s", err)
}
}

if len(tt.remoteConfigPath) > 0 {
Expand Down
7 changes: 7 additions & 0 deletions internal/lefthook/dump_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package lefthook

import "testing"

func TestLefthookDump(t *testing.T) {

}