Esempio n. 1
0
        public override bool Execute()
        {
            RemotingUtility.CleanUpRegisteredChannels();
            XElement assembliesElement = null;
            var      environment       = String.Format("{0}-bit .NET {1}", IntPtr.Size * 8, Environment.Version);

            if (NeedsXml)
            {
                assembliesElement = new XElement("assemblies");
            }

            string originalWorkingFolder = Directory.GetCurrentDirectory();

            using (AssemblyHelper.SubscribeResolve())
            {
                Log.LogMessage(MessageImportance.High, "xUnit.net MSBuild runner ({0})", environment);

                var testAssemblyPaths = Assemblies.Select(assembly =>
                {
                    string assemblyFileName = assembly.GetMetadata("FullPath");
                    string configFileName   = assembly.GetMetadata("ConfigFile");
                    if (configFileName != null && configFileName.Length == 0)
                    {
                        configFileName = null;
                    }

                    return(Tuple.Create(assemblyFileName, configFileName));
                }).ToList();

                if (WorkingFolder != null)
                {
                    Directory.SetCurrentDirectory(WorkingFolder);
                }

                if (ParallelizeAssemblies)
                {
                    var tasks   = testAssemblyPaths.Select(path => Task.Run(() => ExecuteAssembly(path.Item1, path.Item2)));
                    var results = Task.WhenAll(tasks).GetAwaiter().GetResult();
                    foreach (var assemblyElement in results.Where(result => result != null))
                    {
                        assembliesElement.Add(assemblyElement);
                    }
                }
                else
                {
                    foreach (var path in testAssemblyPaths)
                    {
                        var assemblyElement = ExecuteAssembly(path.Item1, path.Item2);
                        if (assemblyElement != null)
                        {
                            assembliesElement.Add(assemblyElement);
                        }
                    }
                }

                if (completionMessages.Count > 0)
                {
                    Log.LogMessage(MessageImportance.High, "=== TEST EXECUTION SUMMARY ===");
                    int longestAssemblyName = completionMessages.Keys.Max(key => key.Length);
                    int longestTotal        = completionMessages.Values.Max(summary => summary.Total.ToString().Length);
                    int longestFailed       = completionMessages.Values.Max(summary => summary.Failed.ToString().Length);
                    int longestSkipped      = completionMessages.Values.Max(summary => summary.Skipped.ToString().Length);
                    int longestTime         = completionMessages.Values.Max(summary => summary.Time.ToString("0.000s").Length);

                    foreach (var message in completionMessages.OrderBy(m => m.Key))
                    {
                        Log.LogMessage(MessageImportance.High,
                                       "   {0}  Total: {1}, Failed: {2}, Skipped: {3}, Time: {4}",
                                       message.Key.PadRight(longestAssemblyName),
                                       message.Value.Total.ToString().PadLeft(longestTotal),
                                       message.Value.Failed.ToString().PadLeft(longestFailed),
                                       message.Value.Skipped.ToString().PadLeft(longestSkipped),
                                       message.Value.Time.ToString("0.000s").PadLeft(longestTime));
                    }
                }
            }

            Directory.SetCurrentDirectory(WorkingFolder ?? originalWorkingFolder);

            if (NeedsXml)
            {
                if (Xml != null)
                {
                    assembliesElement.Save(Xml.GetMetadata("FullPath"));
                }

                if (XmlV1 != null)
                {
                    Transform("xUnit1.xslt", assembliesElement, XmlV1);
                }

                if (Html != null)
                {
                    Transform("HTML.xslt", assembliesElement, Html);
                }
            }

            return(ExitCode == 0);
        }
Esempio n. 2
0
        public override bool Execute()
        {
            Guard.ArgumentNotNull(nameof(Assemblies), Assemblies);

            RemotingUtility.CleanUpRegisteredChannels();

            XElement?assembliesElement = null;

            if (NeedsXml)
            {
                assembliesElement = new XElement("assemblies");
            }

            var appDomains = default(AppDomainSupport?);

            switch (AppDomains?.ToLowerInvariant())
            {
            case null:
                break;

            case "ifavailable":
                appDomains = AppDomainSupport.IfAvailable;
                break;

            case "true":
            case "required":
                appDomains = AppDomainSupport.Required;
                break;

            case "false":
            case "denied":
                appDomains = AppDomainSupport.Denied;
                break;

            default:
                Log.LogError("AppDomains value '{0}' is invalid: must be 'ifavailable', 'required', or 'denied'", AppDomains);
                return(false);
            }

            switch (MaxParallelThreads)
            {
            case null:
            case "default":
                break;

            case "unlimited":
                maxThreadCount = -1;
                break;

            default:
                int threadValue;
                if (!int.TryParse(MaxParallelThreads, out threadValue) || threadValue < 1)
                {
                    Log.LogError("MaxParallelThreads value '{0}' is invalid: must be 'default', 'unlimited', or a positive number", MaxParallelThreads);
                    return(false);
                }

                maxThreadCount = threadValue;
                break;
            }

            var originalWorkingFolder          = Directory.GetCurrentDirectory();
            var internalDiagnosticsMessageSink = DiagnosticMessageSink.ForInternalDiagnostics(Log, InternalDiagnosticMessages);

            using (AssemblyHelper.SubscribeResolveForAssembly(typeof(xunit), internalDiagnosticsMessageSink))
            {
                var reporter = GetReporter();
                if (reporter == null)
                {
                    return(false);
                }

                try
                {
                    logger = new MSBuildLogger(Log);
                    reporterMessageHandler = reporter.CreateMessageHandler(logger, internalDiagnosticsMessageSink).GetAwaiter().GetResult();

                    if (!NoLogo)
                    {
                        Log.LogMessage(MessageImportance.High, $"xUnit.net v3 MSBuild Runner v{ThisAssembly.AssemblyInformationalVersion} ({IntPtr.Size * 8}-bit {RuntimeInformation.FrameworkDescription})");
                    }

                    var project = new XunitProject();
                    foreach (var assembly in Assemblies)
                    {
                        var assemblyFileName = assembly.GetMetadata("FullPath");
                        var configFileName   = assembly.GetMetadata("ConfigFile");
                        if (configFileName != null && configFileName.Length == 0)
                        {
                            configFileName = null;
                        }

                        var targetFramework = AssemblyUtility.GetTargetFramework(assemblyFileName);
                        var projectAssembly = new XunitProjectAssembly(project)
                        {
                            AssemblyFilename = assemblyFileName,
                            ConfigFilename   = configFileName,
                            TargetFramework  = targetFramework
                        };

                        ConfigReader.Load(projectAssembly.Configuration, assemblyFileName, configFileName);

                        if (shadowCopy.HasValue)
                        {
                            projectAssembly.Configuration.ShadowCopy = shadowCopy;
                        }

                        project.Add(projectAssembly);
                    }

                    if (WorkingFolder != null)
                    {
                        Directory.SetCurrentDirectory(WorkingFolder);
                    }

                    var clockTime = Stopwatch.StartNew();

                    if (!parallelizeAssemblies.HasValue)
                    {
                        parallelizeAssemblies = project.All(assembly => assembly.Configuration.ParallelizeAssemblyOrDefault);
                    }

                    if (parallelizeAssemblies.GetValueOrDefault())
                    {
                        var tasks   = project.Assemblies.Select(assembly => Task.Run(() => ExecuteAssembly(assembly, appDomains).AsTask()));
                        var results = Task.WhenAll(tasks).GetAwaiter().GetResult();
                        foreach (var assemblyElement in results.WhereNotNull())
                        {
                            assembliesElement !.Add(assemblyElement);
                        }
                    }
                    else
                    {
                        foreach (var assembly in project.Assemblies)
                        {
                            var assemblyElement = ExecuteAssembly(assembly, appDomains);
                            if (assemblyElement != null)
                            {
                                assembliesElement !.Add(assemblyElement);
                            }
                        }
                    }

                    clockTime.Stop();

                    if (assembliesElement != null)
                    {
                        assembliesElement.Add(new XAttribute("timestamp", DateTime.Now.ToString(CultureInfo.InvariantCulture)));
                    }

                    if (completionMessages.Count > 0)
                    {
                        var summaries = new TestExecutionSummaries {
                            ElapsedClockTime = clockTime.Elapsed
                        };
                        foreach (var completionMessage in completionMessages.OrderBy(kvp => kvp.Key))
                        {
                            summaries.Add(completionMessage.Key, completionMessage.Value);
                        }
                        reporterMessageHandler.OnMessage(summaries);
                    }
                }
                finally
                {
                    reporter.DisposeAsync().GetAwaiter().GetResult();
                }
            }

            Directory.SetCurrentDirectory(WorkingFolder ?? originalWorkingFolder);

            if (NeedsXml && assembliesElement != null)
            {
                if (Xml != null)
                {
                    TransformFactory.Transform("xml", assembliesElement, Xml.GetMetadata("FullPath"));
                }

                if (XmlV1 != null)
                {
                    TransformFactory.Transform("xmlv1", assembliesElement, XmlV1.GetMetadata("FullPath"));
                }

                if (Html != null)
                {
                    TransformFactory.Transform("html", assembliesElement, Html.GetMetadata("FullPath"));
                }

                if (NUnit != null)
                {
                    TransformFactory.Transform("nunit", assembliesElement, NUnit.GetMetadata("FullPath"));
                }

                if (JUnit != null)
                {
                    TransformFactory.Transform("junit", assembliesElement, JUnit.GetMetadata("FullPath"));
                }
            }

            // ExitCode is set to 1 for test failures and -1 for Exceptions.
            return(ExitCode == 0 || (ExitCode == 1 && IgnoreFailures));
        }
Esempio n. 3
0
        public override bool Execute()
        {
            RemotingUtility.CleanUpRegisteredChannels();

            XElement assembliesElement = null;
            var      environment       = $"{IntPtr.Size * 8}-bit .NET {Environment.Version}";

            if (NeedsXml)
            {
                assembliesElement = new XElement("assemblies");
            }

            switch (MaxParallelThreads)
            {
            case null:
            case "default":
                break;

            case "unlimited":
                maxThreadCount = -1;
                break;

            default:
                int threadValue;
                if (!int.TryParse(MaxParallelThreads, out threadValue) || threadValue < 1)
                {
                    Log.LogError("MaxParallelThreads value '{0}' is invalid: must be 'default', 'unlimited', or a positive number", MaxParallelThreads);
                    return(false);
                }

                maxThreadCount = threadValue;
                break;
            }

            var originalWorkingFolder = Directory.GetCurrentDirectory();

            using (AssemblyHelper.SubscribeResolve())
            {
                var reporter = GetReporter();
                if (reporter == null)
                {
                    return(false);
                }

                logger = new MSBuildLogger(Log);
                reporterMessageHandler = MessageSinkWithTypesAdapter.Wrap(reporter.CreateMessageHandler(logger));

                if (!NoLogo)
                {
                    Log.LogMessage(MessageImportance.High, "xUnit.net MSBuild Runner ({0})", environment);
                }

                var project = new XunitProject();
                foreach (var assembly in Assemblies)
                {
                    var assemblyFileName = assembly.GetMetadata("FullPath");
                    var configFileName   = assembly.GetMetadata("ConfigFile");
                    if (configFileName != null && configFileName.Length == 0)
                    {
                        configFileName = null;
                    }

                    var projectAssembly = new XunitProjectAssembly {
                        AssemblyFilename = assemblyFileName, ConfigFilename = configFileName
                    };
                    if (shadowCopy.HasValue)
                    {
                        projectAssembly.Configuration.ShadowCopy = shadowCopy;
                    }

                    project.Add(projectAssembly);
                }

                if (WorkingFolder != null)
                {
                    Directory.SetCurrentDirectory(WorkingFolder);
                }

                var clockTime = Stopwatch.StartNew();

                if (!parallelizeAssemblies.HasValue)
                {
                    parallelizeAssemblies = project.All(assembly => assembly.Configuration.ParallelizeAssemblyOrDefault);
                }

                if (parallelizeAssemblies.GetValueOrDefault())
                {
                    var tasks   = project.Assemblies.Select(assembly => Task.Run(() => ExecuteAssembly(assembly)));
                    var results = Task.WhenAll(tasks).GetAwaiter().GetResult();
                    foreach (var assemblyElement in results.Where(result => result != null))
                    {
                        assembliesElement.Add(assemblyElement);
                    }
                }
                else
                {
                    foreach (var assembly in project.Assemblies)
                    {
                        var assemblyElement = ExecuteAssembly(assembly);
                        if (assemblyElement != null)
                        {
                            assembliesElement.Add(assemblyElement);
                        }
                    }
                }

                clockTime.Stop();

                if (assembliesElement != null)
                {
                    assembliesElement.Add(new XAttribute("timestamp", DateTime.Now.ToString(CultureInfo.InvariantCulture)));
                }

                if (completionMessages.Count > 0)
                {
                    reporterMessageHandler.OnMessage(new TestExecutionSummary(clockTime.Elapsed, completionMessages.OrderBy(kvp => kvp.Key).ToList()));
                }
            }

            Directory.SetCurrentDirectory(WorkingFolder ?? originalWorkingFolder);

            if (NeedsXml)
            {
                if (Xml != null)
                {
                    assembliesElement.Save(Xml.GetMetadata("FullPath"));
                }

                if (XmlV1 != null)
                {
                    Transform("xUnit1.xslt", assembliesElement, XmlV1);
                }

                if (Html != null)
                {
                    Transform("HTML.xslt", assembliesElement, Html);
                }

                if (NUnit != null)
                {
                    Transform("NUnitXml.xslt", assembliesElement, NUnit);
                }
            }

            // ExitCode is set to 1 for test failures and -1 for Exceptions.
            return(ExitCode == 0 || (ExitCode == 1 && IgnoreFailures));
        }
Esempio n. 4
0
File: xunit.cs Progetto: redoz/xunit
        public override bool Execute()
        {
            RemotingUtility.CleanUpRegisteredChannels();
            XElement assembliesElement = null;
            var      environment       = String.Format("{0}-bit .NET {1}", IntPtr.Size * 8, Environment.Version);

            if (NeedsXml)
            {
                assembliesElement = new XElement("assemblies");
            }

            var originalWorkingFolder = Directory.GetCurrentDirectory();

            using (AssemblyHelper.SubscribeResolve())
            {
                Log.LogMessage(MessageImportance.High, "xUnit.net MSBuild runner ({0})", environment);

                var testAssemblyPaths = Assemblies.Select(assembly =>
                {
                    var assemblyFileName = assembly.GetMetadata("FullPath");
                    var configFileName   = assembly.GetMetadata("ConfigFile");
                    if (configFileName != null && configFileName.Length == 0)
                    {
                        configFileName = null;
                    }

                    return(Tuple.Create(assemblyFileName, configFileName, ConfigReader.Load(assemblyFileName, configFileName)));
                }).ToList();

                if (WorkingFolder != null)
                {
                    Directory.SetCurrentDirectory(WorkingFolder);
                }

                var clockTime = Stopwatch.StartNew();

                if (!parallelizeAssemblies.HasValue)
                {
                    parallelizeAssemblies = testAssemblyPaths.All(tuple => tuple.Item3.ParallelizeAssembly);
                }

                if (parallelizeAssemblies.GetValueOrDefault())
                {
                    var tasks   = testAssemblyPaths.Select(path => Task.Run(() => ExecuteAssembly(path.Item1, path.Item2, path.Item3)));
                    var results = Task.WhenAll(tasks).GetAwaiter().GetResult();
                    foreach (var assemblyElement in results.Where(result => result != null))
                    {
                        assembliesElement.Add(assemblyElement);
                    }
                }
                else
                {
                    foreach (var path in testAssemblyPaths)
                    {
                        var assemblyElement = ExecuteAssembly(path.Item1, path.Item2, path.Item3);
                        if (assemblyElement != null)
                        {
                            assembliesElement.Add(assemblyElement);
                        }
                    }
                }

                clockTime.Stop();

                if (completionMessages.Count > 0)
                {
                    Log.LogMessage(MessageImportance.High, "=== TEST EXECUTION SUMMARY ===");

                    var totalTestsRun       = completionMessages.Values.Sum(summary => summary.Total);
                    var totalTestsFailed    = completionMessages.Values.Sum(summary => summary.Failed);
                    var totalTestsSkipped   = completionMessages.Values.Sum(summary => summary.Skipped);
                    var totalTime           = completionMessages.Values.Sum(summary => summary.Time).ToString("0.000s");
                    var totalErrors         = completionMessages.Values.Sum(summary => summary.Errors);
                    var longestAssemblyName = completionMessages.Keys.Max(key => Path.GetFileNameWithoutExtension(key).Length);
                    var longestTotal        = totalTestsRun.ToString().Length;
                    var longestFailed       = totalTestsFailed.ToString().Length;
                    var longestSkipped      = totalTestsSkipped.ToString().Length;
                    var longestTime         = totalTime.Length;
                    var longestErrors       = totalErrors.ToString().Length;

                    foreach (var message in completionMessages.OrderBy(m => m.Key))
                    {
                        Log.LogMessage(MessageImportance.High,
                                       "   {0}  Total: {1}, Errors: {2}, Failed: {3}, Skipped: {4}, Time: {5}",
                                       Path.GetFileNameWithoutExtension(message.Key).PadRight(longestAssemblyName),
                                       message.Value.Total.ToString().PadLeft(longestTotal),
                                       message.Value.Errors.ToString().PadLeft(longestErrors),
                                       message.Value.Failed.ToString().PadLeft(longestFailed),
                                       message.Value.Skipped.ToString().PadLeft(longestSkipped),
                                       message.Value.Time.ToString("0.000s").PadLeft(longestTime));
                    }

                    if (completionMessages.Count > 1)
                    {
                        Log.LogMessage(MessageImportance.High,
                                       "   {0}         {1}          {2}          {3}           {4}        {5}" + Environment.NewLine +
                                       "           {6} {7}          {8}          {9}           {10}        {11} ({12})",
                                       " ".PadRight(longestAssemblyName),
                                       "-".PadRight(longestTotal, '-'),
                                       "-".PadRight(longestErrors, '-'),
                                       "-".PadRight(longestFailed, '-'),
                                       "-".PadRight(longestSkipped, '-'),
                                       "-".PadRight(longestTime, '-'),
                                       "GRAND TOTAL:".PadLeft(longestAssemblyName),
                                       totalTestsRun,
                                       totalErrors,
                                       totalTestsFailed,
                                       totalTestsSkipped,
                                       totalTime,
                                       clockTime.Elapsed.TotalSeconds.ToString("0.000s"));
                    }
                }
            }

            Directory.SetCurrentDirectory(WorkingFolder ?? originalWorkingFolder);

            if (NeedsXml)
            {
                if (Xml != null)
                {
                    assembliesElement.Save(Xml.GetMetadata("FullPath"));
                }

                if (XmlV1 != null)
                {
                    Transform("xUnit1.xslt", assembliesElement, XmlV1);
                }

                if (Html != null)
                {
                    Transform("HTML.xslt", assembliesElement, Html);
                }
            }

            return(ExitCode == 0);
        }
Esempio n. 5
0
        public override bool Execute()
        {
            RemotingUtility.CleanUpRegisteredChannels();

            if (TeamCity)
            {
                Log.LogError("The 'TeamCity' property is deprecated. Please set the 'Reporter' property to 'teamcity' instead.");
                return(false);
            }
            if (Verbose)
            {
                Log.LogError("The 'Verbose' property is deprecated. Please set the 'Reporter' property to 'verbose' instead.");
                return(false);
            }

            XElement assembliesElement = null;
            var      environment       = $"{IntPtr.Size * 8}-bit .NET {Environment.Version}";

            if (NeedsXml)
            {
                assembliesElement = new XElement("assemblies");
            }

            switch (MaxParallelThreads)
            {
            case null:
            case "default":
                break;

            case "unlimited":
                maxThreadCount = -1;
                break;

            default:
                int threadValue;
                if (!int.TryParse(MaxParallelThreads, out threadValue) || threadValue < 1)
                {
                    Log.LogError("MaxParallelThreads value '{0}' is invalid: must be 'default', 'unlimited', or a positive number", MaxParallelThreads);
                    return(false);
                }

                maxThreadCount = threadValue;
                break;
            }

            var originalWorkingFolder = Directory.GetCurrentDirectory();

            using (AssemblyHelper.SubscribeResolve())
            {
                var reporters = GetAvailableRunnerReporters();
                var reporter  = reporters.FirstOrDefault(r => r.IsEnvironmentallyEnabled);

                if (reporter == null && !string.IsNullOrWhiteSpace(Reporter))
                {
                    reporter = reporters.FirstOrDefault(r => string.Equals(r.RunnerSwitch, Reporter, StringComparison.OrdinalIgnoreCase));
                    if (reporter == null)
                    {
                        var switchableReporters = reporters.Where(r => !string.IsNullOrWhiteSpace(r.RunnerSwitch)).Select(r => r.RunnerSwitch.ToLowerInvariant()).OrderBy(x => x).ToList();
                        if (switchableReporters.Count == 0)
                        {
                            Log.LogError("Reporter value '{0}' is invalid. There are no available reporters.", Reporter);
                        }
                        else
                        {
                            Log.LogError("Reporter value '{0}' is invalid. Available reporters: {1}", Reporter, string.Join(", ", switchableReporters));
                        }

                        return(false);
                    }
                }

                if (reporter == null)
                {
                    reporter = new DefaultRunnerReporter();
                }

                logger = new MSBuildLogger(Log);
                reporterMessageHandler = reporter.CreateMessageHandler(logger);

                if (!NoLogo)
                {
                    Log.LogMessage(MessageImportance.High, "xUnit.net MSBuild Runner ({0})", environment);
                }

                var project = new XunitProject();
                foreach (var assembly in Assemblies)
                {
                    var assemblyFileName = assembly.GetMetadata("FullPath");
                    var configFileName   = assembly.GetMetadata("ConfigFile");
                    if (configFileName != null && configFileName.Length == 0)
                    {
                        configFileName = null;
                    }

                    var projectAssembly = new XunitProjectAssembly {
                        AssemblyFilename = assemblyFileName, ConfigFilename = configFileName
                    };
                    if (shadowCopy.HasValue)
                    {
                        projectAssembly.Configuration.ShadowCopy = shadowCopy;
                    }

                    project.Add(projectAssembly);
                }

                if (WorkingFolder != null)
                {
                    Directory.SetCurrentDirectory(WorkingFolder);
                }

                var clockTime = Stopwatch.StartNew();

                if (!parallelizeAssemblies.HasValue)
                {
                    parallelizeAssemblies = project.All(assembly => assembly.Configuration.ParallelizeAssemblyOrDefault);
                }

                if (parallelizeAssemblies.GetValueOrDefault())
                {
                    var tasks   = project.Assemblies.Select(assembly => Task.Run(() => ExecuteAssembly(assembly)));
                    var results = Task.WhenAll(tasks).GetAwaiter().GetResult();
                    foreach (var assemblyElement in results.Where(result => result != null))
                    {
                        assembliesElement.Add(assemblyElement);
                    }
                }
                else
                {
                    foreach (var assembly in project.Assemblies)
                    {
                        var assemblyElement = ExecuteAssembly(assembly);
                        if (assemblyElement != null)
                        {
                            assembliesElement.Add(assemblyElement);
                        }
                    }
                }

                clockTime.Stop();

                if (completionMessages.Count > 0)
                {
                    reporterMessageHandler.OnMessage(new TestExecutionSummary(clockTime.Elapsed, completionMessages.OrderBy(kvp => kvp.Key).ToList()));
                }
            }

            Directory.SetCurrentDirectory(WorkingFolder ?? originalWorkingFolder);

            if (NeedsXml)
            {
                if (Xml != null)
                {
                    assembliesElement.Save(Xml.GetMetadata("FullPath"));
                }

                if (XmlV1 != null)
                {
                    Transform("xUnit1.xslt", assembliesElement, XmlV1);
                }

                if (Html != null)
                {
                    Transform("HTML.xslt", assembliesElement, Html);
                }

                if (NUnit != null)
                {
                    Transform("NUnitXml.xslt", assembliesElement, NUnit);
                }
            }

            return(ExitCode == 0);
        }
