Skip to content

Commit

Permalink
Make property readonly when possible (#3320)
Browse files Browse the repository at this point in the history
Co-authored-by: Amaury Levé <[email protected]>
  • Loading branch information
Evangelink and Evangelink authored Feb 3, 2022
1 parent 8fdeabe commit cc10486
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ internal class EventLogContainer : IEventLogContainer
/// </param>
public EventLogContainer(string eventLogName, ISet<string> eventSources, ISet<EventLogEntryType> entryTypes, int maxLogEntries, DataCollectionLogger dataCollectionLogger, DataCollectionContext dataCollectionContext)
{
CreateEventLog(eventLogName);
EventLog = new EventLog(eventLogName);
EventLog.EnableRaisingEvents = true;
EventLog.EntryWritten += OnEventLogEntryWritten;
int currentCount = EventLog.Entries.Count;
NextEntryIndexToCollect = currentCount == 0 ? 0 : EventLog.Entries[currentCount - 1].Index + 1;
_eventSources = eventSources;
_entryTypes = entryTypes;
_maxLogEntries = maxLogEntries;
Expand All @@ -69,7 +73,7 @@ public EventLogContainer(string eventLogName, ISet<string> eventSources, ISet<Ev
public List<EventLogEntry> EventLogEntries { get; }

/// <inheritdoc />
public EventLog EventLog { get; private set; }
public EventLog EventLog { get; }

internal int NextEntryIndexToCollect { get; set; }

Expand Down Expand Up @@ -238,14 +242,4 @@ protected virtual void Dispose(bool disposing)
_isDisposed = true;
}
}

private void CreateEventLog(string eventLogName)
{
EventLog = new EventLog(eventLogName);
EventLog.EnableRaisingEvents = true;
EventLog.EntryWritten += OnEventLogEntryWritten;
int currentCount = EventLog.Entries.Count;
NextEntryIndexToCollect =
(currentCount == 0) ? 0 : EventLog.Entries[currentCount - 1].Index + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ internal EventLogDataCollector(IFileHelper fileHelper)
/// <summary>
/// Gets the context data.
/// </summary>
internal Dictionary<DataCollectionContext, EventLogSessionContext> ContextMap { get; private set; }
internal Dictionary<DataCollectionContext, EventLogSessionContext> ContextMap { get; }

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,12 @@ public DiscoveryCriteria DiscoveryCriteria
/// <summary>
/// Parent discovery manager
/// </summary>
internal IProxyDiscoveryManager DiscoveryManager { get; private set; }
internal IProxyDiscoveryManager DiscoveryManager { get; }

/// <summary>
/// Logger manager.
/// </summary>
internal ITestLoggerManager LoggerManager { get; private set; }
internal ITestLoggerManager LoggerManager { get; }

#region ITestDiscoveryEventsHandler2 Methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public LazyExtension(Func<TExtension> creator, TMetadata metadata)
/// </summary>
internal bool IsExtensionCreated { get; private set; }

internal TestPluginInformation TestPluginInfo { get; private set; }
internal TestPluginInformation TestPluginInfo { get; }

/// <summary>
/// Gets the test extension instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,24 @@ protected SettingsProviderExtensionManager(
_logger = logger;

// Populate the map to avoid threading issues
PopulateMap();
SettingsProvidersMap = new Dictionary<string, LazyExtension<ISettingsProvider, ISettingsProviderCapabilities>>();

foreach (var settingsProvider in _settingsProviders)
{
if (SettingsProvidersMap.ContainsKey(settingsProvider.Metadata.SettingsName))
{
_logger.SendMessage(
TestMessageLevel.Error,
string.Format(
CultureInfo.CurrentUICulture,
CommonResources.DuplicateSettingsName,
settingsProvider.Metadata.SettingsName));
}
else
{
SettingsProvidersMap.Add(settingsProvider.Metadata.SettingsName, settingsProvider);
}
}
}

#endregion
Expand All @@ -76,12 +93,12 @@ protected SettingsProviderExtensionManager(
/// <summary>
/// Gets the Unfiltered list of settings providers. Used for the /ListSettingsProviders command line argument.
/// </summary>
public IEnumerable<LazyExtension<ISettingsProvider, Dictionary<string, object>>> UnfilteredSettingsProviders { get; private set; }
public IEnumerable<LazyExtension<ISettingsProvider, Dictionary<string, object>>> UnfilteredSettingsProviders { get; }

/// <summary>
/// Gets the map of settings name to settings provider.
/// </summary>
public Dictionary<string, LazyExtension<ISettingsProvider, ISettingsProviderCapabilities>> SettingsProvidersMap { get; private set; }
public Dictionary<string, LazyExtension<ISettingsProvider, ISettingsProviderCapabilities>> SettingsProvidersMap { get; }

#endregion

Expand Down Expand Up @@ -179,32 +196,6 @@ internal LazyExtension<ISettingsProvider, ISettingsProviderCapabilities> GetSett
}

#endregion


/// <summary>
/// Populate the settings provider map
/// </summary>
private void PopulateMap()
{
SettingsProvidersMap = new Dictionary<string, LazyExtension<ISettingsProvider, ISettingsProviderCapabilities>>();

foreach (var settingsProvider in _settingsProviders)
{
if (SettingsProvidersMap.ContainsKey(settingsProvider.Metadata.SettingsName))
{
_logger.SendMessage(
TestMessageLevel.Error,
string.Format(
CultureInfo.CurrentUICulture,
CommonResources.DuplicateSettingsName,
settingsProvider.Metadata.SettingsName));
}
else
{
SettingsProvidersMap.Add(settingsProvider.Metadata.SettingsName, settingsProvider);
}
}
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ParallelRunDataAggregator(string runSettingsXml)

public bool IsCanceled { get; private set; }

public string RunSettings { get; private set; }
public string RunSettings { get; }

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal abstract class BaseRunTests
/// <summary>
/// The Run configuration. To determine framework and execution thread apartment state.
/// </summary>
private RunConfiguration _runConfiguration;
private readonly RunConfiguration _runConfiguration;

/// <summary>
/// The Serializer to clone testcase object in case of user input test source is package. E.g UWP scenario(appx/build.appxrecipe).
Expand Down Expand Up @@ -147,7 +147,29 @@ protected BaseRunTests(
_testEventsPublisher = testEventsPublisher;
_platformThread = platformThread;
_dataSerializer = dataSerializer;
SetContext();

TestRunCache = new TestRunCache(TestExecutionContext.FrequencyOfRunStatsChangeEvent, TestExecutionContext.RunStatsChangeEventTimeout, OnCacheHit);

RunContext = new RunContext();
RunContext.RunSettings = RunSettingsUtilities.CreateAndInitializeRunSettings(RunSettings);
RunContext.KeepAlive = TestExecutionContext.KeepAlive;
RunContext.InIsolation = TestExecutionContext.InIsolation;
RunContext.IsDataCollectionEnabled = TestExecutionContext.IsDataCollectionEnabled;
RunContext.IsBeingDebugged = TestExecutionContext.IsDebug;

var runConfig = XmlRunSettingsUtilities.GetRunConfigurationNode(RunSettings);
RunContext.TestRunDirectory = RunSettingsUtilities.GetTestResultsDirectory(runConfig);
RunContext.SolutionDirectory = RunSettingsUtilities.GetSolutionDirectory(runConfig);
_runConfiguration = runConfig;

FrameworkHandle = new FrameworkHandle(
_testCaseEventsHandler,
TestRunCache,
TestExecutionContext,
TestRunEventsHandler);
FrameworkHandle.TestRunMessage += OnTestRunMessage;

ExecutorUrisThatRanTests = new List<string>();
}

#endregion
Expand All @@ -157,30 +179,30 @@ protected BaseRunTests(
/// <summary>
/// Gets the run settings.
/// </summary>
protected string RunSettings { get; private set; }
protected string RunSettings { get; }

/// <summary>
/// Gets the test execution context.
/// </summary>
protected TestExecutionContext TestExecutionContext { get; private set; }
protected TestExecutionContext TestExecutionContext { get; }

/// <summary>
/// Gets the test run events handler.
/// </summary>
protected ITestRunEventsHandler TestRunEventsHandler { get; private set; }
protected ITestRunEventsHandler TestRunEventsHandler { get; }

/// <summary>
/// Gets the test run cache.
/// </summary>
protected ITestRunCache TestRunCache { get; private set; }
protected ITestRunCache TestRunCache { get; }

protected bool IsCancellationRequested => _isCancellationRequested;

protected RunContext RunContext { get; private set; }
protected RunContext RunContext { get; }

protected FrameworkHandle FrameworkHandle { get; private set; }
protected FrameworkHandle FrameworkHandle { get; }

protected ICollection<string> ExecutorUrisThatRanTests { get; private set; }
protected ICollection<string> ExecutorUrisThatRanTests { get; }

#endregion

Expand Down Expand Up @@ -320,32 +342,6 @@ private void CancelTestRunInternal(ITestExecutor executor)

#region Private methods

private void SetContext()
{
TestRunCache = new TestRunCache(TestExecutionContext.FrequencyOfRunStatsChangeEvent, TestExecutionContext.RunStatsChangeEventTimeout, OnCacheHit);

RunContext = new RunContext();
RunContext.RunSettings = RunSettingsUtilities.CreateAndInitializeRunSettings(RunSettings);
RunContext.KeepAlive = TestExecutionContext.KeepAlive;
RunContext.InIsolation = TestExecutionContext.InIsolation;
RunContext.IsDataCollectionEnabled = TestExecutionContext.IsDataCollectionEnabled;
RunContext.IsBeingDebugged = TestExecutionContext.IsDebug;

var runConfig = XmlRunSettingsUtilities.GetRunConfigurationNode(RunSettings);
RunContext.TestRunDirectory = RunSettingsUtilities.GetTestResultsDirectory(runConfig);
RunContext.SolutionDirectory = RunSettingsUtilities.GetSolutionDirectory(runConfig);
_runConfiguration = runConfig;

FrameworkHandle = new FrameworkHandle(
_testCaseEventsHandler,
TestRunCache,
TestExecutionContext,
TestRunEventsHandler);
FrameworkHandle.TestRunMessage += OnTestRunMessage;

ExecutorUrisThatRanTests = new List<string>();
}

private void OnTestRunMessage(object sender, TestRunMessageEventArgs e)
{
TestRunEventsHandler.HandleLogMessage(e.Level, e.Message);
Expand Down Expand Up @@ -397,7 +393,7 @@ private bool RunTestInternalWithExecutors(IEnumerable<Tuple<Uri, string>> execut
{
// Commenting this out because of a compatibility issue with Microsoft.Dotnet.ProjectModel released on nuGet.org.
// this.activeExecutor = null;
// var runtimeVersion = string.Concat(PlatformServices.Default.Runtime.RuntimeType, " ",
// var runtimeVersion = string.Concat(PlatformServices.Default.Runtime.RuntimeType, " ",
// PlatformServices.Default.Runtime.RuntimeVersion);
var runtimeVersion = " ";
TestRunEventsHandler?.HandleLogMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public TestLink(Guid id, string name, string storage)
/// <summary>
/// Gets the name.
/// </summary>
public string Name { get; private set; } = string.Empty;
public string Name { get; } = string.Empty;

/// <summary>
/// Gets the storage.
/// </summary>
public string Storage { get; private set; } = string.Empty;
public string Storage { get; } = string.Empty;

/// <summary>
/// Whether this Link is equal to other Link. Compares by Id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static TestListCategory AllResults
/// <summary>
/// Gets the id.
/// </summary>
public TestListCategoryId Id { get; private set; } = new TestListCategoryId();
public TestListCategoryId Id { get; } = new TestListCategoryId();

/// <summary>
/// Gets or sets id of parent category. Use TestCategoryId.Root for top level categories.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public TestMethod(string name, string className)
/// <summary>
/// Gets the name.
/// </summary>
public string Name { get; private set; }
public string Name { get; }

/// <summary>
/// Gets the class name.
/// </summary>
public string ClassName { get; private set; }
public string ClassName { get; }

/// <summary>
/// Gets or sets a value indicating whether is valid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public TimeSpan Duration
/// <summary>
/// Gets the computer name.
/// </summary>
public string ComputerName { get; private set; }
public string ComputerName { get; }

/// <summary>
/// Gets or sets the outcome.
Expand Down Expand Up @@ -408,7 +408,7 @@ public string TestResultsDirectory
/// <summary>
/// Gets the directory containing the test result files, relative to the root results directory
/// </summary>
public string RelativeTestResultsDirectory { get; private set; }
public string RelativeTestResultsDirectory { get; }

/// <summary>
/// Gets or sets the data row info.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override TestType TestType
/// <summary>
/// Gets the test method.
/// </summary>
public TestMethod TestMethod { get; private set; }
public TestMethod TestMethod { get; }

/// <summary>
/// Gets or sets the storage.
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.TestPlatform.ObjectModel/AttachmentSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public class UriDataAttachment
/// Description of the attachment.
/// </summary>
[DataMember]
public string Description { get; private set; }
public string Description { get; }

/// <summary>
/// Uri of the attachment.
/// </summary>
[DataMember]
public Uri Uri { get; private set; }
public Uri Uri { get; }

public UriDataAttachment(Uri uri, string description)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ protected internal DataCollectionContext(SessionId sessionId, TestExecId testExe
/// Identifies the session under which the data collection occurs. Will not be null.
/// </summary>
[DataMember]
public SessionId SessionId { get; private set; }
public SessionId SessionId { get; }

/// <summary>
/// Identifies the test execution under which the data collection occurs,
/// or null if no such test exists.
/// </summary>
[DataMember]
public TestExecId TestExecId { get; private set; }
public TestExecId TestExecId { get; }

/// <summary>
/// Returns true if there is an executing test case associated with this context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed class TestHostLaunchedEventArgs : DataCollectionEventArgs

#region Public properties

public int TestHostProcessId { get; private set; }
public int TestHostProcessId { get; }

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal string TraceFileName
/// <value>
/// The <see cref="StreamWriterRollingHelper"/> for the flat file.
/// </value>
internal StreamWriterRollingHelper RollingHelper { get; private set; }
internal StreamWriterRollingHelper RollingHelper { get; }

/// <summary>
/// Writes the trace messages to the file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal static ArgumentProcessorFactory Create()
/// <summary>
/// Returns all of the available argument processors.
/// </summary>
public IEnumerable<IArgumentProcessor> AllArgumentProcessors { get; private set; }
public IEnumerable<IArgumentProcessor> AllArgumentProcessors { get; }

/// <summary>
/// Gets a mapping between command and Argument Executor.
Expand Down

0 comments on commit cc10486

Please sign in to comment.