Skip to content

Instantly share code, notes, and snippets.

@ivankatliarchuk
Created December 29, 2024 12:38
Show Gist options
  • Save ivankatliarchuk/f29437ffd84032fed41a5b5da92052e2 to your computer and use it in GitHub Desktop.
Save ivankatliarchuk/f29437ffd84032fed41a5b5da92052e2 to your computer and use it in GitHub Desktop.
log-rus-how-to-test-log-Fatalf
package main
import (
"bytes"
"testing"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestEncryptionFailed(t *testing.T) {
defer func() { log.StandardLogger().ExitFunc = nil }()
b := new(bytes.Buffer)
var fatalCrash bool
log.StandardLogger().ExitFunc = func(int) { fatalCrash = true }
log.StandardLogger().SetOutput(b)
// contains log.log.Fatalf("Failed to encrypt the text %#v ", err)
_ = foo.Serialize(false, true, []byte("wrong-key"))
suite.True(fatalCrash, "should fail if encryption key is wrong")
suite.Contains(b.String(), "Failed to encrypt the text")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment