-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Refactor the usage of MustCompile and simplify function signatures #4927
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
Conversation
|
The following is the coverage report on the affected files.
|
|
/assign @lbernick |
fdc8963 to
0a7871e
Compare
|
The following is the coverage report on the affected files.
|
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: vdemeester The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Prior to this commit: 1. MustCompile sometimes is used inside a function though it is meant for initializing global variables holding compiled regular expressions according to this ([docs](https://pkg.go.dev/regexp#MustCompile)). Using it in a function could possibly panic. 2. function `extractExpressionFromString` and `extractVariablesFromString` return too many values i.e. extracted value (slice/string), present (bool) and errorMessage (string) In this fix: - 1. regex compiled from from MustCompile is changed to be global, or MustCompile is replaced with Compile so that we can return error rather than causing potential panic. - 2. the two functions signatures are simplified to just return extracted value and error.
0a7871e to
2c7ad87
Compare
|
The following is the coverage report on the affected files.
|
|
/test tekton-pipeline-unit-tests |
|
/test pull-tekton-pipeline-integration-tests |
|
/test pull-tekton-pipeline-alpha-integration-tests |
1 similar comment
|
/test pull-tekton-pipeline-alpha-integration-tests |
| return nil | ||
| } | ||
|
|
||
| // Extract a the first full string expressions found (e.g "$(input.params.foo)"). Return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: typo in this docstring (extra "a")
| vs, err := extractVariablesFromString(value, prefix) | ||
| if err != nil { | ||
| return &apis.FieldError{ | ||
| Message: fmt.Sprintf("fail to extract variables from string: %v", err), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can just use the error message as is (here and at the other call sites)
| } | ||
| } | ||
|
|
||
| firstMatch, _ := extractExpressionFromString(value, prefix) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably best to handle the error being returned by this function
@chuangw6 thank you for this 🙏 I recommend adding more unit test. Impacting 5% coverage during the refactor is not ideal specially with such low coverage to begin with 🔅 . Please try to make sure we at least maintain the code coverage as is in a refactoring PRs. @vdemeester @lbernick thoughts? |
Definitely agree more testing would be nice here for these helpers! Not sure how much to read into the coverage tool output or how much testing of this package is appropriate to tackle for a refactor-- will leave that up to Chuang |
|
/retest |
Thanks @lbernick! Please look for the Thanks @chuangw6 for your understanding 👍 Appreciate all the simplifications here, the An alternative could be to introduce the unit tests first in a separate PR to bump the existing coverage. |
|
thanks @pritidesai, sgtm! |
|
@chuangw6: PR needs rebase. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
Thanks @pritidesai @lbernick . sgtm! I'll add that! |
|
@pritidesai in light of our discussion yesterday at the pipelines WG around not testing non-exported functions, what do you think is the best way forward here? |
Ideally even if we are not testing non exported functions, we should cover / aim to cover them (through exported ones). If we can’t, this usually mean the unexported function covers more cases that what is exported (and thus is more complex that it needs to be 🤭). |
|
Issues go stale after 90d of inactivity. /lifecycle stale Send feedback to tektoncd/plumbing. |
|
@chuangw6 are you still working on this? would you mind rebasing or closing? |
Sorry I saw this as non-urgent and switched to other stuff before. Happy to get this cleaned up and move it forward. |

Changes
Prior to this commit:
initializing global variables holding compiled regular expressions
according to this (docs). Using
it in a function could possibly panic.
extractExpressionFromStringandextractVariablesFromStringreturn too many values i.e. extracted value (slice/string), present (bool)
and errorMessage (string)
In this fix:
MustCompile is replaced with Compile so that we can return error rather
than causing potential panic.
value and error.
Submitter Checklist
As the author of this PR, please check off the items in this checklist:
functionality, content, code)
(if there are no user facing changes, use release note "NONE")
Release Notes