File size: 10,734 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
$definition = @'
using System;
using System.Collections.Generic;
using System.Threading;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Security;
using System.Collections;

namespace TestHost
{
    public class TestHostRawUI : PSHostRawUserInterface
    {
        private ConsoleColor _backgroundColor = ConsoleColor.Black;
        public override ConsoleColor BackgroundColor
        {
            get { return _backgroundColor; }
            set { _backgroundColor = value; }
        }
        private ConsoleColor _foregroundColor = ConsoleColor.White;
        public override ConsoleColor ForegroundColor
        {
            get { return _foregroundColor; }
            set { _foregroundColor = value; }
        }
        private string _windowTitle = "title";
        public override string WindowTitle
        {
            get { return _windowTitle; }
            set { _windowTitle = value; }
        }
        private Size _bufferSize = new Size(10,10);
        public override Size BufferSize
        {
            get { return _bufferSize; }
            set { _bufferSize = value; }
        }
        private Coordinates _cursorPosition = new Coordinates(0, 0);
        public override Coordinates CursorPosition
        {
            get { return _cursorPosition; }
            set { _cursorPosition = value; }
        }
        private int _cursorSize = 10;
        public override int CursorSize
        {
            get { return _cursorSize; }
            set { _cursorSize = value; }
        }
        private bool _keyAvailable = false;
        public override bool KeyAvailable
        {
            get { return _keyAvailable; }
        }
        public override Size MaxPhysicalWindowSize
        {
            get { throw new NotImplementedException(); }
        }
        public override Size MaxWindowSize
        {
            get { throw new NotImplementedException(); }
        }
        private Coordinates _windowPosition = new Coordinates(0, 0);
        public override Coordinates WindowPosition
        {
            get { return _windowPosition; }

            set { _windowPosition = value; }
        }
        private Size _windowSize = new Size(80, 40);
        public override Size WindowSize
        {
            get { return _windowSize; }
            set { _windowSize = value; }
        }
        public override BufferCell[,] GetBufferContents(Rectangle rectangle) { throw new NotImplementedException(); }
        public override void FlushInputBuffer() { throw new NotImplementedException(); }
        public override KeyInfo ReadKey(ReadKeyOptions options) { throw new NotImplementedException(); }
        public override void SetBufferContents(Coordinates origin, BufferCell[,] contents) { throw new NotImplementedException(); }
        public override void SetBufferContents(Rectangle rectangle, BufferCell fill) { throw new NotImplementedException(); }
        public override void ScrollBufferContents(Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill) { throw new NotImplementedException(); }
    }

    public class Streams
    {
        public ArrayList ConsoleOutput = new ArrayList();
        public ArrayList Input         = new ArrayList();
        public ArrayList Error         = new ArrayList();
        public ArrayList Verbose       = new ArrayList();
        public ArrayList Debug         = new ArrayList();
        public ArrayList Warning       = new ArrayList();
        public ArrayList Information   = new ArrayList();
        public ArrayList Progress      = new ArrayList();
        public ArrayList Prompt        = new ArrayList();
        public void Clear() {
            ConsoleOutput.Clear();
            Input.Clear();
            Error.Clear();
            Verbose.Clear();
            Debug.Clear();
            Warning.Clear();
            Information.Clear();
            Progress.Clear();
            Prompt.Clear();
        }
    }
    public class TestPSHostUserInterface : PSHostUserInterface
    {
        private PSHostRawUserInterface _rawui = new TestHostRawUI();
        public string ReadLineData = "This is readline data";
        public int PromptedChoice = 0;
        public string StringForSecureString = "TEST";
        public string UserNameForCredential = "Admin";
        public object promptResponse = "this is a prompt response";

        public Streams Streams = new Streams();
        public override PSHostRawUserInterface RawUI
        {
            get { return _rawui; }
        }

        public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
        {
            if (descriptions == null || descriptions[0] == null)
            {
                throw new ArgumentException("descriptions");
            }

            string s = descriptions[0].Name;
            Streams.Prompt.Add(caption + ":" + message + ":" + s);
            Dictionary<string, PSObject> d = new Dictionary<string, PSObject>();
            d.Add(s, new PSObject(promptResponse));
            return d;
        }

