|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Microsoft.WindowsAzure.Commands.Common; |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.IO; |
| 19 | +using System.Runtime.InteropServices; |
| 20 | +using System.Text; |
| 21 | +using System.Text.RegularExpressions; |
| 22 | +using System.Threading; |
| 23 | + |
| 24 | +namespace Microsoft.Azure.Commands.Common.Authentication |
| 25 | +{ |
| 26 | + public class TestCoverage : ITestCoverage |
| 27 | + { |
| 28 | + private const string CsvHeaderCommandName = "CommandName"; |
| 29 | + private const string CsvHeaderParameterSetName = "ParameterSetName"; |
| 30 | + private const string CsvHeaderParameters = "Parameters"; |
| 31 | + private const string CsvHeaderSourceScript = "SourceScript"; |
| 32 | + private const string CsvHeaderScriptLineNumber = "LineNumber"; |
| 33 | + private const string CsvHeaderIsSuccess = "IsSuccess"; |
| 34 | + private const string Delimiter = ","; |
| 35 | + |
| 36 | + private readonly IList<string> ExcludedSource = new List<string> |
| 37 | + { |
| 38 | + "Common.ps1", |
| 39 | + "Assert.ps1", |
| 40 | + "AzureRM.Resources.ps1", |
| 41 | + "AzureRM.Storage.ps1" |
| 42 | + }; |
| 43 | + |
| 44 | + |
| 45 | + private static readonly string s_testCoverageRootPath; |
| 46 | + |
| 47 | + private static readonly ReaderWriterLockSlim s_lock = new ReaderWriterLockSlim(); |
| 48 | + |
| 49 | + static TestCoverage() |
| 50 | + { |
| 51 | + |
| 52 | + var repoRootPath = ProbeRepoDirectory(); |
| 53 | + if (!string.IsNullOrEmpty(repoRootPath)) |
| 54 | + { |
| 55 | + s_testCoverageRootPath = Path.Combine(repoRootPath, "artifacts", "TestCoverageAnalysis", "Raw"); |
| 56 | + DirectoryInfo rawDir = new DirectoryInfo(s_testCoverageRootPath); |
| 57 | + if (!rawDir.Exists) |
| 58 | + { |
| 59 | + Directory.CreateDirectory(s_testCoverageRootPath); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static string ProbeRepoDirectory() |
| 65 | + { |
| 66 | + string directoryPath = ".."; |
| 67 | + while (Directory.Exists(directoryPath) && (!Directory.Exists(Path.Combine(directoryPath, "src")) || !Directory.Exists(Path.Combine(directoryPath, "artifacts")))) |
| 68 | + { |
| 69 | + directoryPath = Path.Combine(directoryPath, ".."); |
| 70 | + } |
| 71 | + |
| 72 | + string result = Directory.Exists(directoryPath) ? Path.GetFullPath(directoryPath) : null; |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + private string GenerateCsvHeader() |
| 77 | + { |
| 78 | + StringBuilder headerBuilder = new StringBuilder(); |
| 79 | + headerBuilder.Append(CsvHeaderCommandName).Append(Delimiter) |
| 80 | + .Append(CsvHeaderParameterSetName).Append(Delimiter) |
| 81 | + .Append(CsvHeaderParameters).Append(Delimiter) |
| 82 | + .Append(CsvHeaderSourceScript).Append(Delimiter) |
| 83 | + .Append(CsvHeaderScriptLineNumber).Append(Delimiter) |
| 84 | + .Append(CsvHeaderIsSuccess); |
| 85 | + |
| 86 | + return headerBuilder.ToString(); |
| 87 | + } |
| 88 | + |
| 89 | + private string GenerateCsvItem(string commandName, string parameterSetName, string parameters, string sourceScript, int scriptLineNumber, bool isSuccess) |
| 90 | + { |
| 91 | + StringBuilder itemBuilder = new StringBuilder(); |
| 92 | + itemBuilder.Append(commandName).Append(Delimiter) |
| 93 | + .Append(parameterSetName).Append(Delimiter) |
| 94 | + .Append(parameters).Append(Delimiter) |
| 95 | + .Append(sourceScript).Append(Delimiter) |
| 96 | + .Append(scriptLineNumber).Append(Delimiter) |
| 97 | + .Append(isSuccess.ToString().ToLowerInvariant()); |
| 98 | + |
| 99 | + return itemBuilder.ToString(); |
| 100 | + } |
| 101 | + |
| 102 | + public void LogRawData(AzurePSQoSEvent qos) |
| 103 | + { |
| 104 | +#if DEBUG || TESTCOVERAGE |
| 105 | + string moduleName = qos.ModuleName; |
| 106 | + string commandName = qos.CommandName; |
| 107 | + string sourceScript = qos.SourceScript; |
| 108 | + |
| 109 | + if (string.IsNullOrEmpty(moduleName) || string.IsNullOrEmpty(commandName) || ExcludedSource.Contains(sourceScript)) |
| 110 | + return; |
| 111 | + |
| 112 | + var pattern = @"\\(?:artifacts\\Debug|src)\\(?:Az\.)?(?<ModuleName>[a-zA-Z]+)\\"; |
| 113 | + var match = Regex.Match(sourceScript, pattern, RegexOptions.IgnoreCase); |
| 114 | + var testingModuleName = $"Az.{match.Groups["ModuleName"].Value}"; |
| 115 | + if (string.Compare(testingModuleName, moduleName, true) != 0) |
| 116 | + return; |
| 117 | + |
| 118 | + var csvFilePath = Path.Combine(s_testCoverageRootPath, $"{moduleName}.csv"); |
| 119 | + StringBuilder csvData = new StringBuilder(); |
| 120 | + |
| 121 | + s_lock.EnterWriteLock(); |
| 122 | + try |
| 123 | + { |
| 124 | + if (!File.Exists(csvFilePath)) |
| 125 | + { |
| 126 | + var csvHeader = GenerateCsvHeader(); |
| 127 | + csvData.Append(csvHeader); |
| 128 | + } |
| 129 | + |
| 130 | + csvData.AppendLine(); |
| 131 | + var csvItem = GenerateCsvItem(commandName, qos.ParameterSetName, qos.Parameters, Path.GetFileName(sourceScript), qos.ScriptLineNumber, qos.IsSuccess); |
| 132 | + csvData.Append(csvItem); |
| 133 | + |
| 134 | + File.AppendAllText(csvFilePath, csvData.ToString()); |
| 135 | + } |
| 136 | + catch (Exception ex) |
| 137 | + { |
| 138 | + Console.WriteLine($"##[group]Error occurred when generating raw data of test coverage for module {moduleName}"); |
| 139 | + Console.WriteLine($"##[error]Error Message: {ex.Message}"); |
| 140 | + Console.WriteLine($"##[error]Source: {ex.Source}"); |
| 141 | + Console.WriteLine($"##[error]Stack Trace: {ex.StackTrace}"); |
| 142 | + Console.WriteLine("##[endgroup]"); |
| 143 | + } |
| 144 | + finally |
| 145 | + { |
| 146 | + s_lock.ExitWriteLock(); |
| 147 | + } |
| 148 | +#endif |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments