Skip to content

Commit

Permalink
Test dir validation response in different environments
Browse files Browse the repository at this point in the history
  • Loading branch information
ariskataoka committed Jul 18, 2024
1 parent e5f3219 commit d9efdfb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
33 changes: 26 additions & 7 deletions __tests__/lib/vip-import-validate-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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', () => {
Expand All @@ -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 },
} );
} );
} );
} );
16 changes: 11 additions & 5 deletions src/lib/vip-import-validate-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 >;
Expand All @@ -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
Expand All @@ -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 ) {
Expand Down

0 comments on commit d9efdfb

Please sign in to comment.