|
| 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 | +} |
0 commit comments