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