Esempio n. 6
0
        public override bool Execute()
        {
            RemotingUtility.CleanUpRegisteredChannels();

            XElement assembliesElement = null;
            var      environment       = $"{IntPtr.Size * 8}-bit {CrossPlatform.Version}";

            if (NeedsXml)
            {
                assembliesElement = new XElement("assemblies");
            }

            var appDomains = default(AppDomainSupport?);

            switch (AppDomains?.ToLowerInvariant())    // Using ToLowerInvariant() here for back compat for when this was a boolean
            {
            case null:
                break;

            case "ifavailable":
                appDomains = AppDomainSupport.IfAvailable;
                break;

            case "true":
            case "required":
                appDomains = AppDomainSupport.Required;
                break;

            case "false":
            case "denied":
                appDomains = AppDomainSupport.Denied;
                break;

            default:
                Log.LogError("AppDomains value '{0}' is invalid: must be 'ifavailable', 'required', or 'denied'", AppDomains);
                return(false);
            }

            switch (MaxParallelThreads)
            {
            case null:
            case "default":
                break;

            case "unlimited":
                maxThreadCount = -1;
                break;

            default:
                int threadValue;
                if (!int.TryParse(MaxParallelThreads, out threadValue) || threadValue < 1)
                {
                    Log.LogError("MaxParallelThreads value '{0}' is invalid: must be 'default', 'unlimited', or a positive number", MaxParallelThreads);
                    return(false);
                }

                maxThreadCount = threadValue;
                break;
            }

            var originalWorkingFolder          = Directory.GetCurrentDirectory();
            var internalDiagnosticsMessageSink = DiagnosticMessageSink.ForInternalDiagnostics(Log, InternalDiagnosticMessages);

            using (AssemblyHelper.SubscribeResolveForAssembly(typeof(xunit), internalDiagnosticsMessageSink))
            {
                var reporter = GetReporter();
                if (reporter == null)
                {
                    return(false);
                }

                logger = new MSBuildLogger(Log);
                reporterMessageHandler = MessageSinkWithTypesAdapter.Wrap(reporter.CreateMessageHandler(logger));

                if (!NoLogo)
                {
                    var versionAttribute = typeof(xunit).GetTypeInfo().Assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>();
                    Log.LogMessage(MessageImportance.High, $"xUnit.net MSBuild Runner v{versionAttribute.InformationalVersion} ({environment})");
                }

                var project = new XunitProject();
                foreach (var assembly in Assemblies)
                {
                    var assemblyFileName = assembly.GetMetadata("FullPath");
                    var configFileName   = assembly.GetMetadata("ConfigFile");
                    if (configFileName != null && configFileName.Length == 0)
                    {
                        configFileName = null;
                    }

                    var projectAssembly = new XunitProjectAssembly {
                        AssemblyFilename = assemblyFileName, ConfigFilename = configFileName
                    };
                    if (shadowCopy.HasValue)
                    {
                        projectAssembly.Configuration.ShadowCopy = shadowCopy;
                    }

                    project.Add(projectAssembly);
                }

                if (WorkingFolder != null)
                {
                    Directory.SetCurrentDirectory(WorkingFolder);
                }

                var clockTime = Stopwatch.StartNew();

                if (!parallelizeAssemblies.HasValue)
                {
                    parallelizeAssemblies = project.All(assembly => assembly.Configuration.ParallelizeAssemblyOrDefault);
                }

                if (parallelizeAssemblies.GetValueOrDefault())
                {
                    var tasks   = project.Assemblies.Select(assembly => Task.Run(() => ExecuteAssembly(assembly, appDomains)));
                    var results = Task.WhenAll(tasks).GetAwaiter().GetResult();
                    foreach (var assemblyElement in results.Where(result => result != null))
                    {
                        assembliesElement.Add(assemblyElement);
                    }
                }
                else
                {
                    foreach (var assembly in project.Assemblies)
                    {
                        var assemblyElement = ExecuteAssembly(assembly, appDomains);
                        if (assemblyElement != null)
                        {
                            assembliesElement.Add(assemblyElement);
                        }
                    }
                }

                clockTime.Stop();

                if (assembliesElement != null)
                {
                    assembliesElement.Add(new XAttribute("timestamp", DateTime.Now.ToString(CultureInfo.InvariantCulture)));
                }

                if (completionMessages.Count > 0)
                {
                    reporterMessageHandler.OnMessage(new TestExecutionSummary(clockTime.Elapsed, completionMessages.OrderBy(kvp => kvp.Key).ToList()));
                }
            }

            Directory.SetCurrentDirectory(WorkingFolder ?? originalWorkingFolder);

            if (NeedsXml)
            {
                if (Xml != null)
                {
                    using (var xmlStream = new FileStream(Xml.GetMetadata("FullPath"), FileMode.OpenOrCreate, FileAccess.Write))
                        assembliesElement.Save(xmlStream);
                }

                if (XmlV1 != null)
                {
                    CrossPlatform.Transform(logger, "XmlV1", "xUnit1.xslt", assembliesElement, XmlV1);
                }

                if (Html != null)
                {
                    CrossPlatform.Transform(logger, "Html", "HTML.xslt", assembliesElement, Html);
                }

                if (NUnit != null)
                {
                    CrossPlatform.Transform(logger, "NUnit", "NUnitXml.xslt", assembliesElement, NUnit);
                }

                if (JUnit != null)
                {
                    CrossPlatform.Transform(logger, "JUnit", "JUnitXml.xslt", assembliesElement, JUnit);
                }
            }

            // ExitCode is set to 1 for test failures and -1 for Exceptions.
            return(ExitCode == 0 || (ExitCode == 1 && IgnoreFailures));
        }
