Skip to content

Commit

Permalink
refactored private properties into fields; other refactorings and cod…
Browse files Browse the repository at this point in the history
…e style improvements
  • Loading branch information
csoltenborn committed May 27, 2016
1 parent f8dcb15 commit 2f2f012
Show file tree
Hide file tree
Showing 43 changed files with 572 additions and 580 deletions.
4 changes: 1 addition & 3 deletions GoogleTestAdapter/Common/ILogger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Security.Cryptography.X509Certificates;

namespace GoogleTestAdapter.Common
namespace GoogleTestAdapter.Common
{

public enum Severity { Info, Warning, Error }
Expand Down
8 changes: 4 additions & 4 deletions GoogleTestAdapter/Common/ProcessWaiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GoogleTestAdapter.Common
public class ProcessWaiter
{
public int ProcessExitCode { get; private set; } = -1;
private bool Exited { get; set; } = false;
private bool _exited;


public ProcessWaiter(Process process)
Expand All @@ -22,7 +22,7 @@ public int WaitForExit()
{
lock (this)
{
while (!Exited)
while (!_exited)
{
Monitor.Wait(this);
}
Expand All @@ -33,13 +33,13 @@ public int WaitForExit()

private void OnExited(object sender, EventArgs e)
{
Process process = sender as Process;
var process = sender as Process;
if (process != null)
{
lock (this)
{
ProcessExitCode = process.ExitCode;
Exited = true;
_exited = true;

process.Exited -= OnExited;

Expand Down
22 changes: 11 additions & 11 deletions GoogleTestAdapter/Core/GoogleTestDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class GoogleTestDiscoverer
{
private static readonly Regex CompiledTestFinderRegex = new Regex(SettingsWrapper.TestFinderRegex, RegexOptions.Compiled);

private TestEnvironment TestEnvironment { get; }
private IDiaResolverFactory DiaResolverFactory { get; }
private readonly TestEnvironment _testEnvironment;
private readonly IDiaResolverFactory _diaResolverFactory;

public GoogleTestDiscoverer(TestEnvironment testEnviroment, IDiaResolverFactory diaResolverFactory = null)
{
TestEnvironment = testEnviroment;
DiaResolverFactory = diaResolverFactory ?? DefaultDiaResolverFactory.Instance;
_testEnvironment = testEnviroment;
_diaResolverFactory = diaResolverFactory ?? DefaultDiaResolverFactory.Instance;
}

public void DiscoverTests(IEnumerable<string> executables, ITestFrameworkReporter reporter)
Expand All @@ -36,13 +36,13 @@ public void DiscoverTests(IEnumerable<string> executables, ITestFrameworkReporte

public IList<TestCase> GetTestsFromExecutable(string executable)
{
TestCaseFactory factory = new TestCaseFactory(executable, TestEnvironment, DiaResolverFactory);
var factory = new TestCaseFactory(executable, _testEnvironment, _diaResolverFactory);
IList<TestCase> testCases = factory.CreateTestCases();

TestEnvironment.LogInfo("Found " + testCases.Count + " tests in executable " + executable);
_testEnvironment.LogInfo("Found " + testCases.Count + " tests in executable " + executable);
foreach (TestCase testCase in testCases)
{
TestEnvironment.DebugInfo("Added testcase " + testCase.DisplayName);
_testEnvironment.DebugInfo("Added testcase " + testCase.DisplayName);
}

return testCases;
Expand All @@ -63,15 +63,15 @@ public bool IsGoogleTestExecutable(string executable, string customRegex = "")
matches = SafeMatches(executable, customRegex);
}

TestEnvironment.DebugInfo(
_testEnvironment.DebugInfo(
executable + (matches ? " matches " : " does not match ") + "regex '" + regexUsed + "'");

return matches;
}

private List<string> GetAllGoogleTestExecutables(IEnumerable<string> allExecutables)
{
return allExecutables.Where(e => IsGoogleTestExecutable(e, TestEnvironment.Options.TestDiscoveryRegex)).ToList();
return allExecutables.Where(e => IsGoogleTestExecutable(e, _testEnvironment.Options.TestDiscoveryRegex)).ToList();
}

private bool SafeMatches(string executable, string regex)
Expand All @@ -83,11 +83,11 @@ private bool SafeMatches(string executable, string regex)
}
catch (ArgumentException e)
{
TestEnvironment.LogError($"Regex '{regex}' can not be parsed: {e.Message}");
_testEnvironment.LogError($"Regex '{regex}' can not be parsed: {e.Message}");
}
catch (RegexMatchTimeoutException e)
{
TestEnvironment.LogError($"Regex '{regex}' timed out: {e.Message}");
_testEnvironment.LogError($"Regex '{regex}' timed out: {e.Message}");
}
return matches;
}
Expand Down
30 changes: 15 additions & 15 deletions GoogleTestAdapter/Core/GoogleTestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,56 @@ namespace GoogleTestAdapter
public class GoogleTestExecutor
{

private TestEnvironment TestEnvironment { get; }
private readonly TestEnvironment _testEnvironment;

private ITestRunner Runner { get; set; }
private bool Canceled { get; set; } = false;
private ITestRunner _runner;
private bool _canceled;

public GoogleTestExecutor(TestEnvironment testEnvironment)
{
TestEnvironment = testEnvironment;
_testEnvironment = testEnvironment;
}


public void RunTests(IEnumerable<TestCase> allTestCasesInExecutables, IEnumerable<TestCase> testCasesToRun, ITestFrameworkReporter reporter, IDebuggedProcessLauncher launcher, bool isBeingDebugged, string solutionDirectory)
{
TestCase[] testCasesToRunAsArray = testCasesToRun as TestCase[] ?? testCasesToRun.ToArray();
TestEnvironment.LogInfo("Running " + testCasesToRunAsArray.Length + " tests...");
_testEnvironment.LogInfo("Running " + testCasesToRunAsArray.Length + " tests...");

lock (this)
{
if (Canceled)
if (_canceled)
{
return;
}
ComputeTestRunner(reporter, isBeingDebugged, solutionDirectory);
}

Runner.RunTests(allTestCasesInExecutables, testCasesToRunAsArray, solutionDirectory, null, isBeingDebugged, launcher);
TestEnvironment.LogInfo("Test execution completed.");
_runner.RunTests(allTestCasesInExecutables, testCasesToRunAsArray, solutionDirectory, null, isBeingDebugged, launcher);
_testEnvironment.LogInfo("Test execution completed.");
}

public void Cancel()
{
lock (this)
{
Canceled = true;
Runner?.Cancel();
_canceled = true;
_runner?.Cancel();
}
}

private void ComputeTestRunner(ITestFrameworkReporter reporter, bool isBeingDebugged, string solutionDirectory)
{
if (TestEnvironment.Options.ParallelTestExecution && !isBeingDebugged)
if (_testEnvironment.Options.ParallelTestExecution && !isBeingDebugged)
{
Runner = new ParallelTestRunner(reporter, TestEnvironment, solutionDirectory);
_runner = new ParallelTestRunner(reporter, _testEnvironment, solutionDirectory);
}
else
{
Runner = new PreparingTestRunner(0, solutionDirectory, reporter, TestEnvironment);
if (TestEnvironment.Options.ParallelTestExecution && isBeingDebugged)
_runner = new PreparingTestRunner(0, solutionDirectory, reporter, _testEnvironment);
if (_testEnvironment.Options.ParallelTestExecution && isBeingDebugged)
{
TestEnvironment.DebugInfo(
_testEnvironment.DebugInfo(
"Parallel execution is selected in options, but tests are executed sequentially because debugger is attached.");
}
}
Expand Down
2 changes: 1 addition & 1 deletion GoogleTestAdapter/Core/Helpers/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static TestCase FindTestcase(this IEnumerable<TestCase> testcases, stri

internal static IDictionary<string, List<TestCase>> GroupByExecutable(this IEnumerable<TestCase> testcases)
{
Dictionary<string, List<TestCase>> groupedTestCases = new Dictionary<string, List<TestCase>>();
var groupedTestCases = new Dictionary<string, List<TestCase>>();
foreach (TestCase testCase in testcases)
{
List<TestCase> group;
Expand Down
24 changes: 12 additions & 12 deletions GoogleTestAdapter/Core/Helpers/ProcessLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace GoogleTestAdapter.Helpers

public class ProcessLauncher
{
private readonly ILogger Logger;
private readonly string PathExtension;
private readonly ILogger _logger;
private readonly string _pathExtension;

public ProcessLauncher(ILogger logger, string pathExtension)
{
Logger = logger;
PathExtension = pathExtension;
_logger = logger;
_pathExtension = pathExtension;
}

public List<string> GetOutputOfCommand(string workingDirectory, string command, string param, bool printTestOutput,
Expand All @@ -27,7 +27,7 @@ public List<string> GetOutputOfCommand(string workingDirectory, string command,
public List<string> GetOutputOfCommand(string workingDirectory, string command, string param, bool printTestOutput,
bool throwIfError, out int processExitCode)
{
List<string> output = new List<string>();
var output = new List<string>();
processExitCode = LaunchProcess(workingDirectory, command, param, printTestOutput, throwIfError, output);
return output;
}
Expand All @@ -36,7 +36,7 @@ public List<string> GetOutputOfCommand(string workingDirectory, string command,
private int LaunchProcess(string workingDirectory, string command, string param, bool printTestOutput,
bool throwIfError, List<string> output)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo(command, param)
var processStartInfo = new ProcessStartInfo(command, param)
{
RedirectStandardOutput = true,
RedirectStandardError = false,
Expand All @@ -45,22 +45,22 @@ private int LaunchProcess(string workingDirectory, string command, string param,
WorkingDirectory = workingDirectory
};

if (!string.IsNullOrEmpty(PathExtension))
processStartInfo.EnvironmentVariables["PATH"] = Utils.GetExtendedPath(PathExtension);
if (!string.IsNullOrEmpty(_pathExtension))
processStartInfo.EnvironmentVariables["PATH"] = Utils.GetExtendedPath(_pathExtension);

Process process = Process.Start(processStartInfo);
try
{
ProcessWaiter waiter = new ProcessWaiter(process);
var waiter = new ProcessWaiter(process);
if (printTestOutput)
{
Logger.LogInfo(
_logger.LogInfo(
">>>>>>>>>>>>>>> Output of command '" + command + " " + param + "'");
}
ReadTheStream(process, output, printTestOutput, throwIfError);
if (printTestOutput)
{
Logger.LogInfo("<<<<<<<<<<<<<<< End of Output");
_logger.LogInfo("<<<<<<<<<<<<<<< End of Output");
}
return waiter.WaitForExit();
}
Expand All @@ -79,7 +79,7 @@ private void ReadTheStream(Process process, List<string> streamContent, bool pri
streamContent.Add(line);
if (printTestOutput)
{
Logger.LogInfo(line);
_logger.LogInfo(line);
}
}
if ((throwIfError && process.ExitCode != 0))
Expand Down
8 changes: 4 additions & 4 deletions GoogleTestAdapter/Core/Helpers/RegexTraitParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ namespace GoogleTestAdapter.Helpers
{
public class RegexTraitParser
{
private TestEnvironment TestEnvironment { get; }
private readonly TestEnvironment _testEnvironment;


public RegexTraitParser(TestEnvironment testEnvironment)
{
this.TestEnvironment = testEnvironment;
_testEnvironment = testEnvironment;
}


public List<RegexTraitPair> ParseTraitsRegexesString(string option)
{
List<RegexTraitPair> result = new List<RegexTraitPair>();
var result = new List<RegexTraitPair>();

string[] pairs = option.Split(
new[] { SettingsWrapper.TraitsRegexesPairSeparator },
Expand All @@ -30,7 +30,7 @@ public List<RegexTraitPair> ParseTraitsRegexesString(string option)
}
catch (Exception e)
{
TestEnvironment.LogError(
_testEnvironment.LogError(
"Could not parse pair '" + pair + "', exception message: " + e.Message);
}
}
Expand Down
18 changes: 9 additions & 9 deletions GoogleTestAdapter/Core/Helpers/TestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ private enum LogType { Normal, Debug }


public SettingsWrapper Options { get; }
private ILogger Logger { get; }
private readonly ILogger _logger;


public TestEnvironment(SettingsWrapper options, ILogger logger)
{
this.Options = options;
this.Logger = logger;
Options = options;
_logger = logger;
}


Expand All @@ -30,13 +30,13 @@ public override void Log(Severity severity, string message)
switch (severity)
{
case Severity.Info:
Logger.LogInfo(message);
_logger.LogInfo(message);
break;
case Severity.Warning:
Logger.LogWarning(message);
_logger.LogWarning(message);
break;
case Severity.Error:
Logger.LogError(message);
_logger.LogError(message);
break;
default:
throw new Exception($"Unknown enum literal: {severity}");
Expand All @@ -47,23 +47,23 @@ public void DebugInfo(string message)
{
if (ShouldBeLogged(LogType.Debug))
{
Logger.LogInfo(message);
_logger.LogInfo(message);
}
}

public void DebugWarning(string message)
{
if (ShouldBeLogged(LogType.Debug))
{
Logger.LogWarning(message);
_logger.LogWarning(message);
}
}

public void DebugError(string message)
{
if (ShouldBeLogged(LogType.Debug))
{
Logger.LogError(message);
_logger.LogError(message);
}
}

Expand Down
Loading

0 comments on commit 2f2f012

Please sign in to comment.