Example #1
0
func Example() {
	docstr := `
	{
		"objects": [
		{
			"object": "raw",
			"raw": {
				"identifiers": [
				{
					"identifier": "test",
					"value": "Example"
				}
				]
			}
		}
		],
		"tests": [
		{
			"test": "example",
			"object": "raw",
			"regexp": {
				"value": "Example"
			}
		}
		]
	}
	`
	rdr := strings.NewReader(docstr)
	// Initialize the library.
	scribe.Bootstrap()
	// Load the document from the reader, returning a Document type.
	// LoadDocument() will also call ValidateDocument() to check for any
	// consistency issues.
	doc, err := scribe.LoadDocument(rdr)
	if err != nil {
		fmt.Println("LoadDocument:", err)
		return
	}
	// Analyze the document.
	err = scribe.AnalyzeDocument(doc)
	if err != nil {
		fmt.Println("AnalyzeDocument:", err)
		return
	}
	// Grab the results for the test, most of the time you would loop
	// through the results of GetTestIdentifiers() rather then call a result
	// directly.
	result, err := scribe.GetResults(&doc, "example")
	if err != nil {
		fmt.Println("GetResults:", err)
		return
	}
	if result.MasterResult {
		fmt.Println("true")
	} else {
		fmt.Println("false")
	}

	// Output: true
}
Example #2
0
func main() {
	var (
		docpath      string
		expectedExit bool
		testHooks    bool
		showVersion  bool
		lineFmt      bool
		jsonFmt      bool
		onlyTrue     bool
	)

	err := scribe.Bootstrap()
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
		os.Exit(1)
	}

	flag.BoolVar(&flagDebug, "d", false, "enable debugging")
	flag.BoolVar(&expectedExit, "e", false, "exit if result is unexpected")
	flag.StringVar(&docpath, "f", "", "path to document")
	flag.BoolVar(&lineFmt, "l", false, "output one result per line")
	flag.BoolVar(&jsonFmt, "j", false, "JSON output mode")
	flag.BoolVar(&testHooks, "t", false, "enable test hooks")
	flag.BoolVar(&onlyTrue, "T", false, "only show true outcomes in results")
	flag.BoolVar(&showVersion, "v", false, "show version")
	flag.Parse()

	if showVersion {
		fmt.Fprintf(os.Stdout, "scribe %v\n", scribe.Version)
		os.Exit(0)
	}

	if flagDebug {
		scribe.SetDebug(true, os.Stderr)
	}

	if docpath == "" {
		fmt.Fprintf(os.Stderr, "error: must specify document path\n")
		os.Exit(1)
	}

	scribe.TestHooks(testHooks)

	fd, err := os.Open(docpath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
		os.Exit(1)
	}
	defer fd.Close()
	doc, err := scribe.LoadDocument(fd)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
		os.Exit(1)
	}

	// In expectedExit mode, set a callback in the scribe module that will
	// be called immediately during analysis if a test result does not
	// match the boolean expectedresult parameter in the test. The will
	// result in the tool exiting with return code 2.
	if expectedExit {
		scribe.ExpectedCallback(failExit)
	}

	err = scribe.AnalyzeDocument(doc)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
		os.Exit(1)
	}

	for _, x := range doc.GetTestIdentifiers() {
		tr, err := scribe.GetResults(&doc, x)
		if err != nil {
			fmt.Fprintf(os.Stderr, "error obtaining results for \"%v\": %v\n", x, err)
			continue
		}
		if onlyTrue {
			if !tr.MasterResult {
				continue
			}
		}
		if lineFmt {
			for _, x := range tr.SingleLineResults() {
				fmt.Fprintf(os.Stdout, "%v\n", x)
			}
		} else if jsonFmt {
			fmt.Fprintf(os.Stdout, "%v\n", tr.JSON())
		} else {
			fmt.Fprintf(os.Stdout, "%v\n", tr.String())
		}
	}

	os.Exit(0)
}
Example #3
0
func (r *run) Run(in io.Reader) (resStr string) {
	defer func() {
		if e := recover(); e != nil {
			// return error in json
			r.Results.Errors = append(r.Results.Errors, fmt.Sprintf("%v", e))
			r.Results.Success = false
			endCounters()
			r.Results.Statistics = stats
			err, _ := json.Marshal(r.Results)
			resStr = string(err)
			return
		}
	}()

	// Restrict go runtime processor utilization here, this might be moved
	// into a more generic agent module function at some point.
	runtime.GOMAXPROCS(1)

	// Initialize scribe
	scribelib.Bootstrap()

	// Install the file locator; this allows us to use the file module's
	// search functionality overriding the scribe built-in file system
	// walk function.
	scribelib.InstallFileLocator(fileModuleLocator)

	startCounters()

	// Read module parameters from stdin
	err := modules.ReadInputParameters(in, &r.Parameters)
	if err != nil {
		panic(err)
	}

	err = r.ValidateParameters()
	if err != nil {
		panic(err)
	}

	document := r.Parameters.ScribeDoc
	e := &ScribeElements{}
	e.HumanOutput = r.Parameters.HumanOutput
	e.JSONOutput = r.Parameters.JSONOutput

	e.Results = make([]scribelib.TestResult, 0)
	// Proceed with analysis here. ValidateParameters() will have already
	// validated the document.
	err = scribelib.AnalyzeDocument(document)
	if err != nil {
		panic(err)
	}
	for _, x := range document.GetTestIdentifiers() {
		tr, err := scribelib.GetResults(&document, x)
		if err != nil {
			panic(err)
		}
		if !tr.MasterResult && r.Parameters.OnlyTrue {
			continue
		}
		e.Results = append(e.Results, tr)
	}
	buf, err := buildResults(*e, &r.Results)
	if err != nil {
		panic(err)
	}
	resStr = string(buf)
	return
}