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

feat: add jobs option #861

Merged
merged 12 commits into from
Dec 19, 2024
Prev Previous commit
Next Next commit
fix: add integrity tests and fix some odd merging
  • Loading branch information
mrexox committed Dec 19, 2024
commit 5ef890ade69c9783efbca29d1df7721aac73251e
16 changes: 8 additions & 8 deletions internal/config/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ type Job struct {
Script string `json:"script,omitempty" mapstructure:"script" toml:"script,omitempty" yaml:",omitempty"`
Runner string `json:"runner,omitempty" mapstructure:"runner" toml:"runner,omitempty" yaml:",omitempty"`

Glob string `json:"glob,omitempty" mapstructure:"glob" toml:"glob,omitempty" yaml:",omitempty"`
Root string `json:"root,omitempty" mapstructure:"root" toml:"root,omitempty" yaml:",omitempty"`
Files string `json:"files,omitempty" mapstructure:"files" toml:"files,omitempty" yaml:",omitempty"`
FailText string `json:"fail_text,omitempty" mapstructure:"fail_text" toml:"fail_text,omitempty" yaml:"fail_text,omitempty"`
Glob string `json:"glob,omitempty" mapstructure:"glob" toml:"glob,omitempty" yaml:",omitempty"`
Root string `json:"root,omitempty" mapstructure:"root" toml:"root,omitempty" yaml:",omitempty"`
Files string `json:"files,omitempty" mapstructure:"files" toml:"files,omitempty" yaml:",omitempty"`
FailText string `json:"fail_text,omitempty" koanf:"fail_text" mapstructure:"fail_text" toml:"fail_text,omitempty" yaml:"fail_text,omitempty"`

Tags []string `json:"tags,omitempty" mapstructure:"tags" toml:"tags,omitempty" yaml:",omitempty"`
FileTypes []string `json:"file_types,omitempty" mapstructure:"file_types" toml:"file_types,omitempty" yaml:"file_types,omitempty"`
Tags []string `json:"tags,omitempty" mapstructure:"tags" toml:"tags,omitempty" yaml:",omitempty"`
FileTypes []string `json:"file_types,omitempty" koanf:"file_types" mapstructure:"file_types" toml:"file_types,omitempty" yaml:"file_types,omitempty"`

Env map[string]string `json:"env,omitempty" mapstructure:"env" toml:"env,omitempty" yaml:",omitempty"`

Interactive bool `json:"interactive,omitempty" mapstructure:"interactive" toml:"interactive,omitempty" yaml:",omitempty"`
UseStdin bool `json:"use_stdin,omitempty" mapstructure:"use_stdin" toml:"use_stdin,omitempty" yaml:",omitempty"`
StageFixed bool `json:"stage_fixed,omitempty" mapstructure:"stage_fixed" toml:"stage_fixed,omitempty" yaml:"stage_fixed,omitempty"`
UseStdin bool `json:"use_stdin,omitempty" koanf:"use_stdin" mapstructure:"use_stdin" toml:"use_stdin,omitempty" yaml:",omitempty"`
StageFixed bool `json:"stage_fixed,omitempty" koanf:"stage_fixed" mapstructure:"stage_fixed" toml:"stage_fixed,omitempty" yaml:"stage_fixed,omitempty"`

Exclude interface{} `json:"exclude,omitempty" mapstructure:"exclude" toml:"exclude,omitempty" yaml:",omitempty"`
Skip interface{} `json:"skip,omitempty" mapstructure:"skip" toml:"skip,omitempty,inline" yaml:",omitempty"`
Expand Down
6 changes: 4 additions & 2 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

var (
hookKeyRegexp = regexp.MustCompile(`^(?P<hookName>[^.]+)\.(scripts|commands)`)
hookKeyRegexp = regexp.MustCompile(`^(?P<hookName>[^.]+)\.(scripts|commands|jobs)`)
localConfigNames = []string{"lefthook-local", ".lefthook-local"}
mainConfigNames = []string{"lefthook", ".lefthook"}
extensions = []string{
Expand Down Expand Up @@ -116,16 +116,18 @@ func Load(filesystem afero.Fs, repo *git.Repository) (*Config, error) {
}

// Load optional local config (e.g. lefthook-local.yml)
var noLocal bool
if err := loadOne(secondary, filesystem, repo.RootPath, localConfigNames); err != nil {
var configNotFoundErr ConfigNotFoundError
if ok := errors.As(err, &configNotFoundErr); !ok {
return nil, err
}
noLocal = true
}

// Load local `extends`
localExtends := secondary.Strings("extends")
if len(localExtends) > 0 && !slices.Equal(extends, localExtends) {
if !noLocal && !slices.Equal(extends, localExtends) {
if err := extend(secondary, filesystem, repo.RootPath, localExtends); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ func (r *Runner) RunAll(ctx context.Context) ([]Result, error) {
defer log.StopSpinner()
}

results = append(results, r.runJobs(ctx)...)

scriptDirs := make([]string, 0, len(r.SourceDirs))
for _, sourceDir := range r.SourceDirs {
scriptDirs = append(scriptDirs, filepath.Join(
Expand All @@ -102,6 +100,8 @@ func (r *Runner) RunAll(ctx context.Context) ([]Result, error) {

r.preHook()

results = append(results, r.runJobs(ctx)...)

for _, dir := range scriptDirs {
results = append(results, r.runScripts(ctx, dir)...)
}
Expand Down
16 changes: 16 additions & 0 deletions testdata/job_fail_text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
exec git init
exec git config user.email "you@example.com"
exec git config user.name "Your Name"
exec git add -A
exec lefthook install
! exec git commit -m 'test'
stderr '\s*fails: no such command\s*'

-- lefthook.yml --
output:
- failure
pre-commit:
jobs:
- name: fails
run: oops-no-such-command
fail_text: no such command
57 changes: 57 additions & 0 deletions testdata/job_filter_by_file_type.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[windows] skip

exec git init
exec git config user.email "you@example.com"
exec git config user.name "Your Name"
exec lefthook install
chmod 777 executable
symlink symlink -> results
exec git add -A
exec git commit -m 'test'
exec lefthook run filters
stdout '.*all ❯\s+executable lefthook.yml results symlink\s+┃.*'
stdout '.*filter_text ❯\s+executable lefthook.yml results\s+┃.*'
stdout '.*filter_executable ❯\s+executable\s+┃.*'
stdout '.*filter_symlink ❯\s+symlink\s+┃.*'
stdout '.*filter_not_symlink ❯\s+executable lefthook.yml results\s+┃.*'
stdout '.*filter_not_executable ❯\s+lefthook.yml results symlink\s*'

-- lefthook.yml --
output:
- execution
- skips
filters:
piped: true
jobs:
- name: all
run: echo {all_files}

- name: filter_text
run: echo {all_files}
file_types: text

- name: filter_executable
run: echo {all_files}
file_types: executable

- name: filter_symlink
run: echo {all_files}
file_types: symlink

- name: filter_not_symlink
run: echo {all_files}
file_types: not symlink

- name: filter_not_executable
run: echo {all_files}
file_types:
- not executable

-- results --
some text

-- executable --
#!/bin/sh

echo 'Executable'

125 changes: 125 additions & 0 deletions testdata/job_merging.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
[windows] skip

exec git init
exec lefthook dump
cmp stdout dump.yml
! stderr .

-- lefthook.yml --
extends:
- extends/e1.yml

pre-commit:
jobs:
- name: group
group:
jobs:
- name: child
run: named
- run: 0 no-name
- name: echo
run: echo 0
- run: lefthook.yml

-- extends/e1.yml --
extends:
- extends/e2.yml

pre-commit:
jobs:
- name: group
group:
jobs:
- name: child
run: child named
- run: 1 no-name
- name: echo
run: echo 1
skip: true
- run: e1

e1:
jobs:
- name: echo
run: e1

-- extends/e2.yml --
extends:
- extends/e3.yml

pre-commit:
jobs:
- name: group
glob: "*.rb"
group:
jobs:
- name: child
run: child named with glob
- run: 2 no-name
- name: echo
run: echo 2
tags: ["backend"]
- run: e2

e2:
jobs:
- name: echo
run: e2

-- extends/e3.yml --
pre-commit:
jobs:
- name: group
glob: "*.rb"
group:
jobs:
- name: child
stage_fixed: true
- run: 3 no-name
- name: echo
glob: 3
- run: e3

e3:
jobs:
- name: echo
run: e3

-- dump.yml --
e1:
jobs:
- name: echo
run: e1
e2:
jobs:
- name: echo
run: e2
e3:
jobs:
- name: echo
run: e3
extends:
- extends/e3.yml
pre-commit:
jobs:
- name: group
glob: '*.rb'
group:
jobs:
- name: child
run: child named with glob
stage_fixed: true
- run: 0 no-name
- run: 1 no-name
- run: 2 no-name
- run: 3 no-name
- name: echo
run: echo 2
glob: "3"
tags:
- backend
skip: true
- run: lefthook.yml
- run: e1
- run: e2
- run: e3
24 changes: 24 additions & 0 deletions testdata/job_stage_fixed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
exec git init
exec lefthook install
exec git config user.email "you@example.com"
exec git config user.name "Your Name"
exec git add -A
exec git status --short
exec git commit -m 'test stage_fixed'
exec git status --short
! stdout .

-- lefthook.yml --
min_version: 1.1.1
pre-commit:
jobs:
- stage_fixed: true
run: |
echo newline >> "[file].js"
echo newline >> file.txt

-- file.txt --
sometext

-- [file].js --
somecode