|
| 1 | +#nullable disable |
| 2 | +using Cake.Coverlet; |
| 3 | + |
| 4 | +namespace Common.Addins.Cake.Coverlet; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// A delegate representing the output transformation |
| 8 | +/// </summary> |
| 9 | +/// <param name="fileName">The file name</param> |
| 10 | +/// <param name="directoryPath">The directory path</param> |
| 11 | +/// <returns>The path and name of the file (without extension)</returns> |
| 12 | +public delegate string OutputTransformer(string fileName, string directoryPath); |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Settings used by Cake.Coverlet |
| 16 | +/// </summary> |
| 17 | +public class CoverletSettings : DotNetSettings |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Gets or sets if coverage should be collected |
| 21 | + /// </summary> |
| 22 | + public bool CollectCoverage { get; set; } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Gets or sets the output format for Coverlet |
| 26 | + /// </summary> |
| 27 | + public CoverletOutputFormat CoverletOutputFormat { get; set; } = CoverletOutputFormat.json; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets or sets the threshold for Coverlet to use in percent |
| 31 | + /// </summary> |
| 32 | + public uint? Threshold { get; set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets or sets the type of threshold to apply. |
| 36 | + /// </summary> |
| 37 | + /// <remarks> |
| 38 | + /// This has no effect if Threshold is not set to a value |
| 39 | + /// </remarks> |
| 40 | + public ThresholdType ThresholdType { get; set; } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Gets or sets the output directory the output files |
| 44 | + /// </summary> |
| 45 | + public DirectoryPath CoverletOutputDirectory { get; set; } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Gets or sets the name of the output file excluding format |
| 49 | + /// </summary> |
| 50 | + public string CoverletOutputName { get; set; } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Gets or sets the list of files to exclude |
| 54 | + /// </summary> |
| 55 | + public List<string> ExcludeByFile { get; set; } = new(); |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Gets or sets the list of files to exclude |
| 59 | + /// </summary> |
| 60 | + public List<string> ExcludeByAttribute { get; set; } = new(); |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Gets or sets the exclusion filters |
| 64 | + /// </summary> |
| 65 | + public List<string> Exclude { get; set; } = new(); |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets or sets a inclusion filters |
| 69 | + /// </summary> |
| 70 | + public List<string> Include { get; set; } = new(); |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Gets or sets if the test assembly should be included |
| 74 | + /// </summary> |
| 75 | + public bool? IncludeTestAssembly { get; set; } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Gets or sets the file to merge the results of the run with |
| 79 | + /// </summary> |
| 80 | + public FilePath MergeWithFile { get; set; } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Gets or sets a transformation function taking the <see cref="CoverletOutputName"/> and |
| 84 | + /// returning the new file name without an extension |
| 85 | + /// </summary> |
| 86 | + public OutputTransformer OutputTransformer { get; set; } |
| 87 | + = (fileName, dir) => $@"{dir}\{fileName}"; |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Adds a filter to the list of exclusions |
| 91 | + /// </summary> |
| 92 | + /// <param name="filter">The filter to add</param> |
| 93 | + /// <returns></returns> |
| 94 | + public CoverletSettings WithFilter(string filter) |
| 95 | + { |
| 96 | + Exclude.Add(filter); |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Adds a file to the list of files to exclude |
| 102 | + /// </summary> |
| 103 | + /// <param name="file">The file to exclude</param> |
| 104 | + /// <returns></returns> |
| 105 | + public CoverletSettings WithFileExclusion(string file) |
| 106 | + { |
| 107 | + ExcludeByFile.Add(file); |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Adds a attribute to the list of attribute to exclude |
| 113 | + /// </summary> |
| 114 | + /// <param name="attribute">The attribute to exclude</param> |
| 115 | + /// <returns></returns> |
| 116 | + public CoverletSettings WithAttributeExclusion(string attribute) |
| 117 | + { |
| 118 | + ExcludeByAttribute.Add(attribute); |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Adds a filter to the list of inclusions |
| 124 | + /// </summary> |
| 125 | + /// <param name="file">The filter to add</param> |
| 126 | + /// <returns></returns> |
| 127 | + public CoverletSettings WithInclusion(string file) |
| 128 | + { |
| 129 | + Include.Add(file); |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Add a type of threshold to combine with the existing |
| 135 | + /// </summary> |
| 136 | + /// <param name="type">The type of threshold to add</param> |
| 137 | + /// <returns></returns> |
| 138 | + public CoverletSettings WithThresholdType(ThresholdType type) |
| 139 | + { |
| 140 | + ThresholdType |= type; |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Add a type of format to combine with the existing output formats |
| 146 | + /// </summary> |
| 147 | + /// <param name="format">The format type to add</param> |
| 148 | + /// <returns></returns> |
| 149 | + public CoverletSettings WithFormat(CoverletOutputFormat format) |
| 150 | + { |
| 151 | + CoverletOutputFormat |= format; |
| 152 | + return this; |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Add a default transformer appending the current date time at the time of calling test |
| 157 | + /// </summary> |
| 158 | + /// <returns></returns> |
| 159 | + public CoverletSettings WithDateTimeTransformer() |
| 160 | + { |
| 161 | + OutputTransformer = (fileName, directory) => $@"{directory}\{fileName}-{DateTime.UtcNow:dd-MM-yyyy-HH-mm-ss-FFF}"; |
| 162 | + return this; |
| 163 | + } |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// Sets the output format to be a specific value |
| 167 | + /// </summary> |
| 168 | + /// <param name="format"></param> |
| 169 | + /// <returns></returns> |
| 170 | + public CoverletSettings SetFormat(CoverletOutputFormat format) |
| 171 | + { |
| 172 | + CoverletOutputFormat = format; |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + /// <summary> |
| 177 | + /// Sets the output format to be a specific value |
| 178 | + /// </summary> |
| 179 | + /// <param name="includeTestAssembly"></param> |
| 180 | + /// <returns></returns> |
| 181 | + public CoverletSettings WithIncludeTestAssembly(bool includeTestAssembly) |
| 182 | + { |
| 183 | + IncludeTestAssembly = includeTestAssembly; |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | + /// <summary> |
| 188 | + /// Clones the coverlet settings to a new instance |
| 189 | + /// </summary> |
| 190 | + /// <returns></returns> |
| 191 | + public CoverletSettings Clone() => new() |
| 192 | + { |
| 193 | + CollectCoverage = CollectCoverage, |
| 194 | + CoverletOutputFormat = CoverletOutputFormat, |
| 195 | + Threshold = Threshold, |
| 196 | + ThresholdType = ThresholdType, |
| 197 | + CoverletOutputDirectory = CoverletOutputDirectory == null ? null : DirectoryPath.FromString(CoverletOutputDirectory.FullPath), |
| 198 | + CoverletOutputName = CoverletOutputName, |
| 199 | + Include = new List<string>(Include), |
| 200 | + ExcludeByFile = new List<string>(ExcludeByFile), |
| 201 | + ExcludeByAttribute = new List<string>(ExcludeByAttribute), |
| 202 | + Exclude = new List<string>(Exclude), |
| 203 | + IncludeTestAssembly = IncludeTestAssembly, |
| 204 | + MergeWithFile = MergeWithFile == null ? null : FilePath.FromString(MergeWithFile.FullPath), |
| 205 | + OutputTransformer = OutputTransformer |
| 206 | + }; |
| 207 | +} |
0 commit comments