Skip to content

Commit

Permalink
Update unit tests of listFiles function
Browse files Browse the repository at this point in the history
  • Loading branch information
fluiddot committed Jul 16, 2024
1 parent caa31a0 commit 9e2bde0
Showing 1 changed file with 36 additions and 54 deletions.
90 changes: 36 additions & 54 deletions src/lib/import-export/tests/import/handlers/backup-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// To run tests, execute `npm run test -- src/lib/import-export/tests/import/handlers/backup-handler-factory.test.ts`
import fs from 'fs';
import path from 'path';
import zlib from 'zlib';
import AdmZip from 'adm-zip';
import * as tar from 'tar';
import { BackupHandlerFactory } from '../../../import/handlers/backup-handler-factory';
Expand Down Expand Up @@ -66,71 +65,50 @@ describe( 'BackupHandlerFactory', () => {
} );

describe( 'listFiles', () => {
it( 'should extract files from a gzip archive', async () => {
const archiveFiles = [
'index.php',
'.hidden-file',
'wp-content/.hidden-file',
'wp-content/plugins/hello.php',
'wp-content/themes/twentytwentyfour/theme.json',
'wp-content/uploads/2024/07/image.png',
'__MACOSX/meta-file',
];
const expectedArchiveFiles = [
'index.php',
'wp-content/plugins/hello.php',
'wp-content/themes/twentytwentyfour/theme.json',
'wp-content/uploads/2024/07/image.png',
];

it( 'should list files from a gzip archive', async () => {
const archiveInfo: BackupArchiveInfo = {
path: '/path/to/backup.tar.gz',
type: 'application/gzip',
};
const handler = BackupHandlerFactory.create( archiveInfo );
const extractionDirectory = '/tmp/extracted';

( path.extname as jest.Mock ).mockReturnValue( '.gz' );
( fs.createReadStream as jest.Mock ).mockReturnValue( {
pipe: jest.fn().mockReturnThis(),
on: jest.fn().mockImplementation( ( event, callback ) => {
if ( event === 'finish' ) {
callback();
}
return this;
} ),
} );
( zlib.createGunzip as jest.Mock ).mockReturnValue( {
pipe: jest.fn().mockReturnThis(),
} );
( tar.extract as unknown as jest.Mock ).mockReturnValue( {
on: jest.fn().mockImplementation( ( event, callback ) => {
if ( event === 'finish' ) {
callback();
}
return this;
} ),
jest.spyOn( tar, 't' ).mockImplementation( ( { onReadEntry } ) => {
archiveFiles.forEach( ( path ) => onReadEntry?.( { path } as tar.ReadEntry ) );
} );

await expect(
handler.extractFiles( archiveInfo, extractionDirectory )
).resolves.not.toThrow();
await expect( handler.listFiles( archiveInfo ) ).resolves.toEqual( expectedArchiveFiles );
} );

it( 'should extract files from a zip archive', async () => {
it( 'should list files from a zip archive', async () => {
const archiveInfo: BackupArchiveInfo = {
path: '/path/to/backup.zip',
type: 'application/zip',
};
const handler = BackupHandlerFactory.create( archiveInfo );
const extractionDirectory = '/tmp/extracted';

( path.extname as jest.Mock ).mockReturnValue( '.zip' );

const mockExtractAllToAsync = jest
.fn()
.mockImplementation( ( _path, _overwrite, _keepOriginalPermission, callback ) => {
callback();
} );

( AdmZip as jest.Mock ).mockImplementation( () => ( {
extractAllToAsync: mockExtractAllToAsync,
} ) );

await expect(
handler.extractFiles( archiveInfo, extractionDirectory )
).resolves.not.toThrow();
( AdmZip as jest.Mock ).mockReturnValue( {
getEntries: jest
.fn()
.mockReturnValue( archiveFiles.map( ( file ) => ( { entryName: file } ) ) ),
} );

expect( mockExtractAllToAsync ).toHaveBeenCalledWith(
extractionDirectory,
true,
undefined,
expect.any( Function )
);
await expect( handler.listFiles( archiveInfo ) ).resolves.toEqual( expectedArchiveFiles );
} );

it( 'should list a single SQL file', async () => {
Expand All @@ -139,7 +117,6 @@ describe( 'BackupHandlerFactory', () => {
type: 'application/sql',
};
const handler = BackupHandlerFactory.create( archiveInfo );
( path.extname as jest.Mock ).mockReturnValue( '.sql' );
( path.basename as jest.Mock ).mockReturnValue( 'backup.sql' );
const result = await handler.listFiles( archiveInfo );
expect( result ).toEqual( [ 'backup.sql' ] );
Expand All @@ -154,9 +131,16 @@ describe( 'BackupHandlerFactory', () => {
};
const handler = BackupHandlerFactory.create( archiveInfo );
const extractionDirectory = '/tmp/extracted';
( path.extname as jest.Mock ).mockReturnValue( '.gz' );

( tar.x as unknown as jest.Mock ).mockResolvedValue( undefined );
( fs.createReadStream as jest.Mock ).mockReturnValue( {
pipe: jest.fn().mockReturnThis(),
on: jest.fn().mockImplementation( ( event, callback ) => {
if ( event === 'finish' ) {
callback();
}
return this;
} ),
} );

await expect(
handler.extractFiles( archiveInfo, extractionDirectory )
Expand All @@ -173,7 +157,6 @@ describe( 'BackupHandlerFactory', () => {
};
const handler = BackupHandlerFactory.create( archiveInfo );
const extractionDirectory = '/tmp/extracted';
( path.extname as jest.Mock ).mockReturnValue( '.zip' );
const mockExtractAllToAsync = jest
.fn()
.mockImplementation( ( path, overwrite, keepOriginalPermission, callback ) => {
Expand All @@ -200,7 +183,6 @@ describe( 'BackupHandlerFactory', () => {
};
const handler = BackupHandlerFactory.create( archiveInfo );
const extractionDirectory = '/tmp/extracted';
( path.extname as jest.Mock ).mockReturnValue( '.sql' );
( path.basename as jest.Mock ).mockReturnValue( 'backup.sql' );
( fs.promises.copyFile as jest.Mock ).mockResolvedValue( undefined );

Expand Down

0 comments on commit 9e2bde0

Please sign in to comment.