| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixTestTools |
| 4 | { |
| 5 | using System; |
| 6 | using System.Globalization; |
| 7 | using System.Linq; |
| 8 | using System.Management; |
| 9 | using System.Collections.Generic; |
| 10 | using System.Security.Principal; |
| 11 | using WixInternal.TestSupport.XunitExtensions; |
| 12 | |
| 13 | public class RuntimePrereqFeatureFactAttribute : RuntimeFactAttribute |
| 14 | { |
| 15 | public static HashSet<string> OptionalFeatures = new(StringComparer.OrdinalIgnoreCase); |
| 16 | public static HashSet<string> ServerFeatures = new(StringComparer.OrdinalIgnoreCase); |
| 17 | static RuntimePrereqFeatureFactAttribute() |
| 18 | { |
| 19 | AddFeaturesToSet(ServerFeatures, "Win32_ServerFeature"); |
| 20 | AddFeaturesToSet(OptionalFeatures, "Win32_OptionalFeature"); |
| 21 | } |
| 22 | |
| 23 | private static void AddFeaturesToSet(HashSet<string> featureSet, string featureSetName) |
| 24 | { |
| 25 | try |
| 26 | { |
| 27 | var objMC = new ManagementClass(featureSetName); |
| 28 | var objMOC = objMC?.GetInstances(); |
| 29 | if (objMOC is not null) |
| 30 | { |
| 31 | foreach (var objMO in objMOC) |
| 32 | { |
| 33 | string featureName = (string)objMO.Properties["Name"].Value; |
| 34 | if ((uint)objMO.Properties["InstallState"].Value == 1) |
| 35 | { |
| 36 | featureSet.Add(featureName); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | catch |
| 42 | { |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | public RuntimePrereqFeatureFactAttribute(params string[] prerequisiteFeatures) : base() |
| 47 | { |
| 48 | var missingRequirements = prerequisiteFeatures.Select(x => x).Where(x => !ServerFeatures.Contains(x) && !OptionalFeatures.Contains(x)); |
| 49 | |
| 50 | if (missingRequirements.Any()) |
| 51 | { |
| 52 | this.Skip = "This test is missing the following Feature pre-requisites: " + String.Join(", ", missingRequirements); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |