xamarin-macios/tests/xharness/SolutionGenerator.cs

176 строки
7.8 KiB
C#
Исходник Обычный вид История

using System;
2016-05-26 16:06:52 +03:00
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Text;
using Microsoft.DotNet.XHarness.iOS.Shared.Hardware;
using Microsoft.DotNet.XHarness.iOS.Shared.Utilities;
[Harness] Split out Targets, Utils and remove external links (#8061) * [Harness] Refactor process management to be testable. Move all the extension methods to a class. After this refactor, we will be able to DI the manager in the other classes and assert that the processes are called with the correct parameters without the need of launching them. Also added tests for the manager. We create a dummy console app that will be executed by the tests. The console app has a number of parameters that will be used to ensure that the new process behaves as we want: - Use the passed exit code. - Create child proecesses if needed. - Sleep to force a timeout. - Writer messages to stdout and stderr. Our tests call the dummy app and ensures that the results match the behaviour expected by the dummy app. * Apply suggestions from code review Co-Authored-By: Přemek Vysoký <premek.vysoky@microsoft.com> * Move out utils into a separate namespace * Move Cache to the test project * Fix namespaces after merge * Remove unneeded code * Move Target files * Sort csproj * Refactor Targets * Rename Cache to TempDirectory * Fix using * Move ProjectFileExtensions * Remove dead code * Move Extensions * Add empty StringUtilsTests * Add StringUtils tests * Revert refactorings * Update tests/xharness/Utilities/StringUtils.cs Co-Authored-By: Rolf Bjarne Kvinge <rolf@xamarin.com> * Update tests/xharness/Utilities/StringUtils.cs Co-Authored-By: Rolf Bjarne Kvinge <rolf@xamarin.com> * Add better formatarguments test * Update tests/xharness/Utilities/StringUtils.cs Co-Authored-By: Rolf Bjarne Kvinge <rolf@xamarin.com> Co-authored-by: Manuel de la Pena <mandel@microsoft.com> Co-authored-by: Premek Vysoky <prvysoky@microsoft.com> Co-authored-by: Rolf Bjarne Kvinge <rolf@xamarin.com>
2020-03-10 15:12:33 +03:00
using Xharness.Targets;
2016-05-26 16:06:52 +03:00
namespace Xharness
2016-05-26 16:06:52 +03:00
{
public static class SolutionGenerator
{
static void AddProjectToSolution (IHarness harness, string sln_path, TextWriter solution, string project_path, out string configurations)
2016-05-26 16:06:52 +03:00
{
var project = new XmlDocument ();
project.LoadWithoutNetworkAccess (project_path);
var guid = project.GetProjectGuid ();
solution.WriteLine ("Project(\"{3}\") = \"{0}\", \"{1}\", \"{2}\"", Path.GetFileNameWithoutExtension (project_path), FixProjectPath (sln_path, project_path), guid, project_path.EndsWith (".csproj") ? Target.CSharpGuid : Target.FSharpGuid);
solution.WriteLine ("EndProject");
configurations = string.Format (
"\t\t{0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n" +
"\t\t{0}.Debug|Any CPU.Build.0 = Debug|Any CPU\n" +
"\t\t{0}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU\n" +
"\t\t{0}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU\n" +
"\t\t{0}.Debug|iPhone.ActiveCfg = Debug|Any CPU\n" +
"\t\t{0}.Debug|iPhone.Build.0 = Debug|Any CPU\n" +
"\t\t{0}.Release-bitcode|Any CPU.ActiveCfg = Release-bitcode|Any CPU\n" +
"\t\t{0}.Release-bitcode|Any CPU.Build.0 = Release-bitcode|Any CPU\n" +
"\t\t{0}.Release-bitcode|iPhoneSimulator.ActiveCfg = Release-bitcode|Any CPU\n" +
"\t\t{0}.Release-bitcode|iPhoneSimulator.Build.0 = Release-bitcode|Any CPU\n" +
"\t\t{0}.Release-bitcode|iPhone.ActiveCfg = Release-bitcode|Any CPU\n" +
"\t\t{0}.Release-bitcode|iPhone.Build.0 = Release-bitcode|Any CPU\n" +
"\t\t{0}.Release|Any CPU.ActiveCfg = Release|Any CPU\n" +
"\t\t{0}.Release|Any CPU.Build.0 = Release|Any CPU\n" +
"\t\t{0}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU\n" +
"\t\t{0}.Release|iPhoneSimulator.Build.0 = Release|Any CPU\n" +
"\t\t{0}.Release|iPhone.ActiveCfg = Release|Any CPU\n" +
"\t\t{0}.Release|iPhone.Build.0 = Release|Any CPU\n", guid);
}
static string FixProjectPath (string sln_path, string project_path)
{
var sln_dir = Path.GetDirectoryName (sln_path);
if (project_path.StartsWith (sln_dir + Path.PathSeparator, StringComparison.OrdinalIgnoreCase))
2016-05-26 16:06:52 +03:00
project_path = project_path.Substring (sln_dir.Length + 1);
project_path = project_path.Replace ('/', '\\');
return project_path;
}
public static void CreateSolution (IHarness harness, IEnumerable<Target> targets, string infix, DevicePlatform testPlatform)
2016-05-26 16:06:52 +03:00
{
CreateSolution (harness, targets, null, infix, testPlatform);
2016-05-26 16:06:52 +03:00
}
public static void CreateSolution (IHarness harness, IEnumerable<Target> targets, Target exeTarget, string infix, DevicePlatform testPlatform)
2016-05-26 16:06:52 +03:00
{
var folders = new StringBuilder ();
var srcDirectory = Path.Combine (HarnessConfiguration.RootDirectory, "..", "src");
var sln_path = exeTarget == null ? Path.Combine (HarnessConfiguration.RootDirectory, "tests-" + infix + ".sln") : Path.Combine (Path.GetDirectoryName (exeTarget.ProjectPath), Path.GetFileNameWithoutExtension (exeTarget.ProjectPath) + ".sln");
2016-05-26 16:06:52 +03:00
using (var writer = new StringWriter ()) {
writer.WriteLine ();
writer.WriteLine ("Microsoft Visual Studio Solution File, Format Version 11.00");
writer.WriteLine ("# Visual Studio 2010");
foreach (var target in targets) {
var relatedProjects = target.GetRelatedProjects ();
var hasRelatedProjects = relatedProjects != null;
2016-05-26 16:06:52 +03:00
var folderGuid = string.Empty;
var useFolders = hasRelatedProjects && target.IsExe && exeTarget == null;
2016-05-26 16:06:52 +03:00
if (hasRelatedProjects && target.IsExe) {
2016-05-26 16:06:52 +03:00
if (exeTarget == null) {
CreateSolution (harness, targets, target, infix, testPlatform); // create a solution for just this test project as well
2016-05-26 16:06:52 +03:00
} else if (exeTarget != target) {
continue;
}
}
if (useFolders) {
folderGuid = Guid.NewGuid ().ToString ().ToUpperInvariant ();
writer.WriteLine ("Project(\"{{2150E333-8FDC-42A3-9474-1A3956D46DE8}}\") = \"{0}\", \"{0}\", \"{{{1}}}\"", target.Name, folderGuid);
writer.WriteLine ("EndProject");
}
writer.WriteLine ("Project(\"{3}\") = \"{0}\", \"{1}\", \"{2}\"", Path.GetFileNameWithoutExtension (target.ProjectPath), FixProjectPath (sln_path, Path.GetFullPath (target.ProjectPath)), target.ProjectGuid, target.LanguageGuid);
writer.WriteLine ("EndProject");
if (hasRelatedProjects && target.IsExe) {
foreach (var rp in relatedProjects) {
writer.WriteLine ("Project(\"{3}\") = \"{0}\", \"{1}\", \"{2}\"", Path.GetFileNameWithoutExtension (rp.ProjectPath), FixProjectPath (sln_path, Path.GetFullPath (rp.ProjectPath)), rp.Guid, target.LanguageGuid);
writer.WriteLine ("EndProject");
}
2016-05-26 16:06:52 +03:00
}
if (useFolders) {
folders.AppendFormat ("\t\t{0} = {{{1}}}\n", target.ProjectGuid, folderGuid);
foreach (var rp in relatedProjects)
folders.AppendFormat ("\t\t{0} = {{{1}}}\n", rp.Guid, folderGuid);
2016-05-26 16:06:52 +03:00
}
}
// Add reference to the Touch.Client project
2016-05-26 16:06:52 +03:00
string configuration;
var proj_path = Path.GetFullPath (Path.Combine (srcDirectory, "..", "external", "Touch.Unit", "Touch.Client", testPlatform.AsString (), "Touch.Client-" + testPlatform.AsString () + ".csproj"));
AddProjectToSolution (harness, sln_path, writer, proj_path, out configuration);
2016-05-26 16:06:52 +03:00
writer.WriteLine ("Global");
writer.WriteLine ("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution");
writer.WriteLine ("\t\tDebug|iPhoneSimulator = Debug|iPhoneSimulator");
writer.WriteLine ("\t\tRelease|iPhoneSimulator = Release|iPhoneSimulator");
writer.WriteLine ("\t\tDebug|iPhone = Debug|iPhone");
writer.WriteLine ("\t\tRelease|iPhone = Release|iPhone");
writer.WriteLine ("\t\tRelease-bitcode|iPhone = Release-bitcode|iPhone");
writer.WriteLine ("\t\tDebug|Any CPU = Debug|Any CPU");
writer.WriteLine ("\t\tRelease|Any CPU = Release|Any CPU");
writer.WriteLine ("\tEndGlobalSection");
writer.WriteLine ("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution");
var exePlatforms = new string[] { "iPhone", "iPhoneSimulator" };
var configurations = new string[] { "Debug", "Release", "Release-bitcode" };
foreach (var target in targets) {
if (target.IsExe && exeTarget != null && target != exeTarget)
continue;
foreach (var conf in configurations) {
if (target.IsExe) {
foreach (var platform in exePlatforms) {
writer.WriteLine ("\t\t{0}.{1}|{2}.ActiveCfg = {1}|{2}", target.ProjectGuid, conf, platform);
writer.WriteLine ("\t\t{0}.{1}|{2}.Build.0 = {1}|{2}", target.ProjectGuid, conf, platform);
}
} else {
foreach (var platform in new string[] { "Any CPU", "iPhone", "iPhoneSimulator" }) {
writer.WriteLine ("\t\t{0}.{1}|{2}.ActiveCfg = {1}|Any CPU", target.ProjectGuid, conf, platform);
writer.WriteLine ("\t\t{0}.{1}|{2}.Build.0 = {1}|Any CPU", target.ProjectGuid, conf, platform);
}
}
}
if (target.IsExe) {
var relatedProjects = target.GetRelatedProjects ();
if (relatedProjects != null) {
foreach (var rp in relatedProjects) {
foreach (var conf in configurations) {
foreach (var platform in exePlatforms) {
writer.WriteLine ("\t\t{0}.{1}|{2}.ActiveCfg = {1}|{2}", rp.Guid, conf, platform);
writer.WriteLine ("\t\t{0}.{1}|{2}.Build.0 = {1}|{2}", rp.Guid, conf, platform);
}
2016-05-26 16:06:52 +03:00
}
}
}
2016-05-26 16:06:52 +03:00
}
}
writer.Write (configuration);
writer.WriteLine ("\tEndGlobalSection");
if (folders.Length > 0) {
writer.WriteLine ("\tGlobalSection(NestedProjects) = preSolution");
writer.Write (folders.ToString ());
writer.WriteLine ("\tEndGlobalSection");
}
writer.WriteLine ("EndGlobal");
harness.Save (writer, sln_path);
}
}
}
}