File: parser.go

package info (click to toggle)
golang-github-mozilla-scribe 0.0~git20220110.3fd4271-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 320 kB
  • sloc: makefile: 33
file content (76 lines) | stat: -rw-r--r-- 2,132 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 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 (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"

	"gopkg.in/yaml.v2"
)

// LoadDocument loads a scribe JSON or YAML document from the reader
// specified by r. Returns a Document type that can be passed to
// AnalyzeDocument(). On error, LoadDocument() returns the error that occurred.
func LoadDocument(r io.Reader) (Document, error) {
	var ret Document

	debugPrint("loading new document\n")
	b, err := ioutil.ReadAll(r)
	if err != nil {
		return ret, err
	}
	// clean up leading spaces, tabs and newlines
	b = bytes.TrimLeft(b, " \n\t")
	if len(b) < 10 {
		return ret, fmt.Errorf("the document is too small to be valid (%d bytes)", len(b))
	}
	switch b[0] {
	case '{', '[':
		debugPrint("document is in JSON format\n")
		err = json.Unmarshal(b, &ret)
	default:
		debugPrint("document is in YAML format\n")
		err = yaml.Unmarshal(b, &ret)
	}
	if err != nil {
		return ret, err
	}
	debugPrint("new document has %v test(s)\n", len(ret.Tests))
	debugPrint("new document has %v object(s)\n", len(ret.Objects))
	debugPrint("new document has %v variable(s)\n", len(ret.Variables))
	debugPrint("loaded: %+v\n", ret)

	debugPrint("validating document...\n")
	err = ret.Validate()
	if err != nil {
		return ret, err
	}

	return ret, nil
}

// AnalyzeDocument analyzes a scribe document on the host system. The will
// prepare and execute all tests specified in the scribe document. Returns
// an error if a fatal error occurs.
//
// Note that an error in an individual test does not necessarily represent
// a fatal error condition. In these cases, the test itself will be marked
// as having an error condition (stored in the Err field of the Test).
func AnalyzeDocument(d Document) error {
	debugPrint("preparing objects...\n")
	err := d.prepareObjects()
	if err != nil {
		return err
	}
	debugPrint("analyzing document...\n")
	return d.runTests()
}