private bool UpdateRunSettingsIfRequired(string runsettingsXml, List <string> sources, out string updatedRunSettingsXml)
        {
            bool settingsUpdated = false;

            updatedRunSettingsXml = runsettingsXml;
            IDictionary <string, Architecture> sourcePlatforms  = new Dictionary <string, Architecture>();
            IDictionary <string, Framework>    sourceFrameworks = new Dictionary <string, Framework>();

            if (!string.IsNullOrEmpty(runsettingsXml))
            {
                // TargetFramework is full CLR. Set DesignMode based on current context.
                using (var stream = new StringReader(runsettingsXml))
                    using (var reader = XmlReader.Create(stream, XmlRunSettingsUtilities.ReaderSettings))
                    {
                        var document = new XmlDocument();
                        document.Load(reader);
                        var navigator         = document.CreateNavigator();
                        var runConfiguration  = XmlRunSettingsUtilities.GetRunConfigurationNode(runsettingsXml);
                        var loggerRunSettings = XmlRunSettingsUtilities.GetLoggerRunSettings(runsettingsXml) ?? new LoggerRunSettings();

                        settingsUpdated |= this.UpdateFramework(document, navigator, sources, sourceFrameworks, out Framework chosenFramework);
                        settingsUpdated |= this.UpdatePlatform(document, navigator, sources, sourcePlatforms, out Architecture chosenPlatform);
                        this.CheckSourcesForCompatibility(chosenFramework, chosenPlatform, sourcePlatforms, sourceFrameworks);
                        settingsUpdated |= this.UpdateDesignMode(document, runConfiguration);
                        settingsUpdated |= this.UpdateCollectSourceInformation(document, runConfiguration);
                        settingsUpdated |= this.UpdateTargetDevice(navigator, document, runConfiguration);
                        settingsUpdated |= this.AddOrUpdateConsoleLogger(document, runConfiguration, loggerRunSettings);

                        updatedRunSettingsXml = navigator.OuterXml;
                    }
            }

            return(settingsUpdated);
        }
Beispiel #2
0
        private bool UpdateRunSettingsIfRequired(string runsettingsXml, List <string> sources, IBaseTestEventsRegistrar registrar, out string updatedRunSettingsXml)
        {
            bool settingsUpdated = false;

            updatedRunSettingsXml = runsettingsXml;
            IDictionary <string, Architecture> sourcePlatforms  = new Dictionary <string, Architecture>();
            IDictionary <string, Framework>    sourceFrameworks = new Dictionary <string, Framework>();

            if (!string.IsNullOrEmpty(runsettingsXml))
            {
                // TargetFramework is full CLR. Set DesignMode based on current context.
                using (var stream = new StringReader(runsettingsXml))
                    using (var reader = XmlReader.Create(stream, XmlRunSettingsUtilities.ReaderSettings))
                    {
                        var document = new XmlDocument();
                        document.Load(reader);
                        var navigator         = document.CreateNavigator();
                        var runConfiguration  = XmlRunSettingsUtilities.GetRunConfigurationNode(runsettingsXml);
                        var loggerRunSettings = XmlRunSettingsUtilities.GetLoggerRunSettings(runsettingsXml) ?? new LoggerRunSettings();

                        settingsUpdated |= this.UpdateFramework(document, navigator, sources, sourceFrameworks, registrar, out Framework chosenFramework);

                        // Choose default architecture based on the framework
                        // For .NET core, the default platform architecture should be based on the process.
                        Architecture defaultArchitecture = Architecture.X86;
                        if (chosenFramework.Name.IndexOf("netstandard", StringComparison.OrdinalIgnoreCase) >= 0 ||
                            chosenFramework.Name.IndexOf("netcoreapp", StringComparison.OrdinalIgnoreCase) >= 0 ||
                            chosenFramework.Name.IndexOf("net5", StringComparison.OrdinalIgnoreCase) >= 0)
                        {
#if NETCOREAPP
                            // We are running in vstest.console that is either started via dotnet.exe or via vstest.console.exe .NET Core
                            // executable. For AnyCPU dlls this should resolve 32-bit SDK when running from 32-bit dotnet process and
                            // 64-bit SDK when running from 64-bit dotnet process.
                            defaultArchitecture = Environment.Is64BitProcess ? Architecture.X64 : Architecture.X86;
#else
                            // We are running in vstest.console.exe that was built against .NET Framework. This console prefers 32-bit
                            // because it needs to run as 32-bit to be compatible with QTAgent. It runs as 32-bit both under VS and
                            // in Developer console. Set the default architecture based on the OS architecture, to find 64-bit dotnet SDK
                            // when running AnyCPU dll on 64-bit system, and 32-bit SDK when running AnyCPU dll on 32-bit OS.
                            // We want to find 64-bit SDK because it is more likely to be installed.
                            defaultArchitecture = Environment.Is64BitOperatingSystem ? Architecture.X64 : Architecture.X86;
#endif
                        }

                        settingsUpdated |= this.UpdatePlatform(document, navigator, sources, sourcePlatforms, defaultArchitecture, out Architecture chosenPlatform);
                        this.CheckSourcesForCompatibility(chosenFramework, chosenPlatform, defaultArchitecture, sourcePlatforms, sourceFrameworks, registrar);
                        settingsUpdated |= this.UpdateDesignMode(document, runConfiguration);
                        settingsUpdated |= this.UpdateCollectSourceInformation(document, runConfiguration);
                        settingsUpdated |= this.UpdateTargetDevice(navigator, document, runConfiguration);
                        settingsUpdated |= this.AddOrUpdateConsoleLogger(document, runConfiguration, loggerRunSettings);

                        updatedRunSettingsXml = navigator.OuterXml;
                    }
            }

            return(settingsUpdated);
        }
