mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 10:03:42 +08:00 
			
		
		
		
	`MarshalJSON` would not include the extra attributes because it iterated over the target map rather than the source map. Also fixes JSON unmarshaling for SSH and secrets. The intention was to unmarshal into the struct, but `UnmarshalText` takes priority over the default struct unmarshaling so it didn't work as intended. Tests have been added for all marshaling and unmarshaling methods. Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
		
			
				
	
	
		
			85 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			85 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package buildflags
 | 
						|
 | 
						|
import (
 | 
						|
	"encoding/json"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/stretchr/testify/require"
 | 
						|
	"github.com/zclconf/go-cty/cty"
 | 
						|
)
 | 
						|
 | 
						|
func TestSecrets(t *testing.T) {
 | 
						|
	t.Run("MarshalJSON", func(t *testing.T) {
 | 
						|
		secrets := Secrets{
 | 
						|
			{ID: "mysecret", FilePath: "/local/secret"},
 | 
						|
			{ID: "mysecret2", Env: "TOKEN"},
 | 
						|
		}
 | 
						|
 | 
						|
		expected := `[{"id":"mysecret","src":"/local/secret"},{"id":"mysecret2","env":"TOKEN"}]`
 | 
						|
		actual, err := json.Marshal(secrets)
 | 
						|
		require.NoError(t, err)
 | 
						|
		require.JSONEq(t, expected, string(actual))
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("UnmarshalJSON", func(t *testing.T) {
 | 
						|
		in := `[{"id":"mysecret","src":"/local/secret"},{"id":"mysecret2","env":"TOKEN"}]`
 | 
						|
 | 
						|
		var actual Secrets
 | 
						|
		err := json.Unmarshal([]byte(in), &actual)
 | 
						|
		require.NoError(t, err)
 | 
						|
 | 
						|
		expected := Secrets{
 | 
						|
			{ID: "mysecret", FilePath: "/local/secret"},
 | 
						|
			{ID: "mysecret2", Env: "TOKEN"},
 | 
						|
		}
 | 
						|
		require.Equal(t, expected, actual)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("FromCtyValue", func(t *testing.T) {
 | 
						|
		in := cty.TupleVal([]cty.Value{
 | 
						|
			cty.ObjectVal(map[string]cty.Value{
 | 
						|
				"id":  cty.StringVal("mysecret"),
 | 
						|
				"src": cty.StringVal("/local/secret"),
 | 
						|
			}),
 | 
						|
			cty.ObjectVal(map[string]cty.Value{
 | 
						|
				"id":  cty.StringVal("mysecret2"),
 | 
						|
				"env": cty.StringVal("TOKEN"),
 | 
						|
			}),
 | 
						|
		})
 | 
						|
 | 
						|
		var actual Secrets
 | 
						|
		err := actual.FromCtyValue(in, nil)
 | 
						|
		require.NoError(t, err)
 | 
						|
 | 
						|
		expected := Secrets{
 | 
						|
			{ID: "mysecret", FilePath: "/local/secret"},
 | 
						|
			{ID: "mysecret2", Env: "TOKEN"},
 | 
						|
		}
 | 
						|
		require.Equal(t, expected, actual)
 | 
						|
	})
 | 
						|
 | 
						|
	t.Run("ToCtyValue", func(t *testing.T) {
 | 
						|
		secrets := Secrets{
 | 
						|
			{ID: "mysecret", FilePath: "/local/secret"},
 | 
						|
			{ID: "mysecret2", Env: "TOKEN"},
 | 
						|
		}
 | 
						|
 | 
						|
		actual := secrets.ToCtyValue()
 | 
						|
		expected := cty.ListVal([]cty.Value{
 | 
						|
			cty.ObjectVal(map[string]cty.Value{
 | 
						|
				"id":  cty.StringVal("mysecret"),
 | 
						|
				"src": cty.StringVal("/local/secret"),
 | 
						|
				"env": cty.StringVal(""),
 | 
						|
			}),
 | 
						|
			cty.ObjectVal(map[string]cty.Value{
 | 
						|
				"id":  cty.StringVal("mysecret2"),
 | 
						|
				"src": cty.StringVal(""),
 | 
						|
				"env": cty.StringVal("TOKEN"),
 | 
						|
			}),
 | 
						|
		})
 | 
						|
 | 
						|
		result := actual.Equals(expected)
 | 
						|
		require.True(t, result.True())
 | 
						|
	})
 | 
						|
}
 |