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

Task bre params validation & default values population #466

Merged
merged 25 commits into from
Mar 27, 2020

Conversation

tmilar
Copy link
Contributor

@tmilar tmilar commented Feb 27, 2020

When running a task programatically (ie. from the BuilderRuntimeEnvironment instance), validate task arguments according the constraints specified in the taskDefinition params.

This implies, running a task like this:

await env.run("my-task", {arg1: 123})

would now:

  • have missing optional arguments with a default value available, be properly populated with that value.
  • fail early and in a verbose way, in case of:
    • A required param is missing
    • An argument type is not of the same type of the defined parameter type.

Fixes #191

Tomás Milar and others added 18 commits February 13, 2020 18:03
    * This task was already using an array param, but was
    being implictly handled as an string, and tests were not failing
    because the validation was not in place, also because the
    value was always an empty array thus ignored.

    * Now that we are implementing task arguments type validation,
    this will fail unless a proper type is specified and used, such as
    this newly implemented "array" type.
@@ -20,7 +20,8 @@ export default function() {
.addOptionalVariadicPositionalParam(
"testFiles",
"An optional list of files to test",
[]
[],
types.array
Copy link
Member

Choose a reason for hiding this comment

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

Variadic params are always arrays. IIRC passing a type.string would be the right thing here.

Copy link
Member

Choose a reason for hiding this comment

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

I mean, having types.array is good, but it looses some info, as it's an array of any

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed types.array for now since my original intent would be backwards incompatible, and it's advantages are not clear considering the current param definitions implementation. commit ce41760

/**
* Provides an interface for every valid task argument type.
*/
export interface ArgumentType<T> {
/**
* Type's name.
*/
name: string;
name: ArgumentTypeName;
Copy link
Member

Choose a reason for hiding this comment

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

This prevents plugin authors form creating their own types.

Copy link
Member

Choose a reason for hiding this comment

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

Now that I think of, this is a breaking change also.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted in 91b8b8f

});
}

return Number(strValue);
},
validate: (argName: string, value: any): void => {
const isFloat = Math.floor(value) !== value;
Copy link
Member

Choose a reason for hiding this comment

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

I think this validation should be type of value !== number and possibly check for NaN. float should also accept integers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair enough, I'll fix this

packages/buidler-core/tslint/onlyBuidlerErrorRule.ts Outdated Show resolved Hide resolved
@@ -1,3 +1,4 @@
/* tslint:disable:no-string-literal */ // TODO this is for unit testing priv methods. We shouldn't test these at all?
Copy link
Member

Choose a reason for hiding this comment

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

Yep, this isn't ideal, but I guess it's ok for now. If/when we modify the argument parser (probably soon, right?) we can refactor its tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but would it be worth it? Do we have plans to add further functionality regarding this topic? If it were the case maybe we should write it down as an issue/docs.

…st' task."

    * After PR discussion we've ended up deciding that it's unneeded,
    according to the current implementation logic (ie. variadic params are
    typed "string" even though they all make for one array of strings).

    * Also it changes the API so it would be a breaking change.

This reverts commit df9267d.
…ations, and make it optional.

    * Remove from the interface to prevent introducing a breaking change.
    * Make it optional as well - don't validate if type
    is not defined, or if a custom type doesn't have a validation
    implemented.
@tmilar tmilar force-pushed the task-bre-params branch 2 times, most recently from dfaf2ea to 7b920bb Compare March 27, 2020 15:55
    * Also stop wrapping type.validate() error to enforce
    a BuidlerError. It should already be a BuidlerError,
    otherwise let it bubble and be handled by some upper layer.
Copy link
Member

@alcuadrado alcuadrado left a comment

Choose a reason for hiding this comment

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

🚀

@tmilar tmilar merged commit 5ec33b6 into development Mar 27, 2020
@tmilar tmilar deleted the task-bre-params branch March 27, 2020 20:45
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better handling of arguments in BuidlerRuntimeEnvironment#run
2 participants