Skip to content

Commit 0b31374

Browse files
committed
enhance tests
1 parent b6ea56f commit 0b31374

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

pkg/flasherapi/flasherapi_test.go

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,62 @@
1212
// modify or otherwise use the software for commercial activities involving the
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to license@arduino.cc.
15-
1615
package flasherapi
1716

1817
import (
18+
"context"
19+
"io"
1920
"strings"
2021
"testing"
2122

2223
"github.com/stretchr/testify/require"
24+
25+
"github.com/arduino/arduino-app-cli/pkg/board/remote"
2326
)
2427

28+
// implements remote.RemoteConn
29+
type MockRemoteConn struct {
30+
ReadFileFunc func(path string) (io.ReadCloser, error)
31+
}
32+
33+
func (m *MockRemoteConn) ReadFile(path string) (io.ReadCloser, error) {
34+
return m.ReadFileFunc(path)
35+
}
36+
37+
// Empty definitions
38+
func (m *MockRemoteConn) List(path string) ([]remote.FileInfo, error) {
39+
return nil, nil
40+
}
41+
func (m *MockRemoteConn) MkDirAll(path string) error {
42+
return nil
43+
}
44+
func (m *MockRemoteConn) Remove(path string) error {
45+
return nil
46+
}
47+
func (m *MockRemoteConn) Stats(path string) (remote.FileInfo, error) {
48+
return remote.FileInfo{}, nil
49+
}
50+
func (m *MockRemoteConn) WriteFile(data io.Reader, path string) error {
51+
return nil
52+
}
53+
func (m *MockRemoteConn) GetCmd(cmd string, args ...string) remote.Cmder {
54+
return nil
55+
}
56+
func (m *MockRemoteConn) Forward(ctx context.Context, localPort int, remotePort int) error {
57+
return nil
58+
}
59+
func (m *MockRemoteConn) ForwardKillAll(ctx context.Context) error {
60+
return nil
61+
}
62+
func createBuildInfoConnection(imageVersion string) remote.RemoteConn {
63+
mockConn := MockRemoteConn{
64+
ReadFileFunc: func(path string) (io.ReadCloser, error) {
65+
return io.NopCloser(strings.NewReader(imageVersion)), nil
66+
},
67+
}
68+
return &mockConn
69+
}
70+
2571
func TestParseOSImageVersion(t *testing.T) {
2672
tests := []struct {
2773
name string
@@ -30,10 +76,8 @@ func TestParseOSImageVersion(t *testing.T) {
3076
found bool
3177
}{
3278
{
33-
name: "valid build id",
34-
input: `BUILD_ID="20251006-395"
35-
VARIANT_ID=xfce"
36-
`,
79+
name: "valid build id",
80+
input: "BUILD_ID=\"20251006-395\"\nVARIANT_ID=xfce",
3781
expected: "20251006-395",
3882
found: true,
3983
},
@@ -48,12 +92,20 @@ func TestParseOSImageVersion(t *testing.T) {
4892
found: false,
4993
},
5094
}
51-
5295
for _, tt := range tests {
5396
t.Run(tt.name, func(t *testing.T) {
5497
got, ok := parseOSImageVersion(strings.NewReader(tt.input))
55-
require.Equal(t, tt.found, ok)
56-
require.Equal(t, tt.expected, got)
98+
if ok != tt.found || got != tt.expected {
99+
t.Fatalf("got (%q, %v), expected (%q, %v)", got, ok, tt.expected, tt.found)
100+
}
57101
})
58102
}
59103
}
104+
105+
func TestGetOSImageVersion(t *testing.T) {
106+
const R0_IMAGE_VERSION_ID = "20250807-136"
107+
R0Version := createBuildInfoConnection(R0_IMAGE_VERSION_ID)
108+
AnotherVersion := createBuildInfoConnection("BUILD_ID=20250101-001")
109+
require.Equal(t, GetOSImageVersion(R0Version), R0_IMAGE_VERSION_ID)
110+
require.Equal(t, GetOSImageVersion(AnotherVersion), "20250101-001")
111+
}

0 commit comments

Comments
 (0)