From 9d8dc3d7f8c1af13f48c68a2e74c1d08299a3849 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Fri, 12 Jan 2024 11:38:20 +0300 Subject: [PATCH] fix: test sh syntax in files option and fix skip_output parsing --- internal/git/repository.go | 16 ++++++--------- internal/log/skip_settings.go | 16 +++++++++------ internal/log/skip_settings_test.go | 6 +++--- testdata/sh_syntax_in_files.txt | 32 ++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 testdata/sh_syntax_in_files.txt diff --git a/internal/git/repository.go b/internal/git/repository.go index a0ffe0a6..c1dec4b9 100644 --- a/internal/git/repository.go +++ b/internal/git/repository.go @@ -29,6 +29,9 @@ var ( cmdGitPath = []string{"git", "rev-parse", "--git-dir"} cmdAllFiles = []string{"git", "ls-files", "--cached"} cmdCreateStash = []string{"git", "stash", "create"} + cmdStageFiles = []string{"git", "add"} + cmdRemotes = []string{"git", "branch", " --remotes"} + cmdHideUnstaged = []string{"git", "checkout", "--force", "--"} ) // Repository represents a git repository. @@ -112,7 +115,7 @@ func (r *Repository) PushFiles() ([]string, error) { } if len(r.headBranch) == 0 { - branches, err := r.Git.CmdLines([]string{"git", "branch", " --remotes"}) + branches, err := r.Git.CmdLines(cmdRemotes) if err != nil { return nil, err } @@ -185,14 +188,7 @@ func (r *Repository) SaveUnstaged(files []string) error { } func (r *Repository) HideUnstaged(files []string) error { - _, err := r.Git.Cmd( - append([]string{ - "git", - "checkout", - "--force", - "--", - }, files...), - ) + _, err := r.Git.Cmd(append(cmdHideUnstaged, files...)) return err } @@ -280,7 +276,7 @@ func (r *Repository) AddFiles(files []string) error { } _, err := r.Git.Cmd( - append([]string{"git", "add"}, files...), + append(cmdStageFiles, files...), ) return err diff --git a/internal/log/skip_settings.go b/internal/log/skip_settings.go index 6fbe6f27..47aa1fab 100644 --- a/internal/log/skip_settings.go +++ b/internal/log/skip_settings.go @@ -1,6 +1,8 @@ package log -import "strings" +import ( + "strings" +) const ( skipMeta = 1 << iota @@ -21,12 +23,14 @@ func (s *SkipSettings) ApplySettings(tags string, skipOutput interface{}) { switch typedSkipOutput := skipOutput.(type) { case bool: s.SkipAll(typedSkipOutput) - case []string: - if tags != "" { - typedSkipOutput = append(typedSkipOutput, strings.Split(tags, ",")...) - } + case []interface{}: for _, skipOption := range typedSkipOutput { - s.applySetting(skipOption) + s.applySetting(skipOption.(string)) + } + if tags != "" { + for _, skipOption := range strings.Split(tags, ",") { + s.applySetting(skipOption) + } } } } diff --git a/internal/log/skip_settings_test.go b/internal/log/skip_settings_test.go index 9eb12b53..15706964 100644 --- a/internal/log/skip_settings_test.go +++ b/internal/log/skip_settings_test.go @@ -11,7 +11,7 @@ func TestSkipSetting(t *testing.T) { results map[string]bool }{ { - settings: []string{}, + settings: []interface{}{}, results: map[string]bool{}, }, { @@ -19,14 +19,14 @@ func TestSkipSetting(t *testing.T) { results: map[string]bool{}, }, { - settings: []string{"failure", "execution"}, + settings: []interface{}{"failure", "execution"}, results: map[string]bool{ "failure": true, "execution": true, }, }, { - settings: []string{ + settings: []interface{}{ "meta", "summary", "success", diff --git a/testdata/sh_syntax_in_files.txt b/testdata/sh_syntax_in_files.txt new file mode 100644 index 00000000..a45c9a25 --- /dev/null +++ b/testdata/sh_syntax_in_files.txt @@ -0,0 +1,32 @@ +exec git init +exec lefthook install +exec git config user.email "you@example.com" +exec git config user.name "Your Name" + +exec lefthook run echo_files +stdout '1.txt 10.txt' + +-- lefthook.yml -- +skip_output: + - meta # Skips lefthook version printing + - summary # Skips summary block (successful and failed steps) printing + - empty_summary # Skips summary heading when there are no steps to run + - success # Skips successful steps printing + - failure # Skips failed steps printing + - execution_info # Skips printing `EXECUTE > ...` logging + - skips + +echo_files: + commands: + echo: + files: ls | grep 1 + run: echo {files} + +-- 1.txt -- +1.txt + +-- 10.txt -- +10.txt + +-- 20.txt -- +20.txt