-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathExecute.cs
114 lines (103 loc) · 3.2 KB
/
Execute.cs
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
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace Testably.Abstractions.Testing.Helpers;
internal partial class Execute
{
/// <summary>
/// Flag indicating if the code runs on <see cref="OSPlatform.Linux" />.
/// </summary>
public bool IsLinux { get; }
/// <summary>
/// Flag indicating if the code runs on <see cref="OSPlatform.OSX" />.
/// </summary>
public bool IsMac { get; }
/// <summary>
/// Flag indicating if the code runs in .NET Framework.
/// </summary>
/// <remarks>
/// <see href="https://stackoverflow.com/a/53675231" />
/// </remarks>
public bool IsNetFramework { get; }
/// <summary>
/// Flag indicating if the code runs on <see cref="OSPlatform.Windows" />.
/// </summary>
public bool IsWindows { get; }
/// <summary>
/// The internal implementation of the <see cref="IPath" /> functionality.
/// </summary>
public IPath Path { get; }
/// <summary>
/// The default <see cref="StringComparison" /> used for comparing paths.
/// </summary>
public StringComparison StringComparisonMode { get; }
#if !CAN_SIMULATE_OTHER_OS
[Obsolete("Simulating other operating systems is not supported on .NET Framework")]
#endif
internal Execute(MockFileSystem fileSystem, SimulationMode simulationMode)
{
IsLinux = simulationMode == SimulationMode.Linux;
IsMac = simulationMode == SimulationMode.MacOS;
IsWindows = simulationMode == SimulationMode.Windows;
IsNetFramework = false;
StringComparisonMode = IsLinux
? StringComparison.Ordinal
: StringComparison.OrdinalIgnoreCase;
if (IsLinux)
{
Path = new LinuxPath(fileSystem);
}
else if (IsMac)
{
Path = new MacPath(fileSystem);
}
else if (IsWindows)
{
Path = new WindowsPath(fileSystem);
}
else
{
throw new NotSupportedException(
"The operating system must be one of Linux, OSX or Windows");
}
}
internal Execute(MockFileSystem fileSystem)
{
IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
IsMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
IsNetFramework = RuntimeInformation.FrameworkDescription
.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase);
StringComparisonMode = IsLinux
? StringComparison.Ordinal
: StringComparison.OrdinalIgnoreCase;
Path = new NativePath(fileSystem);
}
internal static string CreateTempFileName(MockFileSystem fileSystem)
{
int i = 0;
string tempPath = fileSystem.Path.GetTempPath();
fileSystem.Directory.CreateDirectory(tempPath);
while (true)
{
string fileName = $"{RandomString(fileSystem, 8)}.tmp";
string path = string.Concat(tempPath, fileName);
try
{
fileSystem.File.Open(path, FileMode.CreateNew, FileAccess.Write).Dispose();
return path;
}
catch (IOException) when (i < 100)
{
i++; // Don't let unforeseen circumstances cause us to loop forever
}
}
}
internal static string RandomString(MockFileSystem fileSystem, int length)
{
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[fileSystem.RandomSystem.Random.Shared.Next(s.Length)]).ToArray());
}
}