|
1 |
| -importfsfrom"fs"; |
| 1 | +/* eslint-disable no-console -- Logging is sort of the whole point here */ |
| 2 | +import*asfsfrom"fs/promises"; |
2 | 3 | importtype{
|
3 | 4 | FullConfig,
|
4 | 5 | Suite,
|
5 | 6 | TestCase,
|
6 | 7 | TestResult,
|
7 | 8 | FullResult,
|
8 | 9 | Reporter,
|
| 10 | +TestError, |
9 | 11 | }from"@playwright/test/reporter";
|
10 | 12 | importaxiosfrom"axios";
|
| 13 | +importtype{Writable}from"stream"; |
11 | 14 |
|
12 | 15 | classCoderReporterimplementsReporter{
|
| 16 | +config:FullConfig|null=null; |
| 17 | +testOutput=newMap<string,Array<[Writable,string]>>(); |
| 18 | +passedCount=0; |
| 19 | +failedTests:TestCase[]=[]; |
| 20 | +timedOutTests:TestCase[]=[]; |
| 21 | + |
13 | 22 | onBegin(config:FullConfig,suite:Suite){
|
14 |
| -// eslint-disable-next-line no-console -- Helpful for debugging |
15 |
| -console.log(`Starting the run with${suite.allTests().length} tests`); |
| 23 | +this.config=config; |
| 24 | +console.log(`==> Running${suite.allTests().length} tests`); |
16 | 25 | }
|
17 | 26 |
|
18 | 27 | onTestBegin(test:TestCase){
|
19 |
| -// eslint-disable-next-line no-console -- Helpful for debugging |
20 |
| -console.log(`Starting test${test.title}`); |
| 28 | +this.testOutput.set(test.id,[]); |
| 29 | +console.log(`==>Starting test${test.title}`); |
21 | 30 | }
|
22 | 31 |
|
23 |
| -onStdOut(chunk:string,test:TestCase,_:TestResult):void{ |
24 |
| -// eslint-disable-next-line no-console -- Helpful for debugging |
25 |
| -console.log( |
26 |
| -`[stdout][${test ?test.title :"unknown"}]:${chunk.replace( |
27 |
| -/\n$/g, |
28 |
| -"", |
29 |
| -)}`, |
30 |
| -); |
| 32 | +onStdOut(chunk:string,test?:TestCase,_?:TestResult):void{ |
| 33 | +if(!test){ |
| 34 | +for(constlineoffilteredServerLogLines(chunk)){ |
| 35 | +console.log(`[stdout]${line}`); |
| 36 | +} |
| 37 | +return; |
| 38 | +} |
| 39 | +this.testOutput.get(test.id)!.push([process.stdout,chunk]); |
31 | 40 | }
|
32 | 41 |
|
33 |
| -onStdErr(chunk:string,test:TestCase,_:TestResult):void{ |
34 |
| -// eslint-disable-next-line no-console -- Helpful for debugging |
35 |
| -console.log( |
36 |
| -`[stderr][${test ?test.title :"unknown"}]:${chunk.replace( |
37 |
| -/\n$/g, |
38 |
| -"", |
39 |
| -)}`, |
40 |
| -); |
| 42 | +onStdErr(chunk:string,test?:TestCase,_?:TestResult):void{ |
| 43 | +if(!test){ |
| 44 | +for(constlineoffilteredServerLogLines(chunk)){ |
| 45 | +console.error(`[stderr]${line}`); |
| 46 | +} |
| 47 | +return; |
| 48 | +} |
| 49 | +this.testOutput.get(test.id)!.push([process.stderr,chunk]); |
41 | 50 | }
|
42 | 51 |
|
43 | 52 | asynconTestEnd(test:TestCase,result:TestResult){
|
44 |
| -// eslint-disable-next-line no-console -- Helpful for debugging |
45 |
| -console.log(`Finished test${test.title}:${result.status}`); |
| 53 | +console.log(`==> Finished test${test.title}:${result.status}`); |
| 54 | + |
| 55 | +if(result.status==="passed"){ |
| 56 | +this.passedCount++; |
| 57 | +} |
| 58 | + |
| 59 | +if(result.status==="failed"){ |
| 60 | +this.failedTests.push(test); |
| 61 | +} |
| 62 | + |
| 63 | +if(result.status==="timedOut"){ |
| 64 | +this.timedOutTests.push(test); |
| 65 | +} |
| 66 | + |
| 67 | +constfsTestTitle=test.title.replaceAll(" ","-"); |
| 68 | +constoutputFile=`test-results/debug-pprof-goroutine-${fsTestTitle}.txt`; |
| 69 | +awaitexportDebugPprof(outputFile); |
46 | 70 |
|
47 | 71 | if(result.status!=="passed"){
|
48 |
| -// eslint-disable-next-line no-console -- Helpful for debugging |
49 |
| -console.log("errors",result.errors,"attachments",result.attachments); |
| 72 | +console.log(`Data from pprof has been saved to${outputFile}`); |
| 73 | +console.log("==> Output"); |
| 74 | +constoutput=this.testOutput.get(test.id)!; |
| 75 | +for(const[target,chunk]ofoutput){ |
| 76 | +target.write(`${chunk.replace(/\n$/g,"")}\n`); |
| 77 | +} |
| 78 | + |
| 79 | +if(result.errors.length>0){ |
| 80 | +console.log("==> Errors"); |
| 81 | +for(consterrorofresult.errors){ |
| 82 | +reportError(error); |
| 83 | +} |
| 84 | +} |
| 85 | + |
| 86 | +if(result.attachments.length>0){ |
| 87 | +console.log("==> Attachments"); |
| 88 | +for(constattachmentofresult.attachments){ |
| 89 | +console.log(attachment); |
| 90 | +} |
| 91 | +} |
50 | 92 | }
|
51 |
| -awaitexportDebugPprof(test.title); |
| 93 | +this.testOutput.delete(test.id); |
52 | 94 | }
|
53 | 95 |
|
54 | 96 | onEnd(result:FullResult){
|
55 |
| -// eslint-disable-next-line no-console -- Helpful for debugging |
56 |
| -console.log(`Finished the run:${result.status}`); |
| 97 | +console.log(`==> Tests${result.status}`); |
| 98 | +console.log(`${this.passedCount} passed`); |
| 99 | +if(this.failedTests.length>0){ |
| 100 | +console.log(`${this.failedTests.length} failed`); |
| 101 | +for(consttestofthis.failedTests){ |
| 102 | +console.log(`${test.location.file} ›${test.title}`); |
| 103 | +} |
| 104 | +} |
| 105 | +if(this.timedOutTests.length>0){ |
| 106 | +console.log(`${this.timedOutTests.length} timed out`); |
| 107 | +for(consttestofthis.timedOutTests){ |
| 108 | +console.log(`${test.location.file} ›${test.title}`); |
| 109 | +} |
| 110 | +} |
57 | 111 | }
|
58 | 112 | }
|
59 | 113 |
|
60 |
| -constexportDebugPprof=async(testName:string)=>{ |
61 |
| -consturl="http://127.0.0.1:6060/debug/pprof/goroutine?debug=1"; |
62 |
| -constoutputFile=`test-results/debug-pprof-goroutine-${testName}.txt`; |
| 114 | +constshouldPrintLine=(line:string)=> |
| 115 | +[" error=EOF","coderd: audit_log"].every((noise)=>!line.includes(noise)); |
63 | 116 |
|
64 |
| -awaitaxios |
65 |
| -.get(url) |
66 |
| -.then((response)=>{ |
67 |
| -if(response.status!==200){ |
68 |
| -thrownewError(`Error: Received status code${response.status}`); |
69 |
| -} |
| 117 | +constfilteredServerLogLines=(chunk:string):string[]=> |
| 118 | +chunk.trimEnd().split("\n").filter(shouldPrintLine); |
70 | 119 |
|
71 |
| -fs.writeFile(outputFile,response.data,(err)=>{ |
72 |
| -if(err){ |
73 |
| -thrownewError(`Error writing to${outputFile}:${err.message}`); |
74 |
| -}else{ |
75 |
| -// eslint-disable-next-line no-console -- Helpful for debugging |
76 |
| -console.log(`Data from${url} has been saved to${outputFile}`); |
77 |
| -} |
78 |
| -}); |
79 |
| -}) |
80 |
| -.catch((error)=>{ |
81 |
| -thrownewError(`Error:${error.message}`); |
82 |
| -}); |
| 120 | +constexportDebugPprof=async(outputFile:string)=>{ |
| 121 | +constresponse=awaitaxios.get( |
| 122 | +"http://127.0.0.1:6060/debug/pprof/goroutine?debug=1", |
| 123 | +); |
| 124 | +if(response.status!==200){ |
| 125 | +thrownewError(`Error: Received status code${response.status}`); |
| 126 | +} |
| 127 | + |
| 128 | +awaitfs.writeFile(outputFile,response.data); |
| 129 | +}; |
| 130 | + |
| 131 | +constreportError=(error:TestError)=>{ |
| 132 | +if(error.location){ |
| 133 | +console.log(`${error.location.file}:${error.location.line}:`); |
| 134 | +} |
| 135 | +if(error.snippet){ |
| 136 | +console.log(error.snippet); |
| 137 | +} |
| 138 | + |
| 139 | +if(error.message){ |
| 140 | +console.log(error.message); |
| 141 | +}else{ |
| 142 | +console.log(error); |
| 143 | +} |
83 | 144 | };
|
84 | 145 |
|
85 | 146 | // eslint-disable-next-line no-unused-vars -- Playwright config uses it
|
|