        public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
        {
            Streams.Prompt.Add(caption + ":" + message);
            return PromptedChoice;
        }

        public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
        {
            Streams.Prompt.Add("Credential:" + caption + ":" + message);
            SecureString ss = ReadLineAsSecureString();
            string userNameToUse = string.IsNullOrEmpty(userName) ? UserNameForCredential : userName;
            return new PSCredential(userNameToUse, ss);
        }

        public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
        {
            Streams.Prompt.Add("Credential:" + caption + ":" + message);
            SecureString ss = ReadLineAsSecureString();
            string userNameToUse = string.IsNullOrEmpty(userName) ? UserNameForCredential : userName;
            return new PSCredential(userNameToUse, ss);
        }

        public override string ReadLine()
        {
            return ReadLineData;
        }

        public override SecureString ReadLineAsSecureString()
        {
            SecureString ss = new SecureString();
            foreach(char c in StringForSecureString.ToCharArray()) { ss.AppendChar(c); }
            return ss;
        }

        // Cmdlets call 'Write' and 'WriteLine' methods implicitly.
        // To see difference between 'Write' and 'WriteLine' with and w/o colors in the debug output
        // we need use a meta information.
        // So we make a output string as:
        // <Foregraund color name> : <Background color name> : <'user value'> : <'NewLine' or 'NoNewLine'>
        //
        public override void Write(string value)
        {
            Streams.ConsoleOutput.Add("::"+value+":NoNewLine");
        }

        public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
        {
            Streams.ConsoleOutput.Add(foregroundColor+":"+backgroundColor+":"+value+":NoNewLine");
        }

        public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
        {
            Streams.ConsoleOutput.Add(foregroundColor+":"+backgroundColor+":"+value+":NewLine");
        }

        public override void WriteDebugLine(string message)
        {
            Streams.Debug.Add(message);
        }

        public override void WriteErrorLine(string value)
        {
            Streams.Error.Add(value);
        }

        public override void WriteLine(string value)
        {
            Streams.ConsoleOutput.Add("::"+value+":NewLine");
        }

        public override void WriteProgress(long sourceId, ProgressRecord record)
        {
            Streams.Progress.Add(record);
        }

        public override void WriteVerboseLine(string message)
        {
            Streams.Verbose.Add(message);
        }

        public override void WriteWarningLine(string message)
        {
            Streams.Warning.Add(message);
        }

        public override void WriteInformation(InformationRecord record)
        {
            HostInformationMessage hostOutput = record.MessageData as HostInformationMessage;
            if (hostOutput != null) {
                 string message = hostOutput.Message;
                 Streams.Information.Add(message);
            }
        }
    }

    public class TestHost : PSHost
    {
        private Guid _instanceId = Guid.NewGuid();
        private PSHostUserInterface _ui = new TestPSHostUserInterface();
        public string _hostName = "TEST HOST";
        public Version _version = new Version(6, 0);
        int promptLevel = 0;
        public override CultureInfo CurrentCulture
        {
            get { return Thread.CurrentThread.CurrentCulture; }
        }

        public override CultureInfo CurrentUICulture
        {
            get {  return Thread.CurrentThread.CurrentUICulture; }
        }

        public override Guid InstanceId
        {
            get { return _instanceId; }
        }

        public override string Name
        {
            get { return _hostName; }
        }

        public override PSHostUserInterface UI
        {
            get { return _ui; }
        }

        public override Version Version
        {
            get { return _version; }
        }

        public override void EnterNestedPrompt()
        {
            promptLevel++;
        }

        public override void ExitNestedPrompt()
        {
            promptLevel--;
        }

        public override void NotifyBeginApplication()
        {
            throw new NotImplementedException();
        }

        public override void NotifyEndApplication()
        {
            throw new NotImplementedException();
        }

        public override void SetShouldExit(int exitCode)
        {
            throw new NotImplementedException();
        }
    }
}

'@
## '
function New-TestHost
{
    If ($IsCoreCLR)
    {
        $references = @()
    }
    else
    {
        $references = "System.Management.Automation"
    }

    if ( ! ("TestHost.TestHost" -as "type" )) {
       $t = Add-Type -pass $definition -ref $references
    }

    [TestHost.TestHost]::New()
}