@@ -16,6 +16,7 @@ const gitconfig = require('gitconfiglocal');
1616const { spawn, execSync } = require ( 'child_process' ) ;
1717const glob = require ( 'glob' ) ;
1818const pGitconfig = promisify ( gitconfig ) ;
19+ const { readCypressConfigFile } = require ( './readCypressConfigUtil' ) ;
1920const CrashReporter = require ( '../testObservability/crashReporter' ) ;
2021
2122exports . debug = ( text , shouldReport = false , throwable = null ) => {
@@ -313,3 +314,76 @@ exports.setBrowserstackCypressCliDependency = (bsConfig) => {
313314 }
314315 }
315316}
317+
318+ exports . deleteSupportFileOrDir = ( fileOrDirPath ) => {
319+ try {
320+ // Sanitize the input to remove any characters that could be used for directory traversal
321+ const sanitizedPath = fileOrDirPath . replace ( / ( \. \. \/ | \. \/ | \/ \/ ) / g, '' ) ;
322+ const resolvedPath = path . resolve ( sanitizedPath ) ;
323+ if ( fs . existsSync ( resolvedPath ) ) {
324+ if ( fs . lstatSync ( resolvedPath ) . isDirectory ( ) ) {
325+ fs . readdirSync ( resolvedPath ) . forEach ( ( file ) => {
326+ const sanitizedFile = file . replace ( / ( \. \. \/ | \. \/ | \/ \/ ) / g, '' ) ;
327+ const currentPath = path . join ( resolvedPath , sanitizedFile ) ;
328+ fs . unlinkSync ( currentPath ) ;
329+ } ) ;
330+ fs . rmdirSync ( resolvedPath ) ;
331+ } else {
332+ fs . unlinkSync ( resolvedPath ) ;
333+ }
334+ }
335+ } catch ( err ) { }
336+ }
337+
338+ exports . getSupportFiles = ( bsConfig , isA11y ) => {
339+ let extension = null ;
340+ try {
341+ extension = bsConfig . run_settings . cypress_config_file . split ( '.' ) . pop ( ) ;
342+ } catch ( err ) { }
343+ let supportFile = '/**/cypress/support/**/*.{js,ts}' ;
344+ let cleanupParams = { } ;
345+ let userSupportFile = null ;
346+ try {
347+ const completeCypressConfigFile = readCypressConfigFile ( bsConfig )
348+ let cypressConfigFile = { } ;
349+ if ( ! utils . isUndefined ( completeCypressConfigFile ) ) {
350+ cypressConfigFile = ! utils . isUndefined ( completeCypressConfigFile . default ) ? completeCypressConfigFile . default : completeCypressConfigFile
351+ }
352+ userSupportFile = cypressConfigFile . e2e ?. supportFile !== null ? cypressConfigFile . e2e ?. supportFile : cypressConfigFile . component ?. supportFile !== null ? cypressConfigFile . component ?. supportFile : cypressConfigFile . supportFile ;
353+ if ( userSupportFile == false && extension ) {
354+ const supportFolderPath = path . join ( process . cwd ( ) , 'cypress' , 'support' ) ;
355+ if ( ! fs . existsSync ( supportFolderPath ) ) {
356+ fs . mkdirSync ( supportFolderPath ) ;
357+ cleanupParams . deleteSupportDir = true ;
358+ }
359+ const sanitizedExtension = extension . replace ( / ( \. \. \/ | \. \/ | \/ \/ ) / g, '' ) ;
360+ const supportFilePath = path . join ( supportFolderPath , `tmpBstackSupportFile.${ sanitizedExtension } ` ) ;
361+ fs . writeFileSync ( supportFilePath , "" ) ;
362+ supportFile = `/cypress/support/tmpBstackSupportFile.${ sanitizedExtension } ` ;
363+ const currEnvVars = bsConfig . run_settings . system_env_vars ;
364+ const supportFileEnv = `CYPRESS_SUPPORT_FILE=${ supportFile . substring ( 1 ) } ` ;
365+ if ( ! currEnvVars ) {
366+ bsConfig . run_settings . system_env_vars = [ supportFileEnv ] ;
367+ } else {
368+ bsConfig . run_settings . system_env_vars = [ ...currEnvVars , supportFileEnv ] ;
369+ }
370+ cleanupParams . deleteSupportFile = true ;
371+ } else if ( typeof userSupportFile == 'string' ) {
372+ if ( userSupportFile . startsWith ( '${' ) && userSupportFile . endsWith ( '}' ) ) {
373+ /* Template strings to reference environment variables */
374+ const envVar = userSupportFile . substring ( 2 , userSupportFile . length - 1 ) ;
375+ supportFile = process . env [ envVar ] ;
376+ } else {
377+ /* Single file / glob pattern */
378+ supportFile = userSupportFile ;
379+ }
380+ } else if ( Array . isArray ( userSupportFile ) ) {
381+ supportFile = userSupportFile [ 0 ] ;
382+ }
383+ } catch ( err ) { }
384+ if ( supportFile && supportFile [ 0 ] != '/' ) supportFile = '/' + supportFile ;
385+ return {
386+ supportFile,
387+ cleanupParams : Object . keys ( cleanupParams ) . length ? cleanupParams : null
388+ } ;
389+ }
0 commit comments