private bool UpdateRunSettingsIfRequired()

in src/vstest.console/TestPlatformHelpers/TestRequestManager.cs [564:679]


    private bool UpdateRunSettingsIfRequired(
        string runsettingsXml,
        IList<string> sources,
        IBaseTestEventsRegistrar registrar,
        out string updatedRunSettingsXml)
    {
        bool settingsUpdated = false;
        updatedRunSettingsXml = runsettingsXml;
        var sourcePlatforms = new Dictionary<string, Architecture>();
        var 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 |= 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
                // This is a special case for 1 version of Nuget.Frameworks that was shipped with using identifier NET5 instead of NETCoreApp5 for .NET 5.
                || 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.
                // As default architecture we specify the expected test host architecture,
                // it can be specified by user on the command line with --arch or through runsettings.
                // If it's not specified by user will be filled by current processor architecture;
                // should be the same as SDK.
                defaultArchitecture = RunSettingsHelper.Instance.IsDefaultTargetArchitecture ?
                    TranslateToArchitecture(_processHelper.GetCurrentProcessArchitecture()) :
                    runConfiguration.TargetPlatform;
#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
                EqtTrace.Verbose($"TestRequestManager.UpdateRunSettingsIfRequired: Default architecture: {defaultArchitecture} IsDefaultTargetArchitecture: {RunSettingsHelper.Instance.IsDefaultTargetArchitecture}, Current process architecture: {_processHelper.GetCurrentProcessArchitecture()}.");
            }

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

            updatedRunSettingsXml = navigator.OuterXml;
        }

        return settingsUpdated;

#if NETCOREAPP
        static Architecture TranslateToArchitecture(PlatformArchitecture targetArchitecture)
        {
            switch (targetArchitecture)
            {
                case PlatformArchitecture.X86:
                    return Architecture.X86;
                case PlatformArchitecture.X64:
                    return Architecture.X64;
                case PlatformArchitecture.ARM:
                    return Architecture.ARM;
                case PlatformArchitecture.ARM64:
                    return Architecture.ARM64;
                case PlatformArchitecture.S390x:
                    return Architecture.S390x;
                default:
                    EqtTrace.Error($"TestRequestManager.TranslateToArchitecture: Unhandled architecture '{targetArchitecture}'.");
                    break;
            }

            // We prefer to not throw in case of unhandled architecture but return Default,
            // it should be handled in a correct way by the callers.
            return Architecture.Default;
        }
#endif
    }