From 955b4457238c5b6bc684c41fc837d00f57249043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Kripalani?= Date: Fri, 14 Aug 2020 21:30:50 +0100 Subject: [PATCH] generator: stamp vectors with generation data; omitempty. (#237) --- tvx/builders/generator.go | 13 ++++++++++++- tvx/schema/schema.go | 12 ++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/tvx/builders/generator.go b/tvx/builders/generator.go index a3e91e33c..1f7c32cdd 100644 --- a/tvx/builders/generator.go +++ b/tvx/builders/generator.go @@ -41,6 +41,14 @@ type Generator struct { wg sync.WaitGroup } +// genData is the generation data to stamp into vectors. +// TODO in the future this should contain the commit of this tool and +// the builder api. +var genData = schema.GenerationData{ + Source: "script", + Version: "v0", +} + type MessageVectorGenItem struct { Metadata *schema.Metadata Func func(*Builder) @@ -100,7 +108,7 @@ func (g *Generator) MessageVectorGroup(group string, vectors ...*MessageVectorGe w = os.Stdout } else { file := filepath.Join(g.OutputPath, fmt.Sprintf("%s--%s.json", group, item.Metadata.ID)) - out, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 0644) + out, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { log.Printf("failed to write to file %s: %s", file, err) return @@ -122,6 +130,9 @@ func (g *Generator) MessageVectorGroup(group string, vectors ...*MessageVectorGe func (g *Generator) generateOne(w io.Writer, b *MessageVectorGenItem, indent bool) { log.Printf("generating test vector: %s", b.Metadata.ID) + // stamp with our generation data. + b.Metadata.Gen = genData + vector := MessageVector(b.Metadata) // TODO: currently if an assertion fails, we call os.Exit(1), which diff --git a/tvx/schema/schema.go b/tvx/schema/schema.go index 87d58078e..7083c2350 100644 --- a/tvx/schema/schema.go +++ b/tvx/schema/schema.go @@ -30,16 +30,16 @@ type Selector string // Metadata provides information on the generation of this test case type Metadata struct { ID string `json:"id"` - Version string `json:"version"` - Desc string `json:"description"` - Comment string `json:"comment"` + Version string `json:"version,omitempty"` + Desc string `json:"description,omitempty"` + Comment string `json:"comment,omitempty"` Gen GenerationData `json:"gen"` } // GenerationData tags the source of this test case type GenerationData struct { - Source string `json:"source"` - Version string `json:"version"` + Source string `json:"source,omitempty"` + Version string `json:"version,omitempty"` } // StateTree represents a state tree within preconditions and postconditions. @@ -92,7 +92,7 @@ func (beb *Base64EncodedBytes) UnmarshalJSON(v []byte) error { // TestVector is a single test case type TestVector struct { Class `json:"class"` - Selector `json:"selector"` + Selector `json:"selector,omitempty"` Meta *Metadata `json:"_meta"` // CAR binary data to be loaded into the test environment, usually a CAR