|
| 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 | +} |