Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions cmd/pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/files"
Expand Down Expand Up @@ -112,6 +113,50 @@ func TestPutStdin_UploadsContent(t *testing.T) {
}
}

func TestPutStdinSetsClientModified(t *testing.T) {
content := "hello from stdin"
cmd := testPutCmdWithStdin(strings.NewReader(content))

start := time.Now().UTC()
var uploadedClientModified *dropbox.DBXTime
mock := &mockFilesClient{
getMetadataFn: func(arg *files.GetMetadataArg) (files.IsMetadata, error) {
return nil, &files.GetMetadataAPIError{}
},
uploadFn: func(arg *files.UploadArg, r io.Reader) (*files.FileMetadata, error) {
uploadedClientModified = arg.ClientModified
data, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}
if string(data) != content {
t.Fatalf("uploaded content = %q, want %q", string(data), content)
}
return &files.FileMetadata{}, nil
},
}
stubFilesClient(t, mock)

err := put(cmd, []string{"-", "/dest.txt"})
end := time.Now().UTC()
if err != nil {
t.Fatalf("put stdin error: %v", err)
}
if uploadedClientModified == nil {
t.Fatal("ClientModified = nil, want stdin spool modified time")
}

got := time.Time(*uploadedClientModified)
lower := start.Add(-time.Second)
upper := end.Add(time.Second)
if got.Before(lower) || got.After(upper) {
t.Fatalf("ClientModified = %s, want between %s and %s", got.Format(time.RFC3339Nano), lower.Format(time.RFC3339Nano), upper.Format(time.RFC3339Nano))
}
if got.Location() != time.UTC {
t.Fatalf("ClientModified location = %v, want UTC", got.Location())
}
}

func TestPutStdinIfExistsSkipDoesNotReadStdin(t *testing.T) {
cmd := testPutCmdWithStdin(failReadReader{t: t})
_ = cmd.Flags().Set("if-exists", "skip")
Expand Down
10 changes: 7 additions & 3 deletions cmd/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,12 @@ func putFile(src, dst string, opts putOptions) error {
return err
}

// Dropbox upload commit timestamps must be UTC with second precision.
func dropboxClientModified(value time.Time) *dropbox.DBXTime {
ts := dropbox.DBXTime(value.UTC().Round(time.Second))
return &ts
}

func putFileWithResult(src, dst string, opts putOptions) (putResult, error) {
ifExists, err := normalizePutIfExists(opts.ifExists)
if err != nil {
Expand Down Expand Up @@ -537,9 +543,7 @@ func putFileWithResult(src, dst string, opts putOptions) (putResult, error) {
commitInfo.Mode.Tag = writeModeForIfExists(ifExists)
commitInfo.StrictConflict = ifExists != putIfExistsOverwrite

// The Dropbox API only accepts timestamps in UTC with second precision.
ts := dropbox.DBXTime(time.Now().UTC().Round(time.Second))
commitInfo.ClientModified = &ts
commitInfo.ClientModified = dropboxClientModified(contentsInfo.ModTime())

if contentsInfo.Size() > singleShotUploadSizeCutoff {
metadata, err := uploadChunked(dbx, uploadProgressReader(contents, contentsInfo.Size(), putErrorOutput(opts)), commitInfo, contentsInfo.Size(), opts.workers, opts.chunkSize, opts.debug)
Expand Down
58 changes: 58 additions & 0 deletions cmd/put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,64 @@ func TestPutTextModeWritesNoStdoutOnSuccess(t *testing.T) {
}
}

func TestDropboxClientModifiedUsesUTCSecondPrecision(t *testing.T) {
input := time.Date(2026, 7, 1, 12, 34, 56, 600*1e6, time.FixedZone("source", -7*60*60))

got := dropboxClientModified(input)
if got == nil {
t.Fatal("dropboxClientModified returned nil")
}

gotTime := time.Time(*got)
want := input.UTC().Round(time.Second)
if !gotTime.Equal(want) {
t.Fatalf("client modified = %s, want %s", gotTime.Format(time.RFC3339Nano), want.Format(time.RFC3339Nano))
}
if gotTime.Location() != time.UTC {
t.Fatalf("client modified location = %v, want UTC", gotTime.Location())
}
}

func TestPutFileUsesSourceModifiedTime(t *testing.T) {
tmpFile := filepath.Join(t.TempDir(), "mtime.txt")
if err := os.WriteFile(tmpFile, []byte("test"), 0644); err != nil {
t.Fatal(err)
}
sourceModTime := time.Date(2020, 4, 23, 14, 37, 4, 0, time.FixedZone("source", -7*60*60))
if err := os.Chtimes(tmpFile, sourceModTime, sourceModTime); err != nil {
t.Fatal(err)
}

var uploadedClientModified *dropbox.DBXTime
mock := &mockFilesClient{
uploadFn: func(arg *files.UploadArg, content io.Reader) (*files.FileMetadata, error) {
uploadedClientModified = arg.ClientModified
if _, err := io.ReadAll(content); err != nil {
t.Fatal(err)
}
return &files.FileMetadata{}, nil
},
}
stubFilesClient(t, mock)

if err := putFile(tmpFile, "/mtime.txt", putOptions{
chunkSize: 1 << 24,
workers: 4,
ifExists: putIfExistsOverwrite,
}); err != nil {
t.Fatalf("putFile error: %v", err)
}

if uploadedClientModified == nil {
t.Fatal("ClientModified = nil, want source file modified time")
}
got := time.Time(*uploadedClientModified)
want := sourceModTime.UTC().Round(time.Second)
if !got.Equal(want) {
t.Fatalf("ClientModified = %s, want %s", got.Format(time.RFC3339Nano), want.Format(time.RFC3339Nano))
}
}

func TestPutFileIfExistsSkipSkipsExistingFile(t *testing.T) {
tmpFile := filepath.Join(t.TempDir(), "test.txt")
if err := os.WriteFile(tmpFile, []byte("test"), 0644); err != nil {
Expand Down
Loading