Skip to content

Commit

Permalink
Merge pull request #1085 from Green-Software-Foundation/unit-test
Browse files Browse the repository at this point in the history
Add unit test coverage
  • Loading branch information
narekhovhannisyan authored Dec 4, 2024
2 parents 13e29bd + 46a9f06 commit feea8f7
Show file tree
Hide file tree
Showing 26 changed files with 1,223 additions and 38 deletions.
32 changes: 29 additions & 3 deletions src/__mocks__/fs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const readFile = async (filePath: string) => {
throw new Error('file not found');
}

if (filePath.includes('fail-yaml')) {
throw new Error('file not found');
}

if (filePath.includes('fail-csv-reader.csv')) {
return `
cpu-cores-available,≈ç≈¬˚∆∑∂´®øˆ´cpu-cores-utilized, ---- cpu-manufacturer,cpu-model-name,cpu-tdp,gpu-count,gpu-model-name,Hardware Information on AWS Documentation & Comments,instance-class,instance-storage,memory-available,platform-memory,release-date,storage-drives
Expand Down Expand Up @@ -76,6 +80,15 @@ export const writeFile = async (pathToFile: string, content: string) => {
const fileContentObject = JSON.parse(fileContent);
const parsedContent = JSON.parse(content);

for (const property in fileContentObject) {
expect(parsedContent).toHaveProperty(property);
}
} else if (pathToFile.includes('package.json-npm2')) {
const updatedPath = pathToFile.replace('-npm2', '');
const fileContent = await fsAsync.readFile(updatedPath, 'utf8');
const fileContentObject = JSON.parse(fileContent);
const parsedContent = JSON.parse(content);

for (const property in fileContentObject) {
expect(parsedContent).toHaveProperty(property);
}
Expand Down Expand Up @@ -112,7 +125,7 @@ export const appendFile = (file: string, appendContent: string) =>
`${file}${appendContent}`;

export const stat = async (filePath: string) => {
if (filePath === 'true') {
if (filePath === 'true' || filePath === 'mock-path') {
return true;
} else {
throw new Error('File not found.');
Expand All @@ -128,7 +141,7 @@ export const access = async (directoryPath: string) => {
};

export const unlink = async (filePath: string) => {
if (filePath === 'true') {
if (filePath === 'true' || filePath === 'mock-path') {
return;
} else {
throw new Error('File not found.');
Expand All @@ -148,14 +161,19 @@ export const readdir = (directoryPath: string) => {
return ['subdir/file2.yml', 'file1.yaml'];
}

if (directoryPath.includes('mock-nested-directory')) {
return ['mock-sub-directory'];
}

return [];
};

export const lstat = (filePath: string) => {
if (
filePath.includes('mock-directory') ||
filePath.includes('mock-sub-directory/subdir') ||
filePath === 'true'
filePath === 'true' ||
filePath.includes('npm-node-test')
) {
return {
isDirectory: () => true,
Expand All @@ -169,3 +187,11 @@ export const lstat = (filePath: string) => {
}
return;
};

export const rm = (path: string) => {
if (path.includes('npm-node-test')) {
return;
} else {
throw new Error('File not found.');
}
};
1 change: 1 addition & 0 deletions src/__mocks__/plugin/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {Mockavizta} from './mockavizta';
export {SciEmbodied} from './sci-embodied';
28 changes: 28 additions & 0 deletions src/__mocks__/plugin/lib/sci-embodied/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {PluginFactory} from '@grnsft/if-core/interfaces';
import {PluginParams} from '@grnsft/if-core/types';

export const SciEmbodied = PluginFactory({
metadata: {
inputs: {
vCPUs: {
description: 'number of CPUs allocated to an application',
unit: 'CPUs',
'aggregation-method': {
time: 'copy',
component: 'copy',
},
},
},
outputs: {
'embodied-carbon': {
description: 'embodied carbon for a resource, scaled by usage',
unit: 'gCO2eq',
'aggregation-method': {
time: 'sum',
component: 'sum',
},
},
},
},
implementation: async (inputs: PluginParams[]) => inputs,
});
26 changes: 26 additions & 0 deletions src/__tests__/common/util/debug-logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,30 @@ describe('util/debug-logger: ', () => {

expect(debugSpy).not.toHaveBeenCalled();
});

it('logs empty messages when the message is `\n`.', () => {
console.debug('\n');

expect(logSpy).toHaveBeenCalledTimes(1);
expect(logSpy).toHaveBeenCalledWith();
});

it('logs messages when the message contains `**Computing`.', () => {
const logMessage = '**Computing some pipline message';
console.debug(logMessage);

expect(debugSpy).toHaveBeenCalledTimes(1);
expect(debugSpy).toHaveBeenCalledWith(expect.stringContaining(logMessage));
});

it('logs messages when the message is a number.', () => {
const logMessage = 10;
console.debug(logMessage);

expect(debugSpy).toHaveBeenCalledTimes(1);
expect(debugSpy).toHaveBeenCalledWith(expect.stringContaining('DEBUG:'));
expect(debugSpy).toHaveBeenCalledWith(
expect.stringContaining(logMessage.toString())
);
});
});
28 changes: 19 additions & 9 deletions src/__tests__/common/util/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('util/fs: ', () => {
expect(fsReaddirSpy).toHaveBeenCalledWith('/mock-empty-directory');
});

it('returns YAML files in the directory', async () => {
it('returns YAML files in the directory.', async () => {
const fsReaddirSpy = jest.spyOn(fs, 'readdir');
jest
.spyOn(fs, 'lstat')
Expand All @@ -108,19 +108,29 @@ describe('util/fs: ', () => {
expect(fsReaddirSpy).toHaveBeenCalledWith('/mock-directory');
});

it('recursively finds YAML files in nested directories.', async () => {
it('recursively finds YAML files if the directory exists.', async () => {
const fsReaddirSpy = jest.spyOn(fs, 'readdir');
jest
.spyOn(fs, 'lstat')
.mockResolvedValue({isDirectory: () => false} as any);
const result = await getYamlFiles('/mock-sub-directory');

jest.spyOn(fs, 'lstat').mockImplementation((path: any) => {
return Promise.resolve({
isDirectory: () => {
if (path.endsWith('.yaml') || path.endsWith('.yml')) {
return false;
}
return true;
},
} as any);
});

const path = '/mock-nested-directory';
const result = await getYamlFiles(path);

expect.assertions(2);
expect(result).toEqual([
'/mock-sub-directory/subdir/file2.yml',
'/mock-sub-directory/file1.yaml',
'/mock-nested-directory/mock-sub-directory/subdir/file2.yml',
'/mock-nested-directory/mock-sub-directory/file1.yaml',
]);
expect(fsReaddirSpy).toHaveBeenCalledWith('/mock-directory');
expect(fsReaddirSpy).toHaveBeenCalledWith(path);
});
});

Expand Down
Loading

0 comments on commit feea8f7

Please sign in to comment.