File tree Expand file tree Collapse file tree 2 files changed +45
-8
lines changed
Expand file tree Collapse file tree 2 files changed +45
-8
lines changed Original file line number Diff line number Diff line change @@ -22,11 +22,12 @@ var (
2222 NoColor = noColorIsSet () || os .Getenv ("TERM" ) == "dumb" || ! stdoutIsTerminal ()
2323
2424 // Output defines the standard output of the print functions. By default,
25- // os.Stdout is used.
26- Output = initOutput ()
25+ // stdOut() is used.
26+ Output = stdOut ()
2727
28- // Error defines a color supporting writer for os.Stderr.
29- Error = initError ()
28+ // Error defines the standard error of the print functions. By default,
29+ // stdErr() is used.
30+ Error = stdErr ()
3031
3132 // colorsCache is used to reduce the count of created Color objects and
3233 // allows to reuse already created objects with required Attribute.
@@ -48,18 +49,18 @@ func stdoutIsTerminal() bool {
4849 return isatty .IsTerminal (os .Stdout .Fd ()) || isatty .IsCygwinTerminal (os .Stdout .Fd ())
4950}
5051
51- // initOutput returns a writer for color output.
52+ // stdOut returns a writer for color output.
5253// Returns io.Discard if os.Stdout is nil (e.g., when running as a Windows service).
53- func initOutput () io.Writer {
54+ func stdOut () io.Writer {
5455 if os .Stdout == nil {
5556 return io .Discard
5657 }
5758 return colorable .NewColorableStdout ()
5859}
5960
60- // initError returns a writer for color error output.
61+ // stdErr returns a writer for color error output.
6162// Returns io.Discard if os.Stderr is nil (e.g., when running as a Windows service).
62- func initError () io.Writer {
63+ func stdErr () io.Writer {
6364 if os .Stderr == nil {
6465 return io .Discard
6566 }
Original file line number Diff line number Diff line change @@ -236,6 +236,42 @@ func Test_noColorIsSet(t *testing.T) {
236236 }
237237}
238238
239+ func TestStdoutIsTerminal_NilStdout (t * testing.T ) {
240+ stdout := os .Stdout
241+ os .Stdout = nil
242+ t .Cleanup (func () {
243+ os .Stdout = stdout
244+ })
245+
246+ if stdoutIsTerminal () {
247+ t .Fatal ("stdoutIsTerminal() = true, want false" )
248+ }
249+ }
250+
251+ func TestStdOut_NilStdout (t * testing.T ) {
252+ stdout := os .Stdout
253+ os .Stdout = nil
254+ t .Cleanup (func () {
255+ os .Stdout = stdout
256+ })
257+
258+ if got := stdOut (); got != io .Discard {
259+ t .Fatalf ("stdOut() = %v, want %v" , got , io .Discard )
260+ }
261+ }
262+
263+ func TestStdErr_NilStderr (t * testing.T ) {
264+ stderr := os .Stderr
265+ os .Stderr = nil
266+ t .Cleanup (func () {
267+ os .Stderr = stderr
268+ })
269+
270+ if got := stdErr (); got != io .Discard {
271+ t .Fatalf ("stdErr() = %v, want %v" , got , io .Discard )
272+ }
273+ }
274+
239275func TestColorVisual (t * testing.T ) {
240276 // First Visual Test
241277 Output = colorable .NewColorableStdout ()
You can’t perform that action at this time.
0 commit comments