Skip to content

Commit

Permalink
fix: handling the existence of an attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
alexneo2003 committed Jul 26, 2022
1 parent 69e7178 commit d16efbc
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/playwright-azure-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable no-control-regex */
import * as Test from 'azure-devops-node-api/TestApi'
import * as TestInterfaces from 'azure-devops-node-api/interfaces/TestInterfaces'
import * as azdev from 'azure-devops-node-api'
import * as TestInterfaces from 'azure-devops-node-api/interfaces/TestInterfaces'
import * as Test from 'azure-devops-node-api/TestApi'

import { Reporter, TestCase, TestResult } from '@playwright/test/reporter'

import { TestPoint } from 'azure-devops-node-api/interfaces/TestInterfaces'
import { WebApi } from 'azure-devops-node-api'
import chalk from 'chalk'
import { readFileSync } from 'fs'
import { ICoreApi } from 'azure-devops-node-api/CoreApi'
import { TeamProject } from 'azure-devops-node-api/interfaces/CoreInterfaces'
import { TestPoint } from 'azure-devops-node-api/interfaces/TestInterfaces'
import chalk from 'chalk'
import crypto from 'crypto'
import { existsSync, readFileSync } from 'fs'

export function createGuid(): string {
return crypto.randomBytes(16).toString('hex');
Expand Down Expand Up @@ -366,21 +366,26 @@ class AzureDevOpsReporter implements Reporter {
return await Promise.all(
testResult.attachments.map(async (attachment) => {
if (this.attachmentsType!.includes((attachment.name as TAttachmentType[number]))) {
const attachments: TestInterfaces.TestAttachmentRequestModel = {
attachmentType: 'GeneralAttachment',
fileName: `${attachment.name}-${createGuid()}.${attachment.contentType.split('/')[1]}`,
stream: readFileSync(attachment.path!, { encoding: 'base64' })
};

if (!this.testApi)
this.testApi = await this.connection.getTestApi();
const response = await this.testApi.createTestResultAttachment(
attachments,
this.projectName,
this.runId!,
caseId
);
return response.url;
if (existsSync(attachment.path!)) {
const attachments: TestInterfaces.TestAttachmentRequestModel = {
attachmentType: 'GeneralAttachment',
fileName: `${attachment.name}-${createGuid()}.${attachment.contentType.split('/')[1]}`,
stream: readFileSync(attachment.path!, { encoding: 'base64' })
};

if (!this.testApi)
this.testApi = await this.connection.getTestApi();
const response = await this.testApi.createTestResultAttachment(
attachments,
this.projectName,
this.runId!,
caseId
);
return response.url;
} else {
this.log(chalk.red(`Attachment ${attachment.path} does not exist.`));
return '';
}
} else {
return '';
}
Expand Down

0 comments on commit d16efbc

Please sign in to comment.