-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(playwrighttesting): separate api, data processing and utility la…
…yers
- Loading branch information
Siddharth Singha Roy
committed
Oct 16, 2024
1 parent
59f2ec4
commit 7345746
Showing
19 changed files
with
1,034 additions
and
824 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...Developer.MicrosoftPlaywrightTesting.TestLogger/src/Implementation/CloudRunErrorParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Interface; | ||
|
||
namespace Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Implementation | ||
{ | ||
internal class CloudRunErrorParser : ICloudRunErrorParser | ||
{ | ||
private List<string> InformationalMessages { get; set; } = new(); | ||
private List<string> ProcessedErrorMessageKeys { get; set; } = new(); | ||
private readonly ILogger _logger; | ||
private readonly IConsoleWriter _consoleWriter; | ||
public CloudRunErrorParser(ILogger? logger = null, IConsoleWriter? consoleWriter = null) | ||
{ | ||
_logger = logger ?? new Logger(); | ||
_consoleWriter = consoleWriter ?? new ConsoleWriter(); | ||
} | ||
|
||
public bool TryPushMessageAndKey(string? message, string? key) | ||
{ | ||
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(message)) | ||
{ | ||
return false; | ||
} | ||
if (ProcessedErrorMessageKeys.Contains(key!)) | ||
{ | ||
return false; | ||
} | ||
_logger.Info($"Adding message with key: {key}"); | ||
|
||
ProcessedErrorMessageKeys.Add(key!); | ||
InformationalMessages.Add(message!); | ||
return true; | ||
} | ||
|
||
public void PushMessage(string message) | ||
{ | ||
InformationalMessages.Add(message); | ||
} | ||
|
||
public void DisplayMessages() | ||
{ | ||
if (InformationalMessages.Count > 0) | ||
_consoleWriter.WriteLine(); | ||
int index = 1; | ||
foreach (string message in InformationalMessages) | ||
{ | ||
_consoleWriter.WriteLine($"{index++}) {message}"); | ||
} | ||
} | ||
|
||
public void HandleScalableRunErrorMessage(string? message) | ||
{ | ||
if (string.IsNullOrEmpty(message)) | ||
{ | ||
return; | ||
} | ||
foreach (TestResultError testResultErrorObj in TestResultErrorConstants.ErrorConstants) | ||
{ | ||
if (testResultErrorObj.Pattern.IsMatch(message)) | ||
{ | ||
TryPushMessageAndKey(testResultErrorObj.Message, testResultErrorObj.Key); | ||
} | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...Azure.Developer.MicrosoftPlaywrightTesting.TestLogger/src/Implementation/ConsoleWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Interface; | ||
|
||
namespace Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Implementation | ||
{ | ||
internal class ConsoleWriter : IConsoleWriter | ||
{ | ||
public void WriteLine(string? message = null) | ||
{ | ||
if (message == null) | ||
{ | ||
Console.WriteLine(); | ||
} | ||
else | ||
{ | ||
Console.WriteLine(message); | ||
} | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...esting/Azure.Developer.MicrosoftPlaywrightTesting.TestLogger/src/Implementation/Logger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Interface; | ||
|
||
namespace Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Implementation; | ||
|
||
internal enum LogLevel | ||
{ | ||
Debug, | ||
Info, | ||
Warning, | ||
Error | ||
} | ||
|
||
internal class Logger : ILogger | ||
{ | ||
internal static bool EnableDebug { get { return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(Constants.s_pLAYWRIGHT_SERVICE_DEBUG)); } set { } } | ||
|
||
#pragma warning disable CA1822 // Mark members as static | ||
private void Log(LogLevel level, string message) | ||
#pragma warning restore CA1822 // Mark members as static | ||
{ | ||
if (EnableDebug) | ||
{ | ||
Console.WriteLine($"{DateTime.Now} [{level}]: {message}"); | ||
} | ||
} | ||
|
||
public void Debug(string message) | ||
{ | ||
Log(LogLevel.Debug, message); | ||
} | ||
|
||
public void Error(string message) | ||
{ | ||
Log(LogLevel.Error, message); | ||
} | ||
|
||
public void Info(string message) | ||
{ | ||
Log(LogLevel.Info, message); | ||
} | ||
|
||
public void Warning(string message) | ||
{ | ||
Log(LogLevel.Warning, message); | ||
} | ||
}; |
63 changes: 63 additions & 0 deletions
63
...Azure.Developer.MicrosoftPlaywrightTesting.TestLogger/src/Implementation/ServiceClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
using System; | ||
using Azure.Core.Serialization; | ||
using Azure.Core; | ||
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Client; | ||
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Interface; | ||
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Model; | ||
using System.Text.Json; | ||
|
||
namespace Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Implementation | ||
{ | ||
internal class ServiceClient : IServiceClient | ||
{ | ||
private readonly ReportingTestResultsClient _reportingTestResultsClient; | ||
private readonly ReportingTestRunsClient _reportingTestRunsClient; | ||
private readonly CloudRunMetadata _cloudRunMetadata; | ||
private static string AccessToken { get => $"Bearer {Environment.GetEnvironmentVariable(ServiceEnvironmentVariable.PlaywrightServiceAccessToken)}"; set { } } | ||
private static string CorrelationId { get => Guid.NewGuid().ToString(); set { } } | ||
|
||
public ServiceClient(CloudRunMetadata cloudRunMetadata, ReportingTestResultsClient? reportingTestResultsClient = null, ReportingTestRunsClient? reportingTestRunsClient = null) | ||
{ | ||
_cloudRunMetadata = cloudRunMetadata; | ||
_reportingTestResultsClient = reportingTestResultsClient ?? new ReportingTestResultsClient(_cloudRunMetadata.BaseUri); | ||
_reportingTestRunsClient = reportingTestRunsClient ?? new ReportingTestRunsClient(_cloudRunMetadata.BaseUri); | ||
} | ||
|
||
public TestRunDtoV2? PatchTestRunInfo(TestRunDtoV2 run) | ||
{ | ||
Response apiResponse = _reportingTestRunsClient.PatchTestRunInfo(_cloudRunMetadata.WorkspaceId!, _cloudRunMetadata.RunId!, RequestContent.Create(run), ReporterConstants.s_aPPLICATION_JSON, AccessToken, CorrelationId); | ||
if (apiResponse.Content != null) | ||
{ | ||
return apiResponse.Content!.ToObject<TestRunDtoV2>(new JsonObjectSerializer()); | ||
} | ||
return null; | ||
} | ||
|
||
public TestRunShardDto? PatchTestRunShardInfo(int shardId, TestRunShardDto runShard) | ||
{ | ||
Response apiResponse = _reportingTestRunsClient.PatchTestRunShardInfo(_cloudRunMetadata.WorkspaceId!, _cloudRunMetadata.RunId!, shardId.ToString(), RequestContent.Create(runShard), ReporterConstants.s_aPPLICATION_JSON, AccessToken, CorrelationId); | ||
if (apiResponse.Content != null) | ||
{ | ||
return apiResponse.Content!.ToObject<TestRunShardDto>(new JsonObjectSerializer()); | ||
} | ||
return null; | ||
} | ||
|
||
public void UploadBatchTestResults(UploadTestResultsRequest uploadTestResultsRequest) | ||
{ | ||
_reportingTestResultsClient.UploadBatchTestResults(_cloudRunMetadata.WorkspaceId!, RequestContent.Create(JsonSerializer.Serialize(uploadTestResultsRequest)), AccessToken, CorrelationId, null); | ||
} | ||
|
||
public TestResultsUri? GetTestRunResultsUri() | ||
{ | ||
Response response = _reportingTestRunsClient.GetTestRunResultsUri(_cloudRunMetadata.WorkspaceId!, _cloudRunMetadata.RunId!, AccessToken, CorrelationId, null); | ||
if (response.Content != null) | ||
{ | ||
return response.Content!.ToObject<TestResultsUri>(new JsonObjectSerializer()); | ||
} | ||
return null; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ure.Developer.MicrosoftPlaywrightTesting.TestLogger/src/Interface/ICloudRunErrorParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Interface | ||
{ | ||
internal interface ICloudRunErrorParser | ||
{ | ||
void HandleScalableRunErrorMessage(string? message); | ||
bool TryPushMessageAndKey(string? message, string? key); | ||
void PushMessage(string message); | ||
void DisplayMessages(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ing/Azure.Developer.MicrosoftPlaywrightTesting.TestLogger/src/Interface/IConsoleWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Interface | ||
{ | ||
internal interface IConsoleWriter | ||
{ | ||
void WriteLine(string? message = null); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ing/Azure.Developer.MicrosoftPlaywrightTesting.TestLogger/src/Interface/IDataProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Model; | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
|
||
namespace Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Interface | ||
{ | ||
internal interface IDataProcessor | ||
{ | ||
TestRunDtoV2 GetTestRun(); | ||
TestRunShardDto GetTestRunShard(); | ||
TestResults GetTestCaseResultData(TestResult testResultSource); | ||
} | ||
} |
Oops, something went wrong.