• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evilmartians / lefthook / 8504903245

01 Apr 2024 06:45AM UTC coverage: 81.325% (+3.4%) from 77.962%
8504903245

Pull #684

github

web-flow
Merge e09d0a9fa into 599459eb8
Pull Request #684: feat: add priorities to scripts

37 of 39 new or added lines in 3 files covered. (94.87%)

2 existing lines in 2 files now uncovered.

2565 of 3154 relevant lines covered (81.33%)

7.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/internal/config/command.go
1
package config
2

3
import (
4
        "errors"
5
        "strings"
6

7
        "github.com/spf13/viper"
8

9
        "github.com/evilmartians/lefthook/internal/git"
10
)
11

12
var errFilesIncompatible = errors.New("One of your runners contains incompatible file types")
13

14
type Command struct {
15
        Run string `json:"run" mapstructure:"run" toml:"run" yaml:"run"`
16

17
        Skip  interface{}       `json:"skip,omitempty"  mapstructure:"skip"  toml:"skip,omitempty,inline" yaml:",omitempty"`
18
        Only  interface{}       `json:"only,omitempty"  mapstructure:"only"  toml:"only,omitempty,inline" yaml:",omitempty"`
19
        Tags  []string          `json:"tags,omitempty"  mapstructure:"tags"  toml:"tags,omitempty"        yaml:",omitempty"`
20
        Glob  string            `json:"glob,omitempty"  mapstructure:"glob"  toml:"glob,omitempty"        yaml:",omitempty"`
21
        Files string            `json:"files,omitempty" mapstructure:"files" toml:"files,omitempty"       yaml:",omitempty"`
22
        Env   map[string]string `json:"env,omitempty"   mapstructure:"env"   toml:"env,omitempty"         yaml:",omitempty"`
23

24
        Root     string `json:"root,omitempty"     mapstructure:"root"     toml:"root,omitempty"     yaml:",omitempty"`
25
        Exclude  string `json:"exclude,omitempty"  mapstructure:"exclude"  toml:"exclude,omitempty"  yaml:",omitempty"`
26
        Priority int    `json:"priority,omitempty" mapstructure:"priority" toml:"priority,omitempty" yaml:",omitempty"`
27

28
        FailText    string `json:"fail_text,omitempty"   mapstructure:"fail_text"   toml:"fail_text,omitempty"   yaml:"fail_text,omitempty"`
29
        Interactive bool   `json:"interactive,omitempty" mapstructure:"interactive" toml:"interactive,omitempty" yaml:",omitempty"`
30
        UseStdin    bool   `json:"use_stdin,omitempty"   mapstructure:"use_stdin"   toml:"use_stdin,omitempty"   yaml:",omitempty"`
31
        StageFixed  bool   `json:"stage_fixed,omitempty" mapstructure:"stage_fixed" toml:"stage_fixed,omitempty" yaml:"stage_fixed,omitempty"`
32
}
33

34
func (c Command) Validate() error {
3✔
35
        if !isRunnerFilesCompatible(c.Run) {
3✔
36
                return errFilesIncompatible
37
        }
38

3✔
39
        return nil
6✔
40
}
41

42
func (c Command) DoSkip(gitState git.State) bool {
3✔
43
        skipChecker := NewSkipChecker(NewOsExec())
6✔
44
        return skipChecker.Check(gitState, c.Skip, c.Only)
3✔
45
}
3✔
46

3✔
47
type commandRunReplace struct {
3✔
48
        Run string `mapstructure:"run"`
3✔
49
}
3✔
50

51
func mergeCommands(base, extra *viper.Viper) (map[string]*Command, error) {
6✔
52
        if base == nil && extra == nil {
6✔
UNCOV
53
                return nil, nil
×
54
        }
55

6✔
56
        if base == nil {
18✔
57
                return unmarshalCommands(extra.Sub("commands"))
6✔
58
        }
6✔
59

60
        if extra == nil {
18✔
61
                return unmarshalCommands(base.Sub("commands"))
6✔
62
        }
6✔
63

64
        commandsOrigin := base.Sub("commands")
12✔
65
        commandsOverride := extra.Sub("commands")
6✔
66
        if commandsOrigin == nil {
12✔
67
                return unmarshalCommands(commandsOverride)
6✔
68
        }
12✔
69
        if commandsOverride == nil {
12✔
70
                return unmarshalCommands(commandsOrigin)
12✔
71
        }
6✔
72

6✔
73
        runReplaces := make(map[string]*commandRunReplace)
12✔
74
        for key := range commandsOrigin.AllSettings() {
12✔
75
                var replace commandRunReplace
6✔
76

6✔
77
                substructure := commandsOrigin.Sub(key)
12✔
78
                if substructure == nil {
18✔
79
                        continue
6✔
80
                }
6✔
81

6✔
82
                if err := substructure.Unmarshal(&replace); err != nil {
12✔
83
                        return nil, err
×
84
                }
85

86
                runReplaces[key] = &replace
12✔
87
        }
88

89
        err := commandsOrigin.MergeConfigMap(commandsOverride.AllSettings())
6✔
90
        if err != nil {
12✔
91
                return nil, err
92
        }
93

6✔
94
        commands, err := unmarshalCommands(commandsOrigin)
12✔
95
        if err != nil {
6✔
96
                return nil, err
×
97
        }
98

6✔
99
        for key, replace := range runReplaces {
18✔
100
                if replace.Run != "" {
12✔
101
                        commands[key].Run = strings.ReplaceAll(commands[key].Run, CMD, replace.Run)
6✔
102
                }
6✔
103
        }
12✔
104

12✔
105
        return commands, nil
12✔
106
}
6✔
107

108
func unmarshalCommands(v *viper.Viper) (map[string]*Command, error) {
6✔
109
        if v == nil {
18✔
110
                return nil, nil
6✔
111
        }
6✔
112

6✔
113
        commands := make(map[string]*Command)
18✔
114
        if err := v.Unmarshal(&commands); err != nil {
12✔
115
                return nil, err
6✔
116
        }
117

6✔
118
        return commands, nil
12✔
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc