[Harness] Fix XHarness unit tests (#8208)

Co-authored-by: Rolf Bjarne Kvinge <rolf@xamarin.com>
This commit is contained in:
Přemek Vysoký 2020-03-27 14:46:54 +01:00 коммит произвёл GitHub
Родитель b55b8e8e0b
Коммит 46a2d96c55
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 44 добавлений и 40 удалений

Просмотреть файл

@ -20,8 +20,11 @@ namespace Xharness.Tests.BCLTestImporter.Tests {
public void GenerateCodeNullProjectName () public void GenerateCodeNullProjectName ()
{ {
var tmp = Path.GetTempFileName (); var tmp = Path.GetTempFileName ();
Assert.ThrowsAsync <ArgumentNullException> (() => File.WriteAllText (tmp, "Hello");
BCLTestInfoPlistGenerator.GenerateCodeAsync (File.Create (tmp), null)); using (var stream = new FileStream (tmp, FileMode.Open)) {
Assert.ThrowsAsync<ArgumentNullException> (() => BCLTestInfoPlistGenerator.GenerateCodeAsync (stream, null));
}
File.Delete (tmp); File.Delete (tmp);
} }

Просмотреть файл

@ -1,10 +1,8 @@
using System; using System.IO;
using System.IO;
using System.Linq; using System.Linq;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using Xharness.BCLTestImporter; using Xharness.BCLTestImporter;
using Xharness.BCLTestImporter.Templates;
using Xharness.BCLTestImporter.Xamarin; using Xharness.BCLTestImporter.Xamarin;
namespace Xharness.Tests.BCLTestImporter.Xamarin.Tests { namespace Xharness.Tests.BCLTestImporter.Xamarin.Tests {
@ -67,11 +65,11 @@ namespace Xharness.Tests.BCLTestImporter.Xamarin.Tests {
Directory.CreateDirectory (ignoreFilesRootDir); Directory.CreateDirectory (ignoreFilesRootDir);
// create a few ignore files // create a few ignore files
foreach (var a in assemblies) { foreach (var a in assemblies) {
File.Create (Path.Combine (ignoreRootDir, $"iOS-{a}.ignore")); File.WriteAllText (Path.Combine (ignoreRootDir, $"iOS-{a}.ignore"), string.Empty);
File.Create (Path.Combine (ignoreRootDir, $"macOSFull-{a}.ignore")); File.WriteAllText (Path.Combine (ignoreRootDir, $"macOSFull-{a}.ignore"), string.Empty);
File.Create (Path.Combine (ignoreRootDir, $"macOS-{a}.ignore")); File.WriteAllText (Path.Combine (ignoreRootDir, $"macOS-{a}.ignore"), string.Empty);
File.Create (Path.Combine (ignoreRootDir, $"tvOS-{a}.ignore")); File.WriteAllText (Path.Combine (ignoreRootDir, $"tvOS-{a}.ignore"), string.Empty);
File.Create (Path.Combine (ignoreRootDir, $"watchOS-{a}.ignore")); File.WriteAllText (Path.Combine (ignoreRootDir, $"watchOS-{a}.ignore"), string.Empty);
} }
} }
@ -82,7 +80,7 @@ namespace Xharness.Tests.BCLTestImporter.Xamarin.Tests {
Directory.CreateDirectory (traitsRootDir); Directory.CreateDirectory (traitsRootDir);
// create a few ignore files // create a few ignore files
foreach (var t in traitsFiles) foreach (var t in traitsFiles)
File.Create (Path.Combine (traitsRootDir, t)); File.WriteAllText (Path.Combine (traitsRootDir, t), string.Empty);
} }
[SetUp] [SetUp]

Просмотреть файл

@ -4,7 +4,6 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Moq;
using NUnit.Framework; using NUnit.Framework;
using Xharness.Execution; using Xharness.Execution;
using Xharness.Logging; using Xharness.Logging;
@ -21,7 +20,6 @@ namespace Xharness.Tests.Execution.Tests {
string stderrLogPath; string stderrLogPath;
string stdoutMessage; string stdoutMessage;
string stderrMessage; string stderrMessage;
Mock<ILogs> logs;
ILog executionLog; ILog executionLog;
ILog stdoutLog; ILog stdoutLog;
ILog stderrLog; ILog stderrLog;
@ -38,7 +36,6 @@ namespace Xharness.Tests.Execution.Tests {
stdoutLogPath = Path.GetTempFileName (); stdoutLogPath = Path.GetTempFileName ();
stdoutMessage = "Hola mundo!!!"; stdoutMessage = "Hola mundo!!!";
stderrMessage = "Adios mundo cruel"; stderrMessage = "Adios mundo cruel";
logs = new Mock<ILogs> ();
executionLog = new LogFile ("my execution log", logPath); executionLog = new LogFile ("my execution log", logPath);
stdoutLog = new LogFile ("my stdout log", stdoutLogPath); stdoutLog = new LogFile ("my stdout log", stdoutLogPath);
stderrLog = new LogFile ("my stderr log", stderrLogPath); stderrLog = new LogFile ("my stderr log", stderrLogPath);
@ -60,6 +57,7 @@ namespace Xharness.Tests.Execution.Tests {
testProcess?.Dispose (); testProcess?.Dispose ();
testProcess = null; testProcess = null;
manager = null; manager = null;
if (File.Exists (logPath)) if (File.Exists (logPath))
File.Delete (logPath); File.Delete (logPath);
if (File.Exists (stderrLogPath)) if (File.Exists (stderrLogPath))
@ -73,6 +71,8 @@ namespace Xharness.Tests.Execution.Tests {
bool stdoutFound = false; bool stdoutFound = false;
bool stderrFound = false; bool stderrFound = false;
executionLog?.Dispose ();
using (var reader = new StreamReader (logPath)) { using (var reader = new StreamReader (logPath)) {
string line; string line;
while ((line = reader.ReadLine ()) != null) { while ((line = reader.ReadLine ()) != null) {
@ -91,12 +91,13 @@ namespace Xharness.Tests.Execution.Tests {
[TestCase (0, 60, false, true, Description = "Timeout" )] // 0, long timeout, failure, timeout [TestCase (0, 60, false, true, Description = "Timeout" )] // 0, long timeout, failure, timeout
public async Task ExecuteCommandAsyncTest (int resultCode, int timeoutCount, bool success, bool timeout) public async Task ExecuteCommandAsyncTest (int resultCode, int timeoutCount, bool success, bool timeout)
{ {
var args = new List<string> (); var args = new List<string> {
args.Add (dummyProcess); dummyProcess,
args.Add ($"--exit-code={resultCode}"); $"--exit-code={resultCode}",
args.Add ($"--timeout={timeoutCount}"); $"--timeout={timeoutCount}",
args.Add ($"--stdout=\"{stdoutMessage}\""); $"--stdout=\"{stdoutMessage}\"",
args.Add ($"--stderr=\"{stderrMessage}\""); $"--stderr=\"{stderrMessage}\""
};
var result = await manager.ExecuteCommandAsync ("mono", args, executionLog, new TimeSpan (0, 0, 5)); var result = await manager.ExecuteCommandAsync ("mono", args, executionLog, new TimeSpan (0, 0, 5));
if (!timeout) if (!timeout)
Assert.AreEqual (resultCode, result.ExitCode, "exit code"); Assert.AreEqual (resultCode, result.ExitCode, "exit code");
@ -155,6 +156,9 @@ namespace Xharness.Tests.Execution.Tests {
bool stdoutFound = false; bool stdoutFound = false;
bool stderrFound = false; bool stderrFound = false;
stdoutLog?.Dispose ();
stderrLog?.Dispose ();
using (var reader = new StreamReader (stdoutLogPath)) { using (var reader = new StreamReader (stdoutLogPath)) {
string line; string line;
while ((line = reader.ReadLine ()) != null) { while ((line = reader.ReadLine ()) != null) {

Просмотреть файл

@ -72,7 +72,7 @@ namespace Xharness.Tests.Hardware.Tests {
MlaunchArgument sdkRootArg = passedArguments.Where (a => a is SdkRootArgument).FirstOrDefault(); MlaunchArgument sdkRootArg = passedArguments.Where (a => a is SdkRootArgument).FirstOrDefault();
Assert.IsNotNull (sdkRootArg, "sdk arg missing"); Assert.IsNotNull (sdkRootArg, "sdk arg missing");
AssertArgumentValue (sdkRootArg, sdkPath, "sdk arg wrong"); AssertArgumentValue (sdkRootArg, $"--sdkroot {sdkPath}", "sdk arg wrong");
MlaunchArgument listDevArg = passedArguments.Where (a => a is ListDevicesArgument).FirstOrDefault(); MlaunchArgument listDevArg = passedArguments.Where (a => a is ListDevicesArgument).FirstOrDefault();
Assert.IsNotNull (listDevArg, "list devices arg missing"); Assert.IsNotNull (listDevArg, "list devices arg missing");
@ -99,7 +99,7 @@ namespace Xharness.Tests.Hardware.Tests {
// we get the temp file that was passed as the args, and write our sample xml, which will be parsed to get the devices :) // we get the temp file that was passed as the args, and write our sample xml, which will be parsed to get the devices :)
var tempPath = args.Where (a => a is ListDevicesArgument).First ().AsCommandLineArgument (); var tempPath = args.Where (a => a is ListDevicesArgument).First ().AsCommandLineArgument ();
tempPath = tempPath.Substring(tempPath.IndexOf('=') + 1); tempPath = tempPath.Substring (tempPath.IndexOf('=') + 1).Replace ("\"", string.Empty);
var name = GetType ().Assembly.GetManifestResourceNames ().Where (a => a.EndsWith ("devices.xml", StringComparison.Ordinal)).FirstOrDefault (); var name = GetType ().Assembly.GetManifestResourceNames ().Where (a => a.EndsWith ("devices.xml", StringComparison.Ordinal)).FirstOrDefault ();
using (var outputStream = new StreamWriter (tempPath)) using (var outputStream = new StreamWriter (tempPath))
@ -119,7 +119,7 @@ namespace Xharness.Tests.Hardware.Tests {
MlaunchArgument sdkRootArg = passedArguments.Where (a => a is SdkRootArgument).FirstOrDefault(); MlaunchArgument sdkRootArg = passedArguments.Where (a => a is SdkRootArgument).FirstOrDefault();
Assert.IsNotNull (sdkRootArg, "sdk arg missing"); Assert.IsNotNull (sdkRootArg, "sdk arg missing");
AssertArgumentValue (sdkRootArg, sdkPath, "sdk arg wrong"); AssertArgumentValue (sdkRootArg, $"--sdkroot {sdkPath}", "sdk arg wrong");
MlaunchArgument listDevArg = passedArguments.Where (a => a is ListDevicesArgument).FirstOrDefault(); MlaunchArgument listDevArg = passedArguments.Where (a => a is ListDevicesArgument).FirstOrDefault();
Assert.IsNotNull (listDevArg, "list devices arg missing"); Assert.IsNotNull (listDevArg, "list devices arg missing");

Просмотреть файл

@ -7,7 +7,6 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using Xamarin;
using Xharness.Execution; using Xharness.Execution;
using Xharness.Execution.Mlaunch; using Xharness.Execution.Mlaunch;
using Xharness.Hardware; using Xharness.Hardware;
@ -71,14 +70,14 @@ namespace Xharness.Tests.Hardware.Tests {
}); });
// validate the execution of mlaunch // validate the execution of mlaunch
MlaunchArgument sdkRootArg = passedArguments.Where (a => a is SdkRootArgument).FirstOrDefault(); MlaunchArgument sdkRootArg = passedArguments.Where (a => a is SdkRootArgument).FirstOrDefault ();
Assert.IsNotNull (sdkRootArg, "sdk arg missing"); Assert.IsNotNull (sdkRootArg, "sdk arg missing");
AssertArgumentValue (sdkRootArg, sdkPath, "sdk arg wrong"); AssertArgumentValue (sdkRootArg, $"--sdkroot {sdkPath}", "sdk arg wrong");
MlaunchArgument listSimArg = passedArguments.Where (a => a is ListSimulatorsArgument).FirstOrDefault(); MlaunchArgument listSimArg = passedArguments.Where (a => a is ListSimulatorsArgument).FirstOrDefault ();
Assert.IsNotNull (listSimArg, "list devices arg missing"); Assert.IsNotNull (listSimArg, "list devices arg missing");
MlaunchArgument outputFormatArg = passedArguments.Where (a => a is XmlOutputFormatArgument).FirstOrDefault(); MlaunchArgument outputFormatArg = passedArguments.Where (a => a is XmlOutputFormatArgument).FirstOrDefault ();
Assert.IsNotNull (outputFormatArg, "output format arg missing"); Assert.IsNotNull (outputFormatArg, "output format arg missing");
} }
@ -110,7 +109,7 @@ namespace Xharness.Tests.Hardware.Tests {
// we get the temp file that was passed as the args, and write our sample xml, which will be parsed to get the devices :) // we get the temp file that was passed as the args, and write our sample xml, which will be parsed to get the devices :)
var tempPath = args.Where (a => a is ListSimulatorsArgument).First ().AsCommandLineArgument (); var tempPath = args.Where (a => a is ListSimulatorsArgument).First ().AsCommandLineArgument ();
tempPath = tempPath.Substring(tempPath.IndexOf('=') + 1); tempPath = tempPath.Substring (tempPath.IndexOf ('=') + 1).Replace ("\"", string.Empty);
CopySampleData (tempPath); CopySampleData (tempPath);
return Task.FromResult (new ProcessExecutionResult { ExitCode = 0, TimedOut = false }); return Task.FromResult (new ProcessExecutionResult { ExitCode = 0, TimedOut = false });
@ -121,17 +120,17 @@ namespace Xharness.Tests.Hardware.Tests {
// validate the execution of mlaunch // validate the execution of mlaunch
Assert.AreEqual (mlaunchPath, processPath, "process path"); Assert.AreEqual (mlaunchPath, processPath, "process path");
MlaunchArgument sdkRootArg = passedArguments.Where (a => a is SdkRootArgument).FirstOrDefault(); MlaunchArgument sdkRootArg = passedArguments.Where (a => a is SdkRootArgument).FirstOrDefault ();
Assert.IsNotNull (sdkRootArg, "sdk arg missing"); Assert.IsNotNull (sdkRootArg, "sdk arg missing");
AssertArgumentValue (sdkRootArg, sdkPath, "sdk arg wrong"); AssertArgumentValue (sdkRootArg, $"--sdkroot {sdkPath}", "sdk arg wrong");
MlaunchArgument listSimArg = passedArguments.Where (a => a is ListSimulatorsArgument).FirstOrDefault(); MlaunchArgument listSimArg = passedArguments.Where (a => a is ListSimulatorsArgument).FirstOrDefault ();
Assert.IsNotNull (listSimArg, "list devices arg missing"); Assert.IsNotNull (listSimArg, "list devices arg missing");
MlaunchArgument outputFormatArg = passedArguments.Where (a => a is XmlOutputFormatArgument).FirstOrDefault(); MlaunchArgument outputFormatArg = passedArguments.Where (a => a is XmlOutputFormatArgument).FirstOrDefault ();
Assert.IsNotNull (outputFormatArg, "output format arg missing"); Assert.IsNotNull (outputFormatArg, "output format arg missing");
Assert.AreEqual (75, simulators.AvailableDevices.Count()); Assert.AreEqual (75, simulators.AvailableDevices.Count ());
} }
[TestCase (TestTarget.Simulator_iOS64, 1)] [TestCase (TestTarget.Simulator_iOS64, 1)]
@ -147,7 +146,7 @@ namespace Xharness.Tests.Hardware.Tests {
harness.Setup (h => h.MlaunchPath).Returns (mlaunchPath); harness.Setup (h => h.MlaunchPath).Returns (mlaunchPath);
harness.Setup (h => h.XcodeRoot).Returns (sdkPath); harness.Setup (h => h.XcodeRoot).Returns (sdkPath);
harness harness
.Setup (h => h.ExecuteXcodeCommandAsync ("simctl", It.Is<string []> (args => args[0] == "create"), executionLog.Object, TimeSpan.FromMinutes (1))) .Setup (h => h.ExecuteXcodeCommandAsync ("simctl", It.Is<string []> (args => args [0] == "create"), executionLog.Object, TimeSpan.FromMinutes (1)))
.ReturnsAsync (new ProcessExecutionResult () { ExitCode = 0 }); .ReturnsAsync (new ProcessExecutionResult () { ExitCode = 0 });
// moq It.Is is not working as nicelly as we would like it, we capture data and use asserts // moq It.Is is not working as nicelly as we would like it, we capture data and use asserts
@ -159,7 +158,7 @@ namespace Xharness.Tests.Hardware.Tests {
// we get the temp file that was passed as the args, and write our sample xml, which will be parsed to get the devices :) // we get the temp file that was passed as the args, and write our sample xml, which will be parsed to get the devices :)
var tempPath = args.Where (a => a is ListSimulatorsArgument).First ().AsCommandLineArgument (); var tempPath = args.Where (a => a is ListSimulatorsArgument).First ().AsCommandLineArgument ();
tempPath = tempPath.Substring(tempPath.IndexOf('=') + 1); tempPath = tempPath.Substring (tempPath.IndexOf ('=') + 1).Replace ("\"", string.Empty);
CopySampleData (tempPath); CopySampleData (tempPath);
return Task.FromResult (new ProcessExecutionResult { ExitCode = 0, TimedOut = false }); return Task.FromResult (new ProcessExecutionResult { ExitCode = 0, TimedOut = false });