MCPcopy Create free account
hub / github.com/activecm/rita / TestGetFileContents

Function TestGetFileContents

util/util_test.go:1205–1276  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1203}
1204
1205func TestGetFileContents(t *testing.T) {
1206 // define test cases
1207 tests := []struct {
1208 name string
1209 path string
1210 fileContents []byte
1211 mockReadFile func(afero.Fs, string) ([]byte, error)
1212 expectedError error
1213 }{
1214 {
1215 name: "Valid Generated file",
1216 path: "/valid/file/path",
1217 fileContents: []byte("file contents"),
1218 },
1219 {
1220 name: "Empty File",
1221 path: "/invalid/file/path",
1222 fileContents: []byte(""),
1223 expectedError: ErrFileIsEmtpy,
1224 },
1225 {
1226 name: "Invalid File Path",
1227 path: "/missing/file/path",
1228 expectedError: ErrFileDoesNotExist,
1229 },
1230 {
1231 name: "Read File Error",
1232 path: "/valid/file/path",
1233 fileContents: []byte("file contents"),
1234 mockReadFile: func(_ afero.Fs, _ string) ([]byte, error) {
1235 return nil, fmt.Errorf("forced read file error")
1236 },
1237 expectedError: fmt.Errorf("forced read file error"),
1238 },
1239 }
1240
1241 for _, test := range tests {
1242 t.Run(test.name, func(t *testing.T) {
1243 // restore the original function after the test
1244 originalReadFileFunc := readFile
1245 defer func() { readFile = originalReadFileFunc }()
1246
1247 // mock the readFile function
1248 if test.mockReadFile != nil {
1249 readFile = test.mockReadFile
1250 }
1251
1252 // create a new memory filesystem
1253 afs := afero.NewMemMapFs()
1254
1255 // create the file if the test case specifies contents
1256 if test.fileContents != nil {
1257 require.NoError(t, afero.WriteFile(afs, test.path, test.fileContents, 0644), "failed to create file")
1258 }
1259
1260 // call readFile and check the results
1261 result, err := GetFileContents(afs, test.path)
1262

Callers

nothing calls this directly

Calls 1

GetFileContentsFunction · 0.85

Tested by

no test coverage detected