-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable controlled logging for tests using
testfmt
package. (#1754)
- Loading branch information
Showing
8 changed files
with
147 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package testfmt | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
// TestingLogger is a subset of *testing.T that is used for logging. | ||
type TestingLogger interface { | ||
Log(a ...any) | ||
Logf(format string, a ...any) | ||
Helper() | ||
} | ||
|
||
// Print logs a message to the testing logger if RIVER_TEST_PRINT is set. | ||
func Print(t TestingLogger, a ...any) { | ||
if enabled { | ||
t.Helper() | ||
t.Log(a...) | ||
} | ||
} | ||
|
||
// Printf logs a formatted message to the testing logger if RIVER_TEST_PRINT is set. | ||
func Printf(t TestingLogger, format string, a ...any) { | ||
if enabled { | ||
t.Helper() | ||
t.Logf(format, a...) | ||
} | ||
} | ||
|
||
// Println logs a message to the testing logger if RIVER_TEST_PRINT is set. | ||
func Println(t TestingLogger, a ...any) { | ||
if enabled { | ||
t.Helper() | ||
t.Log(a...) | ||
} | ||
} | ||
|
||
// Log logs a message to the testing logger if RIVER_TEST_PRINT is set. | ||
func Log(t TestingLogger, a ...any) { | ||
if enabled { | ||
t.Helper() | ||
t.Log(a...) | ||
} | ||
} | ||
|
||
// Logf logs a formatted message to the testing logger if RIVER_TEST_PRINT is set. | ||
func Logf(t TestingLogger, format string, a ...any) { | ||
if enabled { | ||
t.Helper() | ||
t.Logf(format, a...) | ||
} | ||
} | ||
|
||
type TestFmt struct { | ||
t TestingLogger | ||
} | ||
|
||
// New returns a new TestFmt that logs to the given testing logger if RIVER_TEST_PRINT is set. | ||
func New(t TestingLogger) TestFmt { | ||
return TestFmt{t} | ||
} | ||
|
||
func (f TestFmt) Print(a ...any) { | ||
Print(f.t, a...) | ||
} | ||
|
||
func (f TestFmt) Printf(format string, a ...any) { | ||
Printf(f.t, format, a...) | ||
} | ||
|
||
func (f TestFmt) Println(a ...any) { | ||
Println(f.t, a...) | ||
} | ||
|
||
func (f TestFmt) Log(a ...any) { | ||
Log(f.t, a...) | ||
} | ||
|
||
func (f TestFmt) Logf(format string, a ...any) { | ||
Logf(f.t, format, a...) | ||
} | ||
|
||
func Enabled() bool { | ||
return enabled | ||
} | ||
|
||
func Enable(v bool) { | ||
enabled = v | ||
} | ||
|
||
var enabled bool | ||
|
||
func init() { | ||
enabled = os.Getenv("RIVER_TEST_PRINT") != "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.