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: check command input is valid array #290

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 12 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ async function run() {
containerDef.image = imageURI;

if (command) {
containerDef.command = command.split(' ')
const invalidCommandErrorMessage = `Invalid command: '${command}'. command must be a valid JSON array.`
try {
const parsedCommand = JSON.parse(command);
if (!Array.isArray(parsedCommand)) {
// The parsed object is not an array.
throw new Error(invalidCommandErrorMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, please correct me if my understanding here is not correct:

Taking the example input you mentioned in PR description: alembic revision --autogenerate -m "Your message here"

The input will be marked as invalid command in this case, but before your change it would be considered correct but would eventually fail, Is this correct?

}
containerDef.command = parsedCommand;
} catch (e) {
// The string is not a valid array.
throw new Error(invalidCommandErrorMessage);
}
}

if (envFiles) {
Expand Down
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,18 @@ async function run() {
containerDef.image = imageURI;

if (command) {
containerDef.command = command.split(' ')
const invalidCommandErrorMessage = `Invalid command: '${command}'. command must be a valid JSON array.`
try {
const parsedCommand = JSON.parse(command);
if (!Array.isArray(parsedCommand)) {
// The parsed object is not an array.
throw new Error(invalidCommandErrorMessage);
}
containerDef.command = parsedCommand;
} catch (e) {
// The string is not a valid array.
throw new Error(invalidCommandErrorMessage);
}
}

if (envFiles) {
Expand Down
19 changes: 18 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ describe('Render task definition', () => {
.mockReturnValueOnce('awslogs')
.mockReturnValueOnce('awslogs-create-group=true\nawslogs-group=/ecs/web\nawslogs-region=us-east-1\nawslogs-stream-prefix=ecs')
.mockReturnValueOnce('key1=value1\nkey2=value2')
.mockReturnValueOnce('npm start --nice --please');
.mockReturnValueOnce('["npm", "start", "--nice", "--please"]');

await run();

Expand Down Expand Up @@ -489,4 +489,21 @@ describe('Render task definition', () => {
}, null, 2)
);
});

test('error returned for invalid command', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('task-definition.json')
.mockReturnValueOnce('web')
.mockReturnValueOnce('nginx:latest')
.mockReturnValueOnce('EXAMPLE=here')
.mockReturnValueOnce('awslogs')
.mockReturnValueOnce('awslogs-create-group=true\nawslogs-group=/ecs/web\nawslogs-region=us-east-1\nawslogs-stream-prefix=ecs')
.mockReturnValueOnce('key1=value1\nkey2=value2')
.mockReturnValueOnce('npm start --nice --please');

await run();

expect(core.setFailed).toBeCalledWith('Invalid command: \'npm start --nice --please\'. command must be a valid JSON array.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please feel free to add additional insights to this, but seems like this change would render previously "valid" commands as "invalid" for other users.

For example npm start --nice --please would be expected to pass earlier but with this change, users would be required to change the format in which they provide the command field.

Is this correct?

});
});
Loading