Skip to content

Commit

Permalink
Add tests and flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeffryes committed Nov 15, 2023
1 parent 28b186c commit 0eb4188
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pkg/tfbridge/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ type ProviderInfo struct {
//
// See https://github.com/pulumi/pulumi-terraform-bridge/issues/1501
XSkipDetailedDiffForChanges bool

// Enables generation of a trimmed, runtime-only metadata file
// to help reduce resource plugin start time
//
// See also pulumi/pulumi-terraform-bridge#1524
GenerateRuntimeMetadata bool
}

// Send logs or status logs to the user.
Expand Down
34 changes: 34 additions & 0 deletions pkg/tfbridge/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tfbridge

import (
"testing"

md "github.com/pulumi/pulumi-terraform-bridge/v3/unstable/metadata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMetadataInfo(t *testing.T) {
data, err := md.New(nil)
require.NoError(t, err)

err = md.Set(data, "hi", []string{"hello", "world"})
require.NoError(t, err)
err = md.Set(data, "auto-aliasing", []string{"1", "2"})
require.NoError(t, err)
err = md.Set(data, "mux", []string{"a", "b"})
require.NoError(t, err)

marshalled := data.Marshal()
require.Equal(t, `{"auto-aliasing":["1","2"],"hi":["hello","world"],"mux":["a","b"]}`, string(marshalled))

info := NewProviderMetadata(marshalled)
assert.Equal(t, "bridge-metadata.json", info.Path)
marshalledInfo := (*md.Data)(info.Data).Marshal()
assert.Equal(t, `{"auto-aliasing":["1","2"],"hi":["hello","world"],"mux":["a","b"]}`, string(marshalledInfo))

runtimeMetadata := info.ExtractRuntimeMetadata()
assert.Equal(t, "runtime-bridge-metadata.json", runtimeMetadata.Path)
runtimeMarshalled := (*md.Data)(runtimeMetadata.Data).Marshal()
assert.Equal(t, `{"auto-aliasing":["1","2"],"mux":["a","b"]}`, string(runtimeMarshalled))
}
2 changes: 1 addition & 1 deletion pkg/tfgen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ func (g *Generator) UnstableGenerateFromSchema(genSchemaResult *GenerateSchemaRe

if info := g.info.MetadataInfo; info != nil {
files[info.Path] = (*metadata.Data)(info.Data).MarshalIndent()
if true {
if g.info.GenerateRuntimeMetadata {
runtimeInfo := info.ExtractRuntimeMetadata()
files[runtimeInfo.Path] = (*metadata.Data)(runtimeInfo.Data).Marshal()
}
Expand Down
26 changes: 25 additions & 1 deletion unstable/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,34 @@ func TestMarshal(t *testing.T) {
err = Set(data, "hi", []string{"hello", "world"})
assert.NoError(t, err)

marshalled := data.MarshalIndent()
assert.Equal(t, `{
"hi": [
"hello",
"world"
]
}`, string(data.MarshalIndent()))
}`, string(marshalled))

parsed, err := New(marshalled)
assert.NoError(t, err)
read, _, err := Get[[]string](parsed, "hi")
assert.NoError(t, err)
assert.Equal(t, []string{"hello", "world"}, read)
}

func TestMarshalIndent(t *testing.T) {
data, err := New(nil)
require.NoError(t, err)

err = Set(data, "hi", []string{"hello", "world"})
assert.NoError(t, err)

marshalled := data.Marshal()
assert.Equal(t, `{"hi":["hello","world"]}`, string(marshalled))

parsed, err := New(marshalled)
assert.NoError(t, err)
read, _, err := Get[[]string](parsed, "hi")
assert.NoError(t, err)
assert.Equal(t, []string{"hello", "world"}, read)
}

0 comments on commit 0eb4188

Please sign in to comment.