check interface PutTestContentsMetadata puts file with given contents to the remote and checks it but unlike TestPutLarge doesn't remove It uploads the object with the mimeType and metadata passed in if set. It returns the object which will have been checked if check is set
(ctx context.Context, t *testing.T, f fs.Fs, file *fstest.Item, useFileHashes bool, contents string, check bool, mimeType string, metadata fs.Metadata, options ...fs.OpenOption)
| 154 | // |
| 155 | // It returns the object which will have been checked if check is set |
| 156 | func PutTestContentsMetadata(ctx context.Context, t *testing.T, f fs.Fs, file *fstest.Item, useFileHashes bool, contents string, check bool, mimeType string, metadata fs.Metadata, options ...fs.OpenOption) fs.Object { |
| 157 | var ( |
| 158 | err error |
| 159 | obj fs.Object |
| 160 | uploadHash *hash.MultiHasher |
| 161 | ) |
| 162 | retry(t, "Put", func() error { |
| 163 | buf := bytes.NewBufferString(contents) |
| 164 | uploadHash = hash.NewMultiHasher() |
| 165 | in := io.TeeReader(buf, uploadHash) |
| 166 | |
| 167 | file.Size = int64(buf.Len()) |
| 168 | // The caller explicitly indicates whether the hashes in the file parameter should be used. If hashes is nil, |
| 169 | // then NewStaticObjectInfo will calculate default hashes for use in the check. |
| 170 | hashes := file.Hashes |
| 171 | if !useFileHashes { |
| 172 | hashes = nil |
| 173 | } |
| 174 | obji := object.NewStaticObjectInfo(file.Path, file.ModTime, file.Size, true, hashes, nil) |
| 175 | if mimeType != "" || metadata != nil { |
| 176 | // force the --metadata flag on temporarily |
| 177 | if metadata != nil { |
| 178 | ci := fs.GetConfig(ctx) |
| 179 | previousMetadata := ci.Metadata |
| 180 | ci.Metadata = true |
| 181 | defer func() { |
| 182 | ci.Metadata = previousMetadata |
| 183 | }() |
| 184 | } |
| 185 | obji.WithMetadata(metadata).WithMimeType(mimeType) |
| 186 | } |
| 187 | obj, err = f.Put(ctx, in, obji, options...) |
| 188 | return err |
| 189 | }) |
| 190 | file.Hashes = uploadHash.Sums() |
| 191 | if check { |
| 192 | // Overwrite time with that in metadata if it is already specified |
| 193 | mtime, ok := metadata["mtime"] |
| 194 | if ok { |
| 195 | modTime, err := time.Parse(time.RFC3339Nano, mtime) |
| 196 | require.NoError(t, err) |
| 197 | file.ModTime = modTime |
| 198 | } |
| 199 | file.Check(t, obj, f.Precision()) |
| 200 | // Re-read the object and check again |
| 201 | obj = fstest.NewObject(ctx, t, f, file.Path) |
| 202 | file.Check(t, obj, f.Precision()) |
| 203 | } |
| 204 | return obj |
| 205 | } |
| 206 | |
| 207 | // PutTestContents puts file with given contents to the remote and checks it but unlike TestPutLarge doesn't remove |
| 208 | func PutTestContents(ctx context.Context, t *testing.T, f fs.Fs, file *fstest.Item, contents string, check bool) fs.Object { |
searching dependent graphs…