1
1
import { vol } from 'memfs' ;
2
- import { type AuditOutputs , auditOutputsSchema } from '@code-pushup/models' ;
2
+ import {
3
+ type AuditOutputs ,
4
+ DEFAULT_PERSIST_CONFIG ,
5
+ DEFAULT_PERSIST_FILENAME ,
6
+ DEFAULT_PERSIST_FORMAT ,
7
+ DEFAULT_PERSIST_OUTPUT_DIR ,
8
+ DEFAULT_PERSIST_SKIP_REPORT ,
9
+ auditOutputsSchema ,
10
+ } from '@code-pushup/models' ;
3
11
import {
4
12
ISO_STRING_REGEXP ,
5
13
MEMFS_VOLUME ,
@@ -8,6 +16,7 @@ import {
8
16
MINIMAL_RUNNER_FUNCTION_MOCK ,
9
17
osAgnosticPath ,
10
18
} from '@code-pushup/test-utils' ;
19
+ import * as utils from '@code-pushup/utils' ;
11
20
import {
12
21
type RunnerResult ,
13
22
executePluginRunner ,
@@ -38,10 +47,14 @@ describe('executeRunnerConfig', () => {
38
47
} ,
39
48
MEMFS_VOLUME ,
40
49
) ;
50
+
51
+ vi . spyOn ( utils , 'executeProcess' ) ;
41
52
} ) ;
42
53
43
54
it ( 'should execute valid runner config' , async ( ) => {
44
- const runnerResult = await executeRunnerConfig ( MINIMAL_RUNNER_CONFIG_MOCK ) ;
55
+ const runnerResult = await executeRunnerConfig ( MINIMAL_RUNNER_CONFIG_MOCK , {
56
+ persist : DEFAULT_PERSIST_CONFIG ,
57
+ } ) ;
45
58
46
59
// data sanity
47
60
expect ( ( runnerResult . audits as AuditOutputs ) [ 0 ] ?. slug ) . toBe ( 'node-version' ) ;
@@ -50,23 +63,42 @@ describe('executeRunnerConfig', () => {
50
63
51
64
// schema validation
52
65
expect ( ( ) => auditOutputsSchema . parse ( runnerResult . audits ) ) . not . toThrow ( ) ;
53
- } ) ;
54
66
55
- it ( 'should use outputTransform when provided' , async ( ) => {
56
- const runnerResult = await executeRunnerConfig ( {
67
+ // executed process configuration
68
+ expect ( utils . executeProcess ) . toHaveBeenCalledWith < [ utils . ProcessConfig ] > ( {
57
69
command : 'node' ,
58
70
args : [ '-v' ] ,
59
- outputFile : 'output.json' ,
60
- outputTransform : ( outputs : unknown ) : Promise < AuditOutputs > =>
61
- Promise . resolve ( [
62
- {
63
- slug : ( outputs as AuditOutputs ) [ 0 ] ! . slug ,
64
- score : 0.3 ,
65
- value : 16 ,
66
- displayValue : '16.0.0' ,
67
- } ,
68
- ] ) ,
71
+ env : expect . objectContaining ( {
72
+ CP_PERSIST_OUTPUT_DIR : DEFAULT_PERSIST_OUTPUT_DIR ,
73
+ CP_PERSIST_FILENAME : DEFAULT_PERSIST_FILENAME ,
74
+ CP_PERSIST_FORMAT : DEFAULT_PERSIST_FORMAT . join ( ',' ) ,
75
+ CP_PERSIST_SKIP_REPORTS : ` ${ DEFAULT_PERSIST_SKIP_REPORT } ` ,
76
+ } ) ,
77
+ observer : {
78
+ onStdout : expect . any ( Function ) ,
79
+ onStderr : expect . any ( Function ) ,
80
+ } ,
69
81
} ) ;
82
+ } ) ;
83
+
84
+ it ( 'should use outputTransform when provided' , async ( ) => {
85
+ const runnerResult = await executeRunnerConfig (
86
+ {
87
+ command : 'node' ,
88
+ args : [ '-v' ] ,
89
+ outputFile : 'output.json' ,
90
+ outputTransform : ( outputs : unknown ) : Promise < AuditOutputs > =>
91
+ Promise . resolve ( [
92
+ {
93
+ slug : ( outputs as AuditOutputs ) [ 0 ] ! . slug ,
94
+ score : 0.3 ,
95
+ value : 16 ,
96
+ displayValue : '16.0.0' ,
97
+ } ,
98
+ ] ) ,
99
+ } ,
100
+ { persist : DEFAULT_PERSIST_CONFIG } ,
101
+ ) ;
70
102
const auditOutputs = runnerResult . audits as AuditOutputs ;
71
103
72
104
expect ( auditOutputs [ 0 ] ?. slug ) . toBe ( 'node-version' ) ;
@@ -75,13 +107,16 @@ describe('executeRunnerConfig', () => {
75
107
76
108
it ( 'should throw if outputTransform throws' , async ( ) => {
77
109
await expect (
78
- executeRunnerConfig ( {
79
- command : 'node' ,
80
- args : [ '-v' ] ,
81
- outputFile : 'output.json' ,
82
- outputTransform : ( ) =>
83
- Promise . reject ( new Error ( 'Error: outputTransform has failed.' ) ) ,
84
- } ) ,
110
+ executeRunnerConfig (
111
+ {
112
+ command : 'node' ,
113
+ args : [ '-v' ] ,
114
+ outputFile : 'output.json' ,
115
+ outputTransform : ( ) =>
116
+ Promise . reject ( new Error ( 'Error: outputTransform has failed.' ) ) ,
117
+ } ,
118
+ { persist : DEFAULT_PERSIST_CONFIG } ,
119
+ ) ,
85
120
) . rejects . toThrow ( 'Error: outputTransform has failed.' ) ;
86
121
} ) ;
87
122
} ) ;
@@ -90,6 +125,7 @@ describe('executeRunnerFunction', () => {
90
125
it ( 'should execute a valid runner function' , async ( ) => {
91
126
const runnerResult : RunnerResult = await executeRunnerFunction (
92
127
MINIMAL_RUNNER_FUNCTION_MOCK ,
128
+ { persist : DEFAULT_PERSIST_CONFIG } ,
93
129
) ;
94
130
const auditOutputs = runnerResult . audits as AuditOutputs ;
95
131
@@ -102,14 +138,12 @@ describe('executeRunnerFunction', () => {
102
138
} ) ;
103
139
104
140
it ( 'should throw if the runner function throws' , async ( ) => {
105
- const nextSpy = vi . fn ( ) ;
106
141
await expect (
107
142
executeRunnerFunction (
108
143
( ) => Promise . reject ( new Error ( 'Error: Runner has failed.' ) ) ,
109
- nextSpy ,
144
+ { persist : DEFAULT_PERSIST_CONFIG } ,
110
145
) ,
111
146
) . rejects . toThrow ( 'Error: Runner has failed.' ) ;
112
- expect ( nextSpy ) . not . toHaveBeenCalled ( ) ;
113
147
} ) ;
114
148
115
149
it ( 'should throw with an invalid runner type' , async ( ) => {
@@ -122,7 +156,9 @@ describe('executeRunnerFunction', () => {
122
156
123
157
describe ( 'executePluginRunner' , ( ) => {
124
158
it ( 'should execute a valid plugin config' , async ( ) => {
125
- const pluginResult = await executePluginRunner ( MINIMAL_PLUGIN_CONFIG_MOCK ) ;
159
+ const pluginResult = await executePluginRunner ( MINIMAL_PLUGIN_CONFIG_MOCK , {
160
+ persist : DEFAULT_PERSIST_CONFIG ,
161
+ } ) ;
126
162
expect ( pluginResult . audits [ 0 ] ?. slug ) . toBe ( 'node-version' ) ;
127
163
} ) ;
128
164
@@ -141,14 +177,17 @@ describe('executePluginRunner', () => {
141
177
) ;
142
178
143
179
await expect (
144
- executePluginRunner ( {
145
- ...MINIMAL_PLUGIN_CONFIG_MOCK ,
146
- runner : {
147
- command : 'node' ,
148
- args : [ '-v' ] ,
149
- outputFile : 'output.json' ,
180
+ executePluginRunner (
181
+ {
182
+ ...MINIMAL_PLUGIN_CONFIG_MOCK ,
183
+ runner : {
184
+ command : 'node' ,
185
+ args : [ '-v' ] ,
186
+ outputFile : 'output.json' ,
187
+ } ,
150
188
} ,
151
- } ) ,
189
+ { persist : DEFAULT_PERSIST_CONFIG } ,
190
+ ) ,
152
191
) . resolves . toStrictEqual ( {
153
192
duration : expect . any ( Number ) ,
154
193
date : expect . any ( String ) ,
@@ -164,16 +203,19 @@ describe('executePluginRunner', () => {
164
203
165
204
it ( 'should yield audit outputs for a valid runner function' , async ( ) => {
166
205
await expect (
167
- executePluginRunner ( {
168
- ...MINIMAL_PLUGIN_CONFIG_MOCK ,
169
- runner : ( ) => [
170
- {
171
- slug : 'node-version' ,
172
- score : 0.3 ,
173
- value : 16 ,
174
- } ,
175
- ] ,
176
- } ) ,
206
+ executePluginRunner (
207
+ {
208
+ ...MINIMAL_PLUGIN_CONFIG_MOCK ,
209
+ runner : ( ) => [
210
+ {
211
+ slug : 'node-version' ,
212
+ score : 0.3 ,
213
+ value : 16 ,
214
+ } ,
215
+ ] ,
216
+ } ,
217
+ { persist : DEFAULT_PERSIST_CONFIG } ,
218
+ ) ,
177
219
) . resolves . toStrictEqual ( {
178
220
duration : expect . any ( Number ) ,
179
221
date : expect . any ( String ) ,
0 commit comments