2015-08-03 17:50:09 +03:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
//
|
|
|
|
// Contributor:
|
|
|
|
// - Aaron Meihm ameihm@mozilla.com
|
|
|
|
|
|
|
|
package scribe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2017-07-19 18:55:45 +03:00
|
|
|
// Object describes data that will be sourced from the system and used in a
|
|
|
|
// test. Tests specify the criteria that will be applied to determine a true
|
|
|
|
// or false result, and tests reference an Object which provides the data the
|
|
|
|
// criteria will be compared to.
|
2015-10-15 18:55:38 +03:00
|
|
|
type Object struct {
|
2016-03-22 00:36:07 +03:00
|
|
|
Object string `json:"object" yaml:"object"`
|
|
|
|
FileContent FileContent `json:"filecontent" yaml:"filecontent"`
|
|
|
|
FileName FileName `json:"filename" yaml:"filename"`
|
|
|
|
Package Pkg `json:"package" yaml:"package"`
|
|
|
|
Raw Raw `json:"raw" yaml:"raw"`
|
|
|
|
HasLine HasLine `json:"hasline" yaml:"hasline"`
|
2015-08-03 17:50:09 +03:00
|
|
|
|
2015-08-03 21:45:11 +03:00
|
|
|
isChain bool // True if object is part of an import chain.
|
2015-08-03 17:50:09 +03:00
|
|
|
prepared bool // True if object has been prepared.
|
|
|
|
err error // The last error condition encountered during preparation.
|
|
|
|
}
|
|
|
|
|
|
|
|
type genericSource interface {
|
|
|
|
prepare() error
|
|
|
|
getCriteria() []evaluationCriteria
|
2015-08-03 21:45:11 +03:00
|
|
|
isChain() bool
|
2015-10-15 18:55:38 +03:00
|
|
|
expandVariables([]Variable)
|
2015-08-04 20:37:24 +03:00
|
|
|
validate(d *Document) error
|
2015-08-03 21:45:11 +03:00
|
|
|
mergeCriteria([]evaluationCriteria)
|
2015-08-04 23:38:17 +03:00
|
|
|
fireChains(*Document) ([]evaluationCriteria, error)
|
2015-08-03 17:50:09 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 18:55:38 +03:00
|
|
|
func (o *Object) validate(d *Document) error {
|
2015-08-03 19:34:54 +03:00
|
|
|
if len(o.Object) == 0 {
|
|
|
|
return fmt.Errorf("an object in document has no identifier")
|
|
|
|
}
|
|
|
|
si := o.getSourceInterface()
|
|
|
|
if si == nil {
|
|
|
|
return fmt.Errorf("%v: no valid source interface", o.Object)
|
|
|
|
}
|
2015-08-04 20:37:24 +03:00
|
|
|
err := si.validate(d)
|
2015-08-03 19:34:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%v: %v", o.Object, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:55:38 +03:00
|
|
|
func (o *Object) markChain() {
|
2015-08-03 21:45:11 +03:00
|
|
|
o.isChain = o.getSourceInterface().isChain()
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:55:38 +03:00
|
|
|
func (o *Object) getSourceInterface() genericSource {
|
2015-08-03 17:50:09 +03:00
|
|
|
if o.Package.Name != "" {
|
|
|
|
return &o.Package
|
|
|
|
} else if o.FileContent.Path != "" {
|
|
|
|
return &o.FileContent
|
|
|
|
} else if o.FileName.Path != "" {
|
|
|
|
return &o.FileName
|
|
|
|
} else if len(o.Raw.Identifiers) > 0 {
|
|
|
|
return &o.Raw
|
2015-08-05 00:54:01 +03:00
|
|
|
} else if o.HasLine.Path != "" {
|
|
|
|
return &o.HasLine
|
2015-08-03 17:50:09 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-15 18:55:38 +03:00
|
|
|
func (o *Object) fireChains(d *Document) error {
|
2015-08-03 21:45:11 +03:00
|
|
|
si := o.getSourceInterface()
|
2015-08-04 23:38:17 +03:00
|
|
|
// We only fire chains on root object types, not on chain entries
|
|
|
|
// themselves.
|
|
|
|
if si.isChain() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// If the object already has encountered an error, don't bother
|
|
|
|
// trying to execute chain entries for it.
|
|
|
|
if o.err != nil {
|
|
|
|
debugPrint("fireChains(): skipping failed object \"%v\"\n", o.Object)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
criteria, err := si.fireChains(d)
|
|
|
|
if err != nil {
|
|
|
|
o.err = err
|
|
|
|
return err
|
|
|
|
}
|
2015-08-04 23:47:55 +03:00
|
|
|
if criteria != nil {
|
|
|
|
si.mergeCriteria(criteria)
|
|
|
|
}
|
2015-08-04 23:38:17 +03:00
|
|
|
return nil
|
2015-08-03 21:45:11 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 18:55:38 +03:00
|
|
|
func (o *Object) prepare(d *Document) error {
|
2015-08-03 21:45:11 +03:00
|
|
|
if o.isChain {
|
|
|
|
debugPrint("prepare(): skipping chain object \"%v\"\n", o.Object)
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-03 17:50:09 +03:00
|
|
|
if o.prepared {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
o.prepared = true
|
|
|
|
|
|
|
|
p := o.getSourceInterface()
|
|
|
|
if p == nil {
|
|
|
|
o.err = fmt.Errorf("object has no valid interface")
|
|
|
|
return o.err
|
|
|
|
}
|
|
|
|
p.expandVariables(d.Variables)
|
|
|
|
err := p.prepare()
|
|
|
|
if err != nil {
|
|
|
|
o.err = err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|