Skip to content

Commit

Permalink
Merge pull request #579 from Green-Software-Foundation/exhaust-units
Browse files Browse the repository at this point in the history
Exhaust unit tests
  • Loading branch information
MariamKhalatova authored Apr 11, 2024
2 parents 767a253 + e32884d commit 48715da
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
98 changes: 98 additions & 0 deletions src/__tests__/unit/lib/exhaust.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
jest.mock('fs', () => require('../../../__mocks__/fs'));

import {exhaust} from '../../../lib/exhaust';

import {ERRORS} from '../../../util/errors';

import {STRINGS} from '../../../config';

const {CliInputError, ModuleInitializationError} = ERRORS;
const {INVALID_EXHAUST_PLUGIN} = STRINGS;

describe('lib/exhaust: ', () => {
describe('exhaust(): ', () => {
const spy = jest.spyOn(global.console, 'log');

beforeEach(() => {
spy.mockReset();
});

it('returns void if no exhaust plugin selected, log should be called.', async () => {
const tree = {};
const context = {
initialize: {
outputs: null,
},
};

// @ts-ignore
const result = await exhaust(tree, context);

expect(result).toBeUndefined();
expect(spy).toHaveBeenCalledTimes(1);
spy.mockReset();
});

it('uses log exhaust plugin as export.', async () => {
const tree = {};
const context = {
initialize: {
outputs: ['log'],
},
};

// @ts-ignore
await exhaust(tree, context);
expect(spy).toHaveBeenCalledTimes(1);
});

it('rejects with cli input error if output path is not provided with yaml.', async () => {
const tree = {};
const context = {
initialize: {
outputs: ['yaml'],
},
};
const expectedMessage = 'Output path is required.';

expect.assertions(2);

try {
// @ts-ignore
await exhaust(tree, context);
} catch (error) {
expect(error).toBeInstanceOf(CliInputError);

if (error instanceof CliInputError) {
expect(error.message).toEqual(expectedMessage);
}
}
});

it('rejects with module init error if output module is not supported.', async () => {
const tree = {};
const context = {
initialize: {
outputs: ['mock'],
},
};
const expectedMessage = INVALID_EXHAUST_PLUGIN(
context.initialize.outputs[0]
);

expect.assertions(2);

try {
// @ts-ignore
await exhaust(tree, context);
} catch (error) {
expect(error).toBeInstanceOf(ModuleInitializationError);

if (error instanceof ModuleInitializationError) {
expect(error.message).toEqual(expectedMessage);
}
}
});
});
});
13 changes: 10 additions & 3 deletions src/lib/exhaust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const initializeExhaustPlugins = (plugins: string[]) =>
plugins.map(initializeExhaustPlugin);

/**
* factory method for exhaust plugins
* Factory method for exhaust plugins.
*/
const initializeExhaustPlugin = (name: string): ExhaustPluginInterface => {
switch (name) {
Expand All @@ -44,7 +44,11 @@ const initializeExhaustPlugin = (name: string): ExhaustPluginInterface => {
* Output manager - Exhaust.
* Grabs output plugins from context, executes every.
*/
export const exhaust = (tree: any, context: Context, outputPath?: string) => {
export const exhaust = async (
tree: any,
context: Context,
outputPath?: string
) => {
const outputPlugins = context.initialize.outputs;

if (!outputPlugins) {
Expand All @@ -54,5 +58,8 @@ export const exhaust = (tree: any, context: Context, outputPath?: string) => {
}

const exhaustPlugins = initializeExhaustPlugins(outputPlugins);
exhaustPlugins.forEach(plugin => plugin.execute(tree, context, outputPath));

for await (const plugin of exhaustPlugins) {
await plugin.execute(tree, context, outputPath);
}
};

0 comments on commit 48715da

Please sign in to comment.