How to use forEachFile method of report Package

Best Syzkaller code snippet using report.forEachFile

report_test.go

Source:report_test.go Github

copy

Full Screen

...16 "github.com/google/syzkaller/pkg/osutil"17)18var flagUpdate = flag.Bool("update", false, "update test files accordingly to current results")19func TestParse(t *testing.T) {20 forEachFile(t, "report", testParseFile)21}22type ParseTest struct {23 FileName string24 Log []byte25 Title string26 StartLine string27 EndLine string28 Corrupted bool29 Suppressed bool30 HasReport bool31 Report []byte32}33func testParseFile(t *testing.T, reporter Reporter, fn string) {34 input, err := os.Open(fn)35 if err != nil {36 t.Fatal(err)37 }38 defer input.Close()39 const (40 phaseHeaders = iota41 phaseLog42 phaseReport43 )44 phase := phaseHeaders45 test := &ParseTest{46 FileName: fn,47 }48 prevEmptyLine := false49 s := bufio.NewScanner(input)50 for s.Scan() {51 switch phase {52 case phaseHeaders:53 const (54 titlePrefix = "TITLE: "55 startPrefix = "START: "56 endPrefix = "END: "57 corruptedPrefix = "CORRUPTED: "58 suppressedPrefix = "SUPPRESSED: "59 )60 switch ln := s.Text(); {61 case strings.HasPrefix(ln, "#"):62 case strings.HasPrefix(ln, titlePrefix):63 test.Title = ln[len(titlePrefix):]64 case strings.HasPrefix(ln, startPrefix):65 test.StartLine = ln[len(startPrefix):]66 case strings.HasPrefix(ln, endPrefix):67 test.EndLine = ln[len(endPrefix):]68 case strings.HasPrefix(ln, corruptedPrefix):69 switch v := ln[len(corruptedPrefix):]; v {70 case "Y":71 test.Corrupted = true72 case "N":73 test.Corrupted = false74 default:75 t.Fatalf("unknown CORRUPTED value %q", v)76 }77 case strings.HasPrefix(ln, suppressedPrefix):78 switch v := ln[len(suppressedPrefix):]; v {79 case "Y":80 test.Suppressed = true81 case "N":82 test.Suppressed = false83 default:84 t.Fatalf("unknown SUPPRESSED value %q", v)85 }86 case ln == "":87 phase = phaseLog88 default:89 t.Fatalf("unknown header field %q", ln)90 }91 case phaseLog:92 if prevEmptyLine && string(s.Bytes()) == "REPORT:" {93 test.HasReport = true94 phase = phaseReport95 } else {96 test.Log = append(test.Log, s.Bytes()...)97 test.Log = append(test.Log, '\n')98 }99 case phaseReport:100 test.Report = append(test.Report, s.Bytes()...)101 test.Report = append(test.Report, '\n')102 }103 prevEmptyLine = len(s.Bytes()) == 0104 }105 if s.Err() != nil {106 t.Fatalf("file scanning error: %v", s.Err())107 }108 if len(test.Log) == 0 {109 t.Fatalf("can't find log in input file")110 }111 testParseImpl(t, reporter, test)112 // In some cases we get output with \r\n for line endings,113 // ensure that regexps are not confused by this.114 bytes.Replace(test.Log, []byte{'\n'}, []byte{'\r', '\n'}, -1)115 testParseImpl(t, reporter, test)116}117func testParseImpl(t *testing.T, reporter Reporter, test *ParseTest) {118 rep := reporter.Parse(test.Log)119 containsCrash := reporter.ContainsCrash(test.Log)120 expectCrash := (test.Title != "")121 if expectCrash && !containsCrash {122 t.Fatalf("ContainsCrash did not find crash")123 }124 if !expectCrash && containsCrash {125 t.Fatalf("ContainsCrash found unexpected crash")126 }127 if rep != nil && rep.Title == "" {128 t.Fatalf("found crash, but title is empty")129 }130 title, corrupted, corruptedReason, suppressed := "", false, "", false131 if rep != nil {132 title = rep.Title133 corrupted = rep.Corrupted134 corruptedReason = rep.CorruptedReason135 suppressed = rep.Suppressed136 }137 if title != test.Title || corrupted != test.Corrupted || suppressed != test.Suppressed {138 if *flagUpdate && test.StartLine == "" && test.EndLine == "" {139 buf := new(bytes.Buffer)140 fmt.Fprintf(buf, "TITLE: %v\n", title)141 if corrupted {142 fmt.Fprintf(buf, "CORRUPTED: Y\n")143 }144 if suppressed {145 fmt.Fprintf(buf, "SUPPRESSED: Y\n")146 }147 fmt.Fprintf(buf, "\n%s", test.Log)148 if test.HasReport {149 fmt.Fprintf(buf, "REPORT:\n%s", test.Report)150 }151 if err := ioutil.WriteFile(test.FileName, buf.Bytes(), 0640); err != nil {152 t.Logf("failed to update test file: %v", err)153 }154 }155 t.Fatalf("want:\nTITLE: %s\nCORRUPTED: %v\nSUPPRESSED: %v\n"+156 "got:\nTITLE: %s\nCORRUPTED: %v (%v)\nSUPPRESSED: %v\n",157 test.Title, test.Corrupted, test.Suppressed,158 title, corrupted, corruptedReason, suppressed)159 }160 if title != "" && len(rep.Report) == 0 {161 t.Fatalf("found crash message but report is empty")162 }163 if rep != nil {164 checkReport(t, rep, test)165 }166}167func checkReport(t *testing.T, rep *Report, test *ParseTest) {168 if test.HasReport && !bytes.Equal(rep.Report, test.Report) {169 t.Fatalf("extracted wrong report:\n%s\nwant:\n%s", rep.Report, test.Report)170 }171 if !bytes.Equal(rep.Output, test.Log) {172 t.Fatalf("bad Output:\n%s", rep.Output)173 }174 if test.StartLine != "" {175 if test.EndLine == "" {176 test.EndLine = test.StartLine177 }178 startPos := bytes.Index(test.Log, []byte(test.StartLine))179 endPos := bytes.Index(test.Log, []byte(test.EndLine)) + len(test.EndLine)180 if rep.StartPos != startPos || rep.EndPos != endPos {181 t.Fatalf("bad start/end pos %v-%v, want %v-%v, line %q",182 rep.StartPos, rep.EndPos, startPos, endPos,183 string(test.Log[rep.StartPos:rep.EndPos]))184 }185 }186}187func TestGuiltyFile(t *testing.T) {188 forEachFile(t, "guilty", testGuiltyFile)189}190func testGuiltyFile(t *testing.T, reporter Reporter, fn string) {191 data, err := ioutil.ReadFile(fn)192 if err != nil {193 t.Fatal(err)194 }195 for bytes.HasPrefix(data, []byte{'#'}) {196 nl := bytes.Index(data, []byte{'\n'})197 if nl == -1 {198 t.Fatalf("unterminated comment in file")199 }200 data = data[nl+1:]201 }202 const prefix = "FILE: "203 if !bytes.HasPrefix(data, []byte(prefix)) {204 t.Fatalf("no %v prefix in file", prefix)205 }206 nlnl := bytes.Index(data[len(prefix):], []byte{'\n', '\n'})207 if nlnl == -1 {208 t.Fatalf("no \\n\\n in file")209 }210 file := string(data[len(prefix) : len(prefix)+nlnl])211 report := data[len(prefix)+nlnl:]212 if guilty := reporter.(guilter).extractGuiltyFile(report); guilty != file {213 t.Fatalf("got guilty %q, want %q", guilty, file)214 }215}216func forEachFile(t *testing.T, dir string, fn func(t *testing.T, reporter Reporter, fn string)) {217 testFilenameRe := regexp.MustCompile("^[0-9]+$")218 for os := range ctors {219 path := filepath.Join("testdata", os, dir)220 if !osutil.IsExist(path) {221 continue222 }223 files, err := ioutil.ReadDir(path)224 if err != nil {225 t.Fatal(err)226 }227 cfg := &mgrconfig.Config{228 TargetOS: os,229 }230 reporter, err := NewReporter(cfg)...

Full Screen

Full Screen

forEachFile

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 dir, err := os.Getwd()4 if err != nil {5 log.Fatal(err)6 }7 report := new(Report)8 report.forEachFile(dir, report.printFile)9}10type Report struct {11}12func (r *Report) printFile(path string, info os.FileInfo, err error) error {13 fmt.Println(path)14}15func (r *Report) forEachFile(root string, fn filepath.WalkFunc) {16 err := filepath.Walk(root, fn)17 if err != nil {18 panic(err)19 }20}

