Skip to content

Commit 878d95e

Browse files
committed
Added implementation for interface ITestCoverage and registered in Azure session
1 parent f8779c8 commit 878d95e

File tree

9 files changed

+364
-1
lines changed

9 files changed

+364
-1
lines changed

.azure-pipelines/powershell-core.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ variables:
1414
AnalysisTimeoutInMinutes: 120
1515
TestTimeoutInMinutes: 180
1616
BuildAzPredictor: false
17+
Azure_PS_TestCoverage: true
1718

1819
trigger: none
1920

.azure-pipelines/windows-powershell.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ variables:
88
IsGenerateBased: $[eq(variables['system.pullRequest.targetBranch'], 'generation')]
99
BuildTimeoutInMinutes: 120
1010
AnalysisTimeoutInMinutes: 120
11+
Azure_PS_TestCoverage: true
1112

1213
trigger: none
1314

src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,10 @@ public void OnImport()
750750
AzureSession.Instance.RegisterComponent(nameof(AzureCredentialFactory), () => new AzureCredentialFactory());
751751
AzureSession.Instance.RegisterComponent(nameof(MsalAccessTokenAcquirerFactory), () => new MsalAccessTokenAcquirerFactory());
752752
AzureSession.Instance.RegisterComponent<ISshCredentialFactory>(nameof(ISshCredentialFactory), () => new SshCredentialFactory());
753+
#if DEBUG || TESTCOVERAGE
754+
AzureSession.Instance.RegisterComponent<ITestCoverage>(nameof(ITestCoverage), () => new TestCoverage());
755+
#endif
756+
753757
#if DEBUG
754758
}
755759
catch (Exception) when (TestMockSupport.RunningMocked)

src/Accounts/Accounts/CommonModule/TelemetryProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public virtual AzurePSQoSEvent CreateQosEvent(InvocationInfo invocationInfo, str
143143
CommandName = invocationInfo?.MyCommand?.Name,
144144
ModuleVersion = TrimModuleVersion(invocationInfo?.MyCommand?.Module?.Version),
145145
ModuleName = TrimModuleName(invocationInfo?.MyCommand?.ModuleName),
146+
SourceScript = invocationInfo?.ScriptName,
147+
ScriptLineNumber = invocationInfo?.ScriptLineNumber ?? 0,
146148
SessionId = MetricHelper.SessionId,
147149
ParameterSetName = parameterSetName,
148150
InvocationName = invocationInfo?.InvocationName,

src/Accounts/Authentication/Config/ConfigInitializer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ private void RegisterConfigs(IConfigManager configManager)
190190
true,
191191
AzurePSDataCollectionProfile.EnvironmentVariableName,
192192
new[] { AppliesTo.Az }));
193+
#if DEBUG || TESTCOVERAGE
194+
configManager.RegisterConfig(new SimpleTypedConfig<bool>(
195+
ConfigKeys.EnableTestCoverage,
196+
"When enabled, the test framework will generate data during test run as a preliminary for the test coverage calculation",
197+
false,
198+
"Azure_PS_TestCoverage",
199+
new[] { AppliesTo.Az }));
200+
#endif
193201

194202
configManager.RegisterConfig(new EnableInterceptSurveyConfig());
195203
configManager.RegisterConfig(new DisplayBreakingChangeWarningsConfig());
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
}

src/shared/ConfigKeys.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ internal static class ConfigKeys
2828
public const string DisplayBreakingChangeWarning = "DisplayBreakingChangeWarning";
2929
public const string DefaultSubscriptionForLogin = "DefaultSubscriptionForLogin";
3030
public const string EnableDataCollection = "EnableDataCollection";
31+
public const string EnableTestCoverage = "EnableTestCoverage";
3132
}
3233
}

0 commit comments

Comments
 (0)