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

EPMRPP-84451 || Rewrite the getAttachments utility to be fully async #142

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 19 additions & 1 deletion src/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
isFalse,
getAttachments,
isErrorLog,
fileExists,
calculateRpStatus,
} from '../utils';
import fs from 'fs';
Expand Down Expand Up @@ -56,6 +57,23 @@ describe('testing utils', () => {
});
});

describe('fileExists', () => {
test('should return true', async () => {
jest.spyOn(fs.promises, 'stat').mockImplementationOnce(() => Promise.resolve({} as fs.Stats));

const existingFilePath = 'existing-file-path';
const isFileExist = await fileExists(existingFilePath);

expect(isFileExist).toBe(true);
});
test('should return false', async () => {
const notExistingFilePath = 'not-existing-file-path';
const isFileExist = await fileExists(notExistingFilePath);

expect(isFileExist).toBe(false);
});
});

describe('promiseErrorHandler', () => {
let spyConsoleError: jest.SpyInstance;
beforeEach(() => {
Expand Down Expand Up @@ -242,7 +260,7 @@ describe('testing utils', () => {
const file1Data = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
const file2Data = Buffer.from([1, 2, 3, 4, 5, 6, 7]);

jest.spyOn(fs, 'existsSync').mockImplementationOnce((): boolean => true);
jest.spyOn(fs.promises, 'stat').mockImplementationOnce(() => Promise.resolve({} as fs.Stats));

jest.spyOn(fs.promises, 'readFile').mockImplementationOnce(async () => file1Data);

Expand Down
17 changes: 16 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ export const sendEventToReporter = (type: string, data: any, suite?: string): vo
process.stdout.write(JSON.stringify({ type, data, suite }));
};

export const fileExists = async (filePath: string) => {
try {
await fsPromises.stat(filePath);
return true;
} catch (error) {
// ENOENT code - File does not exist
if (error.code === 'ENOENT') {
return false;
} else {
throw error;
}
}
};

export const getAttachments = async (
attachments: TestResult['attachments'],
{ uploadTrace, uploadVideo }: AttachmentsConfig = { uploadTrace: true, uploadVideo: true },
Expand Down Expand Up @@ -122,7 +136,8 @@ export const getAttachments = async (
if (body) {
fileContent = body;
} else {
if (!fs.existsSync(attachmentPath)) {
const isFileExist = await fileExists(attachmentPath);
if (!isFileExist) {
return;
}
fileContent = await fsPromises.readFile(attachmentPath);
Expand Down
Loading