github.phpd.cn/thought-machine/please@v12.2.0+incompatible/src/parse/asp/logging_test.go (about) 1 // Tests on the logging functionality. These are kept separate because we 2 // hijack the logger backend to test it which makes it hard to follow what's 3 // going on for other tests. 4 5 package asp 6 7 import ( 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 "gopkg.in/op/go-logging.v1" 13 14 "core" 15 "parse/rules" 16 ) 17 18 type record struct { 19 Level logging.Level 20 Msg string 21 } 22 23 func parseFile(filename string) (*scope, error) { 24 state := core.NewBuildState(1, nil, 4, core.DefaultConfiguration()) 25 pkg := core.NewPackage("test/package") 26 pkg.Filename = "test/package/BUILD" 27 parser := NewParser(state) 28 parser.MustLoadBuiltins("builtins.build_defs", nil, rules.MustAsset("builtins.build_defs.gob")) 29 statements, err := parser.parse(filename) 30 if err != nil { 31 panic(err) 32 } 33 return parser.interpreter.interpretAll(pkg, statements) 34 } 35 36 // assertRecords asserts equality of a series of logging records. 37 func assertRecords(t *testing.T, backend *logging.MemoryBackend, expected []record) { 38 actual := []record{} 39 for node := backend.Head(); node != nil; node = node.Next() { 40 actual = append(actual, record{node.Record.Level, node.Record.Message()}) 41 } 42 assert.Equal(t, expected, actual) 43 } 44 45 func TestLogNotice(t *testing.T) { 46 backend := logging.InitForTesting(logging.NOTICE) 47 _, err := parseFile("src/parse/asp/test_data/interpreter/log.build") 48 require.NoError(t, err) 49 assertRecords(t, backend, []record{ 50 {logging.NOTICE, "//test/package/BUILD: notice"}, 51 {logging.WARNING, "//test/package/BUILD: warning"}, 52 {logging.ERROR, "//test/package/BUILD: error"}, 53 }) 54 } 55 56 func TestLogInfo(t *testing.T) { 57 // N.B. we don't test at DEBUG because then other things get logged from the parser. 58 backend := logging.InitForTesting(logging.INFO) 59 _, err := parseFile("src/parse/asp/test_data/interpreter/log.build") 60 require.NoError(t, err) 61 assertRecords(t, backend, []record{ 62 {logging.INFO, "//test/package/BUILD: info"}, 63 {logging.NOTICE, "//test/package/BUILD: notice"}, 64 {logging.WARNING, "//test/package/BUILD: warning"}, 65 {logging.ERROR, "//test/package/BUILD: error"}, 66 }) 67 }