forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.cs
More file actions
88 lines (76 loc) · 2.67 KB
/
Helpers.cs
File metadata and controls
88 lines (76 loc) · 2.67 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Text;
using Xunit;
class Helpers
{
public static void SetAndReadHelper(Action<TextWriter> setHelper, Func<TextWriter> getHelper, Func<StreamReader, string> readHelper)
{
const string TestString = "Test";
TextWriter oldWriterToRestore = getHelper();
Assert.NotNull(oldWriterToRestore);
try
{
using (MemoryStream memStream = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(memStream))
{
setHelper(sw);
TextWriter newStream = getHelper();
Assert.NotNull(newStream);
newStream.Write(TestString);
newStream.Flush();
memStream.Seek(0, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(memStream))
{
string fromConsole = readHelper(sr);
Assert.Equal(TestString, fromConsole);
}
}
}
}
finally
{
setHelper(oldWriterToRestore);
}
}
public static void RunInRedirectedOutput(Action<MemoryStream> command)
{
// Make sure that redirecting to a memory stream causes no special writing to the stream when using Console.CursorVisible
MemoryStream data = new MemoryStream();
TextWriter savedOut = Console.Out;
try
{
Console.SetOut(new StreamWriter(data, new UTF8Encoding(false), 0x1000, leaveOpen: true) { AutoFlush = true });
command(data);
}
finally
{
Console.SetOut(savedOut);
}
}
public static void RunInNonRedirectedOutput(Action<MemoryStream> command)
{
// Make sure that when writing out to a UnixConsoleStream
// written out.
MemoryStream data = new MemoryStream();
TextWriter savedOut = Console.Out;
try
{
Console.SetOut(
new InterceptStreamWriter(
Console.OpenStandardOutput(),
new StreamWriter(data, new UTF8Encoding(false), 0x1000, leaveOpen: true) { AutoFlush = true },
new UTF8Encoding(false), 0x1000, leaveOpen: true)
{ AutoFlush = true });
command(data);
}
finally
{
Console.SetOut(savedOut);
}
}
}