Skip to content

Commit 62686ea

Browse files
committed
tests: harden args/msg coverage and remove brittle line-number checks
1 parent a662e47 commit 62686ea

3 files changed

Lines changed: 193 additions & 76 deletions

File tree

internal/args/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func scVersion(w io.Writer) error {
283283
fmt.Fprintf(w, "branch=%s%s, ", Branch, dirtyText)
284284
}
285285
if Commit != "" {
286-
fmt.Fprintf(w, "commit=%s, ", Date)
286+
fmt.Fprintf(w, "commit=%s, ", Commit)
287287
}
288288
fmt.Fprintf(w, "built at %s\n", Date)
289289
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
package args
4+
5+
import (
6+
"bytes"
7+
"testing"
8+
9+
"github.com/tj/assert"
10+
)
11+
12+
type versionState struct {
13+
Version string
14+
Commit string
15+
Date string
16+
Branch string
17+
GitState string
18+
GitStatus string
19+
BuiltBy string
20+
Verbose bool
21+
}
22+
23+
func snapshotVersionState() versionState {
24+
return versionState{
25+
Version: Version,
26+
Commit: Commit,
27+
Date: Date,
28+
Branch: Branch,
29+
GitState: GitState,
30+
GitStatus: GitStatus,
31+
BuiltBy: BuiltBy,
32+
Verbose: Verbose,
33+
}
34+
}
35+
36+
func restoreVersionState(s versionState) {
37+
Version = s.Version
38+
Commit = s.Commit
39+
Date = s.Date
40+
Branch = s.Branch
41+
GitState = s.GitState
42+
GitStatus = s.GitStatus
43+
BuiltBy = s.BuiltBy
44+
Verbose = s.Verbose
45+
}
46+
47+
func TestScVersionDevFallback(t *testing.T) {
48+
old := snapshotVersionState()
49+
defer restoreVersionState(old)
50+
51+
Version = ""
52+
Commit = ""
53+
Date = ""
54+
Branch = ""
55+
GitState = ""
56+
GitStatus = ""
57+
BuiltBy = ""
58+
Verbose = false
59+
60+
var out bytes.Buffer
61+
err := scVersion(&out)
62+
assert.Nil(t, err)
63+
assert.Equal(t, "version=dev (no build info)\n", out.String())
64+
}
65+
66+
func TestScVersionReleaseOutput(t *testing.T) {
67+
old := snapshotVersionState()
68+
defer restoreVersionState(old)
69+
70+
Version = "1.2.3"
71+
Commit = "abc1234"
72+
Date = "2026-02-21T12:00:00Z"
73+
Branch = ""
74+
GitState = "clean"
75+
GitStatus = ""
76+
BuiltBy = "ci"
77+
Verbose = false
78+
79+
var out bytes.Buffer
80+
err := scVersion(&out)
81+
assert.Nil(t, err)
82+
assert.Equal(t, "version=1.2.3, commit=abc1234, built at 2026-02-21T12:00:00Z (built by ci)\n", out.String())
83+
}
84+
85+
func TestScVersionBranchFallbackAndDirtyList(t *testing.T) {
86+
old := snapshotVersionState()
87+
defer restoreVersionState(old)
88+
89+
Version = ""
90+
Commit = "abc1234"
91+
Date = "2026-02-21T12:00:00Z"
92+
Branch = "main"
93+
GitState = "dirty"
94+
GitStatus = "M internal/args/handler.go|?? internal/args/new_test.go|"
95+
BuiltBy = ""
96+
Verbose = true
97+
98+
var out bytes.Buffer
99+
err := scVersion(&out)
100+
assert.Nil(t, err)
101+
s := out.String()
102+
assert.Contains(t, s, "https://github.com/rokath/trice\n")
103+
assert.Contains(t, s, "branch=main (local modifications at build time), commit=abc1234, built at 2026-02-21T12:00:00Z\n")
104+
assert.Contains(t, s, "modified files at build time:\n")
105+
assert.Contains(t, s, " M internal/args/handler.go\n")
106+
assert.Contains(t, s, " ?? internal/args/new_test.go\n")
107+
}
108+
109+
func TestIsLogFlagPassed(t *testing.T) {
110+
FlagsInit()
111+
err := fsScLog.Parse([]string{"-ts32", "ms", "-encoding", "CHAR"})
112+
assert.Nil(t, err)
113+
assert.True(t, isLogFlagPassed("ts32"))
114+
assert.True(t, isLogFlagPassed("encoding"))
115+
assert.False(t, isLogFlagPassed("ts16"))
116+
}

