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

fix(playwrighttesting): handling other attachments #31950

Merged
merged 6 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,27 @@ class MPTReporter implements Reporter {
this.testResultBatch.add(testResultObject);
// Store test attachments in array
const testAttachments: string[] = [];
const otherAttachments: any[] = [];
for (const attachment of result.attachments) {
if (attachment.path !== undefined && attachment.path !== "") {
testAttachments.push(attachment.path);
this.uploadMetadata.numTotalAttachments++;
this.uploadMetadata.sizeTotalAttachments += ReporterUtils.getFileSize(attachment.path);
} else if (attachment.body instanceof Buffer) {
otherAttachments.push(attachment);
this.uploadMetadata.numTotalAttachments++;
kashish2508 marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Get raw result object and store it in map
const rawTestResult: RawTestResult = this.reporterUtils.getRawTestResultObject(result);
this.testRawResults.set(testResultObject.testExecutionId, JSON.stringify(rawTestResult));
this._testEndPromises.push(
this._uploadTestResultAttachments(testResultObject.testExecutionId, testAttachments),
this._uploadTestResultAttachments(
testResultObject.testExecutionId,
testAttachments,
otherAttachments,
),
);
} catch (err: any) {
this._addError(`Name: ${err.name}, Message: ${err.message}, Stack: ${err.stack}`);
Expand Down Expand Up @@ -313,38 +321,46 @@ class MPTReporter implements Reporter {
private async _uploadTestResultAttachments(
testExecutionId: string,
testAttachments: string[],
otherAttachments: any[],
): Promise<void> {
try {
this.isTestRunStartSuccess = await this.promiseOnBegin;
if (!this.isTestRunStartSuccess) {
this._addError(`\nUnable to initialize test run report.`);
return;
}
for (const attachmentPath of testAttachments) {
const fileRelativePath = `${testExecutionId}/${ReporterUtils.getFileRelativePath(
attachmentPath,
)}`;

const renewSasUriIfNeeded = async (): Promise<void> => {
kashish2508 marked this conversation as resolved.
Show resolved Hide resolved
if (
this.sasUri === undefined ||
!ReporterUtils.isTimeGreaterThanCurrentPlus10Minutes(this.sasUri)
) {
// Renew the sas uri
this.sasUri = await this.serviceClient.createStorageUri();
reporterLogger.info(
`\nFetched SAS URI with validity: ${this.sasUri.expiresAt} and access: ${this.sasUri.accessLevel}.`,
);
}
await this.storageClient.uploadFile(this.sasUri.uri, attachmentPath, fileRelativePath);
};

for (const attachmentPath of testAttachments) {
const fileRelativePath = `${testExecutionId}/${ReporterUtils.getFileRelativePath(attachmentPath)}`;
await renewSasUriIfNeeded();
await this.storageClient.uploadFileAsync(this.sasUri.uri, attachmentPath, fileRelativePath);
}
const rawTestResult = this.testRawResults.get(testExecutionId);
if (
this.sasUri === undefined ||
!ReporterUtils.isTimeGreaterThanCurrentPlus10Minutes(this.sasUri)
) {
// Renew the sas uri
this.sasUri = await this.serviceClient.createStorageUri();

for (const otherAttachment of otherAttachments) {
await renewSasUriIfNeeded();
const charset = otherAttachment.contentType.match(/charset=(.*)/)?.[1];
kashish2508 marked this conversation as resolved.
Show resolved Hide resolved
kashish2508 marked this conversation as resolved.
Show resolved Hide resolved
await this.storageClient.uploadBufferAsync(
this.sasUri.uri,
otherAttachment.body.toString((charset as any) || "utf-8"),
`${testExecutionId}/${otherAttachment.name}.txt`,
);
}
await this.storageClient.uploadBuffer(

const rawTestResult = this.testRawResults.get(testExecutionId);
await renewSasUriIfNeeded();
await this.storageClient.uploadBufferAsync(
this.sasUri.uri,
rawTestResult[0]!,
`${testExecutionId}/rawTestResult.json`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ class ReporterUtils {
attachmentStatus += ",";
}
attachmentStatus += "trace";
} else if (attachment.contentType === "text/plain") {
if (attachmentStatus !== "") {
attachmentStatus += ",";
}
attachmentStatus += "txt";
}
}
return attachmentStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { reporterLogger } from "../common/logger";
import { Constants } from "../common/constants";

export class StorageClient {
public async uploadFile(uri: string, filePath: string, fileRelativePath: string): Promise<void> {
public async uploadFileAsync(
kashish2508 marked this conversation as resolved.
Show resolved Hide resolved
uri: string,
filePath: string,
fileRelativePath: string,
): Promise<void> {
try {
const cloudFilepath = this.getCloudFilepath(uri, fileRelativePath);
const blobClient = new BlockBlobClient(cloudFilepath);
Expand All @@ -19,7 +23,11 @@ export class StorageClient {
}
}

public async uploadBuffer(uri: string, buffer: string, fileRelativePath: string): Promise<void> {
public async uploadBufferAsync(
kashish2508 marked this conversation as resolved.
Show resolved Hide resolved
uri: string,
buffer: string,
fileRelativePath: string,
): Promise<void> {
try {
const cloudFilepath = this.getCloudFilepath(uri, fileRelativePath);
const blobClient = new BlockBlobClient(cloudFilepath);
Expand Down
Loading