Skip to content

Commit

Permalink
Merge pull request #34 from buildkite/comp-352-agent-re-writes-pipeli…
Browse files Browse the repository at this point in the history
…ne-with-remaining-fields-inserted-into

Add Name and Size fields to Cache - COMP-352
  • Loading branch information
jordandcarter committed Apr 17, 2024
2 parents 1c8eae3 + e3bb1c4 commit 01954f4
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
24 changes: 22 additions & 2 deletions step_command_cache.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
package pipeline

import (
"encoding/json"
"fmt"

"github.com/buildkite/go-pipeline/ordered"
)

var _ ordered.Unmarshaler = (*Cache)(nil)
var _ interface {
json.Marshaler
ordered.Unmarshaler
} = (*Cache)(nil)

var (
errUnsupportedCacheType = fmt.Errorf("unsupported type for cache")
)

// Cache models the cache settings for a given step
type Cache struct {
Paths []string `json:"paths" yaml:"paths"`
Disabled bool `yaml:",omitempty"`
Name string `yaml:"name,omitempty"`
Paths []string `yaml:"paths,omitempty"`
Size string `yaml:"size,omitempty"`

RemainingFields map[string]any `yaml:",inline"`
}

// MarshalJSON marshals the step to JSON. Special handling is needed because
// yaml.v3 has "inline" but encoding/json has no concept of it.
func (c *Cache) MarshalJSON() ([]byte, error) {
if c.Disabled {
return json.Marshal(false)
}
return inlineFriendlyMarshalJSON(c)
}

// UnmarshalOrdered unmarshals from the following types:
// - string: a single path
// - []string: multiple paths
// - ordered.Map: a map containing paths, among potentially other things
func (c *Cache) UnmarshalOrdered(o any) error {
switch v := o.(type) {
case bool:
if !v {
c.Disabled = true
}
case string:
c.Paths = []string{v}

Expand Down
73 changes: 72 additions & 1 deletion step_command_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,71 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestCacheMarshalJSON(t *testing.T) {
t.Parallel()

cases := []struct {
name string
c Cache
want string
}{
{
name: "single path",
c: Cache{Paths: []string{"path/to/cache"}},
want: `{"paths":["path/to/cache"]}`,
},
{
name: "multiple paths",
c: Cache{Paths: []string{"path/to/cache", "another/path"}},
want: `{"paths":["path/to/cache","another/path"]}`,
},
{
name: "empty cache settings block",
c: Cache{},
want: `{}`,
},
{
name: "full cache settings block",
c: Cache{
Paths: []string{"path/to/cache", "another/path"},
Name: "cache-name",
Size: "25g",
},
want: `{"name":"cache-name","paths":["path/to/cache","another/path"],"size":"25g"}`,
},
{
name: "full cache settings block with extra fields",
c: Cache{
Paths: []string{"path/to/cache", "another/path"},
Name: "cache-name",
Size: "25g",
RemainingFields: map[string]any{"extra": "field"},
},
want: `{"extra":"field","name":"cache-name","paths":["path/to/cache","another/path"],"size":"25g"}`,
},
{
name: "disabled cache",
c: Cache{Disabled: true},
want: `false`,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

b, err := tc.c.MarshalJSON()
if err != nil {
t.Fatalf("Cache.MarshalJSON() error: %v", err)
}

if diff := cmp.Diff(string(b), tc.want); diff != "" {
t.Errorf("Cache.MarshalJSON() diff (-got +want):\n%s", diff)
}
})
}
}

func TestCacheUnmarshalOrdered(t *testing.T) {
t.Parallel()

Expand All @@ -31,17 +96,23 @@ func TestCacheUnmarshalOrdered(t *testing.T) {
name: "full cache settings block",
input: ordered.MapFromItems(
ordered.TupleSA{Key: "paths", Value: []any{"path/to/cache", "another/path"}},
ordered.TupleSA{Key: "name", Value: "cache-name"},
ordered.TupleSA{Key: "size", Value: "25g"},
),
want: Cache{Paths: []string{"path/to/cache", "another/path"}},
want: Cache{Paths: []string{"path/to/cache", "another/path"}, Name: "cache-name", Size: "25g"},
},
{
name: "full cache settings block with extra fields",
input: ordered.MapFromItems(
ordered.TupleSA{Key: "paths", Value: []any{"path/to/cache", "another/path"}},
ordered.TupleSA{Key: "name", Value: "cache-name"},
ordered.TupleSA{Key: "size", Value: "25g"},
ordered.TupleSA{Key: "extra", Value: "field"},
),
want: Cache{
Paths: []string{"path/to/cache", "another/path"},
Name: "cache-name",
Size: "25g",
RemainingFields: map[string]any{"extra": "field"},
},
},
Expand Down

0 comments on commit 01954f4

Please sign in to comment.