Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
test_FileSystemProvider.cs207 linesDownload Raw Back to csharp
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Collections.Generic;6using System.Collections.ObjectModel;7using System.Globalization;8using System.IO;9using System.Linq;10using System.Management.Automation;11using System.Management.Automation.Host;12using System.Management.Automation.Internal;13using System.Management.Automation.Internal.Host;14using System.Management.Automation.Provider;15using System.Management.Automation.Runspaces;16using System.Reflection;17using System.Reflection.Metadata;18using Microsoft.CodeAnalysis.CSharp.Syntax;19using Microsoft.PowerShell;20using Microsoft.PowerShell.Commands;21using Xunit;22 23namespace PSTests.Parallel24{25    public class FileSystemProviderTests : IDisposable26    {27        private readonly string testPath;28        private readonly string testContent;29 30        public FileSystemProviderTests()31        {32            testPath = Path.GetTempFileName();33            testContent = "test content!";34            if (File.Exists(testPath))35            {36                File.Delete(testPath);37            }38 39            File.AppendAllText(testPath, testContent);40        }41 42        public void Dispose()43        {44            Dispose(true);45            GC.SuppressFinalize(this);46        }47 48        protected virtual void Dispose(bool disposing)49        {50            if (disposing)51            {52                File.Delete(testPath);53            }54        }55 56        private static ExecutionContext GetExecutionContext()57        {58            CultureInfo currentCulture = CultureInfo.CurrentCulture;59            PSHost hostInterface = new DefaultHost(currentCulture, currentCulture);60            InitialSessionState iss = InitialSessionState.CreateDefault2();61            AutomationEngine engine = new AutomationEngine(hostInterface, iss);62            ExecutionContext executionContext = new ExecutionContext(engine, hostInterface, iss);63            return executionContext;64        }65 66        private static ProviderInfo GetProvider()67        {68            ExecutionContext executionContext = GetExecutionContext();69            SessionStateInternal sessionState = new SessionStateInternal(executionContext);70 71            SessionStateProviderEntry providerEntry = new SessionStateProviderEntry("FileSystem", typeof(FileSystemProvider), null);72            sessionState.AddSessionStateEntry(providerEntry);73            ProviderInfo matchingProvider = sessionState.ProviderList.ToList()[0];74 75            return matchingProvider;76        }77 78        [Fact]79        public void TestCreateJunctionFails()80        {81            if (!Platform.IsWindows)82            {83                Assert.False(InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty));84            }85            else86            {87                Assert.Throws<System.ArgumentException>(delegate { InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty); });88            }89        }90 91        [Fact]92        public void TestGetHelpMaml()93        {94            FileSystemProvider fileSystemProvider = new FileSystemProvider();95            Assert.Equal(fileSystemProvider.GetHelpMaml(string.Empty, string.Empty), string.Empty);96            Assert.Equal(fileSystemProvider.GetHelpMaml("helpItemName", string.Empty), string.Empty);97            Assert.Equal(fileSystemProvider.GetHelpMaml(string.Empty, "path"), string.Empty);98        }99 100        [Fact]101        public void TestMode()102        {103            Assert.Equal(FileSystemProvider.Mode(null), string.Empty);104            FileSystemInfo directoryObject = null;105            FileSystemInfo fileObject = null;106            FileSystemInfo executableObject = null;107 108            if (!Platform.IsWindows)109            {110                directoryObject = new DirectoryInfo(@"/");111                fileObject = new FileInfo(@"/etc/hosts");112                executableObject = new FileInfo(@"/bin/echo");113            }114            else115            {116                directoryObject = new DirectoryInfo(System.Environment.CurrentDirectory);117                fileObject = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);118                executableObject = new FileInfo(Environment.ProcessPath);119            }120 121            Assert.Equal("d----", FileSystemProvider.Mode(PSObject.AsPSObject(directoryObject)).Replace("r", "-"));122            Assert.Equal("-----", FileSystemProvider.Mode(PSObject.AsPSObject(fileObject)).Replace("r", "-").Replace("a", "-"));123            Assert.Equal("-----", FileSystemProvider.Mode(PSObject.AsPSObject(executableObject)).Replace("r", "-").Replace("a", "-"));124        }125 126        [Fact]127        public void TestGetProperty()128        {129            FileSystemProvider fileSystemProvider = new FileSystemProvider();130            ProviderInfo providerInfoToSet = GetProvider();131            fileSystemProvider.SetProviderInformation(providerInfoToSet);132            fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());133            PSObject pso = new PSObject();134            pso.AddOrSetProperty("IsReadOnly", false);135            fileSystemProvider.SetProperty(testPath, pso);136            fileSystemProvider.GetProperty(testPath, new Collection<string>() { "IsReadOnly" });137            FileInfo fileSystemObject1 = new FileInfo(testPath);138            PSObject psobject1 = PSObject.AsPSObject(fileSystemObject1);139            PSPropertyInfo property = psobject1.Properties["IsReadOnly"];140            Assert.False((bool)property.Value);141        }142 143        [Fact]144        public void TestSetProperty()145        {146            FileSystemProvider fileSystemProvider = new FileSystemProvider();147            ProviderInfo providerInfoToSet = GetProvider();148            fileSystemProvider.SetProviderInformation(providerInfoToSet);149            fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());150            fileSystemProvider.GetProperty(testPath, new Collection<string>() { "Name" });151            FileInfo fileSystemObject1 = new FileInfo(testPath);152            PSObject psobject1 = PSObject.AsPSObject(fileSystemObject1);153            PSPropertyInfo property = psobject1.Properties["FullName"];154 155            Assert.Equal(testPath, property.Value);156        }157 158        [Fact]159        public void TestClearProperty()160        {161            FileSystemProvider fileSystemProvider = new FileSystemProvider();162            ProviderInfo providerInfoToSet = GetProvider();163            fileSystemProvider.SetProviderInformation(providerInfoToSet);164            fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());165            fileSystemProvider.ClearProperty(testPath, new Collection<string>() { "Attributes" });166        }167 168        [Fact]169        public void TestGetContentReader()170        {171            FileSystemProvider fileSystemProvider = new FileSystemProvider();172            ProviderInfo providerInfoToSet = GetProvider();173            fileSystemProvider.SetProviderInformation(providerInfoToSet);174            fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());175 176            IContentReader contentReader = fileSystemProvider.GetContentReader(testPath);177            Assert.Equal(contentReader.Read(1)[0], testContent);178            contentReader.Close();179        }180 181        [Fact]182        public void TestGetContentWriter()183        {184            FileSystemProvider fileSystemProvider = new FileSystemProvider();185            ProviderInfo providerInfoToSet = GetProvider();186            fileSystemProvider.SetProviderInformation(providerInfoToSet);187            fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());188 189            IContentWriter contentWriter = fileSystemProvider.GetContentWriter(testPath);190            contentWriter.Write(new List<string>() { "contentWriterTestContent" });191            contentWriter.Close();192            Assert.Equal(File.ReadAllText(testPath), testContent + @"contentWriterTestContent" + System.Environment.NewLine);193        }194 195        [Fact]196        public void TestClearContent()197        {198            FileSystemProvider fileSystemProvider = new FileSystemProvider();199            ProviderInfo providerInfoToSet = GetProvider();200            fileSystemProvider.SetProviderInformation(providerInfoToSet);201            fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());202            fileSystemProvider.ClearContent(testPath);203            Assert.Empty(File.ReadAllText(testPath));204        }205    }206}207