@@ -20,91 +20,91 @@ module.exports = function (genPath, options) {
20
20
if ( ! config ) return ;
21
21
22
22
const codecept = new Codecept ( config , { } ) ;
23
- codecept . init ( testsPath , ( err ) => {
24
- if ( ! config . gherkin ) {
25
- output . error ( 'Gherkin is not enabled in config. Run `codecept gherkin:init` to enable it' ) ;
26
- process . exit ( 1 ) ;
27
- }
28
- if ( ! config . gherkin . steps || ! config . gherkin . steps [ 0 ] ) {
29
- output . error ( 'No gherkin steps defined in config. Exiting' ) ;
30
- process . exit ( 1 ) ;
31
- }
32
- if ( ! config . gherkin . features ) {
33
- output . error ( 'No gherkin features defined in config. Exiting' ) ;
34
- process . exit ( 1 ) ;
35
- }
23
+ codecept . init ( testsPath ) ;
36
24
37
- const files = [ ] ;
38
- glob . sync ( config . gherkin . features , { cwd : global . codecept_dir } ) . forEach ( ( file ) => {
39
- if ( ! fsPath . isAbsolute ( file ) ) {
40
- file = fsPath . join ( global . codecept_dir , file ) ;
41
- }
42
- files . push ( fsPath . resolve ( file ) ) ;
43
- } ) ;
44
- output . print ( `Loaded ${ files . length } files` ) ;
25
+ if ( ! config . gherkin ) {
26
+ output . error ( 'Gherkin is not enabled in config. Run `codecept gherkin:init` to enable it' ) ;
27
+ process . exit ( 1 ) ;
28
+ }
29
+ if ( ! config . gherkin . steps || ! config . gherkin . steps [ 0 ] ) {
30
+ output . error ( 'No gherkin steps defined in config. Exiting' ) ;
31
+ process . exit ( 1 ) ;
32
+ }
33
+ if ( ! config . gherkin . features ) {
34
+ output . error ( 'No gherkin features defined in config. Exiting' ) ;
35
+ process . exit ( 1 ) ;
36
+ }
45
37
46
- let newSteps = [ ] ;
38
+ const files = [ ] ;
39
+ glob . sync ( config . gherkin . features , { cwd : global . codecept_dir } ) . forEach ( ( file ) => {
40
+ if ( ! fsPath . isAbsolute ( file ) ) {
41
+ file = fsPath . join ( global . codecept_dir , file ) ;
42
+ }
43
+ files . push ( fsPath . resolve ( file ) ) ;
44
+ } ) ;
45
+ output . print ( `Loaded ${ files . length } files` ) ;
47
46
48
- const parseSteps = ( steps ) => {
49
- const newSteps = [ ] ;
50
- let currentKeyword = '' ;
51
- for ( const step of steps ) {
52
- if ( step . keyword . trim ( ) === 'And' ) {
53
- if ( ! currentKeyword ) throw new Error ( `There is no active keyword for step '${ step . text } '` ) ;
54
- step . keyword = currentKeyword ;
55
- }
56
- currentKeyword = step . keyword ;
57
- try {
58
- matchStep ( step . text ) ;
59
- } catch ( err ) {
60
- let stepLine = step . text
61
- . replace ( / \" ( .* ?) \" / g, '{string}' )
62
- . replace ( / ( \d + \. \d + ) / , '{float}' )
63
- . replace ( / ( \d + ) / , ' {int} ' ) ;
64
- stepLine = Object . assign ( stepLine , { type : step . keyword . trim ( ) , location : step . location } ) ;
65
- newSteps . push ( stepLine ) ;
66
- }
67
- }
68
- return newSteps ;
69
- } ;
47
+ let newSteps = [ ] ;
70
48
71
- const parseFile = ( file ) => {
72
- const ast = parser . parse ( fs . readFileSync ( file ) . toString ( ) ) ;
73
- for ( const child of ast . feature . children ) {
74
- if ( child . type === 'ScenarioOutline' ) continue ; // skip scenario outline
75
- newSteps = newSteps . concat ( parseSteps ( child . steps ) . map ( ( step ) => {
76
- return Object . assign ( step , { file : file . replace ( global . codecept_dir , '' ) . slice ( 1 ) } ) ;
77
- } ) ) ;
49
+ const parseSteps = ( steps ) => {
50
+ const newSteps = [ ] ;
51
+ let currentKeyword = '' ;
52
+ for ( const step of steps ) {
53
+ if ( step . keyword . trim ( ) === 'And' ) {
54
+ if ( ! currentKeyword ) throw new Error ( `There is no active keyword for step ' ${ step . text } '` ) ;
55
+ step . keyword = currentKeyword ;
78
56
}
79
- } ;
80
-
81
- files . forEach ( file => parseFile ( file ) ) ;
57
+ currentKeyword = step . keyword ;
58
+ try {
59
+ matchStep ( step . text ) ;
60
+ } catch ( err ) {
61
+ let stepLine = step . text
62
+ . replace ( / \" ( .* ?) \" / g, '{string}' )
63
+ . replace ( / ( \d + \. \d + ) / , '{float}' )
64
+ . replace ( / ( \d + ) / , ' {int} ' ) ;
65
+ stepLine = Object . assign ( stepLine , { type : step . keyword . trim ( ) , location : step . location } ) ;
66
+ newSteps . push ( stepLine ) ;
67
+ }
68
+ }
69
+ return newSteps ;
70
+ } ;
82
71
83
- let stepFile = config . gherkin . steps [ 0 ] ;
84
- if ( ! fsPath . isAbsolute ( stepFile ) ) {
85
- stepFile = fsPath . join ( global . codecept_dir , stepFile ) ;
72
+ const parseFile = ( file ) => {
73
+ const ast = parser . parse ( fs . readFileSync ( file ) . toString ( ) ) ;
74
+ for ( const child of ast . feature . children ) {
75
+ if ( child . type === 'ScenarioOutline' ) continue ; // skip scenario outline
76
+ newSteps = newSteps . concat ( parseSteps ( child . steps ) . map ( ( step ) => {
77
+ return Object . assign ( step , { file : file . replace ( global . codecept_dir , '' ) . slice ( 1 ) } ) ;
78
+ } ) ) ;
86
79
}
80
+ } ;
81
+
82
+ files . forEach ( file => parseFile ( file ) ) ;
83
+
84
+ let stepFile = config . gherkin . steps [ 0 ] ;
85
+ if ( ! fsPath . isAbsolute ( stepFile ) ) {
86
+ stepFile = fsPath . join ( global . codecept_dir , stepFile ) ;
87
+ }
87
88
88
- const snippets = newSteps
89
- . filter ( ( value , index , self ) => self . indexOf ( value ) === index )
90
- . map ( ( step ) => {
91
- return `
89
+ const snippets = newSteps
90
+ . filter ( ( value , index , self ) => self . indexOf ( value ) === index )
91
+ . map ( ( step ) => {
92
+ return `
92
93
${ step . type } ('${ step } ', () => {
93
94
// From "${ step . file } " ${ JSON . stringify ( step . location ) }
94
95
throw new Error('Not implemented yet');
95
96
});` ;
96
- } ) ;
97
+ } ) ;
97
98
98
- if ( ! snippets . length ) {
99
- output . print ( 'No new snippets found' ) ;
100
- return ;
101
- }
102
- output . success ( `Snippets generated: ${ snippets . length } ` ) ;
103
- output . print ( snippets . join ( '\n' ) ) ;
99
+ if ( ! snippets . length ) {
100
+ output . print ( 'No new snippets found' ) ;
101
+ return ;
102
+ }
103
+ output . success ( `Snippets generated: ${ snippets . length } ` ) ;
104
+ output . print ( snippets . join ( '\n' ) ) ;
104
105
105
- if ( ! options . dryRun ) {
106
- output . success ( `Snippets added to ${ output . colors . bold ( stepFile ) } ` ) ;
107
- fs . writeFileSync ( stepFile , fs . readFileSync ( stepFile ) . toString ( ) + snippets . join ( '\n' ) + '\n' ) ; // eslint-disable-line
108
- }
109
- } ) ;
106
+ if ( ! options . dryRun ) {
107
+ output . success ( `Snippets added to ${ output . colors . bold ( stepFile ) } ` ) ;
108
+ fs . writeFileSync ( stepFile , fs . readFileSync ( stepFile ) . toString ( ) + snippets . join ( '\n' ) + '\n' ) ; // eslint-disable-line
109
+ }
110
110
} ;
0 commit comments