Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blame Sequence File Changes #1716

Merged
merged 6 commits into from
Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class BlameCollector : DataCollector, ITestExecutionEnvironmentSpecifier
private DataCollectionEvents events;
private DataCollectionLogger logger;
private IProcessDumpUtility processDumpUtility;
private List<TestCase> testSequence;
private List<Guid> testSequence;
private Dictionary<Guid, BlameTestObject> testObjectDictionary;
private IBlameReaderWriter blameReaderWriter;
private XmlElement configurationElement;
private int testStartCount;
Expand Down Expand Up @@ -88,7 +89,8 @@ public override void Initialize(
this.dataCollectionSink = dataSink;
this.context = environmentContext;
this.configurationElement = configurationElement;
this.testSequence = new List<TestCase>();
this.testSequence = new List<Guid>();
this.testObjectDictionary = new Dictionary<Guid, BlameTestObject>();
this.logger = logger;

// Subscribing to events
Expand Down Expand Up @@ -155,7 +157,15 @@ private void EventsTestCaseStart(object sender, TestCaseStartEventArgs e)
EqtTrace.Info("Blame Collector : Test Case Start");
}

this.testSequence.Add(e.TestElement);
var blameTestObject = new BlameTestObject(e.TestElement);

// Add guid to list of test sequence to maintain the order.
this.testSequence.Add(blameTestObject.Id);

// Add the test object to the dictionary.
this.testObjectDictionary.Add(blameTestObject.Id, blameTestObject);

// Increment test start count.
this.testStartCount++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make dict

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dictionary and list used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.testStartCount++; [](start = 12, length = 22)

Do we still need this>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will have to check for entire dictionary if any test remains incomplete to decide if sequence is to be written to file. This makes an easier check.

}

Expand All @@ -172,6 +182,12 @@ private void EventsTestCaseEnd(object sender, TestCaseEndEventArgs e)
}

this.testEndCount++;

// Update the test object in the dictionary as the test has completed.
if (this.testObjectDictionary.ContainsKey(e.TestElement.Id))
{
this.testObjectDictionary[e.TestElement.Id].IsCompleted = true;
}
}

/// <summary>
Expand All @@ -192,7 +208,7 @@ private void SessionEnded_Handler(object sender, SessionEndEventArgs args)
if (this.testStartCount > this.testEndCount)
{
var filepath = Path.Combine(this.GetResultsDirectory(), Constants.AttachmentFileName + "_" + this.attachmentGuid);
filepath = this.blameReaderWriter.WriteTestSequence(this.testSequence, filepath);
filepath = this.blameReaderWriter.WriteTestSequence(this.testSequence, this.testObjectDictionary, filepath);
var fileTranferInformation = new FileTransferInformation(this.context.SessionDataCollectionContext, filepath, true);
this.dataCollectionSink.SendFileAsync(fileTranferInformation);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.TestPlatform.Extensions.BlameDataCollector
{
using System;
using System.IO;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;

public class BlameTestObject
{
private Guid id;
private string fullyQualifiedName;
private string source;
private bool isCompleted;
private string displayName;

#region Constructor

/// <summary>
/// Initializes a new instance of the <see cref="BlameTestObject"/> class.
/// </summary>
public BlameTestObject()
{
// Default constructor
}

/// <summary>
/// Initializes a new instance of the <see cref="BlameTestObject"/> class.
/// </summary>
/// <param name="fullyQualifiedName">
/// Fully qualified name of the test case.
/// </param>
/// <param name="executorUri">
/// The Uri of the executor to use for running this test.
/// </param>
/// <param name="source">
/// Test container source from which the test is discovered.
/// </param>
public BlameTestObject(string fullyQualifiedName, Uri executorUri, string source)
{
ValidateArg.NotNullOrEmpty(fullyQualifiedName, "fullyQualifiedName");
ValidateArg.NotNull(executorUri, "executorUri");
ValidateArg.NotNullOrEmpty(source, "source");

this.Id = Guid.Empty;
this.FullyQualifiedName = fullyQualifiedName;
this.ExecutorUri = executorUri;
this.Source = source;
this.IsCompleted = false;
}

/// <summary>
/// Initializes a new instance of the <see cref="BlameTestObject"/> class.
/// </summary>
/// <param name="testCase">
/// The test case
/// </param>
public BlameTestObject(TestCase testCase)
{
this.Id = testCase.Id;
this.FullyQualifiedName = testCase.FullyQualifiedName;
this.ExecutorUri = testCase.ExecutorUri;
this.Source = testCase.Source;
this.DisplayName = testCase.DisplayName;
this.IsCompleted = false;
}

#endregion

#region Properties

/// <summary>
/// Gets or sets the id of the test case.
/// </summary>
public Guid Id
{
get
{
return this.id;
}

set
{
this.id = value;
}
}

/// <summary>
/// Gets or sets the fully qualified name of the test case.
/// </summary>
public string FullyQualifiedName
{
get
{
return this.fullyQualifiedName;
}

set
{
this.fullyQualifiedName = value;
}
}

/// <summary>
/// Gets or sets the Uri of the Executor to use for running this test.
/// </summary>
public Uri ExecutorUri
{
get; set;
}

/// <summary>
/// Gets or sets the test container source from which the test is discovered.
/// </summary>
public string Source
{
get
{
return this.source;
}

set
{
this.source = value;
}
}

/// <summary>
/// Gets or sets a value indicating whether test case is completed or not.
/// </summary>
public bool IsCompleted
{
get
{
return this.isCompleted;
}

set
{
this.isCompleted = value;
}
}

/// <summary>
/// Gets or sets the display name of the test case
/// </summary>
public string DisplayName
{
get
{
return this.displayName;
}

set
{
this.displayName = value;
}
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ internal static class Constants
/// </summary>
public const string TestSourceAttribute = "Source";

/// <summary>
/// Test Completed Attribute.
/// </summary>
public const string TestCompletedAttribute = "Completed";

/// <summary>
/// Test Display Name Attribute.
/// </summary>
public const string TestDisplayNameAttribute = "DisplayName";

/// <summary>
/// Friendly name of the data collector
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Microsoft.TestPlatform.Extensions.BlameDataCollector
{
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;

Expand All @@ -11,16 +12,17 @@ public interface IBlameReaderWriter
/// <summary>
/// Writes tests to document
/// </summary>
/// <param name="testSequence">List of tests in sequence</param>
/// <param name="testSequence">List of test guid in sequence</param>
/// <param name="testObjectDictionary">Dictionary of test objects</param>
/// <param name="filePath">The path of file</param>
/// <returns>File Path</returns>
string WriteTestSequence(List<TestCase> testSequence, string filePath);
string WriteTestSequence(List<Guid> testSequence, Dictionary<Guid, BlameTestObject> testObjectDictionary, string filePath);

/// <summary>
/// Reads all tests from file
/// </summary>
/// <param name="filePath">The path of saved file</param>
/// <returns>All tests</returns>
List<TestCase> ReadTestSequence(string filePath);
List<BlameTestObject> ReadTestSequence(string filePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Microsoft.TestPlatform.Extensions.BlameDataCollector
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
Expand Down Expand Up @@ -46,18 +47,23 @@ protected XmlReaderWriter(IFileHelper fileHelper)
#endregion

/// <summary>
/// Adds tests to document and saves document to file
/// Writes test Sequence to file.
/// Protected for testing purposes
/// </summary>
/// <param name="testSequence">
/// The test Sequence.
/// Sequence of Guids
/// </param>
/// <param name="testObjectDictionary">
/// Dictionary of test objects
/// </param>
/// <param name="filePath">
/// The file Path.
/// </param>
/// <returns>File path</returns>
public string WriteTestSequence(List<TestCase> testSequence, string filePath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestCase [](start = 45, length = 8)

check for outcome/result

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no outcome field in TestCase object.

public string WriteTestSequence(List<Guid> testSequence, Dictionary<Guid, BlameTestObject> testObjectDictionary, string filePath)
{
ValidateArg.NotNull(testSequence, nameof(testSequence));
ValidateArg.NotNull(testObjectDictionary, nameof(testObjectDictionary));
ValidateArg.NotNullOrEmpty(filePath, nameof(filePath));

filePath = filePath + ".xml";
Expand All @@ -68,13 +74,20 @@ public string WriteTestSequence(List<TestCase> testSequence, string filePath)
var blameTestRoot = xmlDocument.CreateElement(Constants.BlameRootNode);
xmlDocument.AppendChild(xmlDeclaration);

foreach (var testCase in testSequence)
foreach (var testGuid in testSequence)
{
var testElement = xmlDocument.CreateElement(Constants.BlameTestNode);
testElement.SetAttribute(Constants.TestNameAttribute, testCase.FullyQualifiedName);
testElement.SetAttribute(Constants.TestSourceAttribute, testCase.Source);
if (testObjectDictionary.ContainsKey(testGuid))
{
var testObject = testObjectDictionary[testGuid];

blameTestRoot.AppendChild(testElement);
var testElement = xmlDocument.CreateElement(Constants.BlameTestNode);
testElement.SetAttribute(Constants.TestNameAttribute, testObject.FullyQualifiedName);
testElement.SetAttribute(Constants.TestDisplayNameAttribute, testObject.DisplayName);
testElement.SetAttribute(Constants.TestSourceAttribute, testObject.Source);
testElement.SetAttribute(Constants.TestCompletedAttribute, testObject.IsCompleted.ToString());

blameTestRoot.AppendChild(testElement);
}
}

xmlDocument.AppendChild(blameTestRoot);
Expand All @@ -91,7 +104,7 @@ public string WriteTestSequence(List<TestCase> testSequence, string filePath)
/// </summary>
/// <param name="filePath">The path of test sequence file</param>
/// <returns>Test Case List</returns>
public List<TestCase> ReadTestSequence(string filePath)
public List<BlameTestObject> ReadTestSequence(string filePath)
{
ValidateArg.NotNull(filePath, nameof(filePath));

Expand All @@ -100,7 +113,7 @@ public List<TestCase> ReadTestSequence(string filePath)
throw new FileNotFoundException();
}

var testCaseList = new List<TestCase>();
var testCaseList = new List<BlameTestObject>();
try
{
// Reading test sequence
Expand All @@ -113,11 +126,13 @@ public List<TestCase> ReadTestSequence(string filePath)
var root = xmlDocument.LastChild;
foreach (XmlNode node in root)
{
var testCase = new TestCase
var testCase = new BlameTestObject
{
FullyQualifiedName =
node.Attributes[Constants.TestNameAttribute].Value,
Source = node.Attributes[Constants.TestSourceAttribute].Value
Source = node.Attributes[Constants.TestSourceAttribute].Value,
DisplayName = node.Attributes[Constants.TestDisplayNameAttribute].Value,
IsCompleted = node.Attributes[Constants.TestCompletedAttribute].Value == "True" ? true : false
};
testCaseList.Add(testCase);
}
Expand Down
Loading