1
1
import chai from 'chai' ;
2
- import { RocketCli } from '../src/RocketCli.js' ;
3
- import path from 'path' ;
4
- import globby from 'globby' ;
5
- import fs , { move , remove } from 'fs-extra' ;
6
- import prettier from 'prettier' ;
7
2
import { fileURLToPath } from 'url' ;
8
- import { existsSync } from 'fs' ;
9
-
10
- const { expect } = chai ;
11
-
12
- let fixtureDir = '' ;
3
+ import path from 'path' ;
4
+ import { setupTestCli } from './test-helpers.js' ;
13
5
14
- export function setFixtureDir ( importMetaUrl ) {
15
- fixtureDir = path . dirname ( fileURLToPath ( importMetaUrl ) ) ;
6
+ export function prepareTestCli ( importMetaUrl ) {
7
+ const dir = path . dirname ( fileURLToPath ( importMetaUrl ) ) ;
8
+ return ( cwd , cliOptions = [ 'build' ] , options = { } ) => setupTestCli ( cwd , cliOptions , options , dir ) ;
16
9
}
17
10
18
- /**
19
- * @typedef {object } readOutputOptions
20
- * @property {boolean } stripToBody
21
- * @property {boolean } stripStartEndWhitespace
22
- * @property {boolean } stripScripts
23
- * @property {boolean } formatHtml
24
- * @property {boolean } replaceImageHashes
25
- * @property {start|build } type
26
- */
11
+ const { expect } = chai ;
27
12
28
13
/**
29
14
* @param {function } method
@@ -44,163 +29,3 @@ export async function expectThrowsAsync(method, { errorMatch, errorMessage } = {
44
29
expect ( error . message ) . to . equal ( errorMessage ) ;
45
30
}
46
31
}
47
-
48
- export async function readOutput (
49
- cli ,
50
- fileName ,
51
- {
52
- stripToBody = false ,
53
- stripStartEndWhitespace = true ,
54
- stripScripts = false ,
55
- formatHtml = false ,
56
- type = 'build' ,
57
- replaceImageHashes = false ,
58
- } = { } ,
59
- ) {
60
- if ( ! cli || ! cli . config ) {
61
- throw new Error ( `No valid cli provided to readOutput - you passed a ${ typeof cli } : ${ cli } ` ) ;
62
- }
63
-
64
- const outputDir =
65
- type === 'bootstrap'
66
- ? path . join ( cli . config . outputDir , '..' )
67
- : type === 'build'
68
- ? cli . config . outputDir
69
- : cli . config . outputDevDir ;
70
-
71
- let text = await fs . promises . readFile ( path . join ( outputDir , fileName ) ) ;
72
- text = text . toString ( ) ;
73
- if ( stripToBody ) {
74
- const bodyOpenTagEnd = text . indexOf ( '>' , text . indexOf ( '<body' ) + 1 ) + 1 ;
75
- const bodyCloseTagStart = text . indexOf ( '</body>' ) ;
76
- text = text . substring ( bodyOpenTagEnd , bodyCloseTagStart ) ;
77
- }
78
- if ( stripScripts ) {
79
- const scriptOpenTagEnd = text . indexOf ( '<script>' ) ;
80
- const scriptCloseTagStart = text . indexOf ( '</script>' , scriptOpenTagEnd ) + 9 ;
81
- text = text . substring ( 0 , scriptOpenTagEnd ) + text . substring ( scriptCloseTagStart ) ;
82
- }
83
- if ( replaceImageHashes ) {
84
- text = text . replace ( / \/ i m a g e s \/ ( [ a - z 0 - 9 ] + ) - / g, '/images/__HASH__-' ) ;
85
- }
86
- if ( formatHtml ) {
87
- text = prettier . format ( text , { parser : 'html' , printWidth : 100 } ) ;
88
- }
89
- if ( stripStartEndWhitespace ) {
90
- text = text . trim ( ) ;
91
- }
92
- return text ;
93
- }
94
-
95
- export async function getfixtureExpectedFiles ( pathToDir ) {
96
- const cwd = path . join ( fixtureDir , pathToDir ) ;
97
- const paths = await globby ( '**/*' , { cwd, absolute : true , dot : true } ) ;
98
- return paths ;
99
- }
100
-
101
- export async function execute ( pathToConfig , { type = 'start' , captureLog = false } = { } ) {
102
- let log = [ ] ;
103
- const origLog = console . log ;
104
- if ( captureLog ) {
105
- console . log = ( ...args ) => {
106
- log = [ ...log , ...args ] ;
107
- } ;
108
- }
109
-
110
- const configFile = path . join ( fixtureDir , pathToConfig . split ( '/' ) . join ( path . sep ) ) ;
111
- const configFileDir = path . dirname ( configFile ) ;
112
-
113
- const cli = new RocketCli ( {
114
- argv : [ type , '--config-file' , configFile ] ,
115
- } ) ;
116
-
117
- await cli . setup ( ) ;
118
- cli . config . outputDevDir = path . join ( configFileDir , '__output-dev' ) ;
119
- cli . config . devServer . open = false ;
120
- cli . config . devServer . port = 8080 ;
121
- cli . config . watch = false ;
122
- cli . config . outputDir = path . join ( configFileDir , '__output' ) ;
123
-
124
- await fs . emptyDir ( cli . config . outputDevDir ) ;
125
- await fs . emptyDir ( cli . config . outputDir ) ;
126
-
127
- await cli . run ( ) ;
128
-
129
- /**
130
- * @param {* } cli
131
- * @param {string } fileName
132
- * @param {readOutputOptions } options
133
- */
134
- async function readOutput2 ( fileName , options = { } ) {
135
- options . type = type ;
136
- return readOutput ( cli , fileName , options ) ;
137
- }
138
-
139
- function outputExists ( fileName ) {
140
- const outputDir = type === 'build' ? cli . config . outputDir : cli . config . outputDevDir ;
141
- const filePath = path . join ( outputDir , fileName ) ;
142
-
143
- return fs . existsSync ( filePath ) ;
144
- }
145
-
146
- if ( captureLog ) {
147
- console . log = origLog ;
148
- }
149
- return { log, readOutput : readOutput2 , cli, outputExists } ;
150
- }
151
-
152
- export async function executeBootstrap ( pathToDir ) {
153
- const configFileDir = path . join ( fixtureDir , pathToDir . split ( '/' ) . join ( path . sep ) ) ;
154
- const cli = new RocketCli ( { argv : [ 'bootstrap' ] } ) ;
155
-
156
- await cli . setup ( ) ;
157
- cli . config . outputDevDir = path . join ( configFileDir , '__output-dev' ) ;
158
- cli . config . devServer . open = false ;
159
- cli . config . devServer . port = 8080 ;
160
- cli . config . watch = false ;
161
- cli . config . outputDir = path . join ( configFileDir , '__output' ) ;
162
-
163
- await fs . emptyDir ( configFileDir ) ;
164
- await cli . run ( ) ;
165
-
166
- return { cli } ;
167
- }
168
-
169
- export async function executeUpgrade ( pathToConfig ) {
170
- const configFile = path . join ( fixtureDir , pathToConfig . split ( '/' ) . join ( path . sep ) ) ;
171
- const cli = new RocketCli ( {
172
- argv : [ 'upgrade' , '--config-file' , configFile ] ,
173
- } ) ;
174
- await cli . setup ( ) ;
175
-
176
- // restore from backup if available - in cases the test did stop in the middle
177
- if ( cli . config . _inputDirCwdRelative ) {
178
- const backupDir = path . join ( cli . config . _inputDirCwdRelative , '..' , 'docs_backup' ) ;
179
- if ( existsSync ( backupDir ) ) {
180
- await remove ( cli . config . _inputDirCwdRelative ) ;
181
- await move ( backupDir , cli . config . _inputDirCwdRelative ) ;
182
- }
183
- }
184
- await cli . run ( ) ;
185
- return {
186
- cli,
187
- fileExists : fileName => {
188
- const outputDir = cli . config . _inputDirCwdRelative ;
189
- return fs . existsSync ( path . join ( outputDir , fileName ) ) ;
190
- } ,
191
- readFile : async fileName => {
192
- // TODO: use readOutput once it's changed to read full file paths
193
- const filePath = path . join ( cli . config . _inputDirCwdRelative , fileName ) ;
194
- const text = await fs . promises . readFile ( filePath ) ;
195
- return text . toString ( ) ;
196
- } ,
197
- } ;
198
- }
199
-
200
- export function trimWhiteSpace ( inString ) {
201
- return inString
202
- . split ( '\n' )
203
- . map ( line => line . trim ( ) )
204
- . filter ( line => line )
205
- . join ( '\n' ) ;
206
- }
0 commit comments