-
Notifications
You must be signed in to change notification settings - Fork 220
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
handle "run" command in the skip/only setting #634
Merged
mrexox
merged 10 commits into
evilmartians:master
from
prog-supdex:skip_only_conditions
Mar 13, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f4d8b25
handle "run" command in the skip/only setting
prog-supdex c96d362
fix by codereview
prog-supdex 64257bd
chore: add a testscript
mrexox bbba913
Merge branch 'evilmartians:master' into skip_only_conditions
prog-supdex fce5f53
call skip commands via shell
prog-supdex fedebb0
try to use command for windows
prog-supdex dc3cf4d
ci: run integration tests on Windows
mrexox a27cd6d
checking windows section in testscript
prog-supdex 9a36234
test another syntax testscript
prog-supdex 348e67c
work with windows
prog-supdex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package config | ||
|
||
import ( | ||
"os/exec" | ||
"runtime" | ||
) | ||
|
||
type Exec interface { | ||
Cmd(commandLine string) bool | ||
} | ||
|
||
type osExec struct{} | ||
|
||
// NewOsExec returns an object that executes given commands in the OS. | ||
func NewOsExec() Exec { | ||
return &osExec{} | ||
} | ||
|
||
// Cmd runs plain string command. It checks only exit code and returns bool value. | ||
func (o *osExec) Cmd(commandLine string) bool { | ||
if commandLine == "" { | ||
return false | ||
} | ||
|
||
var cmd *exec.Cmd | ||
if runtime.GOOS == "windows" { | ||
cmd = exec.Command("powershell", "-Command", commandLine) | ||
} else { | ||
cmd = exec.Command("sh", "-c", commandLine) | ||
} | ||
|
||
err := cmd.Run() | ||
|
||
return err == nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/gobwas/glob" | ||
|
||
"github.com/evilmartians/lefthook/internal/git" | ||
"github.com/evilmartians/lefthook/internal/log" | ||
) | ||
|
||
type SkipChecker struct { | ||
Executor Exec | ||
} | ||
|
||
func NewSkipChecker(executor Exec) *SkipChecker { | ||
if executor == nil { | ||
executor = NewOsExec() | ||
} | ||
|
||
return &SkipChecker{Executor: executor} | ||
} | ||
|
||
func (sc *SkipChecker) Check(gitState git.State, skip interface{}, only interface{}) bool { | ||
if skip != nil { | ||
if sc.matches(gitState, skip) { | ||
return true | ||
} | ||
} | ||
|
||
if only != nil { | ||
return !sc.matches(gitState, only) | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (sc *SkipChecker) matches(gitState git.State, value interface{}) bool { | ||
switch typedValue := value.(type) { | ||
case bool: | ||
return typedValue | ||
case string: | ||
return typedValue == gitState.Step | ||
case []interface{}: | ||
return sc.matchesSlices(gitState, typedValue) | ||
} | ||
return false | ||
} | ||
|
||
func (sc *SkipChecker) matchesSlices(gitState git.State, slice []interface{}) bool { | ||
for _, state := range slice { | ||
switch typedState := state.(type) { | ||
case string: | ||
if typedState == gitState.Step { | ||
return true | ||
} | ||
case map[string]interface{}: | ||
if sc.matchesRef(gitState, typedState) { | ||
return true | ||
} | ||
|
||
if sc.matchesCommands(typedState) { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (sc *SkipChecker) matchesRef(gitState git.State, typedState map[string]interface{}) bool { | ||
ref, ok := typedState["ref"].(string) | ||
if !ok { | ||
return false | ||
} | ||
|
||
if ref == gitState.Branch { | ||
return true | ||
} | ||
|
||
g := glob.MustCompile(ref) | ||
|
||
return g.Match(gitState.Branch) | ||
} | ||
|
||
func (sc *SkipChecker) matchesCommands(typedState map[string]interface{}) bool { | ||
commandLine, ok := typedState["run"].(string) | ||
if !ok { | ||
return false | ||
} | ||
|
||
result := sc.Executor.Cmd(commandLine) | ||
|
||
log.Debugf("[lefthook] skip/only cmd: %s, result: %t", commandLine, result) | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
[windows] skip | ||
|
||
exec git init | ||
exec lefthook add pre-commit | ||
! stderr . | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
[windows] skip | ||
|
||
exec git init | ||
exec lefthook dump | ||
cmp stdout lefthook-dumped.yml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
[windows] skip | ||
|
||
exec git init | ||
exec lefthook install | ||
exec git config user.email "[email protected]" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
[windows] skip | ||
|
||
exec git init | ||
exec lefthook install | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
[windows] skip | ||
|
||
exec git init | ||
exec lefthook install | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
[windows] skip | ||
|
||
chmod 0700 hook.sh | ||
chmod 0700 commit-with-interrupt.sh | ||
exec git init | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
[windows] skip | ||
|
||
exec git init | ||
exec lefthook install | ||
exec git config user.email "[email protected]" | ||
exec git config user.name "Your Name" | ||
|
||
exec lefthook run echo_files | ||
|
||
stdout '1.txt 10.txt' | ||
|
||
-- lefthook.yml -- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[windows] skip | ||
|
||
exec git init | ||
exec git add -A | ||
exec lefthook run skip | ||
! stdout 'Ha-ha!' | ||
exec lefthook run no-skip | ||
stdout 'Ha-ha!' | ||
|
||
exec lefthook run skip-var | ||
! stdout 'Ha-ha!' | ||
|
||
env VAR=1 | ||
exec lefthook run skip-var | ||
stdout 'Ha-ha!' | ||
|
||
-- lefthook.yml -- | ||
skip_output: | ||
- skips | ||
- meta | ||
- summary | ||
- execution_info | ||
skip: | ||
skip: | ||
- run: test 1 -eq 1 | ||
commands: | ||
echo: | ||
run: echo 'Ha-ha!' | ||
|
||
no-skip: | ||
skip: | ||
- run: "[ 1 -eq 0 ]" | ||
commands: | ||
echo: | ||
run: echo 'Ha-ha!' | ||
|
||
skip-var: | ||
skip: | ||
- run: test -z $VAR | ||
commands: | ||
echo: | ||
run: echo 'Ha-ha!' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrexox I added calling the command via
sh -c
but as you said, it may not work withWindows
if it does not include WSL, Cygwin, and so on.So I added checking the OS, but I couldn't check it because we don't have a running test-integrity for windows here
I think there was a reason why it was not added to ci-cd for windows :)
But what do you think about checking the
windows
and adding test integrity for Windows to ci-cd?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's try it out!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrexox, Unfortunately, the `[windows] skip' command was necessary to skip some of the Windows test integrations.
Also, I use
PowerShell
for Windows commands because it supports more complex commands (e.g. withif
).We can avoid skipping some
test-integrities
, but it requires adapting those tests, and it is difficult without a Windows machine.The example of skipping test here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but we checked the
skip run
for Windows, and it's working.I added the separated file
skip_run_windows
for itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I think this is acceptable for now. Thank you for digging into testscript, I appreciate it. I will try to figure out how to make it work for Windows when I have a chance to work on a Windows machine.