MCPcopy Create free account
hub / github.com/bracesdev/errtrace / extractLogs

Function extractLogs

cmd/errtrace/main_test.go:862–892  ·  view source on GitHub ↗

extractLogs parses the "// want" comments in src into a slice of logLine structs.

(src []byte)

Source from the content-addressed store, hash-verified

860// extractLogs parses the "// want" comments in src
861// into a slice of logLine structs.
862func extractLogs(src []byte) ([]logLine, error) {
863 fset := token.NewFileSet()
864 f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
865 if err != nil {
866 return nil, errtrace.Wrap(fmt.Errorf("parse: %w", err))
867 }
868
869 var logs []logLine
870 for _, c := range f.Comments {
871 for _, l := range c.List {
872 _, lit, ok := strings.Cut(l.Text, "// want:")
873 if !ok {
874 continue
875 }
876
877 pos := fset.Position(l.Pos())
878 s, err := strconv.Unquote(lit)
879 if err != nil {
880 return nil, errtrace.Wrap(fmt.Errorf("%s:bad string literal: %s", pos, lit))
881 }
882
883 logs = append(logs, logLine{Line: pos.Line, Msg: s})
884 }
885 }
886
887 sort.Slice(logs, func(i, j int) bool {
888 return logs[i].Line < logs[j].Line
889 })
890
891 return logs, nil
892}
893
894func parseLogOutput(file, s string) ([]logLine, error) {
895 var logs []logLine

Callers 1

testGoldenContentsFunction · 0.85

Calls 2

WrapFunction · 0.92
PosMethod · 0.65

Tested by

no test coverage detected