Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitce322aa

Browse files
mirkoCrobumirkoCrobu
mirkoCrobu
authored and
mirkoCrobu
committed
make code example not mandatory
1 parent45b7220 commitce322aa

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
iferr!=nil {
102+
iferrors.Is(err,os.ErrNotExist) {
103+
returnnil,nil
104+
}
102105
returnnil,fmt.Errorf("cannot read examples directory %q: %w",targetDir,err)
103106
}
104107
returndirEntries,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+
constvalidBrickID="arduino:arduino_cloud"
13+
14+
funcsetupTestStore() (*StaticStore,string) {
15+
baseDir:=paths.New("testdata").String()
16+
returnNewStaticStore(baseDir),baseDir
17+
}
18+
19+
funcTestGetBrickReadmeFromID(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+
namestring
30+
brickIDstring
31+
wantContentstring
32+
wantErrbool
33+
wantErrIserror
34+
wantErrMsgstring
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:=rangetestCases {
59+
t.Run(tc.name,func(t*testing.T) {
60+
61+
content,err:=store.GetBrickReadmeFromID(tc.brickID)
62+
63+
iftc.wantErr {
64+
65+
require.Error(t,err,"should have returned an error")
66+
67+
iftc.wantErrIs!=nil {
68+
69+
require.ErrorIs(t,err,tc.wantErrIs,"error type mismatch")
70+
}
71+
iftc.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+
funcTestGetBrickComposeFilePathFromID(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+
namestring
92+
brickIDstring
93+
wantPathstring
94+
wantErrbool
95+
wantErrMsgstring
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:=rangetestCases {
113+
t.Run(tc.name,func(t*testing.T) {
114+
path,err:=store.GetBrickComposeFilePathFromID(tc.brickID)
115+
116+
iftc.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+
funcTestGetBrickCodeExamplesPathFromID(t*testing.T) {
130+
store,_:=setupTestStore()
131+
132+
constexpectedEntryCount=2
133+
134+
testCases:= []struct {
135+
namestring
136+
brickIDstring
137+
wantNilListbool
138+
wantEntryCountint
139+
wantErrbool
140+
wantErrMsgstring
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:=rangetestCases {
164+
t.Run(tc.name,func(t*testing.T) {
165+
pathList,err:=store.GetBrickCodeExamplesPathFromID(tc.brickID)
166+
iftc.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+
iftc.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)

[8]ページ先頭

©2009-2025 Movatter.jp