Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions test/xUnit/Asserts/PriorityOrderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> t
{
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<int>("Priority");
}

GetOrCreate(sortedMethods, priority).Add(testCase);
}
Expand All @@ -29,15 +31,20 @@ public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> t
{
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<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key) where TValue : new()
private static TValue GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue> 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;
Expand Down
3 changes: 2 additions & 1 deletion test/xUnit/csharp/test_Binders.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
5 changes: 3 additions & 2 deletions test/xUnit/csharp/test_CorePsPlatform.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
3 changes: 2 additions & 1 deletion test/xUnit/csharp/test_ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
68 changes: 38 additions & 30 deletions test/xUnit/csharp/test_FileSystemProvider.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,23 +13,30 @@
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);
Expand All @@ -38,18 +45,19 @@ void IDisposable.Dispose()
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];

Expand All @@ -59,7 +67,7 @@ private ProviderInfo GetProvider()
[Fact]
public void TestCreateJunctionFails()
{
if(!Platform.IsWindows)
if (!Platform.IsWindows)
{
Assert.False(InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty));
}
Expand All @@ -73,20 +81,20 @@ public void TestCreateJunctionFails()
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");
Expand All @@ -99,9 +107,9 @@ public void TestMode()
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]
Expand All @@ -111,15 +119,15 @@ public void TestGetProperty()
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<string>(){"IsReadOnly"});
fileSystemProvider.GetProperty(testPath, new Collection<string>(){ "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);
}
Expand All @@ -133,12 +141,12 @@ public void TestSetProperty()
ProviderInfo providerInfoToSet = GetProvider();
fileSystemProvider.SetProviderInformation(providerInfoToSet);
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
fileSystemProvider.GetProperty(testPath, new Collection<string>(){"Name"});
fileSystemProvider.GetProperty(testPath, new Collection<string>(){ "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);
}
Expand All @@ -152,7 +160,7 @@ public void TestClearProperty()
ProviderInfo providerInfoToSet = GetProvider();
fileSystemProvider.SetProviderInformation(providerInfoToSet);
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
fileSystemProvider.ClearProperty(testPath, new Collection<string>(){"Attributes"});
fileSystemProvider.ClearProperty(testPath, new Collection<string>(){ "Attributes" });
}

[Fact]
Expand All @@ -164,7 +172,7 @@ public void TestGetContentReader()
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();
}

Expand All @@ -177,9 +185,9 @@ public void TestGetContentWriter()
fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());

IContentWriter contentWriter = fileSystemProvider.GetContentWriter(testPath);
contentWriter.Write(new List<string>(){"contentWriterTestContent"});
contentWriter.Write(new List<string>(){ "contentWriterTestContent" });
contentWriter.Close();
Assert.Equal(File.ReadAllText(testPath), testContent+@"contentWriterTestContent"+ System.Environment.NewLine);
Assert.Equal(File.ReadAllText(testPath), testContent + @"contentWriterTestContent" + System.Environment.NewLine);
}

[Fact]
Expand Down
3 changes: 2 additions & 1 deletion test/xUnit/csharp/test_MshSnapinInfo.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
16 changes: 11 additions & 5 deletions test/xUnit/csharp/test_PSConfiguration.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,13 +9,14 @@
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;

Expand Down Expand Up @@ -43,14 +44,15 @@ public PowerShellPolicyFixture()
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());
Expand Down Expand Up @@ -102,6 +104,7 @@ public void Dispose()
{
File.Move(currentUserConfigBackupFile, currentUserConfigFile);
}

InternalTestHooks.BypassGroupPolicyCaching = originalTestHookValue;
}

Expand Down Expand Up @@ -264,6 +267,7 @@ public void CleanupConfigFiles()
{
File.Delete(systemWideConfigFile);
}

if (File.Exists(currentUserConfigFile))
{
File.Delete(currentUserConfigFile);
Expand Down Expand Up @@ -322,8 +326,10 @@ public void SetupConfigFile3()
public void SetupConfigFile4()
{
CleanupConfigFiles();

// System wide config file is empty
CreateEmptyFile(systemWideConfigFile);

// Current user config file is empty
CreateEmptyFile(currentUserConfigFile);
}
Expand All @@ -338,7 +344,7 @@ private void CreateEmptyFile(string fileName)

public class PowerShellPolicyTests : IClassFixture<PowerShellPolicyFixture>
{
PowerShellPolicyFixture fixture;
private PowerShellPolicyFixture fixture;

public PowerShellPolicyTests(PowerShellPolicyFixture fixture)
{
Expand Down
3 changes: 2 additions & 1 deletion test/xUnit/csharp/test_PSVersionInfo.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
8 changes: 5 additions & 3 deletions test/xUnit/csharp/test_Runspace.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -30,6 +30,7 @@ public void TestRunspaceWithPipeline()
++objCount;
Assert.NotNull(result);
}

Assert.Equal(count, objCount);
}

Expand All @@ -56,6 +57,7 @@ public void TestRunspaceWithPowerShell()
++objCount;
Assert.NotNull(result);
}

Assert.Equal(count, objCount);
}

Expand Down
Loading