Guidance or suggestions needed for when .proto files are generating classes
See original GitHub issueI have projects that have .proto files in them and that are using the Grpc.Tools nuget package. That package will “compile” the contents of the proto file into c# classes whenever the proto is changed and saved or when the project is built.
This generated code does not carry the [ExcludedFromCodeCoverage]
attribute, nor is it in a .g.cs
file, but it does have an //<AutoGenerated>
on the first line, and all of the code is inside of a #region Designer generated code
region. Coverlet is currently including this code as “not covered”, but as its code that is generated by a 3rd party tool, I think it should be excluded from the coverage evaluation.
What should or can I do here to avoid having this code be included in the coverage evaluation?
EDIT For completeness, the things that are commonly used to denote code that is generated and that should be excluded are
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
[GeneratedCode]
- files ending in
.g.cs
, .g.vb
, or similar - files ending in
.designer.cs
, .designer.vb
, or similar - code inside a region
#region Windows Form Designer generated code
- There are one or two more that I cant remember at the moment.
protobuf compiler uses none of these conventions and instead uses
- code inside a region
#region Designer generated code
- a comment preceding code in the file the form of an xml element that looks like
// <AutoGenerated>
An example proto file contents
syntax = "proto3";
option csharp_namespace = "Core.Schema";
package example_data_schema;
message ExampleData {
bytes id = 1;
string name = 3;
int32 code = 5;
string description = 7;
}
generates a code file that starts like this
// <auto-generated>
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: Protos/example_data_payloads.proto
// </auto-generated>
#pragma warning disable 1591, 0612, 3021
#region Designer generated code
using pb = global::Google.Protobuf;
using pbc = global::Google.Protobuf.Collections;
using pbr = global::Google.Protobuf.Reflection;
using scg = global::System.Collections.Generic;
namespace Core.Payloads {
/// <summary>Holder for reflection information generated from Protos/example_data_payloads.proto</summary>
public static partial class ExampleDataPayloadsReflection {
#region Descriptor
/// <summary>File descriptor for Protos/example_data_payloads.proto</summary>
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
}
private static pbr::FileDescriptor descriptor;
static ExampleDataPayloadsReflection() {
byte[] descriptorData = global::System.Convert.FromBase64String(
//400 more lines of protocol buffer support
Also linking https://github.com/grpc/grpc/issues/25950
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:9 (6 by maintainers)
Top GitHub Comments
@petli - once the GrpcTools version that marks the generated code as
[GeneratedCode]
is released I think the runsettings is going to be the way to go since we can just push a single file for every repo, or alter the build defs to create/modify to include one. I didnt consider the runsettings since we use a mix of mstest, nunit, and xunit, and had mentally associated it as being an mstest thing.I think it is because there is no
IncludeByAttribute
, so users who would want code coverage on[GeneratedCode]
would have no way of getting that.[ExcludeFromCoverage]
/[ExcludeFromCodeCoverage]
are the only two attributes that clearly states that code should be excluded.Adding
[GeneratedCode]
to the list of standard exclusion would be a backward-incompatible change, so it would require a new major version in addition to a mechanism to revert to the old behaviour.