@@ -14,6 +14,7 @@ import {
14
14
versionBump ,
15
15
VSIX ,
16
16
LicenseProcessor ,
17
+ printAndValidatePackagedFiles ,
17
18
} from '../package' ;
18
19
import { ManifestPackage } from '../manifest' ;
19
20
import * as path from 'path' ;
@@ -88,6 +89,63 @@ function createManifest(extra: Partial<ManifestPackage> = {}): ManifestPackage {
88
89
} ;
89
90
}
90
91
92
+ const PROCESS_ERROR_MESSAGE = 'PROCESS ERROR' ;
93
+ async function testPrintAndValidatePackagedFiles ( files : IFile [ ] , cwd : string , manifest : ManifestPackage , options : IPackageOptions , errorExpected : boolean , warningExpected : boolean ) : Promise < void > {
94
+ const originalLogError = log . error ;
95
+ const originalLogWarn = log . warn ;
96
+ const originalProcessExit = process . exit ;
97
+ const warns : string [ ] = [ ] ;
98
+ const errors : string [ ] = [ ] ;
99
+ let exited = false ;
100
+ let errorThrown : string | undefined ;
101
+ log . error = ( message : string ) => errors . push ( message ) ;
102
+ log . warn = ( message : string ) => warns . push ( message ) ;
103
+ process . exit = ( ( ) => { exited = true ; throw Error ( PROCESS_ERROR_MESSAGE ) ; } ) as ( ) => never ;
104
+
105
+ try {
106
+ await printAndValidatePackagedFiles ( files , cwd , manifest , options ) ;
107
+ } catch ( e : any ) {
108
+ if ( e instanceof Error && e . message !== PROCESS_ERROR_MESSAGE ) {
109
+ errorThrown = e . message + '\n' + e . stack ;
110
+ }
111
+ } finally {
112
+ process . exit = originalProcessExit ;
113
+ log . error = originalLogError ;
114
+ log . warn = originalLogWarn ;
115
+ }
116
+
117
+ // Validate that the correct number of errors and warnings were thrown
118
+ const messages = [ ] ;
119
+
120
+ if ( errorExpected !== ! ! errors . length ) {
121
+ if ( errors . length ) {
122
+ messages . push ( ...errors ) ;
123
+ } else {
124
+ messages . push ( 'Expected an error' ) ;
125
+ }
126
+ }
127
+
128
+ if ( warningExpected !== ! ! warns . length ) {
129
+ if ( warns . length ) {
130
+ messages . push ( ...warns ) ;
131
+ } else {
132
+ messages . push ( 'Expected a warning' ) ;
133
+ }
134
+ }
135
+
136
+ if ( ! errorExpected && exited ) {
137
+ messages . push ( 'Process exited' ) ;
138
+ }
139
+
140
+ if ( ! errorExpected && ! ! errorThrown && ! exited ) {
141
+ messages . push ( 'Error thrown: ' + errorThrown ) ;
142
+ }
143
+
144
+ if ( messages . length ) {
145
+ throw new Error ( messages . join ( '\n' ) ) ;
146
+ }
147
+ }
148
+
91
149
describe ( 'collect' , function ( ) {
92
150
this . timeout ( 60000 ) ;
93
151
@@ -133,22 +191,55 @@ describe('collect', function () {
133
191
] ) ;
134
192
} ) ;
135
193
136
- it ( 'should include content of manifest.files' , async ( ) => {
194
+ it ( 'manifest.files' , async ( ) => {
137
195
const cwd = fixture ( 'manifestFiles' ) ;
138
196
const manifest = await readManifest ( cwd ) ;
139
197
const files = await collect ( manifest , { cwd } ) ;
140
198
const names = files . map ( f => f . path ) . sort ( ) ;
141
199
200
+ await testPrintAndValidatePackagedFiles ( files , cwd , manifest , { } , false , false ) ;
201
+
142
202
assert . deepStrictEqual ( names , [
143
203
'[Content_Types].xml' ,
144
204
'extension.vsixmanifest' ,
205
+ 'extension/LICENSE.txt' ,
145
206
'extension/foo/bar/hello.txt' ,
146
207
'extension/foo2/bar2/include.me' ,
147
208
'extension/foo3/bar3/hello.txt' ,
148
209
'extension/package.json' ,
149
210
] ) ;
150
211
} ) ;
151
212
213
+ it ( 'manifest.files unused-files-patterns check 1' , async ( ) => {
214
+ const cwd = fixture ( 'manifestFiles' ) ;
215
+ const manifest = await readManifest ( cwd ) ;
216
+
217
+ const manifestCopy = { ...manifest , files : [ ...manifest . files ?? [ ] , 'extension/foo/bar/bye.txt' ] } ;
218
+ const files = await collect ( manifestCopy , { cwd } ) ;
219
+
220
+ await testPrintAndValidatePackagedFiles ( files , cwd , manifestCopy , { } , true , false ) ;
221
+ } ) ;
222
+
223
+ it ( 'manifest.files unused-files-patterns check 2' , async ( ) => {
224
+ const cwd = fixture ( 'manifestFiles' ) ;
225
+ const manifest = await readManifest ( cwd ) ;
226
+
227
+ const manifestCopy = { ...manifest , files : [ ...manifest . files ?? [ ] , 'extension/fo' ] } ;
228
+ const files = await collect ( manifestCopy , { cwd } ) ;
229
+
230
+ await testPrintAndValidatePackagedFiles ( files , cwd , manifestCopy , { } , true , false ) ;
231
+ } ) ;
232
+
233
+ it ( 'manifest.files unused-files-patterns check 3' , async ( ) => {
234
+ const cwd = fixture ( 'manifestFiles' ) ;
235
+ const manifest = await readManifest ( cwd ) ;
236
+
237
+ const manifestCopy = { ...manifest , files : [ '**' ] } ;
238
+ const files = await collect ( manifestCopy , { cwd } ) ;
239
+
240
+ await testPrintAndValidatePackagedFiles ( files , cwd , manifestCopy , { } , false , false ) ;
241
+ } ) ;
242
+
152
243
it ( 'should ignore devDependencies' , ( ) => {
153
244
const cwd = fixture ( 'devDependencies' ) ;
154
245
return readManifest ( cwd )
0 commit comments