New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance defaultHWAddrFunc() and tests to hit 100% coverage #57
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,8 +24,10 @@ package uuid | |||||
import ( | ||||||
"bytes" | ||||||
"crypto/rand" | ||||||
"errors" | ||||||
"fmt" | ||||||
"net" | ||||||
"strings" | ||||||
"testing" | ||||||
"time" | ||||||
) | ||||||
|
@@ -376,6 +378,103 @@ func testNewV5DifferentNamespaces(t *testing.T) { | |||||
} | ||||||
} | ||||||
|
||||||
func Test_defaultHWAddrFunc(t *testing.T) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like this underscore very much, because no other top level tests have one. Indeed, I don't feel strongly about this at all, so we can leave it as-is if you'd like. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with @acln0 , Let's have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
tests := []struct { | ||||||
n string | ||||||
fn func() ([]net.Interface, error) | ||||||
hw net.HardwareAddr | ||||||
e string | ||||||
}{ | ||||||
{ | ||||||
n: "Error", | ||||||
fn: func() ([]net.Interface, error) { | ||||||
return nil, errors.New("controlled failure") | ||||||
}, | ||||||
e: "controlled failure", | ||||||
}, | ||||||
{ | ||||||
n: "NoValidHWAddrReturned", | ||||||
fn: func() ([]net.Interface, error) { | ||||||
s := []net.Interface{ | ||||||
{ | ||||||
Index: 1, | ||||||
MTU: 1500, | ||||||
Name: "test0", | ||||||
HardwareAddr: net.HardwareAddr{1, 2, 3, 4}, | ||||||
}, | ||||||
{ | ||||||
Index: 2, | ||||||
MTU: 1500, | ||||||
Name: "lo0", | ||||||
HardwareAddr: net.HardwareAddr{5, 6, 7, 8}, | ||||||
}, | ||||||
} | ||||||
|
||||||
return s, nil | ||||||
}, | ||||||
e: "uuid: no HW address found", | ||||||
}, | ||||||
{ | ||||||
n: "ValidHWAddrReturned", | ||||||
fn: func() ([]net.Interface, error) { | ||||||
s := []net.Interface{ | ||||||
{ | ||||||
Index: 1, | ||||||
MTU: 1500, | ||||||
Name: "test0", | ||||||
HardwareAddr: net.HardwareAddr{1, 2, 3, 4}, | ||||||
}, | ||||||
{ | ||||||
Index: 2, | ||||||
MTU: 1500, | ||||||
Name: "lo0", | ||||||
HardwareAddr: net.HardwareAddr{5, 6, 7, 8, 9, 0}, | ||||||
}, | ||||||
} | ||||||
|
||||||
return s, nil | ||||||
}, | ||||||
hw: net.HardwareAddr{5, 6, 7, 8, 9, 0}, | ||||||
}, | ||||||
} | ||||||
|
||||||
for _, tt := range tests { | ||||||
t.Run(tt.n, func(t *testing.T) { | ||||||
// set the netInterfaces variable (function) for the test | ||||||
// and then set it back to default in the deferred function | ||||||
netInterfaces = tt.fn | ||||||
defer func() { | ||||||
netInterfaces = net.Interfaces | ||||||
}() | ||||||
|
||||||
var hw net.HardwareAddr | ||||||
acln0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
var err error | ||||||
|
||||||
hw, err = defaultHWAddrFunc() | ||||||
|
||||||
if len(tt.e) > 0 { | ||||||
if err == nil { | ||||||
t.Fatalf("defaultHWAddrFunc() error = <nil>, should contain %q", tt.e) | ||||||
} | ||||||
|
||||||
if !strings.Contains(err.Error(), tt.e) { | ||||||
t.Fatalf("defaultHWAddrFunc() error = %q, should contain %q", err.Error(), tt.e) | ||||||
} | ||||||
|
||||||
return | ||||||
} | ||||||
|
||||||
if err != nil && tt.e == "" { | ||||||
t.Fatalf("defaultHWAddrFunc() error = %q, want <nil>", err.Error()) | ||||||
} | ||||||
|
||||||
if !bytes.Equal(hw, tt.hw) { | ||||||
t.Fatalf("hw = %#v, want %#v", hw, tt.hw) | ||||||
} | ||||||
}) | ||||||
} | ||||||
} | ||||||
|
||||||
func BenchmarkGenerator(b *testing.B) { | ||||||
b.Run("NewV1", func(b *testing.B) { | ||||||
for i := 0; i < b.N; i++ { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the call site for
netInterfaces
, we useifaces, err := netInterfaces()
and rely on type inference. I would prefer adding an explicit type here, in the style of these hooks: https://golang.org/pkg/internal/poll/#pkg-variables, to better communicate the signature of what is being hooked, so the reader doesn't have to go through yet another indirection.Perhaps
var netInterfaces func() ([]net.Interface, error) = net.Interfaces
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like how it clutters up the code. It would be easier for my editor to give me the function header definition over trying to read that line.
I was trying to hit 100% without making the code too messy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to push back a little here. I like having the type inline, be it a little bit more cluttered as it may. Editors might help alleviate the extra indirection via highlighting, but I'd like to avoid it altogether.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally getting back around to working on this PR, and this change results in a
golint
failure.As a result I'm just going to omit it at this time.