From 585d9b180fc09dfa90f313061cb4cf79c131cebc Mon Sep 17 00:00:00 2001 From: Juan Leni Date: Mon, 2 Sep 2024 17:09:01 +0200 Subject: [PATCH] improve mocks --- .gitignore | 2 ++ .vscode/settings.json | 18 ++++++++++++++++++ Makefile | 2 +- ledger_hid.go | 2 +- ledger_mock.go | 16 +++++++++++----- ledger_test.go | 18 +++++++++++++++++- 6 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index ee4c829..bae6e66 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,5 @@ cmake-build-debug/ \.idea/workspace\.xml \.idea/ vendor/ + +coverage.txt diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..99c33b4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,18 @@ +{ + "go.testFlags": [ + "-tags=ledger_mock", + "-v", + "-race", + "-coverprofile=coverage.txt", + "-covermode=atomic" + ], + "go.coverOnSave": true, + "go.coverageOptions": "showUncoveredCodeOnly", + "go.coverageDecorator": { + "type": "gutter", + "coveredHighlightColor": "rgba(64,128,128,0.5)", + "uncoveredHighlightColor": "rgba(128,64,64,0.25)", + "coveredGutterStyle": "blockblue", + "uncoveredGutterStyle": "slashyellow" + } +} \ No newline at end of file diff --git a/Makefile b/Makefile index a7fa35c..6f35bd5 100644 --- a/Makefile +++ b/Makefile @@ -49,4 +49,4 @@ lint: golangci-lint run test: - go test -tags ledger_mock -v -race ./... + go test -tags ledger_mock -v -race ./... -coverprofile=coverage.txt -covermode=atomic diff --git a/ledger_hid.go b/ledger_hid.go index 7718479..6f8a1a6 100644 --- a/ledger_hid.go +++ b/ledger_hid.go @@ -53,7 +53,7 @@ var supportedLedgerProductID = map[uint16]int{ 0x5: 0, // Ledger Nano S Plus } -func NewLedgerAdmin() *LedgerAdminHID { +func NewLedgerAdmin() LedgerAdmin { return &LedgerAdminHID{} } diff --git a/ledger_mock.go b/ledger_mock.go index af1857f..e9057fc 100644 --- a/ledger_mock.go +++ b/ledger_mock.go @@ -32,7 +32,7 @@ type LedgerDeviceMock struct { commands map[string]string } -func NewLedgerAdmin() *LedgerAdminMock { +func NewLedgerAdmin() LedgerAdmin { return &LedgerAdminMock{} } @@ -44,15 +44,13 @@ func (admin *LedgerAdminMock) CountDevices() int { return 1 } -func (admin *LedgerAdminMock) Connect(deviceIndex int) (*LedgerDeviceMock, error) { +func (admin *LedgerAdminMock) Connect(deviceIndex int) (LedgerDevice, error) { return NewLedgerDeviceMock(), nil } func NewLedgerDeviceMock() *LedgerDeviceMock { return &LedgerDeviceMock{ - commands: map[string]string{ - "e001000000": "311000040853706563756c6f73000b53706563756c6f734d4355", - }, + commands: make(map[string]string), } } @@ -64,6 +62,14 @@ func (ledger *LedgerDeviceMock) Exchange(command []byte) ([]byte, error) { return nil, fmt.Errorf("unknown command: %s", hexCommand) } +func (ledger *LedgerDeviceMock) SetCommandReplies(commands map[string]string) { + ledger.commands = commands +} + +func (ledger *LedgerDeviceMock) ClearCommands() { + ledger.commands = make(map[string]string) +} + func (ledger *LedgerDeviceMock) Close() error { // Nothing to do here return nil diff --git a/ledger_test.go b/ledger_test.go index 6f99623..82060df 100644 --- a/ledger_test.go +++ b/ledger_test.go @@ -1,3 +1,6 @@ +//go:build ledger_mock +// +build ledger_mock + /******************************************************************************* * (c) Zondax AG * @@ -96,6 +99,13 @@ func Test_BasicExchange(t *testing.T) { } defer ledger.Close() + // Set expected replies for the commands (only if using mock) + if mockLedger, ok := ledger.(*LedgerDeviceMock); ok { + mockLedger.SetCommandReplies(map[string]string{ + "e001000000": "311000040853706563756c6f73000b53706563756c6f734d4355", + }) + } + // Call device info (this should work in main menu and many apps) message := []byte{0xE0, 0x01, 0, 0, 0} @@ -132,11 +142,17 @@ func TestGetVersion(t *testing.T) { } defer ledger.Close() + // Set expected replies for the commands (only if using mock) + if mockLedger, ok := ledger.(*LedgerDeviceMock); ok { + mockLedger.SetCommandReplies(map[string]string{ + "e001000000": "311000040853706563756c6f73000b53706563756c6f734d4355", + }) + } + // Call device info (this should work in main menu and many apps) message := []byte{0xE0, 0x01, 0, 0, 0} response, err := ledger.Exchange(message) assert.NoError(t, err) assert.NotEmpty(t, response, "Response should not be empty") - }