Esempio n. 7
0
        public override bool Execute()
        {
            RemotingUtility.CleanUpRegisteredChannels();

            try
            {
                ExitCode = 0;

                string        projectFilename = ProjectFile.GetMetadata("FullPath");
                XunitProject  project         = XunitProject.Load(projectFilename);
                IRunnerLogger logger          =
                    TeamCity ? (IRunnerLogger) new TeamCityLogger(Log) :
                    Verbose ? new VerboseLogger(Log) :
                    new StandardLogger(Log);

                Log.LogMessage(MessageImportance.High, "xUnit.net MSBuild runner ({0}-bit .NET {1})", IntPtr.Size * 8, Environment.Version);

                foreach (XunitProjectAssembly assembly in project.Assemblies)
                {
                    using (Stream htmlStream = ResourceStream("HTML.xslt"))
                        using (Stream nunitStream = ResourceStream("NUnitXml.xslt"))
                            using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.AssemblyFilename, assembly.ConfigFilename, assembly.ShadowCopy))
                            {
                                Log.LogMessage(MessageImportance.High, "  Test assembly: {0}", assembly.AssemblyFilename);
                                Log.LogMessage(MessageImportance.High, "  xunit.dll version: {0}", wrapper.XunitVersion);

                                List <IResultXmlTransform> transforms = new List <IResultXmlTransform>();

                                foreach (KeyValuePair <string, string> kvp in assembly.Output)
                                {
                                    switch (kvp.Key.ToLowerInvariant())
                                    {
                                    case "xml":
                                        transforms.Add(new NullTransformer(kvp.Value));
                                        break;

                                    case "html":
                                        transforms.Add(new XslStreamTransformer(htmlStream, kvp.Value));
                                        break;

                                    case "nunit":
                                        transforms.Add(new XslStreamTransformer(nunitStream, kvp.Value));
                                        break;

                                    default:
                                        Log.LogWarning("Unknown output type: '{0}'", kvp.Key);
                                        break;
                                    }
                                }

                                TestRunner runner = new TestRunner(wrapper, logger);
                                if (runner.RunAssembly(transforms) == TestRunnerResult.Failed)
                                {
                                    ExitCode = -1;
                                }
                            }
                }

                StandardLogger stdLogger = logger as StandardLogger;
                if (stdLogger != null)
                {
                    Log.LogMessage(MessageImportance.High,
                                   "TOTAL Tests: {0}, Failures: {1}, Skipped: {2}, Time: {3} seconds",
                                   stdLogger.Total,
                                   stdLogger.Failed,
                                   stdLogger.Skipped,
                                   stdLogger.Time.ToString("0.000"));
                }
            }
            catch (Exception ex)
            {
                Exception e = ex;

                while (e != null)
                {
                    Log.LogError(e.GetType().FullName + ": " + e.Message);

                    foreach (string stackLine in e.StackTrace.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        Log.LogError(stackLine);
                    }

                    e = e.InnerException;
                }

                ExitCode = -1;
            }

            return(ExitCode == 0);
        }
