compile: adds metadata field to .manifest (#4306)

Fixes: #4289

Signed-off-by: marensws <msws@live.no>
This commit is contained in:
Maren-Sofie Stubø
2022-01-31 14:26:46 +01:00
committed by GitHub
parent 01ca4a6902
commit a18f53d187
3 changed files with 34 additions and 1 deletions
+1 -1
View File
@@ -180,7 +180,7 @@ func (m Manifest) Copy() Manifest {
func (m Manifest) String() string {
m.Init()
return fmt.Sprintf("<revision: %q, roots: %v, wasm: %+v>", m.Revision, *m.Roots, m.WasmResolvers)
return fmt.Sprintf("<revision: %q, roots: %v, wasm: %+v, metadata: %+v>", m.Revision, *m.Roots, m.WasmResolvers, m.Metadata)
}
func (m Manifest) rootSet() stringSet {
+11
View File
@@ -75,6 +75,7 @@ type Compiler struct {
bvc *bundle.VerificationConfig // represents the key configuration used to verify a signed bundle
bsc *bundle.SigningConfig // represents the key configuration used to generate a signed bundle
keyID string // represents the name of the default key used to verify a signed bundle
metadata *map[string]interface{} // represents additional data included in .manifest file
}
// New returns a new compiler instance that can be invoked.
@@ -180,6 +181,12 @@ func (c *Compiler) WithCapabilities(capabilities *ast.Capabilities) *Compiler {
return c
}
//WithMetadata sets the additional data to be included in .manifest
func (c *Compiler) WithMetadata(metadata *map[string]interface{}) *Compiler {
c.metadata = metadata
return c
}
// Build compiles and links the input files and outputs a bundle to the writer.
func (c *Compiler) Build(ctx context.Context) error {
@@ -223,6 +230,10 @@ func (c *Compiler) Build(ctx context.Context) error {
c.bundle.Manifest.Revision = *c.revision
}
if c.metadata != nil {
c.bundle.Manifest.Metadata = *c.metadata
}
if err := c.bundle.FormatModules(false); err != nil {
return err
}
+22
View File
@@ -703,6 +703,28 @@ func TestCompilerSetRevision(t *testing.T) {
})
}
func TestCompilerSetMetadata(t *testing.T) {
files := map[string]string{
"test.rego": `package test
p = true`,
}
test.WithTempFS(files, func(root string) {
metadata := map[string]interface{}{"OPA version": "0.36.1"}
compiler := New().WithPaths(root).WithMetadata(&metadata)
err := compiler.Build(context.Background())
if err != nil {
t.Fatal(err)
}
if compiler.bundle.Manifest.Metadata["OPA version"] != "0.36.1" {
t.Fatal("expected metadata to be set but got:", compiler.bundle.Manifest)
}
})
}
func TestCompilerOutput(t *testing.T) {
// NOTE(tsandall): must use format package here because the compiler formats.
files := map[string]string{