Repository: rclone/rclone
File: fstest/fstests/fstests.go (Go)

Code before:
		file.Size = int64(buf.Len())
		// Callers don't always set up file.Hashes, so it is an empty map, but NewStaticObjectInfo will only create
		// default hashes if its argument is nil.
		hashes := file.Hashes
		if len(hashes) == 0 {
			hashes = nil
		}
		obji := object.NewStaticObjectInfo(file.Path, file.ModTime, file.Size, true, hashes, nil)
		if mimeType != "" || metadata != nil {
			// force the --metadata flag on temporarily
			if metadata != nil {
				ci := fs.GetConfig(ctx)
				previousMetadata := ci.Metadata
				ci.Metadata = true
				defer func() {
					ci.Metadata = previousMetadata
				}()
			}
			obji.WithMetadata(metadata).WithMimeType(mimeType)
		}
		obj, err = f.Put(ctx, in, obji, options...)
		return err
	})
	file.Hashes = uploadHash.Sums()
	if check {
		// Default mtime header for everything except b2
		if mtimeHeaderName == "" {
			mtimeHeaderName = "mtime"
		}
		// Overwrite time with that in metadata if it is already specified
		mtime, ok := metadata[mtimeHeaderName]
		if ok {
			modTime, err := time.Parse(time.RFC3339Nano, mtime)
			require.NoError(t, err)
			file.ModTime = modTime
		}
		file.Check(t, obj, f.Precision())
		// Re-read the object and check again
		obj = fstest.NewObject(ctx, t, f, file.Path)
		file.Check(t, obj, f.Precision())
	}
	return obj
}

// PutTestContents puts file with given contents to the remote and checks it but unlike TestPutLarge doesn't remove
func PutTestContents(ctx context.Context, t *testing.T, f fs.Fs, file *fstest.Item, contents string, check bool) fs.Object {
	return PutTestContentsMetadata(ctx, t, f, file, contents, check, "", nil, "")
}

// testPut puts file with random contents to the remote
func testPut(ctx context.Context, t *testing.T, f fs.Fs, file *fstest.Item) (string, fs.Object) {

Review comment (suggestion):
I'm not sure I agree with this part...

In rclone's metadata scheme the `mtime` should **always** be the name of the key in the metadata and it is the job of the backend to translate it.

So I don't think this should have mtimeHeaderName passed into it.


Code after:
		file.Size = int64(buf.Len())
		// The caller explicitly indicates whether the hashes in the file parameter should be used. If hashes is nil,
		// then NewStaticObjectInfo will calculate default hashes for use in the check.
		hashes := file.Hashes
		if !useFileHashes {
			hashes = nil
		}
		obji := object.NewStaticObjectInfo(file.Path, file.ModTime, file.Size, true, hashes, nil)
		if mimeType != "" || metadata != nil {
			// force the --metadata flag on temporarily
			if metadata != nil {
				ci := fs.GetConfig(ctx)
				previousMetadata := ci.Metadata
				ci.Metadata = true
				defer func() {
					ci.Metadata = previousMetadata
				}()
			}
			obji.WithMetadata(metadata).WithMimeType(mimeType)
		}
		obj, err = f.Put(ctx, in, obji, options...)
		return err
	})
	file.Hashes = uploadHash.Sums()
	if check {
		// Overwrite time with that in metadata if it is already specified
		mtime, ok := metadata["mtime"]
		if ok {
			modTime, err := time.Parse(time.RFC3339Nano, mtime)
			require.NoError(t, err)
			file.ModTime = modTime
		}
		file.Check(t, obj, f.Precision())
		// Re-read the object and check again
		obj = fstest.NewObject(ctx, t, f, file.Path)
		file.Check(t, obj, f.Precision())
	}
	return obj
}

// PutTestContents puts file with given contents to the remote and checks it but unlike TestPutLarge doesn't remove
func PutTestContents(ctx context.Context, t *testing.T, f fs.Fs, file *fstest.Item, contents string, check bool) fs.Object {
	return PutTestContentsMetadata(ctx, t, f, file, false, contents, check, "", nil)
}

// testPut puts file with random contents to the remote
func testPut(ctx context.Context, t *testing.T, f fs.Fs, file *fstest.Item) (string, fs.Object) {
	return testPutMimeType(ctx, t, f, file, "", nil)
}

// testPutMimeType puts file with random contents to the remote and the mime type given