Full Screen

Full Screen

forEachFile

Using AI Code Generation

copy

Full Screen

1import (2type report struct {3}4func (r report) forEachFile(callback func(path string, info os.FileInfo)) error {5 return filepath.Walk(r.path, func(path string, info os.FileInfo, err error) error {6 if err != nil {7 }8 callback(path, info)9 })10}11func main() {12 r := report{13 }14 err := r.forEachFile(func(path string, info os.FileInfo) {15 if strings.HasSuffix(path, ".go") {16 fmt.Println(path)17 }18 })19 if err != nil {20 log.Fatal(err)21 }22}

Full Screen

Full Screen

forEachFile

Using AI Code Generation

copy

Full Screen

1import (2type report struct {3}4func (r *report) forEachFile(path string, f os.FileInfo, err error) error {5 if err != nil {6 }7 if f.IsDir() {8 }9 if strings.HasSuffix(f.Name(), r.suffix) {10 }11}12func main() {13 r := &report{suffix: ".go"}14 err := filepath.Walk("./", r.forEachFile)15 if err != nil {16 log.Fatal(err)17 }18 fmt.Printf("Total number of files with suffix %q: %d\n", r.suffix, r.count)19}

Full Screen

Full Screen

forEachFile

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 report := &Report{}4 err := report.forEachFile(".", func(path string, info os.FileInfo, err error) error {5 if err != nil {6 }7 fmt.Println(path)8 })9 if err != nil {10 log.Fatal(err)11 }12}13import (14func main() {15 report := &Report{}16 err := report.forEachFile(".", func(path string, info os.FileInfo, err error) error {17 if err != nil {18 }19 if !info.IsDir() {20 }21 })22 if err != nil {23 log.Fatal(err)24 }25 fmt.Printf("%d files\n", numFiles)26}27import (28func main() {29 report := &Report{}30 err := report.forEachFile(".", func(path string, info os.FileInfo, err error) error {31 if err != nil {32 }33 if !info.IsDir() && filepath.Ext(path) == ".go" {34 }35 })36 if err != nil {37 log.Fatal(err)38 }39 fmt.Printf("%d files\n", numFiles)40}41import (42func main()

Full Screen

Full Screen

forEachFile

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := new(report)4 file, err := os.Create("report.txt")5 if err != nil {6 log.Fatal(err)7 }8 defer file.Close()9 r.forEachFile(file, "C:/Users/Pranav/Desktop/")10}11type report struct{}12func (r *report) forEachFile(file *os.File, root string) error {13 err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {14 if err != nil {15 }16 if info.IsDir() {17 }18 fmt.Fprintln(file, info.Name())19 })20}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful