forked from sirupsen/logrus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move test functions and test utils functions in their own package
- Loading branch information
Showing
3 changed files
with
79 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package testutils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
. "github.com/sirupsen/logrus" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func LogAndAssertJSON(t *testing.T, log func(*Logger), assertions func(fields Fields)) { | ||
var buffer bytes.Buffer | ||
var fields Fields | ||
|
||
logger := New() | ||
logger.Out = &buffer | ||
logger.Formatter = new(JSONFormatter) | ||
|
||
log(logger) | ||
|
||
err := json.Unmarshal(buffer.Bytes(), &fields) | ||
require.Nil(t, err) | ||
|
||
assertions(fields) | ||
} | ||
|
||
func LogAndAssertText(t *testing.T, log func(*Logger), assertions func(fields map[string]string)) { | ||
var buffer bytes.Buffer | ||
|
||
logger := New() | ||
logger.Out = &buffer | ||
logger.Formatter = &TextFormatter{ | ||
DisableColors: true, | ||
} | ||
|
||
log(logger) | ||
|
||
fields := make(map[string]string) | ||
for _, kv := range strings.Split(buffer.String(), " ") { | ||
if !strings.Contains(kv, "=") { | ||
continue | ||
} | ||
kvArr := strings.Split(kv, "=") | ||
key := strings.TrimSpace(kvArr[0]) | ||
val := kvArr[1] | ||
if kvArr[1][0] == '"' { | ||
var err error | ||
val, err = strconv.Unquote(val) | ||
require.NoError(t, err) | ||
} | ||
fields[key] = val | ||
} | ||
assertions(fields) | ||
} |
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