Skip to content

Commit 0f8bd6a

Browse files
authored
Merge pull request #19 from arduino/issue_795
fix: make example code optional for brick example details endpoint request
2 parents 2a0d019 + 42d625e commit 0f8bd6a

File tree

6 files changed

+153
-0
lines changed

6 files changed

+153
-0
lines changed

internal/store/store.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ func (s *StaticStore) GetBrickCodeExamplesPathFromID(brickID string) (paths.Path
9999
targetDir := paths.New(s.codeExamplesPath, namespace, brickName)
100100
dirEntries, err := targetDir.ReadDir()
101101
if err != nil {
102+
if errors.Is(err, os.ErrNotExist) {
103+
return nil, nil
104+
}
102105
return nil, fmt.Errorf("cannot read examples directory %q: %w", targetDir, err)
103106
}
104107
return dirEntries, nil

internal/store/store_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package store
2+
3+
import (
4+
"testing"
5+
6+
"github.com/arduino/go-paths-helper"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
const validBrickID = "arduino:arduino_cloud"
11+
12+
func TestGetBrickReadmeFromID(t *testing.T) {
13+
store := NewStaticStore(paths.New("testdata", "assets", "0.4.8").String())
14+
15+
testCases := []struct {
16+
name string
17+
brickID string
18+
wantContent string
19+
wantErr bool
20+
wantErrMsg string
21+
}{
22+
{
23+
name: "Success - file found",
24+
brickID: validBrickID,
25+
wantContent: "## Readme test file",
26+
wantErr: false,
27+
},
28+
{
29+
name: "Failure - file not found",
30+
brickID: "namespace:non_existent_brick",
31+
wantContent: "",
32+
wantErr: true,
33+
wantErrMsg: "open testdata/assets/0.4.8/docs/namespace/non_existent_brick/README.md: no such file or directory",
34+
},
35+
{
36+
name: "Failure - invalid ID",
37+
brickID: "invalid-id",
38+
wantContent: "",
39+
wantErr: true,
40+
wantErrMsg: "invalid ID",
41+
},
42+
}
43+
44+
for _, tc := range testCases {
45+
t.Run(tc.name, func(t *testing.T) {
46+
content, err := store.GetBrickReadmeFromID(tc.brickID)
47+
if tc.wantErr {
48+
require.Error(t, err, "should have returned an error")
49+
if tc.wantErrMsg != "" {
50+
require.EqualError(t, err, tc.wantErrMsg, "error message mismatch")
51+
}
52+
} else {
53+
require.NoError(t, err, "should not have returned an error")
54+
}
55+
require.Equal(t, tc.wantContent, content, "content mismatch")
56+
})
57+
}
58+
}
59+
60+
func TestGetBrickComposeFilePathFromID(t *testing.T) {
61+
store := NewStaticStore(paths.New("testdata", "assets", "0.4.8").String())
62+
63+
testCases := []struct {
64+
name string
65+
brickID string
66+
wantPath string
67+
wantErr bool
68+
wantErrMsg string
69+
}{
70+
{
71+
name: "Success - valid ID",
72+
brickID: validBrickID,
73+
wantPath: "testdata/assets/0.4.8/compose/arduino/arduino_cloud/brick_compose.yaml",
74+
wantErr: false,
75+
},
76+
{
77+
name: "Failure - invalid ID",
78+
brickID: "invalid ID",
79+
wantPath: "",
80+
wantErr: true,
81+
wantErrMsg: "invalid ID",
82+
},
83+
}
84+
85+
for _, tc := range testCases {
86+
t.Run(tc.name, func(t *testing.T) {
87+
path, err := store.GetBrickComposeFilePathFromID(tc.brickID)
88+
if tc.wantErr {
89+
require.Error(t, err, "function was expected to return an error")
90+
require.Nil(t, path, "path was expected to be nil")
91+
require.EqualError(t, err, tc.wantErrMsg, "error message mismatch")
92+
} else {
93+
require.NoError(t, err, "function was not expected to return an error")
94+
require.NotNil(t, path, "path was expected to be not nil")
95+
require.Equal(t, tc.wantPath, path.String(), "path string mismatch")
96+
}
97+
})
98+
}
99+
}
100+
101+
func TestGetBrickCodeExamplesPathFromID(t *testing.T) {
102+
store := NewStaticStore(paths.New("testdata", "assets", "0.4.8").String())
103+
104+
testCases := []struct {
105+
name string
106+
brickID string
107+
wantEntryCount int
108+
wantErr string
109+
}{
110+
{
111+
name: "Success - directory found",
112+
brickID: validBrickID,
113+
wantEntryCount: 2,
114+
wantErr: "",
115+
},
116+
{
117+
name: "Success - directory not found",
118+
brickID: "namespace:non_existent_brick",
119+
wantEntryCount: 0,
120+
wantErr: "",
121+
},
122+
{
123+
name: "Failure - invalid ID",
124+
brickID: "invalid-id",
125+
wantEntryCount: 0,
126+
wantErr: "invalid ID",
127+
},
128+
}
129+
for _, tc := range testCases {
130+
t.Run(tc.name, func(t *testing.T) {
131+
pathList, err := store.GetBrickCodeExamplesPathFromID(tc.brickID)
132+
if tc.wantErr != "" {
133+
require.Error(t, err, "should have returned an error")
134+
require.EqualError(t, err, tc.wantErr, "error message mismatch")
135+
} else {
136+
require.NoError(t, err, "should not have returned an error")
137+
}
138+
if tc.wantEntryCount == 0 {
139+
require.Nil(t, pathList, "pathList should be nil")
140+
} else {
141+
require.NotNil(t, pathList, "pathList should not be nil")
142+
}
143+
require.Equal(t, tc.wantEntryCount, len(pathList), "entry count mismatch")
144+
})
145+
}
146+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## brick compose test file
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Readme test file
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# test file 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#test file 2

0 commit comments

Comments
 (0)