From d9efdfb3d767c78217ff99a57bf48654d25f098e Mon Sep 17 00:00:00 2001 From: Ariana Kataoka Date: Thu, 18 Jul 2024 23:19:32 +1000 Subject: [PATCH] Test dir validation response in different environments --- __tests__/lib/vip-import-validate-files.js | 33 +++++++++++++++++----- src/lib/vip-import-validate-files.ts | 16 +++++++---- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/__tests__/lib/vip-import-validate-files.js b/__tests__/lib/vip-import-validate-files.js index ac29d7caf1..13ee9a7ca6 100644 --- a/__tests__/lib/vip-import-validate-files.js +++ b/__tests__/lib/vip-import-validate-files.js @@ -215,13 +215,13 @@ describe( 'lib/vip-import-validate-files', () => { expect( mockConsoleError ).not.toHaveBeenCalled(); } ); } ); - describe( 'findNestedDirectories()', () => { - // Mocking file system and chalk - jest.mock( 'fs' ); - jest.mock( 'chalk', () => ( { - red: jest.fn( () => 'red' ), - } ) ); + // Mocking file system and chalk + jest.mock( 'fs' ); + jest.mock( 'chalk', () => ( { + red: jest.fn( () => 'red' ), + } ) ); + describe( 'findNestedDirectories', () => { let readdirSyncMock; let statSyncMock; @@ -257,7 +257,7 @@ describe( 'lib/vip-import-validate-files', () => { const result = findNestedDirectories( '/empty/dir' ); - expect( result ).toEqual( { files: [], directoryStructureObj: {} } ); + expect( result ).toEqual( { files: [], directoryStructureObj: { '/empty/dir': true } } ); } ); it( 'should filter out hidden files', () => { @@ -271,5 +271,24 @@ describe( 'lib/vip-import-validate-files', () => { directoryStructureObj: { '/dir/with/hidden/files': true }, } ); } ); + + it( 'should recursively find nested directories', () => { + readdirSyncMock.mockImplementation( dir => { + if ( dir === '/dir' ) return [ 'subdir' ]; + if ( dir === '/dir/subdir' ) return [ 'file1.txt', 'file2.txt' ]; + return []; + } ); + statSyncMock.mockImplementation( filePath => { + if ( filePath === '/dir/subdir' ) return { isDirectory: () => true }; + return { isDirectory: () => false }; + } ); + + const result = findNestedDirectories( '/dir' ); + + expect( result ).toEqual( { + files: [ '/dir/subdir/file1.txt', '/dir/subdir/file2.txt' ], + directoryStructureObj: { '/dir': true, '/dir/subdir': true }, + } ); + } ); } ); } ); diff --git a/src/lib/vip-import-validate-files.ts b/src/lib/vip-import-validate-files.ts index 21183c0371..634eb15bd6 100644 --- a/src/lib/vip-import-validate-files.ts +++ b/src/lib/vip-import-validate-files.ts @@ -258,9 +258,6 @@ const recommendAcceptableFileNames = (): void => { * * @param {string} directory Root directory, or the given (current) directory */ -const files: string[] = []; -const directoryStructureObj: Record< string, boolean > = {}; - interface FindNestedDirectoriesResult { files: string[]; directoryStructureObj: Record< string, boolean >; @@ -270,6 +267,8 @@ export const findNestedDirectories = ( directory: string ): FindNestedDirectoriesResult | undefined => { let nestedDirectories: string[]; + let files: string[] = []; + let directoryStructureObj: Record< string, boolean > = {}; try { // Read nested directories within the given directory @@ -285,14 +284,21 @@ export const findNestedDirectories = ( // Keep looking for nested directories until we hit individual files if ( statSync.isDirectory() ) { - findNestedDirectories( filePath ); + const nestedResult = findNestedDirectories( filePath ); + if ( nestedResult ) { + files = files.concat( nestedResult.files ); + directoryStructureObj = { + ...directoryStructureObj, + ...nestedResult.directoryStructureObj, + }; + } } else { // Once we hit media files, add the path of all existing directories // as object keys to validate directory structure later on directoryStructureObj[ directory ] = true; // Also, push individual files to an array to do individual file validations later on - return files.push( filePath ); + files.push( filePath ); } } ); } catch ( error ) {