Skip to content

Commit

Permalink
EPMRPP-84451 || Rewrite the getAttachments utility to be fully async
Browse files Browse the repository at this point in the history
  • Loading branch information
AliakseiLiasnitski committed Mar 27, 2024
1 parent 9ece26c commit 059c6de
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
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
16 changes: 15 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export const getAttachments = async (
if (body) {
fileContent = body;
} else {
if (!fs.existsSync(attachmentPath)) {
const isFileExist = await fileExists(attachmentPath);

Check failure on line 125 in src/utils.ts

View workflow job for this annotation

GitHub Actions / test

'fileExists' was used before it was defined
if (!isFileExist) {
return;
}
fileContent = await fsPromises.readFile(attachmentPath);
Expand All @@ -142,6 +143,19 @@ export const getAttachments = async (
return (await Promise.all(readFilePromises)).filter(Boolean);
};

export const fileExists = async (path: string) => {

Check failure on line 146 in src/utils.ts

View workflow job for this annotation

GitHub Actions / test

'path' is already declared in the upper scope
try {
await fsPromises.stat(path);
return true;
} catch (error) {
if (error.code === 'ENOENT') { // File does not exist

Check failure on line 151 in src/utils.ts

View workflow job for this annotation

GitHub Actions / test

Insert `⏎·····`
return false;
} else {
throw error;
}
}
}

Check failure on line 157 in src/utils.ts

View workflow job for this annotation

GitHub Actions / test

Insert `;`

export const isErrorLog = (message: string): boolean => {
return message.toLowerCase().includes('error');
};
Expand Down

0 comments on commit 059c6de

Please sign in to comment.