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

bazel coverage does not calculate coverage correctly #4247

Open
bejelith opened this issue Feb 4, 2025 · 3 comments
Open

bazel coverage does not calculate coverage correctly #4247

bejelith opened this issue Feb 4, 2025 · 3 comments

Comments

@bejelith
Copy link

bejelith commented Feb 4, 2025

when running bazel coverage on this code i get 0 lines covered in the report
types.go

func WithReadOnly(readOnly bool) DataSourceOption {
	return func(d *DataSourceRequest) *DataSourceRequest {
		d.ReadOnly = readOnly
		return d
	}
}

types_test.go

func TestWithReadOnly(t *testing.T) {
	tests := []struct {
		name     string
		readOnly bool
	}{
		{"Set readOnly to true", true},
		{"Set readOnly to false", false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ds := &DataSourceRequest{}
			option := WithReadOnly(tt.readOnly)
			option(ds)
			assert.Equal(t, tt.readOnly, ds.ReadOnly)
		})
	}
}

but running go test -coverprofile=output ./... returns the correct amount of covered lines.
Im running coverage with the following config

coverage --experimental_split_coverage_postprocessing
coverage --experimental_fetch_all_coverage_outputs
coverage --combined_report=lcov
coverage --nocache_test_results

Is this a bug or am I missing something?

@fmeum
Copy link
Member

fmeum commented Feb 4, 2025

Bazel excludes coverage of test code by default. Could you try --instrument_test_targets?

@bejelith
Copy link
Author

bejelith commented Feb 4, 2025

@fmeum ur answer fixed my issue but now my coverage report is full of _test.go files.

Let me be more specific the code is in two files: in types.go and the test (TestWithReadOnly) is in types_test.go

It'd espect types.go to have 100% coverage without having to instrument types_test.go

@fmeum
Copy link
Member

fmeum commented Feb 4, 2025

Could you share a self-contained reproducer? That would make it easier to understand your target structure and experiment with different settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants