Skip to content

Commit ba196cc

Browse files
committed
test: centralize git identity and gate colors on TTY
Drives the test identity via GIT_AUTHOR_* / GIT_COMMITTER_* env vars in a single TestMain per package and drops the per-test 'git config user.email/name' boilerplate (kept only where a test exercises the user.name fallback). Replaces the env-only colorEnabled() check with one that also inspects the writer with golang.org/x/term.IsTerminal, so output piped to a buffer or captured by CI no longer carries ANSI codes.
1 parent 92676e8 commit ba196cc

6 files changed

Lines changed: 45 additions & 28 deletions

File tree

cmd/diffs/main_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"net"
7+
"os"
78
"os/exec"
89
"path/filepath"
910
"strings"
@@ -15,6 +16,18 @@ import (
1516
"github.com/imfing/diffs-cli/internal/server"
1617
)
1718

19+
func TestMain(m *testing.M) {
20+
for k, v := range map[string]string{
21+
"GIT_AUTHOR_NAME": "Test",
22+
"GIT_AUTHOR_EMAIL": "test@example.com",
23+
"GIT_COMMITTER_NAME": "Test",
24+
"GIT_COMMITTER_EMAIL": "test@example.com",
25+
} {
26+
_ = os.Setenv(k, v)
27+
}
28+
os.Exit(m.Run())
29+
}
30+
1831
func TestTargetPathFromArgs(t *testing.T) {
1932
tests := []struct {
2033
name string
@@ -62,7 +75,6 @@ func TestRootCommandRejectsDirectPRTarget(t *testing.T) {
6275
}
6376

6477
func TestLocalCommandRejectsNonGitRepository(t *testing.T) {
65-
t.Setenv("NO_COLOR", "1")
6678
dir := t.TempDir()
6779
var errOut bytes.Buffer
6880
cmd := newRootCommand(time.Time{})

cmd/diffs/output.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/imfing/diffs-cli/internal/server"
11+
"golang.org/x/term"
1112
)
1213

1314
const reloadDebounce = 500 * time.Millisecond
@@ -43,8 +44,15 @@ func (e quietError) Unwrap() error {
4344
return e.err
4445
}
4546

46-
func colorEnabled() bool {
47-
return os.Getenv("NO_COLOR") == "" && os.Getenv("TERM") != "dumb"
47+
func colorEnabled(w io.Writer) bool {
48+
if os.Getenv("NO_COLOR") != "" || os.Getenv("TERM") == "dumb" {
49+
return false
50+
}
51+
f, ok := w.(*os.File)
52+
if !ok {
53+
return false
54+
}
55+
return term.IsTerminal(int(f.Fd()))
4856
}
4957

5058
func colors(enabled bool) terminalColors {

cmd/diffs/serve.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func runServerTarget(cmd *cobra.Command, opts *cliOptions, targetPath string, st
2525
if targetPath == "/local" {
2626
root, err := gitRoot(displayCWD)
2727
if err != nil {
28-
printLocalGitHelp(errOut, displayCWD, colorEnabled())
28+
printLocalGitHelp(errOut, displayCWD, colorEnabled(errOut))
2929
_ = cmd.Help()
3030
return quietError{err: err}
3131
}
@@ -44,7 +44,7 @@ func runServerTarget(cmd *cobra.Command, opts *cliOptions, targetPath string, st
4444
Watch: targetPath == "/local",
4545
}
4646
if targetPath == "/local" {
47-
reload := newReloadLogger(out, colorEnabled())
47+
reload := newReloadLogger(out, colorEnabled(out))
4848
cfg.OnChange = func(files []server.ChangedFile) {
4949
reload(time.Now(), files)
5050
}
@@ -68,15 +68,15 @@ func runServerTarget(cmd *cobra.Command, opts *cliOptions, targetPath string, st
6868
}
6969
url := browserURL(ln.Addr(), targetPath)
7070
if fallback != nil {
71-
printPortFallback(out, fallback.Requested, fallback.Actual, colorEnabled())
71+
printPortFallback(out, fallback.Requested, fallback.Actual, colorEnabled(out))
7272
}
7373
printStartup(out, startupInfo{
7474
URL: url,
7575
Target: targetLabel(targetPath, displayCWD),
7676
CWD: displayCWD,
7777
Watching: targetPath == "/local",
7878
Elapsed: time.Since(started),
79-
}, colorEnabled())
79+
}, colorEnabled(out))
8080

8181
if !opts.noOpen {
8282
if err := openBrowser(url); err != nil {

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ require (
66
github.com/fsnotify/fsnotify v1.10.1
77
github.com/pelletier/go-toml/v2 v2.3.1
88
github.com/spf13/cobra v1.10.2
9+
golang.org/x/term v0.43.0
910
)
1011

1112
require (
1213
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1314
github.com/spf13/pflag v1.0.9 // indirect
14-
golang.org/x/sys v0.13.0 // indirect
15+
golang.org/x/sys v0.44.0 // indirect
1516
)

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiT
1111
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
1212
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1313
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
14-
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
15-
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
14+
golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
15+
golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
16+
golang.org/x/term v0.43.0 h1:S4RLU2sB31O/NCl+zFN9Aru9A/Cq2aqKpTZJ6B+DwT4=
17+
golang.org/x/term v0.43.0/go.mod h1:lrhlHNdQJHO+1qVYiHfFKVuVioJIheAc3fBSMFYEIsk=
1618
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

internal/server/server_test.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ import (
1919
"github.com/imfing/diffs-cli/internal/appconfig"
2020
)
2121

22+
func TestMain(m *testing.M) {
23+
for k, v := range map[string]string{
24+
"GIT_AUTHOR_NAME": "Test",
25+
"GIT_AUTHOR_EMAIL": "test@example.com",
26+
"GIT_COMMITTER_NAME": "Test",
27+
"GIT_COMMITTER_EMAIL": "test@example.com",
28+
} {
29+
_ = os.Setenv(k, v)
30+
}
31+
os.Exit(m.Run())
32+
}
33+
2234
func TestConfigIncludesCurrentBranch(t *testing.T) {
2335
dir := t.TempDir()
2436
git(t, dir, "init")
@@ -131,8 +143,6 @@ func TestGitDiffNoIndexUsesDevNullHeader(t *testing.T) {
131143
func TestLocalDiffIncludesStagedAndUnstagedTrackedChanges(t *testing.T) {
132144
dir := t.TempDir()
133145
git(t, dir, "init")
134-
git(t, dir, "config", "user.email", "test@example.com")
135-
git(t, dir, "config", "user.name", "Test")
136146
writeFile(t, filepath.Join(dir, "tracked.txt"), "one\n")
137147
git(t, dir, "add", "tracked.txt")
138148
git(t, dir, "commit", "-m", "init")
@@ -154,8 +164,6 @@ func TestLocalDiffIncludesStagedAndUnstagedTrackedChanges(t *testing.T) {
154164
func TestEventsStreamOnLocalFileChange(t *testing.T) {
155165
dir := t.TempDir()
156166
git(t, dir, "init")
157-
git(t, dir, "config", "user.email", "test@example.com")
158-
git(t, dir, "config", "user.name", "Test")
159167
writeFile(t, filepath.Join(dir, "tracked.txt"), "one\n")
160168
git(t, dir, "add", "tracked.txt")
161169
git(t, dir, "commit", "-m", "init")
@@ -203,8 +211,6 @@ func TestEventsStreamOnLocalFileChange(t *testing.T) {
203211
func TestOnChangeRunsOnLocalFileChange(t *testing.T) {
204212
dir := t.TempDir()
205213
git(t, dir, "init")
206-
git(t, dir, "config", "user.email", "test@example.com")
207-
git(t, dir, "config", "user.name", "Test")
208214
writeFile(t, filepath.Join(dir, "tracked.txt"), "one\n")
209215
git(t, dir, "add", "tracked.txt")
210216
git(t, dir, "commit", "-m", "init")
@@ -242,8 +248,6 @@ func TestOnChangeRunsOnLocalFileChange(t *testing.T) {
242248
func TestOnChangeIgnoresGitCleanBuildOutput(t *testing.T) {
243249
dir := t.TempDir()
244250
git(t, dir, "init")
245-
git(t, dir, "config", "user.email", "test@example.com")
246-
git(t, dir, "config", "user.name", "Test")
247251
writeFile(t, filepath.Join(dir, ".gitignore"), "web/dist/\n")
248252
git(t, dir, "add", ".gitignore")
249253
git(t, dir, "commit", "-m", "init")
@@ -281,8 +285,6 @@ func TestOnChangeIgnoresGitCleanBuildOutput(t *testing.T) {
281285
func TestEventsStreamIgnoresGitCleanBuildOutput(t *testing.T) {
282286
dir := t.TempDir()
283287
git(t, dir, "init")
284-
git(t, dir, "config", "user.email", "test@example.com")
285-
git(t, dir, "config", "user.name", "Test")
286288
writeFile(t, filepath.Join(dir, ".gitignore"), "web/dist/\n")
287289
git(t, dir, "add", ".gitignore")
288290
git(t, dir, "commit", "-m", "init")
@@ -330,8 +332,6 @@ func TestEventsStreamIgnoresGitCleanBuildOutput(t *testing.T) {
330332
func TestWatcherDisabledDoesNotObserveLocalChanges(t *testing.T) {
331333
dir := t.TempDir()
332334
git(t, dir, "init")
333-
git(t, dir, "config", "user.email", "test@example.com")
334-
git(t, dir, "config", "user.name", "Test")
335335
writeFile(t, filepath.Join(dir, "tracked.txt"), "one\n")
336336
git(t, dir, "add", "tracked.txt")
337337
git(t, dir, "commit", "-m", "init")
@@ -361,8 +361,6 @@ func TestWatcherDisabledDoesNotObserveLocalChanges(t *testing.T) {
361361
func TestGitStatusReturnsChangedPaths(t *testing.T) {
362362
dir := t.TempDir()
363363
git(t, dir, "init")
364-
git(t, dir, "config", "user.email", "test@example.com")
365-
git(t, dir, "config", "user.name", "Test")
366364
writeFile(t, filepath.Join(dir, ".gitignore"), "web/dist/\n")
367365
writeFile(t, filepath.Join(dir, "tracked.txt"), "one\n")
368366
git(t, dir, "add", ".gitignore", "tracked.txt")
@@ -396,8 +394,6 @@ func TestGitStatusReturnsChangedPaths(t *testing.T) {
396394
func TestGitStatusActions(t *testing.T) {
397395
dir := t.TempDir()
398396
git(t, dir, "init")
399-
git(t, dir, "config", "user.email", "test@example.com")
400-
git(t, dir, "config", "user.name", "Test")
401397
writeFile(t, filepath.Join(dir, "tracked.txt"), "one\n")
402398
git(t, dir, "add", "tracked.txt")
403399
git(t, dir, "commit", "-m", "init")
@@ -429,8 +425,6 @@ func TestGitStatusActions(t *testing.T) {
429425
func TestGitStatusUsesNewPathForRenames(t *testing.T) {
430426
dir := t.TempDir()
431427
git(t, dir, "init")
432-
git(t, dir, "config", "user.email", "test@example.com")
433-
git(t, dir, "config", "user.name", "Test")
434428
writeFile(t, filepath.Join(dir, "old.txt"), "one\n")
435429
git(t, dir, "add", "old.txt")
436430
git(t, dir, "commit", "-m", "init")

0 commit comments

Comments
 (0)