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

feat: clean empty arrays and objects from the task def file #52

Merged
merged 2 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 30 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,43 @@ function findAppSpecKey(obj, keyName) {
throw new Error(`AppSpec file must include property '${keyName}'`);
}

function undefinedOrNullReplacer(_, value) {
if (value === null || value === undefined) {
function isEmptyValue(value) {
if (value === null || value === undefined || value === '') {
return true;
}

if (Array.isArray(value) && value.length === 0) {
return true;
}

if (typeof value === 'object' && Object.values(value).length === 0) {
return true;
}

return false;
}

function emptyValueReplacer(_, value) {
if (isEmptyValue(value)) {
return undefined;
}

if (typeof value === 'object') {
for (var childValue of Object.values(value)) {
if (!isEmptyValue(childValue)) {
// the object has at least one non-empty property
return value;
}
}
// the object has no non-empty property
return undefined;
}

return value;
}

function cleanNullKeys(obj) {
return JSON.parse(JSON.stringify(obj, undefinedOrNullReplacer));
return JSON.parse(JSON.stringify(obj, emptyValueReplacer));
}

function removeIgnoredAttributes(taskDef) {
Expand Down
38 changes: 38 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,44 @@ describe('Deploy to ECS', () => {
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family'});
});

test('cleans empty arrays out of the task definition contents', async () => {
fs.readFileSync.mockImplementation((pathInput, encoding) => {
if (encoding != 'utf8') {
throw new Error(`Wrong encoding ${encoding}`);
}

return '{ "tags": [], "family": "task-def-family" }';
});

await run();
expect(core.setFailed).toHaveBeenCalledTimes(0);
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, { family: 'task-def-family'});
});

test('cleans empty strings and objects out of the task definition contents', async () => {
fs.readFileSync.mockImplementation((pathInput, encoding) => {
if (encoding != 'utf8') {
throw new Error(`Wrong encoding ${encoding}`);
}

return '{ "memory": "", "containerDefinitions": [ { "name": "sample-container", "logConfiguration": {}, "repositoryCredentials": { "credentialsParameter": "" }, "cpu": 0, "essential": false } ], "requiresCompatibilities": [ "EC2" ], "family": "task-def-family" }';
});

await run();
expect(core.setFailed).toHaveBeenCalledTimes(0);
expect(mockEcsRegisterTaskDef).toHaveBeenNthCalledWith(1, {
family: 'task-def-family',
containerDefinitions: [
{
name: 'sample-container',
cpu: 0,
essential: false
}
],
requiresCompatibilities: [ 'EC2' ]
});
});

test('cleans invalid keys out of the task definition contents', async () => {
fs.readFileSync.mockImplementation((pathInput, encoding) => {
if (encoding != 'utf8') {
Expand Down