Skip to content

Enforce all code dirs have tests#453

Closed
greghaynes wants to merge 1 commit into
knative:masterfrom
greghaynes:feature/enforce-stub-tests
Closed

Enforce all code dirs have tests#453
greghaynes wants to merge 1 commit into
knative:masterfrom
greghaynes:feature/enforce-stub-tests

Conversation

@greghaynes
Copy link
Copy Markdown
Contributor

Proposed Changes

  • In order to get accurate coverage results we attempt to always have a
    _test.go file in directories with code. Add a test to enfoce this in CI
    and add stub_tests.go to dirs which fail this test.

Release Note

NONE

In order to get accurate coverage results we attempt to always have a
_test.go file in directories with code. Add a test to enfoce this in CI
and add stub_tests.go to dirs which fail this test.
@knative-prow-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: greghaynes
To fully approve this pull request, please assign additional approvers.
We suggest the following additional approver: inlined

If they are not already assigned, you can assign the PR to them by writing /assign @inlined in a comment when ready.

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

The pull request process is described here

Details 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

@knative-prow-robot knative-prow-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Sep 19, 2018
Copy link
Copy Markdown
Contributor

@n3wscott n3wscott left a comment

Choose a reason for hiding this comment

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

the majority of the directories added will not have code in them, or they are exec package main places that are hard to unit test

Comment thread cmd/webhook/stub_test.go

import "testing"

func TestStub(t *testing.T) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

cmd dirs don't tend to have tests.

limitations under the License.
*/

package logkey
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

there is no code in logkey dir

limitations under the License.
*/

package eventing
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

there is no code here

limitations under the License.
*/

package logkey
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

there is no code here

limitations under the License.
*/

package main
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this has the same rules with cmd, not sure it is testable

limitations under the License.
*/

package main
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

question adding stub for package main

limitations under the License.
*/

package testing
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

unit tests for testing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

testing inception? :)

I can add this to the skip list.

@greghaynes
Copy link
Copy Markdown
Contributor Author

Theres a couple classes of untested things here -

cmd/things which are difficult to test: I don't see much of a downside to having these in the coverage reports. I agree these can be a pain to test and the cost/benefit might not be there but there also shouldn't be harm in keeping them as part of the coverage reports. If they start to be a big drain on coverage amounts that's probably a good signal that we should move more things out of cmd / find a way to test parts of it.

dirs which only have a register.go / constants defined (e.g. pkg/apis/eventing/): I agree its not particularly useful to assert we have tests defined for a dir containing only constants. I could add a skip for dirs which only contain a register.go (if thats a strong enough convention) or we could manually add these dirs to the skip grep. If we want to get really fancy maybe we can add some comment metadata which to these pure-constants files which tell the check to ignore them :).

@n3wscott
Copy link
Copy Markdown
Contributor

Oh! Neat, I missed that there was a magic script that is making these. Cool!!

@n3wscott
Copy link
Copy Markdown
Contributor

In that case it might also need to get hooked up to one of the hack scripts, perhaps the presubmit if we have one

@greghaynes
Copy link
Copy Markdown
Contributor Author

Yep, this script is just checking these files exist but has some support for overrides. I added it to presubmit but im not sure if we have to add it anywhere else for the CI jobs to pick it up?

Comment thread test/presubmit-tests.sh
# reports resulting in inaccurately high coverage results. Fix by adding a
# stub_tests.go with no tests to these directories.
# This function namespaced with eventing to allow adding to test-infra without name collision
function check_eventing_subdirs_without_tests() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this kind of check is too hard. You're checking all directories and failing the build test job if there are directories without tests, even if they're unrelated to the current PR. This, at least, add a burden to PRs unrelated to these empty directories. Also, you're now enforcing a code coverage blocker, and as such I think it should be (1) agreed by the leads (instead of being just decided in a PR) and (2) only imposed after knative/eventing is in a better coverage state (so the burden is lower).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

By the way, if the goal is just to get accurate test coverage and the number is wrong because of missing test files, the code coverage tool should be responsible for dealing that, not the repo or the developers. @steuhs is the right point of contact about the coverage tool.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The flow is fix repo to pass and add this assertion in one PR, you are then guarded against failing this test from that point on in future PR's. This aspect isn't really different than any other test.

I think its less the tool and more that you need to make a distinction between intention. Cover works by being run on a directory and (correctly) reports unknown whenever there is no coverage in a dir. We need to then distinguish between subdirs such as pkg/client where we intentionally lack coverage (not our code) and dirs where we simply haven't provided any.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Makes sense. Please merge knative/test-infra#134 first and update the vendored test-infra to use the helper method.

@adrcunha
Copy link
Copy Markdown
Contributor

It's been a while since the coverage job was updated to correctly handle directories without tests, so this PR can be closed. It's been stale for 6 months.

@adrcunha adrcunha closed this Mar 20, 2019
Cali0707 pushed a commit to Cali0707/eventing that referenced this pull request Feb 1, 2024
…tion references (knative#7455) (knative#453)

* Allow configuring disallow cross namespaces Brokers configuration ref

Instead of always allowing to specify cross namespace configuration
references for Broker allow users to configure whether to disallow
such references as it might be problematic in multi tenant
environments.



* Codegen



---------

Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
Co-authored-by: Pierangelo Di Pilato <pierdipi@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants