1+ import { TeamcityEvent , TestResult , TestStarted , TestSuiteStarted } from '../ProblemMatcher' ;
2+ import { capitalize } from '../utils' ;
3+
4+ export class PHPUnitFixer {
5+ static fixDetails ( results = new Map < string , TestResult > ( ) , testResult : TestResult & {
6+ name : string ,
7+ locationHint ?: string ,
8+ file ?: string ,
9+ details ?: Array < { file : string , line : number } > ,
10+ } ) {
11+ if ( testResult . details && testResult . file ) {
12+ return testResult ;
13+ }
14+
15+ const result = Array . from ( results . values ( ) ) . reverse ( ) . find ( ( result ) => {
16+ return [ TeamcityEvent . testSuiteStarted , TeamcityEvent . testStarted ] . includes ( result . event ) ;
17+ } ) as ( TestSuiteStarted | TestStarted | undefined ) ;
18+
19+ if ( ! result ) {
20+ return testResult ;
21+ }
22+
23+ const file = result . file ! ;
24+ if ( ! testResult . file ) {
25+ testResult . file = file ;
26+ }
27+
28+ if ( ! testResult . details ) {
29+ testResult . details = [ { file : file , line : 1 } ] ;
30+ }
31+
32+ if ( ! testResult . locationHint ) {
33+ const locationHint = result . locationHint ?. split ( '::' ) . slice ( 0 , 2 ) . join ( '::' ) ;
34+ testResult . locationHint = [ locationHint , testResult . name ]
35+ . filter ( value => ! ! value )
36+ . join ( '::' ) ;
37+ }
38+
39+ return testResult ;
40+ }
41+ }
42+
43+ class Str {
44+ static prefix = '__pest_evaluable_' ;
45+
46+ static evaluable ( code : string ) {
47+ return this . prefix + code . replace ( / _ / g, '__' ) . replace ( / \s / g, '_' ) . replace ( / [ ^ a - z A - Z 0 - 9 _ \u0080 - \uFFFF ] / g, '_' ) ;
48+ }
49+ }
50+
51+ export class PestV1Fixer {
52+ static fixLocationHint ( locationHint : string ) {
53+ return this . fixDataSet ( / ^ t e s t s \/ / . test ( locationHint ) ? locationHint : locationHint . substring ( locationHint . lastIndexOf ( 'tests/' ) ) ) ;
54+ }
55+
56+ static fixFlowId ( results = new Map < string , TestResult > ( ) , testResult ?: TestResult ) {
57+ if ( ! testResult ) {
58+ return testResult ;
59+ }
60+
61+ const events = [ TeamcityEvent . testStarted , TeamcityEvent . testFailed , TeamcityEvent . testIgnored ] ;
62+ if ( 'event' in testResult && ! events . includes ( testResult . event ) || ( testResult as any ) . flowId ) {
63+ return testResult ;
64+ }
65+
66+ const result = Array . from ( results . values ( ) ) . reverse ( ) . find ( ( result : TestResult ) => {
67+ if ( testResult . event !== TeamcityEvent . testStarted ) {
68+ return result . event === TeamcityEvent . testStarted && ( result as any ) . name === ( testResult as any ) . name ;
69+ }
70+
71+ const matched = ( testResult as any ) . id ?. match ( / \( (?< id > .+ ) \) / ) ;
72+
73+ return matched && ( result as any ) . id === matched . groups ?. id . replace ( / \\ / g, '/' ) + 'Test' ;
74+ } ) ;
75+
76+ ( testResult as any ) . flowId = ( result as any ) ?. flowId ;
77+
78+ return testResult ;
79+ }
80+
81+ private static fixDataSet ( locationHint : string ) {
82+ const matched = locationHint . match ( / (?< description > .+ ) \s w i t h \s \( ' (?< data > .+ ) ' \) / ) ;
83+
84+ return matched && matched . groups ?. description
85+ ? `${ matched . groups . description } with data set "(\'${ matched . groups . data } \')"`
86+ : locationHint ;
87+ }
88+ }
89+
90+ export class PestV2Fixer {
91+ static fixId ( location : string , name : string ) {
92+ return this . hasPrefix ( name ) ? name : location ;
93+ }
94+
95+ static isEqualsPestV2DataSetId ( result : TestResult , testItemId : string ) {
96+ if ( ! ( 'id' in result ) || ! this . hasPrefix ( result . id ) ) {
97+ return false ;
98+ }
99+
100+ let [ classFQN , method ] = testItemId . split ( '::' ) ;
101+ classFQN = capitalize ( classFQN . replace ( / \/ / g, '\\' ) . replace ( / \. p h p $ / , '' ) ) ;
102+
103+ return [ classFQN , this . methodName ( method ) ] . join ( '::' ) === result . id ;
104+ }
105+
106+ private static hasPrefix ( id ?: string ) {
107+ return id && new RegExp ( Str . prefix ) . test ( id ) ;
108+ }
109+
110+ static methodName ( methodName : string ) {
111+ methodName = methodName . replace ( / \{ @ \* } / g, '*/' ) ;
112+ const matched = methodName . match ( / (?< method > .* ) \s w i t h \s d a t a \s s e t \s (?< dataset > .+ ) / ) ;
113+ let dataset = '' ;
114+ if ( matched ) {
115+ methodName = matched . groups ! . method ;
116+ dataset = matched . groups ! . dataset . replace ( / \| ' / g, '\'' ) ;
117+ }
118+
119+ return Str . evaluable ( methodName ) + dataset ;
120+ }
121+ }
0 commit comments