Beispiel #3
0
        /// <summary>
        /// Add console logger in runsettings.
        /// </summary>
        /// <param name="document">Runsettings document.</param>
        /// <param name="runsettingsXml">Runsettings xml.</param>
        private void AddConsoleLogger(XmlDocument document, string runsettingsXml)
        {
            var consoleLogger = new LoggerSettings
            {
                FriendlyName          = ConsoleLogger.FriendlyName,
                Uri                   = new Uri(ConsoleLogger.ExtensionUri),
                AssemblyQualifiedName = typeof(ConsoleLogger).AssemblyQualifiedName,
                CodeBase              = typeof(ConsoleLogger).GetTypeInfo().Assembly.Location,
                IsEnabled             = true
            };

            var loggerRunSettings = XmlRunSettingsUtilities.GetLoggerRunSettings(runsettingsXml) ?? new LoggerRunSettings();

            loggerRunSettings.LoggerSettingsList.Add(consoleLogger);
            RunSettingsProviderExtensions.UpdateRunSettingsXmlDocumentInnerXml(document, Constants.LoggerRunSettingsName, loggerRunSettings.ToXml().InnerXml);
        }
        private bool UpdateRunSettingsIfRequired(string runsettingsXml, List <string> sources, IBaseTestEventsRegistrar registrar, out string updatedRunSettingsXml)
        {
            bool settingsUpdated = false;

            updatedRunSettingsXml = runsettingsXml;
            IDictionary <string, Architecture> sourcePlatforms  = new Dictionary <string, Architecture>();
            IDictionary <string, Framework>    sourceFrameworks = new Dictionary <string, Framework>();

            if (!string.IsNullOrEmpty(runsettingsXml))
            {
                // TargetFramework is full CLR. Set DesignMode based on current context.
                using (var stream = new StringReader(runsettingsXml))
                    using (var reader = XmlReader.Create(stream, XmlRunSettingsUtilities.ReaderSettings))
                    {
                        var document = new XmlDocument();
                        document.Load(reader);
                        var navigator         = document.CreateNavigator();
                        var runConfiguration  = XmlRunSettingsUtilities.GetRunConfigurationNode(runsettingsXml);
                        var loggerRunSettings = XmlRunSettingsUtilities.GetLoggerRunSettings(runsettingsXml) ?? new LoggerRunSettings();

                        settingsUpdated |= this.UpdateFramework(document, navigator, sources, sourceFrameworks, registrar, out Framework chosenFramework);

                        // Choose default architecture based on the framework
                        // For .NET core, the default platform architecture should be based on the process.
                        // For a 64 bit process,
                        Architecture defaultArchitecture = Architecture.X86;
                        if (chosenFramework.Name.IndexOf("netstandard", StringComparison.OrdinalIgnoreCase) >= 0 ||
                            chosenFramework.Name.IndexOf("netcoreapp", StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            var currentProcessName = this.processHelper.GetProcessName(this.processHelper.GetCurrentProcessId());
                            defaultArchitecture = (currentProcessName.StartsWith("dotnet", StringComparison.OrdinalIgnoreCase) && !Environment.Is64BitProcess) ? Architecture.X86: Architecture.X64;
                        }

                        settingsUpdated |= this.UpdatePlatform(document, navigator, sources, sourcePlatforms, defaultArchitecture, out Architecture chosenPlatform);
                        this.CheckSourcesForCompatibility(chosenFramework, chosenPlatform, sourcePlatforms, sourceFrameworks, registrar);
                        settingsUpdated |= this.UpdateDesignMode(document, runConfiguration);
                        settingsUpdated |= this.UpdateCollectSourceInformation(document, runConfiguration);
                        settingsUpdated |= this.UpdateTargetDevice(navigator, document, runConfiguration);
                        settingsUpdated |= this.AddOrUpdateConsoleLogger(document, runConfiguration, loggerRunSettings);

                        updatedRunSettingsXml = navigator.OuterXml;
                    }
            }

            return(settingsUpdated);
        }
Beispiel #5
0
        /// <summary>
        /// Add console logger in runsettings if exists.
        /// </summary>
        /// <param name="document">Runsettings document.</param>
        /// <param name="runsettingsXml">Runsettings xml.</param>
        /// <returns>True if updated console logger in runsettings successfully.</returns>
        private bool UpdateConsoleLoggerIfExists(XmlDocument document, string runsettingsXml)
        {
            var defaultConsoleLogger = new LoggerSettings
            {
                FriendlyName = ConsoleLogger.FriendlyName,
                Uri          = new Uri(ConsoleLogger.ExtensionUri)
            };

            var loggerRunSettings   = XmlRunSettingsUtilities.GetLoggerRunSettings(runsettingsXml) ?? new LoggerRunSettings();
            var existingLoggerIndex = loggerRunSettings.GetExistingLoggerIndex(defaultConsoleLogger);

            // Update assemblyQualifiedName and codeBase of existing logger.
            if (existingLoggerIndex >= 0)
            {
                var consoleLogger = loggerRunSettings.LoggerSettingsList[existingLoggerIndex];
                consoleLogger.AssemblyQualifiedName = typeof(ConsoleLogger).AssemblyQualifiedName;
                consoleLogger.CodeBase = typeof(ConsoleLogger).GetTypeInfo().Assembly.Location;
                RunSettingsProviderExtensions.UpdateRunSettingsXmlDocumentInnerXml(document, Constants.LoggerRunSettingsName, loggerRunSettings.ToXml().InnerXml);
                return(true);
            }

            return(false);
        }
Beispiel #6
0
        /// <summary>
        /// Initializes all the loggers passed by user
        /// </summary>
        public void Initialize(string runSettings)
        {
            // Enable logger events
            EnableLogging();

            // Store test run directory. This runsettings is the final runsettings merging CLI args and runsettings.
            this.testRunDirectory = GetResultsDirectory(runSettings);
            this.targetFramework  = GetTargetFramework(runSettings)?.Name;

            var loggers = XmlRunSettingsUtilities.GetLoggerRunSettings(runSettings);

            foreach (var logger in loggers?.LoggerSettingsList ?? Enumerable.Empty <LoggerSettings>())
            {
                // Dont add logger if its disabled.
                if (!logger.IsEnabled)
                {
                    continue;
                }

                var parameters        = GetParametersFromConfigurationElement(logger.Configuration);
                var loggerInitialized = false;

                // Try initializing logger by type.
                if (!string.IsNullOrWhiteSpace(logger.AssemblyQualifiedName))
                {
                    loggerInitialized = InitializeLoggerByType(logger.AssemblyQualifiedName, logger.CodeBase, parameters);
                }

                // Try initializing logger by uri.
                if (!loggerInitialized &&
                    !string.IsNullOrWhiteSpace(logger.Uri?.ToString()))
                {
                    loggerInitialized = InitializeLoggerByUri(logger.Uri, parameters);
                }

                // Try initializing logger by friendly name.
                if (!loggerInitialized &&
                    TryGetUriFromFriendlyName(logger.FriendlyName, out var loggerUri) &&
                    loggerUri != null)
                {
                    loggerInitialized = InitializeLoggerByUri(loggerUri, parameters);
                }

                // Output error if logger is not initialized.
                if (!loggerInitialized)
                {
                    var value = !string.IsNullOrWhiteSpace(logger.AssemblyQualifiedName)
                        ? logger.AssemblyQualifiedName
                        : !string.IsNullOrWhiteSpace(logger.Uri?.ToString())
                            ? logger.Uri.ToString()
                            : logger.FriendlyName;

                    throw new InvalidLoggerException(
                              string.Format(
                                  CultureInfo.CurrentUICulture,
                                  CommonResources.LoggerNotFound,
                                  value));
                }
            }

            requestData.MetricsCollection.Add(TelemetryDataConstants.LoggerUsed, string.Join <Type>(",", this.initializedLoggers.ToArray()));
        }
Beispiel #7
0
        /// <summary>
        /// Add logger to runsettings.
        /// </summary>
        /// <param name="loggerArgument"></param>
        /// <param name="runSettingsManager"></param>
        public static void AddLoggerToRunSettings(string loggerArgument, IRunSettingsProvider runSettingsManager)
        {
            if (string.IsNullOrWhiteSpace(loggerArgument))
            {
                HandleInvalidArgument(loggerArgument);
            }

            var settings = runSettingsManager.ActiveRunSettings?.SettingsXml;

            if (settings == null)
            {
                runSettingsManager.AddDefaultRunSettings();
                settings = runSettingsManager.ActiveRunSettings?.SettingsXml;
            }

            var    loggerRunSettings = XmlRunSettingsUtilities.GetLoggerRunSettings(settings) ?? new LoggerRunSettings();
            string loggerIdentifier  = null;
            Dictionary <string, string> parameters = null;
            var parseSucceeded = LoggerUtilities.TryParseLoggerArgument(loggerArgument, out loggerIdentifier, out parameters);

            if (parseSucceeded)
            {
                var logger = default(LoggerSettings);

                try
                {
                    // Logger as uri in command line.
                    var loggerUri = new Uri(loggerIdentifier);
                    logger = new LoggerSettings
                    {
                        Uri       = loggerUri,
                        IsEnabled = true
                    };
                }
                catch (UriFormatException)
                {
                    // Logger as friendlyName in command line.
                    logger = new LoggerSettings
                    {
                        FriendlyName = loggerIdentifier,
                        IsEnabled    = true
                    };
                }

                // Converting logger console params to Configuration element
                if (parameters != null && parameters.Count > 0)
                {
                    var XmlDocument = new XmlDocument();
                    var outerNode   = XmlDocument.CreateElement("Configuration");
                    foreach (KeyValuePair <string, string> entry in parameters)
                    {
                        var node = XmlDocument.CreateElement(entry.Key);
                        node.InnerText = entry.Value;
                        outerNode.AppendChild(node);
                    }

                    logger.Configuration = outerNode;
                }

                // Remove existing logger.
                var existingLoggerIndex = loggerRunSettings.GetExistingLoggerIndex(logger);
                if (existingLoggerIndex >= 0)
                {
                    loggerRunSettings.LoggerSettingsList.RemoveAt(existingLoggerIndex);
                }

                loggerRunSettings.LoggerSettingsList.Add(logger);
            }
            else
            {
                HandleInvalidArgument(loggerArgument);
            }

            runSettingsManager.UpdateRunSettingsNodeInnerXml(Constants.LoggerRunSettingsName, loggerRunSettings.ToXml().InnerXml);
        }