Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't reset custom metadata when not specified #89

Merged
merged 1 commit into from
Jul 26, 2015
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
9 changes: 8 additions & 1 deletion repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,17 @@ func (r *Repo) AddTargetsWithExpires(paths []string, custom json.RawMessage, exp
if err != nil {
return err
}
path = util.NormalizeTarget(path)

// if we have custom metadata, set it, otherwise maintain
// existing metadata if present
if len(custom) > 0 {
meta.Custom = &custom
} else if t, ok := t.Targets[path]; ok {
meta.Custom = t.Custom
}
t.Targets[util.NormalizeTarget(path)] = meta

t.Targets[path] = meta
return nil
}); err != nil {
return err
Expand Down
37 changes: 37 additions & 0 deletions repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,3 +885,40 @@ func (RepoSuite) TestManageMultipleTargets(c *C) {
c.Assert(err, IsNil)
c.Assert(t.Targets, HasLen, 0)
}

func (RepoSuite) TestCustomTargetMetadata(c *C) {
files := map[string][]byte{
"/foo.txt": []byte("foo"),
"/bar.txt": []byte("bar"),
"/baz.txt": []byte("baz"),
}
local := MemoryStore(make(map[string]json.RawMessage), files)
r, err := NewRepo(local)
c.Assert(err, IsNil)

custom := json.RawMessage(`{"foo":"bar"}`)
assertCustomMeta := func(file string, custom *json.RawMessage) {
t, err := r.targets()
c.Assert(err, IsNil)
target, ok := t.Targets["/"+file]
if !ok {
c.Fatalf("missing target file: %s", file)
}
c.Assert(target.Custom, DeepEquals, custom)
}

// check custom metadata gets added to the target
c.Assert(r.AddTarget("foo.txt", custom), IsNil)
assertCustomMeta("foo.txt", &custom)

// check adding bar.txt with no metadata doesn't affect foo.txt
c.Assert(r.AddTarget("bar.txt", nil), IsNil)
assertCustomMeta("bar.txt", nil)
assertCustomMeta("foo.txt", &custom)

// check adding all files with no metadata doesn't reset existing metadata
c.Assert(r.AddTargets(nil, nil), IsNil)
assertCustomMeta("baz.txt", nil)
assertCustomMeta("bar.txt", nil)
assertCustomMeta("foo.txt", &custom)
}