Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logic for checking valid exporters #414

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lib/import-export/export/export-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export async function exportBackup(
removeExportListeners();
}
break;
} else {
onEvent( { event: ExportEvents.EXPORT_ERROR, data: null } );
throw new Error( 'No suitable exporter found for the site' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that we weren't raising an error when no exporter are found.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this, I had a similar concern earlier in #391 (comment) 👍

}
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/lib/import-export/export/exporters/default-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,22 @@ export class DefaultExporter extends EventEmitter implements Exporter {
return false;
}

const requiredPaths = [ 'wp-content', 'wp-includes', 'wp-load.php', 'wp-config.php' ];
const requiredPaths = [
{ path: 'wp-content', isDir: true },
{ path: 'wp-includes', isDir: true },
{ path: 'wp-load.php', isDir: false },
{ path: 'wp-config.php', isDir: false },
];

this.siteFiles = await this.getSiteFiles();

return requiredPaths.every( ( requiredPath ) =>
this.siteFiles.some( ( file ) => file.includes( requiredPath ) )
this.siteFiles.some( ( file ) => {
const relativePath = path.relative( this.options.site.path, file );
return requiredPath.isDir
? relativePath.startsWith( requiredPath.path )
: relativePath === requiredPath.path;
} )
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe( 'DefaultExporter', () => {
{ path: '/path/to/site/wp-content/plugins/plugin1', name: 'plugin1.php', isFile: () => true },
{ path: '/path/to/site/wp-content/themes/theme1', name: 'index.php', isFile: () => true },
{ path: '/path/to/site/wp-includes/index.php', name: 'index.php', isFile: () => true },
{ path: '/path/to/site/wp-load.php', name: 'wp-load.php', isFile: () => true },
{ path: '/path/to/site', name: 'wp-load.php', isFile: () => true },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were getting false positives in the export unit tests due to the previous logic of canHandle.

];

( fsPromises.readdir as jest.Mock ).mockResolvedValue( mockFiles );
Expand Down
Loading