pkg/msg/msg_blackbox_test.go

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,94 @@ package msg_test
44

55
import (
66
"errors"
7-
"log"
7+
"strings"
8+
"testing"
89

910
"github.com/rokath/trice/pkg/msg"
11+
"github.com/rokath/trice/pkg/tst"
1012
)
1113

12-
func ExampleInfo() {
13-
msg.Info("code issue")
14-
// Output:
15-
// Error in msg_blackbox_test.go:14: func 'github.com/rokath/trice/pkg/msg_test.ExampleInfo' -> code issue
14+
func TestInfoOutput(t *testing.T) {
15+
out := tst.CaptureStdOut(func() {
16+
msg.Info("code issue")
17+
})
18+
if !strings.Contains(out, "code issue") {
19+
t.Fatalf("missing message in output: %q", out)
20+
}
21+
if !strings.Contains(out, "msg_blackbox_test.go") {
22+
t.Fatalf("missing file marker in output: %q", out)
23+
}
1624
}
1725

18-
func ExampleOnErr() {
19-
var e error
20-
msg.OnErr(e)
21-
e = errors.New("s.th. went wrong")
22-
msg.OnErr(e)
23-
// Output:
24-
// Error in msg_blackbox_test.go:23: func 'github.com/rokath/trice/pkg/msg_test.ExampleOnErr' -> s.th. went wrong
26+
func TestOnErrOutput(t *testing.T) {
27+
out := tst.CaptureStdOut(func() {
28+
msg.OnErr(nil)
29+
msg.OnErr(errors.New("s.th. went wrong"))
30+
})
31+
if !strings.Contains(out, "s.th. went wrong") {
32+
t.Fatalf("missing error string in output: %q", out)
33+
}
2534
}
2635

27-
func ExampleFatalOnErr() {
28-
log.SetFlags(0)
29-
var e error
30-
msg.FatalOnErr(e)
31-
// Output:
36+
func TestInfoOnErrOutput(t *testing.T) {
37+
out := tst.CaptureStdOut(func() {
38+
msg.InfoOnErr(nil, "just in case")
39+
msg.InfoOnErr(errors.New("s.th. went wrong"), "just in case")
40+
})
41+
if !strings.Contains(out, "just in case") {
42+
t.Fatalf("missing info line in output: %q", out)
43+
}
44+
if !strings.Contains(out, "s.th. went wrong") {
45+
t.Fatalf("missing error line in output: %q", out)
46+
}
3247
}
3348

34-
func ExampleInfoOnErr() {
35-
var e error
36-
msg.InfoOnErr(e, "just in case")
37-
e = errors.New("s.th. went wrong")
38-
msg.InfoOnErr(e, "just in case")
39-
// Output:
40-
// just in case
41-
// Error in msg_blackbox_test.go:39: func 'github.com/rokath/trice/pkg/msg_test.ExampleInfoOnErr' -> s.th. went wrong
49+
func TestOnTrueAndOnFalseOutput(t *testing.T) {
50+
outTrue := tst.CaptureStdOut(func() {
51+
msg.OnTrue(false)
52+
msg.OnTrue(true)
53+
})
54+
if !strings.Contains(outTrue, "<nil>") {
55+
t.Fatalf("missing OnTrue marker in output: %q", outTrue)
56+
}
57+
58+
outFalse := tst.CaptureStdOut(func() {
59+
msg.OnFalse(true)
60+
msg.OnFalse(false)
61+
})
62+
if !strings.Contains(outFalse, "<nil>") {
63+
t.Fatalf("missing OnFalse marker in output: %q", outFalse)
64+
}
4265
}
4366

44-
func ExampleFatalInfoOnErr() {
45-
var e error
46-
msg.FatalInfoOnErr(e, "just in case")
47-
// Output:
67+
func TestInfoOnTrueAndInfoOnFalseOutput(t *testing.T) {
68+
outTrue := tst.CaptureStdOut(func() {
69+
msg.InfoOnTrue(false, "just in case")
70+
msg.InfoOnTrue(true, "just in case")
71+
})
72+
if !strings.Contains(outTrue, "just in case") {
73+
t.Fatalf("missing InfoOnTrue text in output: %q", outTrue)
74+
}
75+
76+
outFalse := tst.CaptureStdOut(func() {
77+
msg.InfoOnFalse(true, "just in case")
78+
msg.InfoOnFalse(false, "just in case")
79+
})
80+
if !strings.Contains(outFalse, "just in case") {
81+
t.Fatalf("missing InfoOnFalse text in output: %q", outFalse)
82+
}
4883
}
4984

50-
func ExampleOnTrue() {
51-
msg.OnTrue(false)
52-
msg.OnTrue(true)
53-
// Output:
54-
// Error in msg_blackbox_test.go:53: func 'github.com/rokath/trice/pkg/msg_test.ExampleOnTrue' -> <nil>
55-
}
56-
57-
func ExampleFatalOnTrue() {
58-
msg.FatalOnTrue(false)
59-
// Output:
60-
}
61-
62-
func ExampleInfoOnTrue() {
63-
msg.InfoOnTrue(false, "just in case")
64-
msg.InfoOnTrue(true, "just in case")
65-
// Output:
66-
// Error in msg_blackbox_test.go:65: func 'github.com/rokath/trice/pkg/msg_test.ExampleInfoOnTrue' -> just in case
67-
}
68-
69-
func ExampleFatalInfoOnTrue() {
70-
msg.FatalInfoOnTrue(false, "just in case")
71-
// Output:
72-
}
73-
74-
func ExampleOnFalse() {
75-
msg.OnFalse(true)
76-
msg.OnFalse(false)
77-
// Output:
78-
// Error in msg_blackbox_test.go:77: func 'github.com/rokath/trice/pkg/msg_test.ExampleOnFalse' -> <nil>
79-
}
80-
81-
func ExampleFatalOnFalse() {
82-
msg.FatalOnFalse(true)
83-
// Output:
84-
}
85-
86-
func ExampleInfoOnFalse() {
87-
msg.InfoOnFalse(true, "just in case")
88-
msg.InfoOnFalse(false, "just in case")
89-
// Output:
90-
// Error in msg_blackbox_test.go:89: func 'github.com/rokath/trice/pkg/msg_test.ExampleInfoOnFalse' -> just in case
91-
}
92-
93-
func ExampleFatalInfoOnFalse() {
94-
msg.FatalInfoOnFalse(true, "just in case")
95-
// Output:
85+
func TestFatalFunctionsNoopPaths(t *testing.T) {
86+
out := tst.CaptureStdOut(func() {
87+
msg.FatalOnErr(nil)
88+
msg.FatalInfoOnErr(nil, "just in case")
89+
msg.FatalOnTrue(false)
90+
msg.FatalInfoOnTrue(false, "just in case")
91+
msg.FatalOnFalse(true)
92+
msg.FatalInfoOnFalse(true, "just in case")
93+
})
94+
if out != "" {
95+
t.Fatalf("expected no output for noop fatal paths, got: %q", out)
96+
}
9697
}

0 commit comments

Comments
 (0)