Const is considered a line of code that needs coverage

Currently a const in golang is considered a line of code that needs coverage.

I have tried adding a unit test that uses the const and it still says in is not covered.

const in golang should not be considered for code coverage.

const hello = "world"
or
const { hello = "world" my = "name" }

Hello @prolink007,

Just to be sure, you are importing coverage report from go test, right?
Is the problematic file containing only const and/or enums?

What I suspect to happen is that go test will not consider const, and not add the file at all in the report if it contains only const. In this case, we are computing a coverage approximation, wrongly considering const and enums.

Does it make sense so far?

Here is the exact package that is causing the problem. And it is giving us an error with the const saying it is not covered.

package event

const (
	Create = "create"
	Update = "update"
	Delete = "delete"
)

type Message struct {
}

type Writer interface {
	Write(m []Message) error
}

Okay, that what I expected…

This will affect any file containing only type and enum, as soon as you add executable code, this missing coverage will disappear.

Ticket created: SONARSLANG-530.

In the meantime, if you don’t plan to add code in this file, the workaround is to exclude these files from the coverage (via sonar.coverage.exclusions, or General Settings > Analysis Scope > Code Coverage > Coverage Exclusions in the UI), since there is nothing to cover anyway.

Hope it helps,
Quentin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.