Skip to content

Commit 557ff11

Browse files
mirkoCrobumirkoCrobu
authored andcommitted
add store test and task fmt
1 parent 9de80c8 commit 557ff11

File tree

2 files changed

+187
-1
lines changed

2 files changed

+187
-1
lines changed

.licensed.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ cache_path: .licenses
66
apps:
77
- source_path: ./cmd/arduino-app-cli
88

9-
109
reviewed:
1110
go:
1211
# TODO: remove it after releasing this https://github.com/arduino/go-win32-utils/pull/10

internal/store/store_test.go

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package store
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/arduino/go-paths-helper"
9+
"github.com/stretchr/testify/require"
10+
11+
"github.com/arduino/arduino-app-cli/internal/orchestrator/config"
12+
)
13+
14+
const validBrickID = "arduino:arduino_cloud"
15+
16+
func setupTestStore(t *testing.T) (*StaticStore, string) {
17+
cfg, err := config.NewFromEnv()
18+
require.NoError(t, err)
19+
baseDir := paths.New("../e2e/daemon/testdata", "assets", cfg.RunnerVersion).String()
20+
return NewStaticStore(baseDir), baseDir
21+
}
22+
23+
func TestGetBrickReadmeFromID(t *testing.T) {
24+
25+
store, baseDir := setupTestStore(t)
26+
27+
namespace, brickName, _ := parseBrickID(validBrickID)
28+
29+
expectedReadmePath := filepath.Join(baseDir, "docs", namespace, brickName, "README.md")
30+
expectedContent, err := os.ReadFile(expectedReadmePath)
31+
require.NoError(t, err, "Error Reading README file: %s", expectedReadmePath)
32+
require.NotEmpty(t, expectedContent, "ReadME file is empty: %s", expectedReadmePath)
33+
34+
testCases := []struct {
35+
name string
36+
brickID string
37+
wantContent string
38+
wantErr bool
39+
wantErrIs error
40+
wantErrMsg string
41+
}{
42+
{
43+
name: "Success - file found",
44+
brickID: validBrickID,
45+
wantContent: string(expectedContent),
46+
wantErr: false,
47+
},
48+
{
49+
name: "Failure - file not found",
50+
brickID: "namespace:non_existent_brick",
51+
wantContent: "",
52+
wantErr: true,
53+
wantErrIs: os.ErrNotExist,
54+
},
55+
{
56+
name: "Failure - invalid ID",
57+
brickID: "invalid-id",
58+
wantContent: "",
59+
wantErr: true,
60+
wantErrMsg: "invalid ID",
61+
},
62+
}
63+
64+
for _, tc := range testCases {
65+
t.Run(tc.name, func(t *testing.T) {
66+
67+
content, err := store.GetBrickReadmeFromID(tc.brickID)
68+
69+
if tc.wantErr {
70+
71+
require.Error(t, err, "should have returned an error")
72+
73+
if tc.wantErrIs != nil {
74+
75+
require.ErrorIs(t, err, tc.wantErrIs, "error type mismatch")
76+
}
77+
if tc.wantErrMsg != "" {
78+
require.EqualError(t, err, tc.wantErrMsg, "error message mismatch")
79+
}
80+
} else {
81+
require.NoError(t, err, "should not have returned an error")
82+
}
83+
require.Equal(t, tc.wantContent, content, "content mismatch")
84+
})
85+
}
86+
}
87+
88+
func TestGetBrickComposeFilePathFromID(t *testing.T) {
89+
90+
store, baseDir := setupTestStore(t)
91+
92+
namespace, brickName, _ := parseBrickID(validBrickID)
93+
94+
expectedPathString := filepath.Join(baseDir, "compose", namespace, brickName, "brick_compose.yaml")
95+
96+
testCases := []struct {
97+
name string
98+
brickID string
99+
wantPath string
100+
wantErr bool
101+
wantErrMsg string
102+
}{
103+
{
104+
name: "Success - valid ID",
105+
brickID: validBrickID,
106+
wantPath: expectedPathString,
107+
wantErr: false,
108+
},
109+
{
110+
name: "Failure - invalid ID",
111+
brickID: "invalid ID",
112+
wantPath: "",
113+
wantErr: true,
114+
wantErrMsg: "invalid ID",
115+
},
116+
}
117+
118+
for _, tc := range testCases {
119+
t.Run(tc.name, func(t *testing.T) {
120+
path, err := store.GetBrickComposeFilePathFromID(tc.brickID)
121+
122+
if tc.wantErr {
123+
require.Error(t, err, "function was expected to return an error")
124+
require.Nil(t, path, "path was expected to be nil")
125+
require.EqualError(t, err, tc.wantErrMsg, "error message mismatch")
126+
} else {
127+
require.NoError(t, err, "function was not expected to return an error")
128+
require.NotNil(t, path, "path was expected to be not nil")
129+
require.Equal(t, tc.wantPath, path.String(), "path string mismatch")
130+
}
131+
})
132+
}
133+
}
134+
135+
func TestGetBrickCodeExamplesPathFromID(t *testing.T) {
136+
store, _ := setupTestStore(t)
137+
138+
const expectedEntryCount = 3
139+
140+
testCases := []struct {
141+
name string
142+
brickID string
143+
wantNilList bool
144+
wantEntryCount int
145+
wantErr bool
146+
wantErrMsg string
147+
}{
148+
{
149+
name: "Success - directory found",
150+
brickID: validBrickID,
151+
wantNilList: false,
152+
wantEntryCount: expectedEntryCount,
153+
wantErr: false,
154+
},
155+
{
156+
name: "Success - directory not found",
157+
brickID: "namespace:non_existent_brick",
158+
wantNilList: true,
159+
wantErr: false,
160+
},
161+
{
162+
name: "Failure - invalid ID",
163+
brickID: "invalid-id",
164+
wantNilList: true,
165+
wantErr: true,
166+
wantErrMsg: "invalid ID",
167+
},
168+
}
169+
for _, tc := range testCases {
170+
t.Run(tc.name, func(t *testing.T) {
171+
pathList, err := store.GetBrickCodeExamplesPathFromID(tc.brickID)
172+
if tc.wantErr {
173+
require.Error(t, err, "should have returned an error")
174+
require.EqualError(t, err, tc.wantErrMsg, "error message mismatch")
175+
} else {
176+
require.NoError(t, err, "should not have returned an error")
177+
}
178+
179+
if tc.wantNilList {
180+
require.Nil(t, pathList, "pathList should be nil")
181+
} else {
182+
require.NotNil(t, pathList, "pathList should not be nil")
183+
}
184+
require.Equal(t, tc.wantEntryCount, len(pathList), "entry count mismatch")
185+
})
186+
}
187+
}

0 commit comments

Comments
 (0)