Skip to content

Commit

Permalink
Add tests and error handling for ExtraVarsTransformer
Browse files Browse the repository at this point in the history
In this commit, tests have been added for the ExtraVarsTransformer function to ensure proper functioning and error handling. The function now includes a try-catch block to manage potential errors during the transformation of extraVars. In addition, import statements in the ExtraVars.test.ts file have been rearranged.
  • Loading branch information
manu committed Jul 2, 2024
1 parent e3db5cd commit 32a88d9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
22 changes: 13 additions & 9 deletions server/src/integrations/ansible/utils/ExtraVarsTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { API } from 'ssm-shared-lib';
import logger from '../../../logger';

function transformExtraVars(extraVars: API.ExtraVars) {
const stringifyObject = extraVars
.map((e) => {
return `"${e.extraVar}": "${e.value}"`;
})
.reduce((previousValue, currentValue) => {
return previousValue + ',' + currentValue;
});
logger.debug(stringifyObject);
return JSON.parse('{' + stringifyObject + '}');
try {
const stringifyObject = extraVars
.map((e) => {
return `"${e.extraVar}": "${e.value}"`;
})
.reduce((previousValue, currentValue) => {
return previousValue + ',' + currentValue;
});
logger.debug(stringifyObject);
return JSON.parse('{' + stringifyObject + '}');
} catch (error: any) {
throw new Error('Error during transformExtraVars');
}
}

export default {
Expand Down
2 changes: 1 addition & 1 deletion server/src/tests/integrations/ansible/ExtraVars.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, vi, expect, describe } from 'vitest';
import { describe, expect, test, vi } from 'vitest';
import extraVarsOps from '../../../integrations/ansible/utils/ExtraVars';

describe('test ExtraVars', () => {
Expand Down
59 changes: 59 additions & 0 deletions server/src/tests/integrations/ansible/ExtraVarsTransformer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, expect, test } from 'vitest';
import { API } from 'ssm-shared-lib';
import ExtraVarsTransformer from '../../../integrations/ansible/utils/ExtraVarsTransformer';

describe('transformExtraVars function', () => {
test('should transform list of extraVars into object correctly', () => {
const input: API.ExtraVar[] = [
{ extraVar: 'var1', value: 'value1' },
{ extraVar: 'var2', value: 'value2' },
{ extraVar: 'var3', value: 'value3' },
];
const result = ExtraVarsTransformer.transformExtraVars(input);
expect(result).toEqual({
var1: 'value1',
var2: 'value2',
var3: 'value3',
});
});

test('empty input', () => {
const input: API.ExtraVar[] = [];
expect(() => ExtraVarsTransformer.transformExtraVars(input)).toThrow(
'Error during transformExtraVars',
);
});

test('should handle missing value field in extraVar', () => {
const input: API.ExtraVar[] = [{ extraVar: 'var1', value: 'value1' }, { extraVar: 'var2' }];
const result = ExtraVarsTransformer.transformExtraVars(input);
expect(result).toEqual({
var1: 'value1',
var2: 'undefined',
});
});

test('should handle missing extraVar field', () => {
// @ts-expect-error incorrect typing test
const input: API.ExtraVar[] = [{ value: 'value1' }, { extraVar: 'var2', value: 'value2' }];
const result = ExtraVarsTransformer.transformExtraVars(input);
expect(result).toEqual({
undefined: 'value1',
var2: 'value2',
});
});

test('undefined input', () => {
// @ts-expect-error incorrect typing test
expect(() => ExtraVarsTransformer.transformExtraVars(undefined)).toThrow(
'Error during transformExtraVars',
);
});

test('null input', () => {
// @ts-expect-error incorrect typing test
expect(() => ExtraVarsTransformer.transformExtraVars(null)).toThrow(
'Error during transformExtraVars',
);
});
});

0 comments on commit 32a88d9

Please sign in to comment.