Esempio n. 8
0
        public override bool Execute()
        {
            RemotingUtility.CleanUpRegisteredChannels();

            if (!GuardParameters())
            {
                ExitCode = -2;
                return(false);
            }

            StringBuilder assembliesNode = new StringBuilder();

            assembliesNode.Append("<assemblies>");

            if (WorkingFolder != null)
            {
                Directory.SetCurrentDirectory(WorkingFolder);
            }

            ExitCode = 0;
            Log.LogMessage(MessageImportance.High, "xUnit.net MSBuild runner ({0}-bit .NET {1})", IntPtr.Size * 8, Environment.Version);

            IRunnerLogger logger =
                TeamCity ? (IRunnerLogger) new TeamCityLogger(Log) :
                Verbose ? new VerboseLogger(Log) :
                new StandardLogger(Log);

            if (Assembly != null)
            {
                string assemblyFilename = Assembly.GetMetadata("FullPath");
                assembliesNode.Append(ExecuteAssembly(assemblyFilename, ConfigFile, logger));
            }
            else
            {
                foreach (ITaskItem assembly in Assemblies)
                {
                    string assemblyFilename = assembly.GetMetadata("FullPath");
                    string configFilename   = assembly.GetMetadata("ConfigFile");
                    if (configFilename.Length == 0)
                    {
                        configFilename = null;
                    }

                    assembliesNode.Append(ExecuteAssembly(assemblyFilename, configFilename, logger));
                }
            }

            assembliesNode.Append("</assemblies>");

            string fullXml = assembliesNode.ToString();

            if (Xml != null)
            {
                new NullTransformer(Xml.GetMetadata("FullPath")).Transform(fullXml);
            }

            if (Html != null)
            {
                using (Stream htmlStream = ResourceStream("HTML.xslt"))
                    new XslStreamTransformer(htmlStream, Html.GetMetadata("FullPath")).Transform(fullXml);
            }

            return(ExitCode == 0);
        }