Skip to content

Commit ea10348

Browse files
committed
Adding zero-on-missing flag, to change how check behaves when there isn't a computed value for a stored measure.
1 parent 2be52d6 commit ea10348

File tree

4 files changed

+57
-20
lines changed

4 files changed

+57
-20
lines changed

cmd/check.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"io"
88
)
99

10-
func Check(prefix string, slack int, write bool, inputType string, input io.Reader) int {
10+
func Check(prefix string, slack int, write bool, inputType string, zeroOnMissing bool, input io.Reader) int {
1111
// Parse the measures from stdin
1212
log.INFO.Println("Parsing measures from stdin")
1313
passedMeasures, err := store.ParseMeasures(input, store.ParseInputType(inputType))
@@ -48,7 +48,7 @@ func Check(prefix string, slack int, write bool, inputType string, input io.Read
4848
return 40
4949
} else {
5050
log.INFO.Println("Checking passed measure against stored value")
51-
finalMeasures, compareErr := store.CompareMeasures(prefix, commitmeasure.CommitHash, commitmeasure.Measures, passedMeasures, slack)
51+
finalMeasures, compareErr := store.CompareMeasures(prefix, commitmeasure.CommitHash, commitmeasure.Measures, passedMeasures, slack, zeroOnMissing)
5252

5353
if write {
5454
log.INFO.Println("Writing measure values.")

cmd/check_test.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,46 @@ func TestCheck(t *testing.T) {
3535

3636
t.Logf("Running check command w: %t i: %s", false, "foo,6")
3737

38-
errCode := Check("", 0, true, "csv", strings.NewReader("foo,6"))
38+
errCode := Check("", 0, true, "csv", false, strings.NewReader("foo,6"))
3939

4040
if errCode != 50 {
4141
t.Fatalf("Check command passed unexpectedly!")
4242
}
4343

44-
errCode = Check("", 0, true, "csv", strings.NewReader("foo,6"))
44+
errCode = Check("", 0, true, "csv", false, strings.NewReader("foo,6"))
4545

4646
if errCode != 50 {
4747
t.Fatalf("Check command passed unexpectedly!")
4848
}
4949
}
5050

51+
func TestZeroMissing(t *testing.T) {
52+
if testing.Verbose() {
53+
log.SetLogThreshold(log.LevelInfo)
54+
log.SetStdoutThreshold(log.LevelInfo)
55+
}
56+
57+
createEmptyGitRepo(t)
58+
59+
runCheck(t, true, "foo,5")
60+
61+
t.Logf("Running check command w: %t i: %s", false, "")
62+
63+
errCode := Check("", 0, true, "csv", false, strings.NewReader(""))
64+
65+
if errCode != 50 {
66+
t.Fatalf("Check command passed unexpectedly!")
67+
}
68+
69+
t.Logf("Running check command zero on missing w: %t i: %s", false, "")
70+
71+
errCode = Check("", 0, true, "csv", false, strings.NewReader(""))
72+
73+
if errCode != 0 {
74+
t.Fatalf("Check command failed unexpectedly!")
75+
}
76+
}
77+
5178
func TestCheckPrefix(t *testing.T) {
5279
if testing.Verbose() {
5380
log.SetLogThreshold(log.LevelInfo)
@@ -62,7 +89,7 @@ func TestCheckPrefix(t *testing.T) {
6289

6390
t.Logf("Running check command p: %s w: %t i: %s", "foobar", false, "foo,6")
6491

65-
errCode := Check("foobar", 0, false, "csv", strings.NewReader("foo,6"))
92+
errCode := Check("foobar", 0, false, "csv", false, strings.NewReader("foo,6"))
6693

6794
if errCode != 50 {
6895
t.Fatalf("Check command passed unexpectedly!")
@@ -84,7 +111,7 @@ func TestCheckSlack(t *testing.T) {
84111

85112
t.Logf("Running check command p: %s w: %t i: %s", "pageweight", false, "gzippedjs,16")
86113

87-
errCode := Check("pageweight", slack, false, "csv", strings.NewReader("gzippedjs,16"))
114+
errCode := Check("pageweight", slack, false, "csv", false, strings.NewReader("gzippedjs,16"))
88115

89116
if errCode != 50 {
90117
t.Fatalf("Check command passed unexpectedly!")
@@ -105,7 +132,7 @@ func TestCheckExcuse(t *testing.T) {
105132

106133
t.Logf("Running check command p: %s w: %t i: %s", "foobar", false, "foo,6")
107134

108-
errCode := Check("foobar", 0, false, "csv", strings.NewReader("foo,6"))
135+
errCode := Check("foobar", 0, false, "csv", false, strings.NewReader("foo,6"))
109136

110137
if errCode != 50 {
111138
t.Fatalf("Check command passed unexpectedly!")
@@ -117,7 +144,7 @@ func TestCheckExcuse(t *testing.T) {
117144

118145
t.Logf("Running check command p: %s w: %t i: %s", "barfoo", false, "foo,7")
119146

120-
errCode = Check("barfoo", 0, false, "csv", strings.NewReader("foo,7"))
147+
errCode = Check("barfoo", 0, false, "csv", false, strings.NewReader("foo,7"))
121148

122149
if errCode != 50 {
123150
t.Fatalf("Check command passed unexpectedly!")
@@ -138,15 +165,15 @@ func TestCheckWithCheckstyleInput(t *testing.T) {
138165

139166
t.Logf("Running check command p: %s w: %t i: %s", "jshint", true, checkStyleFile)
140167

141-
errCode := Check("jshint", 0, true, "checkstyle", checkStyleFile)
168+
errCode := Check("jshint", 0, true, "checkstyle", false, checkStyleFile)
142169

143170
if errCode != 0 {
144171
t.Fatalf("Check command failed! Error code: %d", errCode)
145172
}
146173

147174
t.Logf("Running check command p: %s w: %t i: %s", "jshint", false, "errors,951")
148175

149-
errCode = Check("jshint", 0, false, "csv", strings.NewReader("errors,951"))
176+
errCode = Check("jshint", 0, false, "csv", false, strings.NewReader("errors,951"))
150177

151178
if errCode != 50 {
152179
t.Fatalf("Check command passed unexpectedly!")
@@ -174,7 +201,7 @@ func runCheckP(t *testing.T, prefix string, write bool, input string) {
174201
func runCheckPS(t *testing.T, prefix string, slack int, write bool, input string) {
175202
t.Logf("Running check command p: %s s: %d, w: %t i: %s", prefix, slack, write, input)
176203

177-
errCode := Check(prefix, slack, write, "csv", strings.NewReader(input))
204+
errCode := Check(prefix, slack, write, "csv", false, strings.NewReader(input))
178205

179206
if errCode != 0 {
180207
t.Fatalf("Check command failed! Error code: %d", errCode)

git-ratchet.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var GitTag string // Will be passed to the compiler by scripts/build.sh
1313
func main() {
1414
var write bool
1515
var verbose bool
16+
var zeroOnMissing bool
1617
var prefix string
1718
var slack int
1819
var inputType string
@@ -36,8 +37,8 @@ The most recent stored values are found by walking up the commit graph and looki
3637
log.SetLogThreshold(log.LevelInfo)
3738
log.SetStdoutThreshold(log.LevelInfo)
3839
}
39-
40-
err := ratchet.Check(prefix, slack, write, inputType, os.Stdin)
40+
41+
err := ratchet.Check(prefix, slack, write, inputType, zeroOnMissing, os.Stdin)
4142
if err != 0 {
4243
os.Exit(err)
4344
}
@@ -47,6 +48,7 @@ The most recent stored values are found by walking up the commit graph and looki
4748
checkCmd.Flags().BoolVarP(&write, "write", "w", false, "write values if no increase is detected. only use on your CI server.")
4849
checkCmd.Flags().IntVarP(&slack, "slack", "s", 0, "slack value, increase within the range of the slack is acceptable.")
4950
checkCmd.Flags().StringVarP(&inputType, "inputType", "i", "csv", "input type. csv and checkstyle available.")
51+
checkCmd.Flags().BoolVarP(&zeroOnMissing, "zero-on-missing", "z", false, "set measure values to zero on missing..")
5052

5153
var measure string
5254
var excuse string

store/reader.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,13 @@ func ParseMeasuresCheckstyle(r io.Reader) ([]Measure, error) {
155155
return []Measure{{Name: "errors", Value: errors, Baseline: errors}}, nil
156156
}
157157

158-
func CompareMeasures(prefix string, hash string, storedm []Measure, computedm []Measure, slack int) ([]Measure, error) {
159-
if len(computedm) == 0 {
160-
return computedm, errors.New("No measures passed to git-ratchet to compare against.")
161-
}
162-
158+
func CompareMeasures(prefix string, hash string, storedm []Measure, computedm []Measure, slack int, zeroOnMissing bool) ([]Measure, error) {
163159
if len(storedm) == 0 {
164160
return computedm, errors.New("No stored measures to compare against.")
165161
}
166162

167163
failing := make([]string, 0)
164+
zeroMes := make([]Measure, 0)
168165

169166
i := 0
170167
j := 0
@@ -181,7 +178,11 @@ func CompareMeasures(prefix string, hash string, storedm []Measure, computedm []
181178
log.INFO.Printf("Checking meaures: %s %s", stored.Name, computed.Name)
182179
if stored.Name < computed.Name {
183180
log.ERROR.Printf("Missing computed value for stored measure: %s", stored.Name)
184-
failing = append(failing, stored.Name)
181+
if zeroOnMissing {
182+
zeroMes = append(zeroMes, Measure{Name: stored.Name, Value: 0, Baseline: 0})
183+
} else {
184+
failing = append(failing, stored.Name)
185+
}
185186
i++
186187
} else if computed.Name < stored.Name {
187188
log.WARN.Printf("New measure found: %s", computed.Name)
@@ -200,7 +201,11 @@ func CompareMeasures(prefix string, hash string, storedm []Measure, computedm []
200201
for i < len(storedm) {
201202
stored := storedm[i]
202203
log.ERROR.Printf("Missing computed value for stored measure: %s", stored.Name)
203-
failing = append(failing, stored.Name)
204+
if zeroOnMissing {
205+
zeroMes = append(zeroMes, Measure{Name: stored.Name, Value: 0, Baseline: 0})
206+
} else {
207+
failing = append(failing, stored.Name)
208+
}
204209
i++
205210
}
206211

@@ -247,6 +252,9 @@ func CompareMeasures(prefix string, hash string, storedm []Measure, computedm []
247252
return computedm, errors.New("One or more metrics currently failing.")
248253
}
249254
}
255+
256+
computedm = append(computedm, zeroMes...)
257+
sort.Sort(ByName(computedm))
250258

251259
return computedm, nil
252260
}

0 commit comments

Comments
 (0)