@@ -183,6 +183,43 @@ async function failWriteSucceed({ file, watchedFile }) {
183183
184184tmpdir . refresh ( ) ;
185185
186+ function createGlobFileStructure ( nameOfTheDir ) {
187+ const rootDir = tmpdir . resolve ( nameOfTheDir ) ;
188+ mkdirSync ( rootDir ) ;
189+
190+ const rootDirGlob = path . resolve ( rootDir , '**/*.js' ) ;
191+ const directory1 = path . join ( rootDir , 'directory1' ) ;
192+ const directory2 = path . join ( rootDir , 'directory2' ) ;
193+
194+ mkdirSync ( directory1 ) ;
195+ mkdirSync ( directory2 ) ;
196+
197+ const tmpJsFile1 = createTmpFile ( '' , '.js' , directory1 ) ;
198+ const tmpJsFile2 = createTmpFile ( '' , '.js' , directory1 ) ;
199+ const tmpJsFile3 = createTmpFile ( '' , '.js' , directory2 ) ;
200+ const tmpJsFile4 = createTmpFile ( '' , '.js' , directory2 ) ;
201+ const tmpJsFile5 = createTmpFile ( '' , '.js' , directory2 ) ;
202+
203+ const mainJsFile = createTmpFile ( 'console.log(\'running\')' , '.js' , rootDir ) ;
204+ const watchedFiles = [ tmpJsFile1 , tmpJsFile2 , tmpJsFile3 , tmpJsFile4 , tmpJsFile5 ] ;
205+
206+
207+ return { rootDir, rootDirGlob, mainJsFile, watchedFiles } ;
208+ }
209+
210+ function expectRepeatedCompletes ( mainJsFile , n ) {
211+ const expectedStdout = [ ] ;
212+ for ( let i = 0 ; i < n ; i ++ ) {
213+ if ( i !== 0 ) {
214+ expectedStdout . push ( `Restarting ${ inspect ( ( mainJsFile ) ) } ` ) ;
215+ }
216+ expectedStdout . push ( 'running' ) ;
217+ expectedStdout . push ( `Completed running ${ inspect ( mainJsFile ) } . Waiting for file changes before restarting...` ) ;
218+ }
219+ return expectedStdout ;
220+ }
221+
222+
186223describe ( 'watch mode' , { concurrency : ! process . env . TEST_PARALLEL , timeout : 60_000 } , ( ) => {
187224 it ( 'should watch changes to a file' , async ( ) => {
188225 const file = createTmpFile ( ) ;
@@ -937,48 +974,74 @@ process.on('message', (message) => {
937974 } ) ;
938975
939976 it ( 'should watch files from a given glob pattern --watch-path=./**/*.js' , async ( ) => {
977+ const {
978+ rootDirGlob,
979+ mainJsFile,
980+ watchedFiles,
981+ } = createGlobFileStructure ( 'globtestdir-1' ) ;
982+
983+ const args = [ '--watch-path' , rootDirGlob , mainJsFile ] ;
940984
941- const tmpDirForGlobTest = tmpdir . resolve ( 'glob-test-dir' ) ;
942- mkdirSync ( tmpDirForGlobTest ) ;
985+ const { stderr, stdout } = await runWriteSucceed ( {
986+ args,
987+ watchedFile : watchedFiles ,
988+ } ) ;
943989
944- const globPattern = path . resolve ( tmpDirForGlobTest , '**/*.js' ) ;
990+ assert . strictEqual ( stderr , '' ) ;
991+ assert . deepStrictEqual ( stdout , expectRepeatedCompletes ( mainJsFile , 6 ) ) ;
992+ } ) ;
945993
946- const directory1 = path . join ( tmpDirForGlobTest , 'directory1' ) ;
947- const directory2 = path . join ( tmpDirForGlobTest , 'directory2' ) ;
994+ it ( 'should not be able to watch glob pattern paths without read access to the directory' , async ( ) => {
995+ const {
996+ rootDirGlob,
997+ mainJsFile,
998+ watchedFiles,
999+ } = createGlobFileStructure ( 'globtestdir-2' ) ;
9481000
949- mkdirSync ( directory1 ) ;
950- mkdirSync ( directory2 ) ;
1001+ const args = [ '--permission' , '--watch-path' , rootDirGlob , mainJsFile ] ;
1002+ const { stderr, stdout } = await runWriteSucceed ( {
1003+ args,
1004+ watchedFile : watchedFiles ,
1005+ } ) ;
9511006
952- const tmpJsFile1 = createTmpFile ( '' , '.js' , directory1 ) ;
953- const tmpJsFile2 = createTmpFile ( '' , '.js' , directory1 ) ;
954- const tmpJsFile3 = createTmpFile ( '' , '.js' , directory2 ) ;
955- const tmpJsFile4 = createTmpFile ( '' , '.js' , directory2 ) ;
956- const tmpJsFile5 = createTmpFile ( '' , '.js' , directory2 ) ;
1007+ assert . match ( stderr , / E R R _ A C C E S S _ D E N I E D / ) ;
1008+ assert . deepStrictEqual ( stdout , [ ] ) ;
1009+ } ) ;
1010+ it ( 'should not be able to watch glob pattern paths with partial read access' , async ( ) => {
1011+ const {
1012+ rootDir,
1013+ rootDirGlob,
1014+ mainJsFile,
1015+ watchedFiles,
1016+ } = createGlobFileStructure ( 'globtestdir-3' ) ;
1017+
1018+ const allowedSubDirectory = path . join ( rootDir , 'directory1' ) ;
1019+ const args = [ '--permission' , '--allow-fs-read' , allowedSubDirectory , '--watch-path' , rootDirGlob , mainJsFile ] ;
1020+ const { stderr, stdout } = await runWriteSucceed ( {
1021+ args,
1022+ watchedFile : watchedFiles ,
1023+ } ) ;
9571024
958- const mainJsFile = createTmpFile ( 'console.log(\'running\')' , '.js' , tmpDirForGlobTest ) ;
1025+ assert . match ( stderr , / E R R _ A C C E S S _ D E N I E D / ) ;
1026+ assert . deepStrictEqual ( stdout , [ ] ) ;
1027+ } ) ;
9591028
960- const args = [ '--watch-path' , globPattern , mainJsFile ] ;
961- const watchedFiles = [ tmpJsFile1 , tmpJsFile2 , tmpJsFile3 , tmpJsFile4 , tmpJsFile5 ] ;
1029+ it ( 'should be able to watch glob pattern paths with full read access to the directory' , async ( ) => {
1030+ const {
1031+ rootDir,
1032+ rootDirGlob,
1033+ mainJsFile,
1034+ watchedFiles,
1035+ } = createGlobFileStructure ( 'globtestdir-4' ) ;
9621036
1037+ const args = [ '--permission' , '--allow-fs-read' , rootDir , '--watch-path' , rootDirGlob , mainJsFile ] ;
9631038 const { stderr, stdout } = await runWriteSucceed ( {
9641039 args,
9651040 watchedFile : watchedFiles ,
9661041 } ) ;
9671042
968- function expectRepeatedCompletes ( n ) {
969- const expectedStdout = [ ] ;
970- for ( let i = 0 ; i < n ; i ++ ) {
971- if ( i !== 0 ) {
972- expectedStdout . push ( `Restarting ${ inspect ( ( mainJsFile ) ) } ` ) ;
973- }
974- expectedStdout . push ( 'running' ) ;
975- expectedStdout . push ( `Completed running ${ inspect ( mainJsFile ) } . Waiting for file changes before restarting...` ) ;
976- }
977- return expectedStdout ;
978- }
979-
9801043 assert . strictEqual ( stderr , '' ) ;
981- assert . deepStrictEqual ( stdout , expectRepeatedCompletes ( 6 ) ) ;
982-
1044+ assert . deepStrictEqual ( stdout , expectRepeatedCompletes ( mainJsFile , 6 ) ) ;
9831045 } ) ;
1046+
9841047} ) ;
0 commit comments