From 13b95a6144fdea14dd523028119e70653a38a237 Mon Sep 17 00:00:00 2001 From: Rootul Patel Date: Fri, 27 Oct 2023 15:55:13 -0400 Subject: [PATCH 1/3] test: ledger support by running against built binary --- test/ledger/ledger_test.go | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 test/ledger/ledger_test.go diff --git a/test/ledger/ledger_test.go b/test/ledger/ledger_test.go new file mode 100644 index 0000000000..3198d45b8a --- /dev/null +++ b/test/ledger/ledger_test.go @@ -0,0 +1,54 @@ +package ledger + +import ( + "bytes" + "os" + "os/exec" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestLedgerSupport(t *testing.T) { + t.Run("build with Ledger support", func(t *testing.T) { + // Generate the binary with Ledger support + cmd := exec.Command("go", "build", "-tags", "ledger", "-o", "celestia-appd", "../../cmd/celestia-appd") + err := cmd.Run() + require.NoError(t, err) + + // Clean up the binary + // defer os.Remove("celestia-appd") + + // Run the binary + cmd = exec.Command("./celestia-appd", "keys", "add", "test-key-name", "--ledger") + var out bytes.Buffer + cmd.Stderr = &out + err = cmd.Run() + require.Error(t, err) + + // Verify the output contains an error message + want := "Error: failed to generate ledger key: failed to retrieve device: ledger nano S: LedgerHID device (idx 0) not found. Ledger LOCKED OR Other Program/Web Browser may have control of device.\n" + assert.Equal(t, want, out.String()) + }) + t.Run("build without Ledger support", func(t *testing.T) { + // Generate the binary without Ledger support + cmd := exec.Command("go", "build", "-o", "celestia-appd", "../../cmd/celestia-appd") + err := cmd.Run() + require.NoError(t, err) + + // Clean up the binary + defer os.Remove("celestia-appd") + + // Run the binary + cmd = exec.Command("./celestia-appd", "keys", "add", "test-key-name", "--ledger") + var out bytes.Buffer + cmd.Stderr = &out + err = cmd.Run() + require.Error(t, err) + + // Verify the output contains an error message + want := "Error: failed to generate ledger key: failed to retrieve device: ledger nano S: support for ledger devices is not available in this executable\n" + assert.Equal(t, want, out.String()) + }) +} From 4090022f303ea2cf1bfff9de3a140874e20fde0f Mon Sep 17 00:00:00 2001 From: Rootul Patel Date: Fri, 27 Oct 2023 15:59:30 -0400 Subject: [PATCH 2/3] refactor: table driven test --- test/ledger/ledger_test.go | 84 ++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/test/ledger/ledger_test.go b/test/ledger/ledger_test.go index 3198d45b8a..478eda7548 100644 --- a/test/ledger/ledger_test.go +++ b/test/ledger/ledger_test.go @@ -11,44 +11,48 @@ import ( ) func TestLedgerSupport(t *testing.T) { - t.Run("build with Ledger support", func(t *testing.T) { - // Generate the binary with Ledger support - cmd := exec.Command("go", "build", "-tags", "ledger", "-o", "celestia-appd", "../../cmd/celestia-appd") - err := cmd.Run() - require.NoError(t, err) - - // Clean up the binary - // defer os.Remove("celestia-appd") - - // Run the binary - cmd = exec.Command("./celestia-appd", "keys", "add", "test-key-name", "--ledger") - var out bytes.Buffer - cmd.Stderr = &out - err = cmd.Run() - require.Error(t, err) - - // Verify the output contains an error message - want := "Error: failed to generate ledger key: failed to retrieve device: ledger nano S: LedgerHID device (idx 0) not found. Ledger LOCKED OR Other Program/Web Browser may have control of device.\n" - assert.Equal(t, want, out.String()) - }) - t.Run("build without Ledger support", func(t *testing.T) { - // Generate the binary without Ledger support - cmd := exec.Command("go", "build", "-o", "celestia-appd", "../../cmd/celestia-appd") - err := cmd.Run() - require.NoError(t, err) - - // Clean up the binary - defer os.Remove("celestia-appd") - - // Run the binary - cmd = exec.Command("./celestia-appd", "keys", "add", "test-key-name", "--ledger") - var out bytes.Buffer - cmd.Stderr = &out - err = cmd.Run() - require.Error(t, err) - - // Verify the output contains an error message - want := "Error: failed to generate ledger key: failed to retrieve device: ledger nano S: support for ledger devices is not available in this executable\n" - assert.Equal(t, want, out.String()) - }) + type testCase struct { + name string + ledger bool + want string + } + + testCases := []testCase{ + { + name: "ledger support enabled", + ledger: true, + want: "Error: failed to generate ledger key: failed to retrieve device: ledger nano S: LedgerHID device (idx 0) not found. Ledger LOCKED OR Other Program/Web Browser may have control of device.\n", + }, + { + name: "ledger support disabled", + ledger: false, + want: "Error: failed to generate ledger key: failed to retrieve device: ledger nano S: support for ledger devices is not available in this executable\n", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + var cmd *exec.Cmd + if tc.ledger { + // Generate the binary with Ledger support + cmd = exec.Command("go", "build", "-tags", "ledger", "-o", "celestia-appd", "../../cmd/celestia-appd") + } else { + // Generate the binary without Ledger support + cmd = exec.Command("go", "build", "-o", "celestia-appd", "../../cmd/celestia-appd") + } + err := cmd.Run() + require.NoError(t, err) + + // Clean up the binary + defer os.Remove("celestia-appd") + + // Run the binary + cmd = exec.Command("./celestia-appd", "keys", "add", "test-key-name", "--ledger") + var out bytes.Buffer + cmd.Stderr = &out + err = cmd.Run() + require.Error(t, err) + assert.Equal(t, tc.want, out.String()) + }) + } } From 71f51545cc606cb7338c5b26a3c72f71ad799e8b Mon Sep 17 00:00:00 2001 From: Rootul Patel Date: Sun, 29 Oct 2023 13:46:38 +0100 Subject: [PATCH 3/3] fix: skip ledger test in short mode --- test/ledger/ledger_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/ledger/ledger_test.go b/test/ledger/ledger_test.go index 478eda7548..eeda5314d9 100644 --- a/test/ledger/ledger_test.go +++ b/test/ledger/ledger_test.go @@ -11,6 +11,10 @@ import ( ) func TestLedgerSupport(t *testing.T) { + if testing.Short() { + t.Skip("skipping ledger support test in short mode.") + } + type testCase struct { name string ledger bool