Skip to content

Commit

Permalink
Merge pull request #60 from wp-media/enhancement/59-delete_debug_log_ssh
Browse files Browse the repository at this point in the history
Closes #59: Deletes or rename `debug.log` file before tests
  • Loading branch information
hanna-meda authored Apr 5, 2024
2 parents 788a6ba + 6c949f1 commit d81e89e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/support/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ import { PageUtils } from "../../utils/page-utils";
import { batchUpdateVRTestUrl } from "../../utils/helpers";
import { deleteFolder } from "../../utils/helpers";
import backstop from 'backstopjs';
import { SCENARIO_URLS } from "../../config/wp.config";
import {SCENARIO_URLS, WP_SSH_ROOT_DIR,} from "../../config/wp.config";

import { After, AfterAll, Before, BeforeAll, Status, setDefaultTimeout } from "@cucumber/cucumber";
// import wp, {cp, deleteTransient, generateUsers, resetWP, rm, unzip} from "../../utils/commands";
import {rename, exists, rm} from "../../utils/commands";
// import {configurations, getWPDir} from "../../utils/configurations";

/**
* The Playwright Chromium browser instance used for testing.
*/
let browser: ChromiumBrowser;

/**
* Stores the name of the previous test scenario.
* It is initially undefined until it is assigned a value in the `After` hook.
* @type {string}
*/
let previousScenarioName: string;
/**
* Sets the default timeout for Playwright tests.
* If PWDEBUG environment variable is set, timeout is infinite (-1).
Expand All @@ -42,6 +49,9 @@ setDefaultTimeout(process.env.PWDEBUG ? -1 : 60 * 10000);
* Before all tests, launches the Chromium browser.
*/
BeforeAll(async function (this: ICustomWorld) {
const debugLogPath = `${WP_SSH_ROOT_DIR}wp-content/*.log`;
await rm(debugLogPath);

await deleteFolder('./backstop_data/bitmaps_test');
browser = await chromium.launch({ headless: false });

Expand Down Expand Up @@ -77,6 +87,7 @@ BeforeAll(async function (this: ICustomWorld) {
urls: SCENARIO_URLS
});
}

});

/**
Expand Down Expand Up @@ -139,7 +150,7 @@ Before({tags: 'not @setup'}, async function (this: ICustomWorld) {
this.page = await this.context.newPage();
this.sections = new Sections(this.page, pluginSelectors);
this.utils = new PageUtils(this.page, this.sections);

/**
* To uncomment during implementation of cli
*/
Expand Down Expand Up @@ -167,10 +178,23 @@ Before({tags: '@setup'}, async function(this: ICustomWorld) {
* After each test scenario, performs cleanup tasks and captures screenshots and videos in case of failure.
*/
After(async function (this: ICustomWorld, { pickle, result }) {
previousScenarioName = pickle.name

if (result?.status == Status.FAILED) {
await this.utils.createScreenShot(this, pickle);
}

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

if (debugLogExists && previousScenarioName) {
// Close up white spaces.
previousScenarioName = previousScenarioName.toLowerCase();
previousScenarioName = previousScenarioName.replaceAll(' ', '-');
const newDebugLogPath = `${WP_SSH_ROOT_DIR}wp-content/debug-${previousScenarioName}.log`;
await rename(debugLogPath, newDebugLogPath);
}

await this.page?.close()
await this.context?.close()

Expand Down
75 changes: 75 additions & 0 deletions utils/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,68 @@ export async function cp(origin: string, destination: string): Promise<void> {
});
}

/**
* Renames a file on the host.
*
* @function
* @name rename
* @async
* @param {string} oldName - The current name of the file.
* @param {string} newName - The new name for the file.
* @returns {Promise<void>} - A Promise that resolves when the rename operation is completed.
*/
export async function rename(oldName: string, newName: string): Promise<void> {
if(configurations.type === ServerType.docker) {
await exec(`docker exec -T ${configurations.docker.container} mv ${oldName} ${newName}`, {
cwd: configurations.rootDir,
async: false
});

return;
}

if(configurations.type === ServerType.external) {
await exec(`ssh -i ${configurations.ssh.key} ${configurations.ssh.username}@${configurations.ssh.address} "mv ${oldName} ${newName}"`);
return;
}

exec(`mv ${oldName} ${newName}`, {
cwd: configurations.rootDir,
async: false
});
}

/**
* Checks if a file exists on the server.
*
* @function
* @name exists
* @async
* @param {string} filePath - The path of the file to check.
* @returns {Promise<boolean>} - A Promise that resolves with true if the file exists, false otherwise.
*/
export async function exists(filePath: string): Promise<boolean> {
let command: string;

if(configurations.type === ServerType.docker) {
command = `docker exec -T ${configurations.docker.container} test -f ${filePath}; echo $?`;
} else if(configurations.type === ServerType.external) {
command = `ssh -i ${configurations.ssh.key} ${configurations.ssh.username}@${configurations.ssh.address} 'test -f ${filePath}; echo $?'`;
} else {
command = `test -f ${filePath}; echo $?`;
}

try {
const result = await exec(command, {
cwd: configurations.rootDir,
async: false
});
return result.stdout.trim() === '0';
} catch (error) {
return false;
}
}

/**
* Unzips a compressed file to the specified destination on the server.
*
Expand Down Expand Up @@ -161,6 +223,19 @@ export async function activatePlugin(name: string): Promise<void> {
await wp(`plugin activate ${name}`)
}

/**
* Executes a SQL query on the WordPress database using WP-CLI.
*
* @function
* @name query
* @async
* @param {string} query - The SQL query to be executed.
* @returns {Promise<void>} - A Promise that resolves when the query is executed.
*/
export async function query(query: string): Promise<void> {
await wp(`db query "${query}"`)
}

/**
* Deactivates a WordPress plugin using the WP-CLI command.
*
Expand Down

0 comments on commit d81e89e

Please sign in to comment.