Skip to content

Commit

Permalink
Merge pull request #181 from wp-media/enhancement/67-only-rename-debu…
Browse files Browse the repository at this point in the history
…g-log-with-errors-relating-to-wpr

Closes #67: Only rename debug.log with errors relating to WPR
  • Loading branch information
Mai-Saad authored Jan 8, 2025
2 parents 36f8526 + acec67e commit 1204467
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/support/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import { ChromiumBrowser, chromium } from '@playwright/test';
import { Sections } from '../common/sections';
import { selectors as pluginSelectors } from "./../common/selectors";
import { PageUtils } from "../../utils/page-utils";
import { deleteFolder } from "../../utils/helpers";
import { deleteFolder, isWprRelatedError } from "../../utils/helpers";
import {WP_SSH_ROOT_DIR,} from "../../config/wp.config";
import { After, AfterAll, Before, BeforeAll, Status, setDefaultTimeout } from "@cucumber/cucumber";
import {rename, exists, rm, testSshConnection, installRemotePlugin, activatePlugin, uninstallPlugin} from "../../utils/commands";
import {rename, exists, rm, testSshConnection, installRemotePlugin, activatePlugin, uninstallPlugin, readFile} from "../../utils/commands";
// import {configurations, getWPDir} from "../../utils/configurations";

/**
Expand Down Expand Up @@ -167,8 +167,10 @@ After(async function (this: ICustomWorld, { pickle, result }) {

const debugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug.log`;
const debugLogExists = await exists(debugLogPath);
const debugLogContents = await readFile(debugLogPath);
const wprRelatedError = await isWprRelatedError(debugLogContents);

if (debugLogExists && previousScenarioName) {
if (debugLogExists && previousScenarioName && wprRelatedError) {
// Close up white spaces.
previousScenarioName = previousScenarioName.toLowerCase();
previousScenarioName = previousScenarioName.replaceAll(' ', '-');
Expand Down
21 changes: 21 additions & 0 deletions utils/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,25 @@ export async function updatePostStatus(id: number, status: string): Promise<void
await wp(`post update ${id} --post_status=${status}`);
}

/**
* Read file on the server.
*
* @function
* @name readFile
* @async
* @param {string} path - The path to the file to be read.
* @returns {Promise<string>} - A Promise that resolves after file content is read.
*/
export async function readFile(path: string): Promise<string> {
const cwd = configurations.rootDir;
const command = wrapPrefix(`sudo cat ${path}`);
const result = exec(command, { cwd: cwd, async: false });

if (result.code !== 0) {
return '';
}

return result.stdout;
}

export default wp;
22 changes: 22 additions & 0 deletions utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,25 @@ export const getScenarioTag = async(tags: Array<string>): Promise<string> => {

return tag;
}

/**
* Check for WP Rocket related error in debug.log.
*
* @param {string} contents File content to be checked.
*
* @return {Promise<boolean>} Promise that resolves after check is completed.
*/
export const isWprRelatedError = async(contents: string): Promise<boolean> => {
const patterns: Array<string> = [
'/plugins/wp-rocket/',
'WP_Rocket'
];

for (const pattern of patterns) {
if (contents.includes(pattern)) {
return true;
}
}

return false;
}

0 comments on commit 1204467

Please sign in to comment.