testAsset stubs file opening so upload reads body without touching the filesystem.
(t *testing.T, name, contentType, body string)
| 21 | // testAsset stubs file opening so upload reads body without touching the |
| 22 | // filesystem. |
| 23 | func testAsset(t *testing.T, name, contentType, body string) UserAsset { |
| 24 | t.Helper() |
| 25 | |
| 26 | previousOpenFile := openFile |
| 27 | openFile = func(string) (io.ReadCloser, error) { |
| 28 | return io.NopCloser(strings.NewReader(body)), nil |
| 29 | } |
| 30 | t.Cleanup(func() { openFile = previousOpenFile }) |
| 31 | |
| 32 | info, err := fs.Stat(fstest.MapFS{ |
| 33 | name: &fstest.MapFile{Data: []byte(body)}, |
| 34 | }, name) |
| 35 | require.NoError(t, err) |
| 36 | |
| 37 | base := asset{path: "./" + name, info: info, contentType: contentType} |
| 38 | if strings.HasPrefix(contentType, "video/") { |
| 39 | return &videoAsset{base} |
| 40 | } |
| 41 | return &imageAsset{base} |
| 42 | } |
| 43 | |
| 44 | // newTestUploader builds an uploader whose requests land in reg. |
| 45 | func newTestUploader(t *testing.T, reg *httpmock.Registry, host string, targetRepository int64) *Uploader { |
no test coverage detected