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

evilmartians / lefthook / 8373477831

21 Mar 2024 10:31AM UTC coverage: 77.882% (-0.08%) from 77.962%
8373477831

Pull #684

github

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

25 of 32 new or added lines in 3 files covered. (78.13%)

4 existing lines in 2 files now uncovered.

2405 of 3088 relevant lines covered (77.88%)

3.62 hits per line

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

68.25
/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

39
        return nil
3✔
40
}
41

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

NEW
UNCOV
47
func (c Command) GetPriority() int {
×
NEW
UNCOV
48
        return c.Priority
×
NEW
UNCOV
49
}
×
50

51
type commandRunReplace struct {
52
        Run string `mapstructure:"run"`
53
}
54

55
func mergeCommands(base, extra *viper.Viper) (map[string]*Command, error) {
6✔
56
        if base == nil && extra == nil {
6✔
57
                return nil, nil
×
58
        }
×
59

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

64
        if extra == nil {
6✔
65
                return unmarshalCommands(base.Sub("commands"))
×
66
        }
×
67

68
        commandsOrigin := base.Sub("commands")
6✔
69
        commandsOverride := extra.Sub("commands")
6✔
70
        if commandsOrigin == nil {
12✔
71
                return unmarshalCommands(commandsOverride)
6✔
72
        }
6✔
73
        if commandsOverride == nil {
6✔
74
                return unmarshalCommands(commandsOrigin)
×
75
        }
×
76

77
        runReplaces := make(map[string]*commandRunReplace)
6✔
78
        for key := range commandsOrigin.AllSettings() {
12✔
79
                var replace commandRunReplace
6✔
80

6✔
81
                substructure := commandsOrigin.Sub(key)
6✔
82
                if substructure == nil {
6✔
83
                        continue
×
84
                }
85

86
                if err := substructure.Unmarshal(&replace); err != nil {
6✔
87
                        return nil, err
×
88
                }
×
89

90
                runReplaces[key] = &replace
6✔
91
        }
92

93
        err := commandsOrigin.MergeConfigMap(commandsOverride.AllSettings())
6✔
94
        if err != nil {
6✔
95
                return nil, err
×
96
        }
×
97

98
        commands, err := unmarshalCommands(commandsOrigin)
6✔
99
        if err != nil {
6✔
100
                return nil, err
×
101
        }
×
102

103
        for key, replace := range runReplaces {
12✔
104
                if replace.Run != "" {
12✔
105
                        commands[key].Run = strings.ReplaceAll(commands[key].Run, CMD, replace.Run)
6✔
106
                }
6✔
107
        }
108

109
        return commands, nil
6✔
110
}
111

112
func unmarshalCommands(v *viper.Viper) (map[string]*Command, error) {
6✔
113
        if v == nil {
12✔
114
                return nil, nil
6✔
115
        }
6✔
116

117
        commands := make(map[string]*Command)
6✔
118
        if err := v.Unmarshal(&commands); err != nil {
6✔
119
                return nil, err
×
120
        }
×
121

122
        return commands, nil
6✔
123
}
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