Skip to content

Commit

Permalink
fix(playwrighttesting): handling other attachments (Azure#31950)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR
@azure-microsoft-playwright-testing

### Issues associated with this PR


### Describe the problem that is addressed by this PR


### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
  • Loading branch information
kashish2508 authored Dec 20, 2024
1 parent 43e2b91 commit 04745da
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,28 @@ 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++;
this.uploadMetadata.sizeTotalAttachments += ReporterUtils.getBufferSize(attachment.body);
}
}

// 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 All @@ -309,41 +318,48 @@ class MPTReporter implements Reporter {
reporterLogger.error(`\nError in uploading test run information: ${err.message}`);
}
}

private renewSasUriIfNeeded = async (): Promise<void> => {
if (
this.sasUri === undefined ||
!ReporterUtils.isTimeGreaterThanCurrentPlus10Minutes(this.sasUri)
) {
this.sasUri = await this.serviceClient.createStorageUri();
reporterLogger.info(
`\nFetched SAS URI with validity: ${this.sasUri.expiresAt} and access: ${this.sasUri.accessLevel}.`,
);
}
};
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,
)}`;
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}.`,
);
}
const fileRelativePath = `${testExecutionId}/${ReporterUtils.getFileRelativePath(attachmentPath)}`;
await this.renewSasUriIfNeeded();
await this.storageClient.uploadFile(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 this.renewSasUriIfNeeded();
const match = otherAttachment?.contentType?.match(/charset=(.*)/);
const charset = match && match.length > 1 ? match[1] : "utf-8";
await this.storageClient.uploadBuffer(
this.sasUri.uri,
otherAttachment.body.toString((charset as any) || "utf-8"),
`${testExecutionId}/${otherAttachment.name}`,
);
}

const rawTestResult = this.testRawResults.get(testExecutionId);
await this.renewSasUriIfNeeded();
await this.storageClient.uploadBuffer(
this.sasUri.uri,
rawTestResult[0]!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ class ReporterUtils {
return 0;
}
}

public static getBufferSize(attachmentBody: Buffer): number {
try {
const fileSizeInBytes = attachmentBody.length;
return fileSizeInBytes;
} catch (error) {
return 0;
}
}

public redactAccessToken(info: string | undefined): string {
if (!info || ReporterUtils.isNullOrEmpty(this.envVariables.accessToken)) {
return "";
Expand Down Expand Up @@ -468,6 +478,11 @@ class ReporterUtils {
attachmentStatus += ",";
}
attachmentStatus += "trace";
} else if (attachment.contentType === "text/plain") {
if (attachmentStatus !== "") {
attachmentStatus += ",";
}
attachmentStatus += "txt";
}
}
return attachmentStatus;
Expand Down

0 comments on commit 04745da

Please sign in to comment.