Skip to content

Commit

Permalink
Merge pull request #89 from flynn/dont-reset-custom-meta
Browse files Browse the repository at this point in the history
Don't reset custom metadata when not specified
  • Loading branch information
lmars committed Jul 26, 2015
2 parents 64b4add + 85b7d82 commit 55659a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
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)
}

0 comments on commit 55659a6

Please sign in to comment.