File size: 8,536 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Host;
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 System.Reflection.Metadata;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.PowerShell;
using Microsoft.PowerShell.Commands;
using Xunit;

namespace PSTests.Parallel
{
    public class FileSystemProviderTests : IDisposable
    {
        private readonly string testPath;
        private readonly string testContent;

        public FileSystemProviderTests()
        {
            testPath = Path.GetTempFileName();
            testContent = "test content!";
            if (File.Exists(testPath))
            {
                File.Delete(testPath);
            }

            File.AppendAllText(testPath, testContent);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                File.Delete(testPath);
            }
        }

        private static ExecutionContext GetExecutionContext()
        {
            CultureInfo currentCulture = CultureInfo.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 static ProviderInfo GetProvider()
        {
            ExecutionContext executionContext = GetExecutionContext();
            SessionStateInternal sessionState = new SessionStateInternal(executionContext);

            SessionStateProviderEntry providerEntry = new SessionStateProviderEntry("FileSystem", typeof(FileSystemProvider), null);
            sessionState.AddSessionStateEntry(providerEntry);
            ProviderInfo matchingProvider = sessionState.ProviderList.ToList()[0];

            return matchingProvider;
        }

        [Fact]
        public void TestCreateJunctionFails()
        {
            if (!Platform.IsWindows)
            {
                Assert.False(InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty));
            }
            else
            {
                Assert.Throws<System.ArgumentException>(delegate { InternalSymbolicLinkLinkCodeMethods.CreateJunction(string.Empty, string.Empty); });
            }
        }

        [Fact]
        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);
        }

        [Fact]
        public void TestMode()
        {
            Assert.Equal(FileSystemProvider.Mode(null), string.Empty);
            FileSystemInfo directoryObject = null;
            FileSystemInfo fileObject = null;
            FileSystemInfo executableObject = null;

            if (!Platform.IsWindows)
            {
                directoryObject = new DirectoryInfo(@"/");
                fileObject = new FileInfo(@"/etc/hosts");
                executableObject = new FileInfo(@"/bin/echo");
            }
            else
            {
                directoryObject = new DirectoryInfo(System.Environment.CurrentDirectory);
                fileObject = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);
                executableObject = new FileInfo(Environment.ProcessPath);
            }

            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]
        public void TestGetProperty()
        {
            FileSystemProvider fileSystemProvider = new FileSystemProvider();
            ProviderInfo providerInfoToSet = GetProvider();
            fileSystemProvider.SetProviderInformation(providerInfoToSet);
            fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
            PSObject pso = new PSObject();
            pso.AddOrSetProperty("IsReadOnly", false);
            fileSystemProvider.SetProperty(testPath, pso);
            fileSystemProvider.GetProperty(testPath, new Collection<string>() { "IsReadOnly" });
            FileInfo fileSystemObject1 = new FileInfo(testPath);
            PSObject psobject1 = PSObject.AsPSObject(fileSystemObject1);
            PSPropertyInfo property = psobject1.Properties["IsReadOnly"];
            Assert.False((bool)property.Value);
        }

        [Fact]
        public void TestSetProperty()
        {
            FileSystemProvider fileSystemProvider = new FileSystemProvider();
            ProviderInfo providerInfoToSet = GetProvider();
            fileSystemProvider.SetProviderInformation(providerInfoToSet);
            fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
            fileSystemProvider.GetProperty(testPath, new Collection<string>() { "Name" });
            FileInfo fileSystemObject1 = new FileInfo(testPath);
            PSObject psobject1 = PSObject.AsPSObject(fileSystemObject1);
            PSPropertyInfo property = psobject1.Properties["FullName"];

            Assert.Equal(testPath, property.Value);
        }

        [Fact]
        public void TestClearProperty()
        {
            FileSystemProvider fileSystemProvider = new FileSystemProvider();
            ProviderInfo providerInfoToSet = GetProvider();
            fileSystemProvider.SetProviderInformation(providerInfoToSet);
            fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
            fileSystemProvider.ClearProperty(testPath, new Collection<string>() { "Attributes" });
        }

        [Fact]
        public void TestGetContentReader()
        {
            FileSystemProvider fileSystemProvider = new FileSystemProvider();
            ProviderInfo providerInfoToSet = GetProvider();
            fileSystemProvider.SetProviderInformation(providerInfoToSet);
            fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());

            IContentReader contentReader = fileSystemProvider.GetContentReader(testPath);
            Assert.Equal(contentReader.Read(1)[0], testContent);
            contentReader.Close();
        }

        [Fact]
        public void TestGetContentWriter()
        {
            FileSystemProvider fileSystemProvider = new FileSystemProvider();
            ProviderInfo providerInfoToSet = GetProvider();
            fileSystemProvider.SetProviderInformation(providerInfoToSet);
            fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());

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

        [Fact]
        public void TestClearContent()
        {
            FileSystemProvider fileSystemProvider = new FileSystemProvider();
            ProviderInfo providerInfoToSet = GetProvider();
            fileSystemProvider.SetProviderInformation(providerInfoToSet);
            fileSystemProvider.Context = new CmdletProviderContext(GetExecutionContext());
            fileSystemProvider.ClearContent(testPath);
            Assert.Empty(File.ReadAllText(testPath));
        }
    }
}