|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.IO; |
| 8 | + using System.Xml; |
| 9 | + |
| 10 | + using Microsoft.VisualStudio.TestPlatform.ObjectModel; |
| 11 | + using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; |
| 12 | + using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; |
| 13 | + using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; |
| 14 | + |
| 15 | + public class RunConfigurationSettings |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// The settings name. |
| 19 | + /// </summary> |
| 20 | + public const string SettingsName = "RunConfiguration"; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Member variable for RunConfiguration settings |
| 24 | + /// </summary> |
| 25 | + private static RunConfigurationSettings configurationSettings; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Initializes a new instance of the <see cref="RunConfigurationSettings"/> class. |
| 29 | + /// </summary> |
| 30 | + public RunConfigurationSettings() |
| 31 | + { |
| 32 | + this.DesignMode = true; |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets the current settings. |
| 37 | + /// </summary> |
| 38 | + public static RunConfigurationSettings ConfigurationSettings |
| 39 | + { |
| 40 | + get |
| 41 | + { |
| 42 | + if (configurationSettings == null) |
| 43 | + { |
| 44 | + configurationSettings = new RunConfigurationSettings(); |
| 45 | + } |
| 46 | + |
| 47 | + return configurationSettings; |
| 48 | + } |
| 49 | + |
| 50 | + private set |
| 51 | + { |
| 52 | + configurationSettings = value; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Gets a value indicating whether designMode is on(IDE scenario) or off(CLI scenario). |
| 58 | + /// </summary> |
| 59 | + public bool DesignMode { get; private set; } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Populate adapter settings from the context |
| 63 | + /// </summary> |
| 64 | + /// <param name="context"> |
| 65 | + /// The discovery context that contains the runsettings. |
| 66 | + /// </param> |
| 67 | + public static void PopulateSettings(IDiscoveryContext context) |
| 68 | + { |
| 69 | + if (context == null || context.RunSettings == null || string.IsNullOrEmpty(context.RunSettings.SettingsXml)) |
| 70 | + { |
| 71 | + // This will contain default adapter settings |
| 72 | + ConfigurationSettings = new RunConfigurationSettings(); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + var settings = GetSettings(context.RunSettings.SettingsXml, SettingsName); |
| 77 | + |
| 78 | + if (settings != null) |
| 79 | + { |
| 80 | + ConfigurationSettings = settings; |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + ConfigurationSettings = new RunConfigurationSettings(); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Gets the adapter specific settings from the xml. |
| 89 | + /// </summary> |
| 90 | + /// <param name="runsettingsXml"> The xml with the settings passed from the test platform. </param> |
| 91 | + /// <param name="settingName"> The name of the adapter settings to fetch - Its either MSTest or MSTestV2 </param> |
| 92 | + /// <returns> The settings if found. Null otherwise. </returns> |
| 93 | + internal static RunConfigurationSettings GetSettings(string runsettingsXml, string settingName) |
| 94 | + { |
| 95 | + using (var stringReader = new StringReader(runsettingsXml)) |
| 96 | + { |
| 97 | + XmlReader reader = XmlReader.Create(stringReader, XmlRunSettingsUtilities.ReaderSettings); |
| 98 | + |
| 99 | + // read to the fist child |
| 100 | + XmlReaderUtilities.ReadToRootNode(reader); |
| 101 | + reader.ReadToNextElement(); |
| 102 | + |
| 103 | + // Read till we reach nodeName element or reach EOF |
| 104 | + while (!string.Equals(reader.Name, settingName, StringComparison.OrdinalIgnoreCase) |
| 105 | + && |
| 106 | + !reader.EOF) |
| 107 | + { |
| 108 | + reader.SkipToNextElement(); |
| 109 | + } |
| 110 | + |
| 111 | + if (!reader.EOF) |
| 112 | + { |
| 113 | + // read nodeName element. |
| 114 | + return ToSettings(reader.ReadSubtree()); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Resets any settings loaded. |
| 123 | + /// </summary> |
| 124 | + internal static void Reset() |
| 125 | + { |
| 126 | + RunConfigurationSettings.ConfigurationSettings = null; |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Convert the parameter xml to TestSettings |
| 131 | + /// </summary> |
| 132 | + /// <param name="reader">Reader to load the settings from.</param> |
| 133 | + /// <returns>An instance of the <see cref="MSTestSettings"/> class</returns> |
| 134 | + private static RunConfigurationSettings ToSettings(XmlReader reader) |
| 135 | + { |
| 136 | + ValidateArg.NotNull<XmlReader>(reader, "reader"); |
| 137 | + |
| 138 | + // Expected format of the xml is: - |
| 139 | + // |
| 140 | + // <Runsettings> |
| 141 | + // <RunConfiguration> |
| 142 | + // <DesignMode>true</DesignMode> |
| 143 | + // </RunConfiguration> |
| 144 | + // </Runsettings> |
| 145 | + RunConfigurationSettings settings = new RunConfigurationSettings(); |
| 146 | + |
| 147 | + // Read the first element in the section which is either "MSTest"/"MSTestV2" |
| 148 | + reader.ReadToNextElement(); |
| 149 | + |
| 150 | + if (!reader.IsEmptyElement) |
| 151 | + { |
| 152 | + reader.Read(); |
| 153 | + |
| 154 | + while (reader.NodeType == XmlNodeType.Element) |
| 155 | + { |
| 156 | + bool result; |
| 157 | + string elementName = reader.Name.ToUpperInvariant(); |
| 158 | + switch (elementName) |
| 159 | + { |
| 160 | + case "DESIGNMODE": |
| 161 | + { |
| 162 | + if (bool.TryParse(reader.ReadInnerXml(), out result)) |
| 163 | + { |
| 164 | + settings.DesignMode = result; |
| 165 | + PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo( |
| 166 | + "DesignMode value Found : {0} ", |
| 167 | + result); |
| 168 | + } |
| 169 | + |
| 170 | + break; |
| 171 | + } |
| 172 | + |
| 173 | + default: |
| 174 | + { |
| 175 | + reader.SkipToNextElement(); |
| 176 | + break; |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return settings; |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments