generator: stamp vectors with generation data; omitempty. (#237)

This commit is contained in:
Raúl Kripalani 2020-08-14 21:30:50 +01:00 committed by GitHub
parent b2adc12287
commit 955b445723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 7 deletions

View File

@ -41,6 +41,14 @@ type Generator struct {
wg sync.WaitGroup 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 { type MessageVectorGenItem struct {
Metadata *schema.Metadata Metadata *schema.Metadata
Func func(*Builder) Func func(*Builder)
@ -100,7 +108,7 @@ func (g *Generator) MessageVectorGroup(group string, vectors ...*MessageVectorGe
w = os.Stdout w = os.Stdout
} else { } else {
file := filepath.Join(g.OutputPath, fmt.Sprintf("%s--%s.json", group, item.Metadata.ID)) 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 { if err != nil {
log.Printf("failed to write to file %s: %s", file, err) log.Printf("failed to write to file %s: %s", file, err)
return return
@ -122,6 +130,9 @@ func (g *Generator) MessageVectorGroup(group string, vectors ...*MessageVectorGe
func (g *Generator) generateOne(w io.Writer, b *MessageVectorGenItem, indent bool) { func (g *Generator) generateOne(w io.Writer, b *MessageVectorGenItem, indent bool) {
log.Printf("generating test vector: %s", b.Metadata.ID) log.Printf("generating test vector: %s", b.Metadata.ID)
// stamp with our generation data.
b.Metadata.Gen = genData
vector := MessageVector(b.Metadata) vector := MessageVector(b.Metadata)
// TODO: currently if an assertion fails, we call os.Exit(1), which // TODO: currently if an assertion fails, we call os.Exit(1), which

View File

@ -30,16 +30,16 @@ type Selector string
// Metadata provides information on the generation of this test case // Metadata provides information on the generation of this test case
type Metadata struct { type Metadata struct {
ID string `json:"id"` ID string `json:"id"`
Version string `json:"version"` Version string `json:"version,omitempty"`
Desc string `json:"description"` Desc string `json:"description,omitempty"`
Comment string `json:"comment"` Comment string `json:"comment,omitempty"`
Gen GenerationData `json:"gen"` Gen GenerationData `json:"gen"`
} }
// GenerationData tags the source of this test case // GenerationData tags the source of this test case
type GenerationData struct { type GenerationData struct {
Source string `json:"source"` Source string `json:"source,omitempty"`
Version string `json:"version"` Version string `json:"version,omitempty"`
} }
// StateTree represents a state tree within preconditions and postconditions. // 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 // TestVector is a single test case
type TestVector struct { type TestVector struct {
Class `json:"class"` Class `json:"class"`
Selector `json:"selector"` Selector `json:"selector,omitempty"`
Meta *Metadata `json:"_meta"` Meta *Metadata `json:"_meta"`
// CAR binary data to be loaded into the test environment, usually a CAR // CAR binary data to be loaded into the test environment, usually a CAR