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

community: cleanup code, reduce complexity, add make targets #368

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

dhiller
Copy link
Contributor

@dhiller dhiller commented Dec 11, 2024

What this PR does / why we need it:

Reduces complexity in:

  • validators/cmd/sigs-validator
  • generators/cmd/devstats

Adds make targets:

  • lint - will run golangci-lint, additionally gocyclo with > 10
  • test - will run go tests

/kind cleanup
/wg code-quality

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

@kubevirt-bot
Copy link

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@kubevirt-bot kubevirt-bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. wg/code-quality Denotes an issue or PR that relates to the code-quality working group. labels Dec 11, 2024
@kubevirt-bot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign rmohr for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@dhiller dhiller marked this pull request as ready for review December 12, 2024 13:28
@kubevirt-bot kubevirt-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 12, 2024
dhiller added a commit to dhiller/project-infra that referenced this pull request Dec 12, 2024
Based on PR kubevirt/community#368
where new make targets test and lint are introduced we are adding new
presubmit jobs that execute them.

Signed-off-by: Daniel Hiller <[email protected]>
dhiller added a commit to dhiller/project-infra that referenced this pull request Dec 13, 2024
Based on PR kubevirt/community#368
where new make targets test, lint and coverage are introduced we are
adding new presubmit jobs that execute them.

Signed-off-by: Daniel Hiller <[email protected]>
@aburdenthehand
Copy link
Member

/cc @EdDev for wg-code-quality

@kubevirt-bot kubevirt-bot requested a review from EdDev December 16, 2024 09:20
@kubevirt-bot
Copy link

@aburdenthehand: GitHub didn't allow me to request PR reviews from the following users: for.

Note that only kubevirt members and repo collaborators can review this PR, and authors cannot review their own PRs.

In response to this:

/cc @EdDev for wg-code-quality

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-sigs/prow repository.

"testing"
)

func Test_validateGroups(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Is the _ convention? I would expect to see TestValidateGroups

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, good q - I believe that was an autogenerated test func name that I didn't really look at.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@xpivarc I investigated a bit - reason for this weird name is that Goland has auto-generated the func name for the non-exported func that is tested.

Now if I rename the func as you requested the IDE will not find the tested func anymore.

Now my q is: does it matter that much? I'd rather expect the test func name to be as close to the name of the func it tests. WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

Well that is weird, see https://go.dev/doc/code#Testing where TestXXX should work. I won't insist but my eye found it weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that for exported functions it should be as you said, however for non-exported functions whose name is starting with a lowercase letter the usage of an underscore might be more obvious.

Compare

func DoFuncExported() {}

func TestDoFuncExported() {}

to

func doFuncNonExported() {}

func Test_doFuncNonExported() {}

where the latter is the matching case here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And again - this is just to help the IDE to find the test for the func and the func for the test func.

@lyarwood
Copy link
Member

lyarwood commented Jan 9, 2025

/cc

@kubevirt-bot kubevirt-bot requested a review from lyarwood January 9, 2025 10:30
Makefile Outdated
go test ./... -coverprofile=/tmp/coverage.out
covreport -i /tmp/coverage.out -o ${COVERAGE_OUTPUT_PATH}

install-metrics-binaries:
Copy link
Member

Choose a reason for hiding this comment

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

nit - Any reason to have another target with an unrelated name for installing a linting tool?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably over-engineering :)

Makefile Outdated
covreport -i /tmp/coverage.out -o ${COVERAGE_OUTPUT_PATH}

install-metrics-binaries:
if ! command -V golangci-lint; then curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ${GOPATH}/bin ${GOLANGCI_LINT_VERSION} ; fi
Copy link
Member

Choose a reason for hiding this comment

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

nit - It would be nicer to install this into a local bin to avoid changing the users version

https://github.com/kubevirt/common-instancetypes/blob/cef52ded0abde7b37564faa619aa745d10b5e335/Makefile#L151-L154

.golangci.yaml Outdated
- gocyclo
linters-settings:
gocyclo:
min-complexity: 10
Copy link
Member

Choose a reason for hiding this comment

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

Missing new line

Adds a make target `lint` that will be used to run enabled linters,
which are configured via .golangci.yaml .

Initially only the default linters are enabled, additionally the
gocyclo linter with ccn > 10.

Signed-off-by: Daniel Hiller <[email protected]>
Adds a `test` target to the Makefile that executes the go tests.

Signed-off-by: Daniel Hiller <[email protected]>
Adds unit tests for sig validation as precondition of refactoring.

Signed-off-by: Daniel Hiller <[email protected]>
* reduce complexity by converting all groups into a single map
* replace if then with early return
* extract validateLeads, use that in two places

Signed-off-by: Daniel Hiller <[email protected]>
Signed-off-by: Daniel Hiller <[email protected]>
Signed-off-by: Daniel Hiller <[email protected]>
@dhiller
Copy link
Contributor Author

dhiller commented Jan 16, 2025

@lyarwood thank you for the review and apologies for the long wait time. I have addressed your comments, but since a rebase happened some linting errors had entered the code base again. I have fixed these now.

PTAL, thank you! 🙏

Copy link
Member

@lyarwood lyarwood left a comment

Choose a reason for hiding this comment

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

/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Jan 16, 2025
@dhiller
Copy link
Contributor Author

dhiller commented Jan 17, 2025

🏓 @aburdenthehand 🙂

@aburdenthehand
Copy link
Member

@xpivarc - you happy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dco-signoff: yes Indicates the PR's author has DCO signed all their commits. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm Indicates that a PR is ready to be merged. size/XL wg/code-quality Denotes an issue or PR that relates to the code-quality working group.
Projects
Status: Needs review
Development

Successfully merging this pull request may close these issues.

5 participants