Skip to content

Commit ce322aa

Browse files
mirkoCrobumirkoCrobu
authored andcommitted
make code example not mandatory
1 parent 45b7220 commit ce322aa

File tree

6 files changed

+188
-0
lines changed

6 files changed

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