Chasing high coverage with os level packages

68 views
Skip to first unread message

eko...@gmail.com

unread,
May 22, 2018, 6:46:28 AM5/22/18
to golang-nuts
Let's say I want to unit test code that is using eventlog.Open: https://godoc.org/golang.org/x/sys/windows/svc/eventlog#Open

First I define an interface that matches eventlog.Log:
type WindowsEventLogger interface {
    Info(eid uint32, msg string) error
    Error(eid uint32, msg string) error
}

The problem is that eventlog.Open does not convert directly to:
func(source string) (WindowsEventLogger, error)

I have to create a wrapper function that is replaced in the test with a testing implementation of WindowsEventLogger:
func(source string) (WindowsEventLogger, error) { return eventlog.Open(source) }

However this means there's an extra line of code that will not be called from the test and makes the coverage artificially lower. Is there any way to avoid the lower coverage score?

eko...@gmail.com

unread,
May 22, 2018, 8:53:23 AM5/22/18
to golang-nuts
I've found a solution, but it's not as nice as interfaces:

Injectable function type:
func(l *eventlog.Log, eid uint32, msg string) error

Struct methods convert directly to this type without wrappers: (*eventlog.Log).Info and (*eventlog.Log).Error
Reply all
Reply to author
Forward
0 new messages