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

Closes #143 Add report add process to rename and push test files to git #174

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
34 changes: 34 additions & 0 deletions move_report.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

if [ "$#" -lt 1 ]; then
echo "Usage: $0 <destination_directory> [new_name]"
exit 1
fi

SOURCE_FOLDER="test-results"
DESTINATION_DIR=$1
NEW_NAME=$2

# If no destination is passed use the default.
if [ ! -d "$DESTINATION_DIR" ]; then
DESTINATION_DIR="/var/shared/rocket-e2e-reports"
fi

# Determine the new folder name, if folder name is not passed, use date and username
if [ -z "$NEW_NAME" ]; then
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
USERNAME=$(whoami)
NEW_NAME="${TIMESTAMP}_${USERNAME}"
fi

NEW_REPORT_PATH="$DESTINATION_DIR/$NEW_NAME"

mv "$SOURCE_FOLDER" "$NEW_REPORT_PATH"

# Check if the operation was successful
if [ $? -eq 0 ]; then
echo "Folder successfully moved to '$NEW_REPORT_PATH'."
else
echo "Error: Failed to move the folder."
exit 4
fi
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"scripts": {
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"test:e2e": "$npm_package_config_testCommand",
"test:smoke": "$npm_package_config_testCommand --tags @smoke",
"test:e2e": "$npm_package_config_testCommand && npm run push-report --tag=e2e",
"test:smoke": "$npm_package_config_testCommand --tags @smoke && npm run push-report --tag=smoke",
"test:local": "$npm_package_config_testCommand --tags @local",
"test:online": "$npm_package_config_testCommand --tags @online",
"test:vr": "$npm_package_config_testCommand --tags @vr",
Expand All @@ -23,6 +23,7 @@
"test:cpcss": "$npm_package_config_testCommand --tags @cpcss",
"test:performancehints": "$npm_package_config_testCommand --tags @performancehints",
"healthcheck": "ts-node healthcheck.ts",
"push-report": "ts-node report.ts",
"wp-env": "wp-env"
},
"repository": {
Expand Down
80 changes: 80 additions & 0 deletions report.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import path from "path";
import { execFile } from "child_process";
import {promises as fs} from "fs";

/**
* Get the script path and arguments for moving the report.
*
* @return {Promise<{ scriptPath: string; args: string[] }>}
*/
async function initialization(): Promise<{ scriptPath: string; args: string[] }> {
const scriptPath = path.resolve('move_report.sh');
const destination = process.argv[2];
const tag = process.env.npm_config_tag;
const newName = tag
? `${tag}_test`
: null;

// Prepare the arguments for the script
const args = [destination];
if (newName) {
args.push(newName);
}

return { scriptPath, args };
}
/**
* Check if script has permission and if not add the right permission, so it can be executed.
* @param {string} scriptPath The script path.
*
* @return {Promise<void>}
*/
export async function checkScriptPermission(scriptPath: string): Promise<void> {
try {
await fs.access(scriptPath, fs.constants.X_OK);
console.log(`Script already has execute permission: ${scriptPath}`);
} catch (err) {
console.log(`Adding execute permission to ${scriptPath}`);
try {
await fs.chmod(scriptPath, 0o755);
console.log(`Execute permission added successfully: ${scriptPath}`);
} catch (chmodErr) {
console.error(`Failed to set execute permission: ${chmodErr.message}`);
throw chmodErr;
}
}
}

/**
* Move the test-results directory to the given directory.
* @param {string} scriptPath The script path.
* @param args Script argument.
*
* @return {Promise<void>}
*/
export async function moveTestReport(scriptPath: string, args: readonly string[]): Promise<void> {
execFile(scriptPath, args, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return;
}
console.log(`Output: ${stdout}`);
});
}

(async (): Promise<void> => {
try {
const { scriptPath, args } = await initialization();
await checkScriptPermission(scriptPath);
await moveTestReport(scriptPath, args);
} catch (err) {
console.error(`Failed to execute the script: ${err.message}`);
process.exit(1);
}

process.exit(0);
})();
Loading