diff --git a/test/xUnit/Asserts/PriorityOrderer.cs b/test/xUnit/Asserts/PriorityOrderer.cs index 72eff865a9..1b9ea17937 100644 --- a/test/xUnit/Asserts/PriorityOrderer.cs +++ b/test/xUnit/Asserts/PriorityOrderer.cs @@ -19,8 +19,10 @@ namespace TestOrder.TestCaseOrdering { int priority = 0; - foreach (IAttributeInfo attr in testCase.TestMethod.Method.GetCustomAttributes((typeof(TestPriorityAttribute).AssemblyQualifiedName))) + foreach (IAttributeInfo attr in testCase.TestMethod.Method.GetCustomAttributes(typeof(TestPriorityAttribute).AssemblyQualifiedName)) + { priority = attr.GetNamedArgument("Priority"); + } GetOrCreate(sortedMethods, priority).Add(testCase); } @@ -29,15 +31,20 @@ namespace TestOrder.TestCaseOrdering { list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name)); foreach (TTestCase testCase in list) + { yield return testCase; + } } } - static TValue GetOrCreate(IDictionary dictionary, TKey key) where TValue : new() + private static TValue GetOrCreate(IDictionary dictionary, TKey key) where TValue : new() { TValue result; - if (dictionary.TryGetValue(key, out result)) return result; + if (dictionary.TryGetValue(key, out result)) + { + return result; + } result = new TValue(); dictionary[key] = result; diff --git a/test/xUnit/csharp/test_Binders.cs b/test/xUnit/csharp/test_Binders.cs index 45d4ed51ac..7918c65baf 100644 --- a/test/xUnit/csharp/test_Binders.cs +++ b/test/xUnit/csharp/test_Binders.cs @@ -1,8 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Xunit; + using System; using System.Management.Automation.Language; +using Xunit; namespace PSTests.Parallel { diff --git a/test/xUnit/csharp/test_CorePsPlatform.cs b/test/xUnit/csharp/test_CorePsPlatform.cs index daf688380f..0a58a804cc 100644 --- a/test/xUnit/csharp/test_CorePsPlatform.cs +++ b/test/xUnit/csharp/test_CorePsPlatform.cs @@ -1,10 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Xunit; + using System; -using System.IO; using System.Diagnostics; +using System.IO; using System.Management.Automation; +using Xunit; namespace PSTests.Parallel { diff --git a/test/xUnit/csharp/test_ExtensionMethods.cs b/test/xUnit/csharp/test_ExtensionMethods.cs index 08c6beb78b..1e29687759 100644 --- a/test/xUnit/csharp/test_ExtensionMethods.cs +++ b/test/xUnit/csharp/test_ExtensionMethods.cs @@ -1,8 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Xunit; + using System; using System.Management.Automation; +using Xunit; namespace PSTests.Parallel { diff --git a/test/xUnit/csharp/test_FileSystemProvider.cs b/test/xUnit/csharp/test_FileSystemProvider.cs index 6dfe9bf72f..e46171b678 100644 --- a/test/xUnit/csharp/test_FileSystemProvider.cs +++ b/test/xUnit/csharp/test_FileSystemProvider.cs @@ -1,6 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Xunit; + using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -13,23 +13,30 @@ using System.Management.Automation.Internal; using System.Management.Automation.Internal.Host; using System.Management.Automation.Provider; using System.Management.Automation.Runspaces; +using System.Reflection; using Microsoft.PowerShell; using Microsoft.PowerShell.Commands; -using System.Reflection; +using Xunit; namespace PSTests.Parallel { - public class FileSystemProviderTests: IDisposable + public class FileSystemProviderTests : IDisposable { private string testPath; private string testContent; + public FileSystemProviderTests() { testPath = Path.GetTempFileName(); testContent = "test content!"; - if(File.Exists(testPath)) File.Delete(testPath); - File.AppendAllText(testPath,testContent); + if (File.Exists(testPath)) + { + File.Delete(testPath); + } + + File.AppendAllText(testPath, testContent); } + void IDisposable.Dispose() { File.Delete(testPath); @@ -38,18 +45,19 @@ namespace PSTests.Parallel private ExecutionContext GetExecutionContext() { CultureInfo currentCulture = CultureInfo.CurrentCulture; - PSHost hostInterface = new DefaultHost(currentCulture,currentCulture); + PSHost hostInterface = new DefaultHost(currentCulture, currentCulture); InitialSessionState iss = InitialSessionState.CreateDefault2(); AutomationEngine engine = new AutomationEngine(hostInterface, iss); ExecutionContext executionContext = new ExecutionContext(engine, hostInterface, iss); return executionContext; } + private ProviderInfo GetProvider() { ExecutionContext executionContext = GetExecutionContext(); SessionStateInternal sessionState = new SessionStateInternal(executionContext); - SessionStateProviderEntry providerEntry = new SessionStateProviderEntry("FileSystem",typeof(FileSystemProvider), null); + SessionStateProviderEntry providerEntry = new SessionStateProviderEntry("FileSystem", typeof(FileSystemProvider), null); sessionState.AddSessionStateEntry(providerEntry); ProviderInfo matchingProvider = sessionState.ProviderList.ToList()[0]; @@ -59,7 +67,7 @@ namespace PSTests.Parallel [Fact] public void TestCreateJunctionFails() { - if(!Platform.IsWindows) + if (!Platform.IsWindows) { Assert.False(InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty)); } @@ -73,20 +81,20 @@ namespace PSTests.Parallel public void TestGetHelpMaml() { FileSystemProvider fileSystemProvider = new FileSystemProvider(); - Assert.Equal(fileSystemProvider.GetHelpMaml(String.Empty,String.Empty),String.Empty); - Assert.Equal(fileSystemProvider.GetHelpMaml("helpItemName",String.Empty),String.Empty); - Assert.Equal(fileSystemProvider.GetHelpMaml(String.Empty,"path"),String.Empty); + Assert.Equal(fileSystemProvider.GetHelpMaml(string.Empty, string.Empty), string.Empty); + Assert.Equal(fileSystemProvider.GetHelpMaml("helpItemName", string.Empty), string.Empty); + Assert.Equal(fileSystemProvider.GetHelpMaml(string.Empty, "path"), string.Empty); } [Fact] public void TestMode() { - Assert.Equal(FileSystemProvider.Mode(null),String.Empty); + Assert.Equal(FileSystemProvider.Mode(null), string.Empty); FileSystemInfo directoryObject = null; FileSystemInfo fileObject = null; FileSystemInfo executableObject = null; - if(!Platform.IsWindows) + if (!Platform.IsWindows) { directoryObject = new DirectoryInfo(@"/"); fileObject = new FileInfo(@"/etc/hosts"); @@ -99,9 +107,9 @@ namespace PSTests.Parallel executableObject = new FileInfo(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); } - Assert.Equal("d-----", FileSystemProvider.Mode(PSObject.AsPSObject(directoryObject)).Replace("r","-")); - Assert.Equal("------", FileSystemProvider.Mode(PSObject.AsPSObject(fileObject)).Replace("r","-").Replace("a","-")); - Assert.Equal("------", FileSystemProvider.Mode(PSObject.AsPSObject(executableObject)).Replace("r","-").Replace("a","-")); + Assert.Equal("d-----", FileSystemProvider.Mode(PSObject.AsPSObject(directoryObject)).Replace("r", "-")); + Assert.Equal("------", FileSystemProvider.Mode(PSObject.AsPSObject(fileObject)).Replace("r", "-").Replace("a", "-")); + Assert.Equal("------", FileSystemProvider.Mode(PSObject.AsPSObject(executableObject)).Replace("r", "-").Replace("a", "-")); } [Fact] @@ -111,15 +119,15 @@ namespace PSTests.Parallel ProviderInfo providerInfoToSet = GetProvider(); fileSystemProvider.SetProviderInformation(providerInfoToSet); fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext()); - PSObject pso=new PSObject(); - pso.AddOrSetProperty("IsReadOnly",false); + PSObject pso = new PSObject(); + pso.AddOrSetProperty("IsReadOnly", false); fileSystemProvider.SetProperty(testPath, pso); - fileSystemProvider.GetProperty(testPath, new Collection(){"IsReadOnly"}); + fileSystemProvider.GetProperty(testPath, new Collection(){ "IsReadOnly" }); FileInfo fileSystemObject1 = new FileInfo(testPath); - PSObject psobject1=PSObject.AsPSObject(fileSystemObject1); - foreach(PSPropertyInfo property in psobject1.Properties) + PSObject psobject1 = PSObject.AsPSObject(fileSystemObject1); + foreach (PSPropertyInfo property in psobject1.Properties) { - if(property.Name == "IsReadOnly") + if (property.Name == "IsReadOnly") { Assert.False((bool)property.Value); } @@ -133,12 +141,12 @@ namespace PSTests.Parallel ProviderInfo providerInfoToSet = GetProvider(); fileSystemProvider.SetProviderInformation(providerInfoToSet); fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext()); - fileSystemProvider.GetProperty(testPath, new Collection(){"Name"}); + fileSystemProvider.GetProperty(testPath, new Collection(){ "Name" }); FileInfo fileSystemObject1 = new FileInfo(testPath); - PSObject psobject1=PSObject.AsPSObject(fileSystemObject1); - foreach(PSPropertyInfo property in psobject1.Properties) + PSObject psobject1 = PSObject.AsPSObject(fileSystemObject1); + foreach (PSPropertyInfo property in psobject1.Properties) { - if(property.Name == "FullName") + if (property.Name == "FullName") { Assert.Equal(testPath, property.Value); } @@ -152,7 +160,7 @@ namespace PSTests.Parallel ProviderInfo providerInfoToSet = GetProvider(); fileSystemProvider.SetProviderInformation(providerInfoToSet); fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext()); - fileSystemProvider.ClearProperty(testPath, new Collection(){"Attributes"}); + fileSystemProvider.ClearProperty(testPath, new Collection(){ "Attributes" }); } [Fact] @@ -164,7 +172,7 @@ namespace PSTests.Parallel fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext()); IContentReader contentReader = fileSystemProvider.GetContentReader(testPath); - Assert.Equal(contentReader.Read(1)[0],testContent); + Assert.Equal(contentReader.Read(1)[0], testContent); contentReader.Close(); } @@ -177,9 +185,9 @@ namespace PSTests.Parallel fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext()); IContentWriter contentWriter = fileSystemProvider.GetContentWriter(testPath); - contentWriter.Write(new List(){"contentWriterTestContent"}); + contentWriter.Write(new List(){ "contentWriterTestContent" }); contentWriter.Close(); - Assert.Equal(File.ReadAllText(testPath), testContent+@"contentWriterTestContent"+ System.Environment.NewLine); + Assert.Equal(File.ReadAllText(testPath), testContent + @"contentWriterTestContent" + System.Environment.NewLine); } [Fact] diff --git a/test/xUnit/csharp/test_MshSnapinInfo.cs b/test/xUnit/csharp/test_MshSnapinInfo.cs index 8bf6788384..4fe65776d5 100644 --- a/test/xUnit/csharp/test_MshSnapinInfo.cs +++ b/test/xUnit/csharp/test_MshSnapinInfo.cs @@ -1,8 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Xunit; + using System; using System.Management.Automation; +using Xunit; namespace PSTests.Parallel { diff --git a/test/xUnit/csharp/test_PSConfiguration.cs b/test/xUnit/csharp/test_PSConfiguration.cs index af47feee60..eb140b0a3d 100644 --- a/test/xUnit/csharp/test_PSConfiguration.cs +++ b/test/xUnit/csharp/test_PSConfiguration.cs @@ -1,6 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Xunit; + using System; using System.IO; using System.Management.Automation; @@ -9,13 +9,14 @@ using System.Management.Automation.Internal; using System.Reflection; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using Xunit; namespace PSTests.Sequential { [TestCaseOrderer("TestOrder.TestCaseOrdering.PriorityOrderer", "powershell-tests")] public class PowerShellPolicyFixture : IDisposable { - private const string configFileName = "powershell.config.json"; + private const string ConfigFileName = "powershell.config.json"; private readonly string systemWideConfigFile; private readonly string currentUserConfigFile; @@ -43,14 +44,15 @@ namespace PSTests.Sequential Directory.CreateDirectory(currentUserConfigDirectory); } - systemWideConfigFile = Path.Combine(systemWideConfigDirectory, configFileName); - currentUserConfigFile = Path.Combine(currentUserConfigDirectory, configFileName); + systemWideConfigFile = Path.Combine(systemWideConfigDirectory, ConfigFileName); + currentUserConfigFile = Path.Combine(currentUserConfigDirectory, ConfigFileName); if (File.Exists(systemWideConfigFile)) { systemWideConfigBackupFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); File.Move(systemWideConfigFile, systemWideConfigBackupFile); } + if (File.Exists(currentUserConfigFile)) { currentUserConfigBackupFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); @@ -102,6 +104,7 @@ namespace PSTests.Sequential { File.Move(currentUserConfigBackupFile, currentUserConfigFile); } + InternalTestHooks.BypassGroupPolicyCaching = originalTestHookValue; } @@ -264,6 +267,7 @@ namespace PSTests.Sequential { File.Delete(systemWideConfigFile); } + if (File.Exists(currentUserConfigFile)) { File.Delete(currentUserConfigFile); @@ -322,8 +326,10 @@ namespace PSTests.Sequential public void SetupConfigFile4() { CleanupConfigFiles(); + // System wide config file is empty CreateEmptyFile(systemWideConfigFile); + // Current user config file is empty CreateEmptyFile(currentUserConfigFile); } @@ -338,7 +344,7 @@ namespace PSTests.Sequential public class PowerShellPolicyTests : IClassFixture { - PowerShellPolicyFixture fixture; + private PowerShellPolicyFixture fixture; public PowerShellPolicyTests(PowerShellPolicyFixture fixture) { diff --git a/test/xUnit/csharp/test_PSVersionInfo.cs b/test/xUnit/csharp/test_PSVersionInfo.cs index 81d9980e53..c5151e1a8e 100644 --- a/test/xUnit/csharp/test_PSVersionInfo.cs +++ b/test/xUnit/csharp/test_PSVersionInfo.cs @@ -1,8 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Xunit; + using System; using System.Management.Automation; +using Xunit; namespace PSTests.Parallel { diff --git a/test/xUnit/csharp/test_Runspace.cs b/test/xUnit/csharp/test_Runspace.cs index 1625435466..c9060a0da7 100644 --- a/test/xUnit/csharp/test_Runspace.cs +++ b/test/xUnit/csharp/test_Runspace.cs @@ -1,19 +1,19 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Xunit; + using System; using System.Management.Automation; using System.Management.Automation.Runspaces; +using Xunit; namespace PSTests.Parallel { // NOTE: do not call AddCommand("out-host") after invoking or MergeMyResults, // otherwise Invoke will not return any objects - public class RunspaceTests { private static int count = 1; - private static string script = String.Format($"get-command get-command"); + private static string script = string.Format($"get-command get-command"); [Fact] public void TestRunspaceWithPipeline() @@ -30,6 +30,7 @@ namespace PSTests.Parallel ++objCount; Assert.NotNull(result); } + Assert.Equal(count, objCount); } @@ -56,6 +57,7 @@ namespace PSTests.Parallel ++objCount; Assert.NotNull(result); } + Assert.Equal(count, objCount); } diff --git a/test/xUnit/csharp/test_SecuritySupport.cs b/test/xUnit/csharp/test_SecuritySupport.cs index efad910cd1..6090b0d1ee 100644 --- a/test/xUnit/csharp/test_SecuritySupport.cs +++ b/test/xUnit/csharp/test_SecuritySupport.cs @@ -1,8 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Xunit; + using System; using System.Management.Automation; +using Xunit; namespace PSTests.Parallel { diff --git a/test/xUnit/csharp/test_SessionState.cs b/test/xUnit/csharp/test_SessionState.cs index 52281a29b7..6032bcb15a 100644 --- a/test/xUnit/csharp/test_SessionState.cs +++ b/test/xUnit/csharp/test_SessionState.cs @@ -1,6 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Xunit; + using System; using System.Collections; using System.Collections.ObjectModel; @@ -11,6 +11,7 @@ using System.Management.Automation.Internal; using System.Management.Automation.Internal.Host; using System.Management.Automation.Runspaces; using Microsoft.PowerShell; +using Xunit; namespace PSTests.Parallel { @@ -21,7 +22,7 @@ namespace PSTests.Parallel { Skip.IfNot(Platform.IsWindows); CultureInfo currentCulture = CultureInfo.CurrentCulture; - PSHost hostInterface = new DefaultHost(currentCulture,currentCulture); + PSHost hostInterface = new DefaultHost(currentCulture, currentCulture); InitialSessionState iss = InitialSessionState.CreateDefault2(); AutomationEngine engine = new AutomationEngine(hostInterface, iss); ExecutionContext executionContext = new ExecutionContext(engine, hostInterface, iss); diff --git a/test/xUnit/csharp/test_Utils.cs b/test/xUnit/csharp/test_Utils.cs index 2ac7f81b50..9d1aeb249b 100644 --- a/test/xUnit/csharp/test_Utils.cs +++ b/test/xUnit/csharp/test_Utils.cs @@ -1,10 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Xunit; + using System; using System.Management.Automation; using System.Management.Automation.Internal; using System.Reflection; +using Xunit; namespace PSTests.Parallel {