cleanup(netx): another batch of small/simple cleanups (#789)

See https://github.com/ooni/probe/issues/2121
This commit is contained in:
Simone Basso
2022-06-02 13:50:34 +02:00
committed by GitHub
parent 1cb820b19d
commit ae24ba644c
7 changed files with 42 additions and 149 deletions
+9
View File
@@ -69,3 +69,12 @@ func ErrorToStringOrOK(err error) string {
}
return "ok"
}
// ValidLoggerOrDefault is a factory that either returns the logger
// provided as argument, if not nil, or DiscardLogger.
func ValidLoggerOrDefault(logger Logger) Logger {
if logger != nil {
return logger
}
return DiscardLogger
}
+16
View File
@@ -31,3 +31,19 @@ func TestErrorToStringOrOK(t *testing.T) {
}
})
}
func TestValidLoggerOrDefault(t *testing.T) {
t.Run("with nil argument", func(t *testing.T) {
out := ValidLoggerOrDefault(nil)
if out != DiscardLogger {
t.Fatal("unexpected result")
}
})
t.Run("with non-nil argument", func(t *testing.T) {
in := &logDiscarder{}
if ValidLoggerOrDefault(in) != in {
t.Fatal("unexpected result")
}
})
}