lsjson,lsf: make sure metadata appears for directories

This commit is contained in:
Nick Craig-Wood 2024-02-07 21:28:27 +00:00
parent e4d0055b3e
commit 09953d77b5
2 changed files with 22 additions and 9 deletions

View file

@ -194,6 +194,14 @@ func (lj *listJSON) entry(ctx context.Context, entry fs.DirEntry) (*ListJSONItem
}
item.Encrypted = path.Base(item.EncryptedPath)
}
if lj.opt.Metadata {
metadata, err := fs.GetMetadata(ctx, entry)
if err != nil {
fs.Errorf(entry, "Failed to read metadata: %v", err)
} else if metadata != nil {
item.Metadata = metadata
}
}
if do, ok := entry.(fs.IDer); ok {
item.ID = do.ID()
}
@ -224,14 +232,6 @@ func (lj *listJSON) entry(ctx context.Context, entry fs.DirEntry) (*ListJSONItem
item.Tier = do.GetTier()
}
}
if lj.opt.Metadata {
metadata, err := fs.GetMetadata(ctx, x)
if err != nil {
fs.Errorf(x, "Failed to read metadata: %v", err)
} else if metadata != nil {
item.Metadata = metadata
}
}
default:
fs.Errorf(nil, "Unknown type %T in listing in ListJSON", entry)
}

View file

@ -174,7 +174,7 @@ func TestListJSON(t *testing.T) {
}, {
name: "Metadata",
opt: operations.ListJSONOpt{
FilesOnly: true,
FilesOnly: false,
Metadata: true,
},
want: []*operations.ListJSONItem{{
@ -183,6 +183,10 @@ func TestListJSON(t *testing.T) {
Size: 5,
ModTime: operations.Timestamp{When: t1},
IsDir: false,
}, {
Path: "sub",
Name: "sub",
IsDir: true,
}},
},
} {
@ -203,6 +207,15 @@ func TestListJSON(t *testing.T) {
} else {
assert.NotEqual(t, "", got[i].MimeType)
}
if test.opt.Metadata {
features := r.Fremote.Features()
if features.ReadMetadata && !got[i].IsDir {
assert.Greater(t, len(got[i].Metadata), 0, "Expecting metadata for file")
}
if features.ReadDirMetadata && got[i].IsDir {
assert.Greater(t, len(got[i].Metadata), 0, "Expecting metadata for dir")
}
}
if test.opt.ShowHash {
hashes := got[i].Hashes
assert.NotNil(t, hashes)